📄 old_code_2.svn-base
字号:
{ tie (begin_edge, end_edge) = out_edges(node_index,g); }template <class Graph, class RanVar >void GetProbabilities <Graph, RanVar>::set_random_variable(RanVar * a){ random_variable = a; random_variable_index = random_variable->get_index(); }template <class Graph, class RanVar >void GetProbabilities <Graph, RanVar>::operator() (unsigned int xi){ // What we must do here: // * set all the Potentials with the new value // * Compute the probability with a Product random_variable->set_value(xi); variable_potential.set_variable_value( *random_variable ); // setting the Potential attached to the Node // Setting the Potentials attached to the edges for (OutEdgeIterator current_edge = begin_edge; current_edge != end_edge; ++current_edge) { g[*current_edge]->set_variable_value( *random_variable ); } //for_each(begin_edge, end_edge, adapt_graph_edges <void> (g, boost::bind(boost::mem_fn <void, DiscretePotential, unsigned int, unsigned int> (&DiscretePotential::set_variable_value),_1,random_variable_index, xi) ) ); double a = variable_potential.get_potential_value() * make_product_over ( begin_edge, end_edge, adapt_graph_edges <double> (g, mem_fun(&EdgeClass::get_potential_value) ) ); // we now have our probability random_variable->sampling_probabilities[xi] = a;}template <class Graph, class RanVar >void GetProbabilities <Graph, RanVar>::set_sampled_value(unsigned int a){ random_variable->set_value(a); // will probable not be needed if things have been done correctly variable_potential.set_variable_value( *random_variable ); // setting the Potential attached to the Node // Setting the Potentials attached to the edges for (OutEdgeIterator current_edge = begin_edge; current_edge != end_edge; ++current_edge) { g[*current_edge]->set_variable_value( *random_variable ); } }// ******************* End of GetProbabilities Implementation ***************** //// ******************* FactorGraphGetProbabilities Implementation ***************** ///*template <class Graph, class RanVar >FactorGraphGetProbabilities <Graph, RanVar>::~FactorGraphGetProbabilities(){}template <class Graph, class RanVar >FactorGraphGetProbabilities <Graph, RanVar>::FactorGraphGetProbabilities(Graph & a, DiscretePotential & b, VertexDescriptor node_index) : GetProbabilities <Graph, RanVar> (a, b){ tie (begin_potential, end_potential) = adjacent_vertices(node_index,this->g);};template <class Graph, class RanVar >void FactorGraphGetProbabilities <Graph, RanVar>::operator() (unsigned int xi) { // What we must do here: // * set all the Potentials with the new value // * Compute the probability with a Product this->variable_potential.set_variable_value_by_index( this->random_variable_index, xi ); // setting the Potential attached to the Node // Setting the Potentials attached to the neighbouring Potentials for_each(begin_potential, end_potential, adapt_graph_vertexes <void> (this->g, boost::bind(boost::mem_fn <void, DiscretePotential, unsigned int, unsigned int> (&DiscretePotential::set_variable_value_by_index), boost::bind(&MessageNode <RanVar,VertexDescriptor> ::get_potential,_1),this->random_variable_index, xi) ) ); double a = this->variable_potential.get_potential_value() * make_product_over ( begin_potential, end_potential, adapt_graph_vertexes <double> (this->g, boost::bind(&DiscretePotential::get_potential_value, boost::bind(&MessageNode<RanVar,VertexDescriptor>::get_potential,_1)) ) ); // we now have our probability this->random_variable->sampling_probabilities[xi] = a;}template <class Graph, class RanVar >void FactorGraphGetProbabilities <Graph, RanVar>::set_sampled_value(unsigned int a){ this->variable_potential.set_variable_value_by_index( this->random_variable_index, a ); for_each(begin_potential, end_potential, adapt_graph_vertexes <void> (this->g, boost::bind(boost::mem_fn <void, DiscretePotential, unsigned int, unsigned int> (&DiscretePotential::set_variable_value_by_index), boost::bind(&MessageNode <RanVar,VertexDescriptor> ::get_potential,_1),this->random_variable_index, a) ) );}*/// ******************* End of FactorGraphGetProbabilities Implementation ***************** //// Explicit Template Instantiations ////typedef property< edge_index_t, unsigned int, DiscretePotential *> MRFEdgeProperty;//typedef adjacency_list<vecS, vecS, undirectedS, MessageNode <DiscreteRandomVariable, boost::adjacency_list_traits<vecS, vecS, undirectedS>::vertex_descriptor> *, MRFEdgeProperty > MRFGraph;//template class GetProbabilities <MRFGraph, DiscreteRandomVariable>;// Next one must be instantiated because of bad design in Gibbs sampler//template class FactorGraphGetProbabilities <MRFGraph, DiscreteRandomVariable>;//template class ComputeMarginal <DiscreteRandomVariable>;//template class ComputeMessage <DiscreteRandomVariable>;//template class MRFComputeMessage <DiscreteRandomVariable>;// Next two must also be explicitely instantiated because of bad design in the message passing visitors//template class VariableComputeMessage <DiscreteRandomVariable>;//template class PotentialComputeMessage <DiscreteRandomVariable>;// End Template Instantiations ///*template <class RanVar, class VertexDescriptor >void MessageNode <RanVar, VertexDescriptor >::normalize_probabilities(){ cout << "ERROR (FATAL): We called normalize_probabilities() on the base class MessageNode, that should never happen." << endl;}*/*template <class RanVar, class VertexDescriptor >DiscretePotential * MessageNode <RanVar, VertexDescriptor >::get_potential() const{ cout << "ERROR (FATAL): We called get_potential() on the base class MessageNode, that should never happen." << endl; return NULL;}*///*template <class RanVar, class VertexDescriptor >Message MessageNode <RanVar, VertexDescriptor >::send_message( MessageNode &){ cout << "ERROR (FATAL): We called new_send_message() on the base class MessageNode, that should never happen." << endl; return Message();}*//*template <class RanVar, class VertexDescriptor >void MessageNode <RanVar, VertexDescriptor >::update_message( Message){ cout << "ERROR (FATAL): We called new_receive_message() on the base class MessageNode, that should never happen." << endl;}*//*template <class RanVar, class VertexDescriptor >void MessageNode <RanVar, VertexDescriptor >::compute_marginals(){ cout << "ERROR (FATAL): We called compute_marginals() on the base class MessageNode, that should never happen." << endl;}*/template <class RanVar, class VertexDescriptor >void MessageNode <RanVar, VertexDescriptor >::sample_from_joint (DiscretePotential &){ cout << "ERROR (FATAL): We called sample_from_joint() on the base class MessageNode, that should never happen." << endl;}template <class RanVar, class VertexDescriptor >void MessageNode <RanVar, VertexDescriptor >::sample_from_root (){ cout << "ERROR (FATAL): We called sample_from_root() on the base class MessageNode, that should never happen." << endl;}/*template <class RanVar, class VertexDescriptor >Message & MessageNode <RanVar, VertexDescriptor >::get_message(unsigned int u){ list <Message>::iterator it = find_if(messages_list.begin(), messages_list.end(), bind(equal_to<int>(),u,bind(&Message::get_index,_1)) ); #ifndef NDEBUG if (it == messages_list.end()) { cout << "ERROR (FATAL): a message corresponding to the index " << u << " wasn't found " << endl; } #endif return *it; for (list <Message>:: iterator it = messages_list.begin(); it != messages_list.end(); it++) { if ( it->get_index() == u) { return *it; } } cout << "ERROR (FATAL): a message corresponding to the index " << u << " wasn't found " << endl; return (*messages_list.begin());}/* template <class RanVar, class Pot, class VertexDescriptor > void MRFMessageNode <RanVar, Pot, VertexDescriptor>::compute_marginals_from_messages(MessageNode <RanVar, VertexDescriptor> * , VertexDescriptor ) { ComputeMarginal <RanVar> comp_marginal_functor(*potential_ptr, this->messages_list); variable_ptr->compute_over_values(comp_marginal_functor); variable_ptr->normalize_probabilities(); } *//* template <class RanVar, class Pot, class VertexDescriptor > Message MRFMessageNode<RanVar, Pot, VertexDescriptor>::send_message(RanVar & destination_variable, ComputeMessage <RanVar> & comp_msg ) { // Set the Functor's properties comp_msg.set_origin_potential (potential_ptr); comp_msg.set_origin_random_variable (variable_ptr); // Compute over the values in the DESTINATION variable (that's tricky !) destination_variable.compute_over_values(comp_msg); comp_msg.set_message_index(variable_ptr->get_index()); // Return the compute message //comp_msg.get_message().display(); return comp_msg.get_message(); } */// Important design remarks: in order to be totally generic, for the sum we should use a polymorphic function called on the variable (a while loop that would depend on the number of possible values.)// Note that in the end all these value updates should be taken care of by the ProbabilisticModel (or at least the graph) itself via polymorphism...template <class RanVar, class Pot, class VertexDescriptor >void MRFMessageNode<RanVar, Pot, VertexDescriptor>::compute_probabilities_given_parent(){ for (unsigned int xi = 0; xi < variable_ptr->get_number_values(); ++xi) { variable_ptr->set_value(xi); parent_edge_potential.set_variable_value(*variable_ptr); potential_ptr->set_variable_value(*variable_ptr); variable_ptr->sampling_probabilities[xi] = potential_ptr->get_potential_value() * make_product_over (this->messages_list.begin(), this->messages_list.end(), ExtractFromMessage (xi) ) * parent_edge_potential.get_potential_value(); } // Don't forget to normalize variable_ptr->normalize_probabilities();} //typename graph_traits <Graph>::edge_descriptor e = edge(u,v,g).first; // Here we must compute p(v|u), and sample from it //unsigned int u_value = g[u]->get_joint_sampled_value(); // getting the value sampled for the parent //g[u]->get_random_variable()->set_value(u_value); //g[e]->set_variable_value (* g[u]->get_random_variable()); // setting the Potential to the current value, so that we can take a "slice" //g[v]->sample_from_joint(*g[e]);/*template <class RanVar, class Pot, class VertexDescriptor >void PotentialMessageNode<RanVar, Pot, VertexDescriptor>::normalize_marginals(){ // We don't need to do anything here}*//* template <class RanVar, class Pot, class VertexDescriptor > PotentialMessageNode < RanVar, Pot, VertexDescriptor >::PotentialMessageNode () { potential_ptr = NULL; } *//* template <class RanVar, class Pot, class VertexDescriptor > Message PotentialMessageNode < RanVar, Pot, VertexDescriptor >::send_message(RanVar & destination_variable, ComputeMessage <RanVar> & comp_msg) { // Setting the Functor's property comp_msg.set_product_messages( & product_messages); // this ugly reference could be avoided, with a better implementation comp_msg.set_origin_potential(potential_ptr); // In addition we need to set up the Message corresponding to the destination if we already have it... // UPDATE: Taken care directly in the function object... //Message m = get_message(destination_variable.get_index()); //comp_msg.set_destination_message (& m); // same here, this ugly reference could be avoided, with a better implementation // Compute over the Destination variable destination_variable.compute_over_values(comp_msg); //comp_msg.get_message().display(); return comp_msg.get_message(); } */// Unknown and deprecatedtemplate <class RanVar, class Pot, class VertexDescriptor >void MRFMessageNode <RanVar, Pot, VertexDescriptor>::normalize_probabilities(){ variable_ptr->normalize_probabilities ();} //unsigned int a = (*current_tree)[*u]->get_joint_sampled_value(); //DiscreteRandomVariable * current_rv = (*current_tree)[*u]->get_random_variable(); // HERE FOLLOWING IS BROKEN, needs to be done depending of wether factor graph / MRF I think //parent_node.set_variable_to_sampled_value(); //(*current_tree)[*u]->get_potential()->set_variable_valueparent_node //current_rv->set_value(0); // if it will be done correctly there is no need for that line // cout << "Sample from the joint, for " << current_tree->local_to_global(*u)+1 << ": " << a << endl; //for ( typename list <EdgeDescriptor>::iterator it = cut_edges[current_tree->local_to_global(*u)].begin(); it != cut_edges[current_tree->local_to_global(*u)].end(); ++it) //{ // g[*it]->set_variable_value ( *current_rv ); //}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -