Esempio n. 1
0
    void scatter(icontext_type& context, const vertex_type& vertex,
            edge_type& edge) const {
        const vertex_type other = edge.target();
        pagerank_type value = vertex.data().pagerank / vertex.num_out_edges();
        assert(other.id() != vertex.id());
        const sum_pagerank_type msg(value);
        context.signal(other, msg);

    }
Esempio n. 2
0
	void apply(icontext_type& context, vertex_type& vertex, const gather_type& total)
	{
		cout << "apply(), vid=" << vertex.id() << endl;
		cout << "total=" << total << endl;

		// reset incoming messages
		vertex.data().multiplied_incoming_messages.clear();

		// iterate over message targets
		for (set<vertex_id_type>::const_iterator target_it=total.message_targets.begin(); target_it!=total.message_targets.end(); ++target_it)
		{
			vertex_id_type target_id=*target_it;
		
			// iterate over message sources (and the betas)
			for (map<vertex_id_type, VectorXd>::const_iterator source_it=total.message_source_betas.begin(); source_it!=total.message_source_betas.end(); ++source_it)
			{
				vertex_id_type source_id=source_it->first;
				
				// we dont need to consider the kernel matrix of a edge with itself since this is always omitted in the message scheduling
				if (source_id==target_id)
					continue;
					
				cout << "adding message from " << source_id << " to " << vertex.id() << " to construct message from " << vertex.id() << " to " << target_id << endl;
			
				// extract K and beta for incoming message
				MatrixXd K=vertex.data().kernel_dict[pair<vertex_id_type, vertex_id_type>(target_id,source_id)];
				VectorXd beta=source_it->second;
			
				// if beta has zero rows, it is initialised to constant with unit norm (first iteration)
				if (!beta.rows())
				{
					beta=VectorXd::Constant(K.cols(), 1.0);
					beta=beta/beta.norm();
				}
			
				cout << "K_" << vertex.id() << "^(" << target_id << "," << source_id << "):" << endl << K << endl;
				cout << "times" << endl;
				cout << "beta_(" << source_id << "," << vertex.id() << "):" << endl << beta << endl;
			
				// for a fixed source and target node, compute incoming kernelbp message
				VectorXd message=K*beta;
				cout << "message: " << message << endl;
				
				// multiply all messages together
				if (vertex.data().multiplied_incoming_messages.find(target_id)==vertex.data().multiplied_incoming_messages.end())
					vertex.data().multiplied_incoming_messages[target_id]=message;
				else
				{
					cout << "old message product: " << vertex.data().multiplied_incoming_messages[target_id] << endl;
					vertex.data().multiplied_incoming_messages[target_id]=vertex.data().multiplied_incoming_messages[target_id].cwiseProduct(message);
				}
				
				cout << "new message product: " << vertex.data().multiplied_incoming_messages[target_id] << endl;
			}
		}
	}
Esempio n. 3
0
	gather_type gather(icontext_type& context, const vertex_type& vertex, edge_type& edge) const
	{
		cout << "gather(), edge=" << edge.source().id() << "->" << edge.target().id() << ", called from vid=" << vertex.id() << endl;
		
		gather_type gathered;
		
		// add id of other vertex of edge and add id->beta to map if message source
		if (edge.target().id()==vertex.id())
		{
			// incoming edge, outgoing message, only if target is non-observed
			if (!edge.source().data().is_observed)
			{
				gathered.message_targets.insert(edge.source().id());
				cout << "added " << edge.source().id() << " as message target" << endl;
			}
		}
		else
		{
			// outgoing edge, incoming message with beta
			gathered.message_source_betas[edge.target().id()]=edge.data().beta;
			cout << "added " << edge.target().id() << " as message source" << endl;	
		}
					
		cout << "gathered=" << gathered << endl;
		
		return gathered;
	}
Esempio n. 4
0
  // Scatter to scatter_edges edges with the new message value.
  void scatter(icontext_type& context, const vertex_type& vertex, edge_type& edge) const {
    bool isEdgeSource = (vertex.id() == edge.source().id());
    bool hasSameData = isEdgeSource ? (vertex.data() == edge.target().data()) : (vertex.data() == edge.source().data()) ;
    if (!hasSameData) {
      min_combiner combiner;
      combiner.value = message_value;

      context.signal(isEdgeSource ? edge.target() : edge.source(), combiner);
    }
  }
Esempio n. 5
0
  // Receive inbound message (minimum data of adjacent vertices)
  void init(icontext_type& context, const vertex_type& vertex, const message_type& message) {
    // message.value == 4294967295 on first run, so init message_value to vertex data.
    if (message.value == 4294967295) {
      message_value = vertex.id();
    } else {
      // else, set the local copy to the message parameter.
      message_value = message.value;
    }

  }
 gather_type gather(icontext_type& context, const vertex_type& vertex, edge_type& edge) const {
     if (context.iteration() == 0) {
         if (vertex.id() == edge.source().id()) {
             return gather_type(edge.target().id());
         } else {
             return gather_type(edge.source().id());
         }
     } else {
         return gather_type();
     }
 }
        pair<size_t, size_t> count_triangles(const vertex_type& a, const vertex_type& b, const bool inverted=false) const {
            typedef neighbors_type::const_iterator neighbors_iterator_type;

            const vertex_id_type a_id = a.id();
            const vertex_id_type b_id = b.id();

            const neighbors_type& a_adj = a.data().neighbors;
            const neighbors_type& b_adj = b.data().neighbors;

            const bool directed = global_directed;

            if (a_adj.at(b_id) > 1 && a.id() < b.id() && !inverted) {
                return make_pair(0, 0);
            }

            if (a_adj.size() > b_adj.size()) {
                return reverse(count_triangles(b, a, true));
            }

            size_t a_count = 0;
            size_t b_count = 0;

            for (neighbors_iterator_type it_a = a_adj.begin(); it_a != a_adj.end(); it_a++) {
                const vertex_id_type c_id = it_a->first;
                neighbors_iterator_type it_b = b_adj.find(it_a->first);

                if (it_b != b_adj.end()) {
                    if (b_id < c_id) {
                        a_count += directed ? it_b->second : 2;
                    }

                    if (a_id < c_id) {
                        b_count += directed ? it_a->second : 2;
                    }
                }
            }

            return make_pair(a_count, b_count);
        }
Esempio n. 8
0
    /* Gather the weighted rank of the adjacent page   */
    double gather(icontext_type& context, const vertex_type& vertex,
        edge_type& edge) const {

      if (edge.data().role == edge_data::PREDICT)
         return 0;

      bool brows = vertex.id() < (uint)info.get_start_node(false);
      if (info.is_square()) 
        brows = !mi.A_transpose;
      if (mi.A_offset  && mi.x_offset >= 0){
        double val = edge.data().obs * (brows ? edge.target().data().pvec[mi.x_offset] :
            edge.source().data().pvec[mi.x_offset]);
        //printf("gather edge on vertex %d val %lg obs %lg\n", vertex.id(), val, edge.data().obs);
        return val;
      }
      //printf("edge on vertex %d val %lg\n", vertex.id(), 0.0);
      return 0;
    }
Esempio n. 9
0
	void scatter(icontext_type& context, const vertex_type& vertex, edge_type& edge) const
	{
		cout << "scatter(), edge=" << edge.source().id() << "->" << edge.target().id() << ", called from vid=" << vertex.id() << endl;
		cout << "computing message from vid=" << vertex.id() << " to vid=" << edge.source().id() << endl;

		vertex_id_type message_target=edge.source().id();
		
		// find out whether full rank or incomplete Cholesky mode

		// distinguish case this node being observed or not
		VectorXd new_beta;
		if (edge.target().data().is_observed)
		{
			cout << "observed target" << endl;
			
			// extract system solutions and observation kernel vector, base on full rank or incomplete Cholesky
			if (edge.data().full_rank)
			{
				cout << "full rank case" << endl;
				
				MatrixXd L_s=edge.data().solution_matrices["L_s"];
				cout << "L_s:" << L_s << endl;
				MatrixXd L_t=edge.data().solution_matrices["L_t"];
				cout << "L_t:" << L_t << endl;
				VectorXd k=vertex.data().kernel_dict_obs.at(message_target);
				cout << "k:" << k << endl;
			
				// L_{s}^{-T}(L_{s}^{-1}(L_{t}^{-T}(L_{t}^{-1}k_{t}^{s}), from right to left, 4 solver calls
				new_beta=k;
				new_beta=L_t.triangularView<Lower>().solve(new_beta);
				new_beta=L_t.transpose().triangularView<Upper>().solve(new_beta);
				new_beta=L_s.triangularView<Lower>().solve(new_beta);
				new_beta=L_s.transpose().triangularView<Upper>().solve(new_beta);
			}
			else
			{
				cout << "incomplete Cholesky case" << endl;
				MatrixXd Q_s=edge.data().solution_matrices["Q_s"];
				cout << "Q_s:" << Q_s << endl;
				MatrixXd R_s=edge.data().solution_matrices["R_s"];
				cout << "R_s:" << R_s << endl;
				MatrixXd P_s=edge.data().solution_matrices["P_s"];
				cout << "P_s:" << P_s << endl;
				
				MatrixXd Q_t=edge.data().solution_matrices["Q_t"];
				cout << "Q_t:" << Q_t << endl;
				MatrixXd R_t=edge.data().solution_matrices["R_t"];
				cout << "R_t:" << R_t << endl;
				MatrixXd P_t=edge.data().solution_matrices["P_t"];
				cout << "P_t:" << P_t << endl;
				
				MatrixXd W=edge.data().solution_matrices["W"];
				cout << "W:" << W << endl;
				
				VectorXd k=vertex.data().kernel_dict_obs.at(message_target);
				cout << "k:" << k << endl;
				
				// R_{s}^{-1}(Q_{s}^{T}((P_{s}(W_{s}W_{t}^{T}))(R_{t}^{-1}(Q_{t}^{T}(P_{t}k_{\mathcal{I}_{t}}^{(s)})))
				new_beta=k;
				new_beta=P_t.transpose()*new_beta;
				new_beta=Q_t.transpose()*new_beta;
				new_beta=R_t.triangularView<Upper>().solve(new_beta);
				new_beta=W*new_beta;
				new_beta=P_s.transpose()*new_beta;
				new_beta=Q_s.transpose()*new_beta;
				new_beta=R_s.triangularView<Upper>().solve(new_beta);
			}
		}
		else
		{
			cout << "non-observed target" << endl;
			cout << "multiplied_incoming_messages: " << vertex.data().multiplied_incoming_messages << endl;
			
			// extract system solutions, depending on full rank or incomplete Cholesky
			if (edge.data().full_rank)
			{
				cout << "full rank case" << endl;
				MatrixXd L_s=edge.data().solution_matrices["L_s"];
				cout << "L_s:" << L_s << endl;
		
				VectorXd k;
				if (!vertex.data().multiplied_incoming_messages.size())
				{
					cout << "no incoming messages, using constant unit norm vector" << endl;
					k=VectorXd::Constant(L_s.cols(), 1.0/sqrt(L_s.cols()));
				}
				else
				{
					k=vertex.data().multiplied_incoming_messages.at(message_target);
				}
				cout << "k:" << k << endl;
				
				// (K_{s}+\lambda I){}^{-1}k_{ut}^{(s)}=L_{s}^{-T}(L_{s}^{-1}k_{ut}^{(s)}) from right to left, 2 solver calls
				new_beta=k;
				new_beta=L_s.triangularView<Lower>().solve(new_beta);
				new_beta=L_s.transpose().triangularView<Upper>().solve(new_beta);
			}
			else
			{
				cout << "incomplete Cholesky case" << endl;
				
				MatrixXd Q_s=edge.data().solution_matrices["Q_s"];
				cout << "Q_s:" << Q_s << endl;
				MatrixXd R_s=edge.data().solution_matrices["R_s"];
				cout << "R_s:" << R_s << endl;
				MatrixXd P_s=edge.data().solution_matrices["P_s"];
				cout << "P_s:" << P_s << endl;
				
				MatrixXd W=edge.data().solution_matrices["W"];
				cout << "W:" << W << endl;
				
				VectorXd k;
				if (!vertex.data().multiplied_incoming_messages.size())
				{
					cout << "no incoming messages, using constant unit norm vector" << endl;
					k=VectorXd::Constant(W.cols(), 1.0/sqrt(W.cols()));
				}
				else
				{
					k=vertex.data().multiplied_incoming_messages.at(message_target);
				}
				cout << "k:" << k << endl;
				
				// R_{s}^{-1}(Q_{s}^{T}(P_{s}^{T}k_{t}^{(s)}))
				new_beta=k;
				new_beta=W*new_beta;
				new_beta=P_s.transpose()*new_beta;
				new_beta=Q_s.transpose()*new_beta;
				new_beta=R_s.triangularView<Upper>().solve(new_beta);
			}
		}

		// normalise
		new_beta=new_beta/new_beta.norm();
		
		// check whether has changed or not yet existed
		double difference;
		if (!edge.data().beta.rows())
			difference=numeric_limits<double>::infinity();
		else
			difference=(new_beta-edge.data().beta).norm();

		cout << "beta norm difference is " << difference << endl;
		if (difference>BETA_EPSILON)
		{
			// store new message and signal depending node if beta has changed or has not yet existed
			edge.data().beta=new_beta;
			context.signal(edge.source());
			cout << "beta has changed, new_beta=" << new_beta << "\nhas norm=" << new_beta.norm() << ", signalling vid=" << edge.source().id() << endl;
		}
		else
		{
			cout << "converged!\n";
		}
		
		cout << "beta: " << edge.source().id() << "->" << edge.target().id() << ": " << edge.data().beta << endl;
	}
 void scatter(icontext_type& context, const vertex_type& vertex, edge_type& edge) const {
     const vertex_type& other = edge.source().id() == vertex.id() ? edge.target() : edge.source();
     context.signal(other);
 }
 gather_type gather(icontext_type& context, const vertex_type& vertex, edge_type& edge) const {
     const vertex_type& other = edge.source().id() == vertex.id() ? edge.target() : edge.source();
     return gather_type(other.data());
 }
Esempio n. 12
0
 edge_dir_type gather_edges(icontext_type& context,
     const vertex_type& vertex) const {
   if (vertex.id() < rows)
     return OUT_EDGES;
   else return IN_EDGES;
 }