📄 old_code_2.svn-base
字号:
/*void SingleDiscretePotential::set_variable_value_by_index(unsigned int, unsigned int b){ variable_value = b;}void SingleDiscretePotential::set_variable_value_by_index(pair<unsigned int,unsigned int> a){ variable_value = a.second;}*//*void SingleDiscretePotential::set_variable_value(RandomVariable & rv){ variable_value = rv.last_sampled_value;}*//* inline double SingleDiscretePotential::obtain_potential_value(unsigned int, unsigned int b) { variable_value = b; return single_potential_table[variable_value]; } inline double SingleDiscretePotential::obtain_potential_value(std::pair<unsigned int,unsigned int> a) { variable_value = a.second; return single_potential_table[variable_value]; } inline double SingleDiscretePotential::obtain_potential_value(unsigned int a) { variable_value = a; return single_potential_table[variable_value]; } */ /* switch (g[u]->get_type()) { case markov_random_field: { typename graph_traits <Graph>::edge_descriptor e = edge(u,v,g).first; // Create the functor, the syntax is MRFComputeMessage ( edge_potential, origin_variable, origin_potential, origin_messages_list) MRFComputeMessage <RandomVariableClass> comp_msg (*g[e], g[u]->obtain_messages_list()); g[v]->receive_message ( g[u]->send_message(*(g[v]->get_random_variable()), comp_msg) ); }; break; case variable: { // Create the functor VariableComputeMessage <RandomVariableClass> comp_msg (g[u]->obtain_messages_list(), v); g[v]->receive_message ( g[u]->send_message(*(g[u]->get_random_variable()), comp_msg) ); }; break; case potential : { PotentialComputeMessage <RandomVariableClass> comp_msg (g[u]->obtain_messages_list()); comp_msg.set_message_index(u); g[v]->receive_message ( g[u]->send_message(*(g[v]->get_random_variable()), comp_msg) ); }; break; default: cout << "ERROR (FATAL): we encountered an unknown type of MessageNode." << endl; } */ /* switch (g[u]->get_type()) { case markov_random_field: { typename graph_traits <Graph>::edge_descriptor e = edge(u,v,g).first; MRFComputeMessage <RandomVariableClass> comp_msg (*g[e], g[u]->obtain_messages_list()); g[v]->receive_message ( g[u]->send_message(*(g[v]->get_random_variable()), comp_msg) ); }; break; case variable: { VariableComputeMessage <RandomVariableClass> comp_msg (g[u]->obtain_messages_list(), v); g[v]->receive_message ( g[u]->send_message(* g[u]->get_random_variable(), comp_msg) ); }; break; case potential : { PotentialComputeMessage <RandomVariableClass> comp_msg ( g[u]->obtain_messages_list()); comp_msg.set_message_index(u); g[v]->receive_message ( g[u]->send_message(*(g[v]->get_random_variable()), comp_msg) ); }; break; default: cout << "ERROR (FATAL): We encountered an unknown type of MessageNode." << endl; } */ // ******************* ComputeMessage Implementation ***************** //template <class RanVar>ComputeMessage <RanVar>::~ComputeMessage(){}template <class RanVar>ComputeMessage <RanVar>::ComputeMessage(list <Message> & msg_lst): messages_list(msg_lst){}template <class RanVar>Message ComputeMessage <RanVar>::get_message(){ // Note that this function first "reduces" the Message in order to avoid overflows message.reduce(); return message;}template <class RanVar>void ComputeMessage<RanVar>::set_origin_random_variable(RanVar * ){ cout << "ERROR (FATAL): We called set_origin_random_variable() on the base class ComputeMessage, that should never happen." << endl;}template <class RanVar>void ComputeMessage<RanVar>::set_product_messages (vector <double> * ){ cout << "ERROR (CRITICAL): We called set_product_messages() on the base class ComputeMessage, that should never happen." << endl;}// ******************* End of ComputeMessage Implementation ***************** //// ******************* MRFComputeMessage Implementation ***************** //template <class RanVar>MRFComputeMessage <RanVar>::MRFComputeMessage(DiscretePotential & ptl, list <Message> & msg_lst): ComputeMessage<RanVar>( msg_lst), edge_potential(ptl), distribute_flag(false){}template <class RanVar>void MRFComputeMessage <RanVar>::set_random_variable(RanVar * a){ destination_random_variable = a; unsigned int destination_random_variable_idx = a->get_index(); this->message.set_size(a->get_number_values()); list <Message>::iterator destination_message = find_if(this->messages_list.begin(), this->messages_list.end(), bind(equal_to<int>(),destination_random_variable_idx,bind(&Message::get_index,_1)) ); if ( destination_message != this->messages_list.end()) { this->messages_list.push_back(*destination_message); this->messages_list.erase(destination_message); distribute_flag = true; }}template <class RanVar>void MRFComputeMessage <RanVar>::operator() (unsigned int xi) { //cout << "this is an MRF compute Msg" << endl; double sum = 0; //unsigned int origin_index = origin_random_variable->get_index(); destination_random_variable->set_value(xi); edge_potential.set_variable_value(*destination_random_variable); //cout << "0: " << edge_potential.obtain_potential_value(origin_index,0) << endl; //cout << "1: " << edge_potential.obtain_potential_value(origin_index,1) << endl; list<Message>::iterator messages_list_used_end = this->messages_list.end(); if (distribute_flag) { --messages_list_used_end; } if (origin_random_variable->is_conditionned() ) { unsigned int xj = origin_random_variable->last_sampled_value; this->origin_potential->set_variable_value(*origin_random_variable); edge_potential.set_variable_value(*origin_random_variable); sum = this->origin_potential->get_potential_value() * edge_potential.get_potential_value() * make_product_over (this->messages_list.begin(), messages_list_used_end, ExtractFromMessage (xj) ); } else { for (unsigned int xj = 0; xj < origin_random_variable->get_number_values(); ++xj) { origin_random_variable->set_value(xj); this->origin_potential->set_variable_value(*origin_random_variable); edge_potential.set_variable_value(*origin_random_variable); sum += this->origin_potential->get_potential_value() * edge_potential.get_potential_value() * make_product_over (this->messages_list.begin(), messages_list_used_end, ExtractFromMessage (xj) ); } } //this->message.set_value_by_index(xi, sum);}// ******************* End of MRFComputeMessage Implementation ***************** //// ******************* VariableComputeMessage Implementation ***************** //template <class RanVar>VariableComputeMessage <RanVar>::VariableComputeMessage(list <Message> & msg_lst, unsigned int a): ComputeMessage<RanVar>(msg_lst), destination_potential_idx(a){ destination_message = find_if(this->messages_list.begin(), this->messages_list.end(), bind(equal_to<int>(),destination_potential_idx,bind(&Message::get_index,_1)) );}template <class RanVar>void VariableComputeMessage <RanVar>::set_random_variable(RanVar * a){ origin_random_variable = a; this->message.set_size(a->get_number_values());}template <class RanVar>void VariableComputeMessage<RanVar>::operator() (unsigned int xi){ double a = this->origin_potential->obtain_potential_value(origin_random_variable->get_index(),xi) * make_product_over (this->messages_list.begin(), this->messages_list.end(), ExtractFromMessage (xi)); if ( destination_message != this->messages_list.end()) { // a = a/destination_message->get_value_by_index(xi); } //this->message.set_value_by_index(xi,a); }/* template <class RanVar> void VariableComputeMessage<RanVar>::set_message_index (unsigned int a) { message.set_index(a); } */// ******************* End of VariableComputeMessage Implementation ***************** //// ******************* PotentialComputeMessage Implementation ***************** //template <class RanVar>PotentialComputeMessage <RanVar>::PotentialComputeMessage(list <Message> & msg_lst): ComputeMessage<RanVar>(msg_lst){}template <class RanVar>void PotentialComputeMessage <RanVar>::set_random_variable(RanVar * a){ destination_random_variable_idx = a->get_index(); this->message.set_size(a->get_number_values()); destination_message = find_if(this->messages_list.begin(), this->messages_list.end(), bind(equal_to<int>(),destination_random_variable_idx,bind(&Message::get_index,_1)) );}template <class RanVar>void PotentialComputeMessage<RanVar>::operator() (unsigned int xi){ double a = this->origin_potential->sum_product(destination_random_variable_idx, xi, *product_messages); if ( destination_message != this->messages_list.end()) { // a = a/destination_message->get_value_by_index(xi); } //this->message.set_value_by_index(xi,a);}/* template <class RanVar> void PotentialComputeMessage <RanVar>::set_message_index (unsigned int a) { message.set_index(a); } */// ******************* End of PotentialComputeMessage Implementation ***************** // // ******************* ComputeMessage Declaration ******************** //// This ComputeMessage class is used as a function object to compute a message// There is a base class and several derived classes, they all are used as helperstemplate <class RanVar>class ComputeMessage : public ComputeOverInt <RanVar> { protected: Message message; // will be used to send the new message, every subclass will need it // This is the potential present at the node; if we assume that every variable has an attached Potential, every subclass will // need it so it is in the base class DiscretePotential * origin_potential; std::list < Message> & messages_list; // this is the message list used in the original Variable node public: ~ComputeMessage(); ComputeMessage(std::list <Message> &); inline void set_origin_potential(DiscretePotential *); void set_message_index (unsigned int); Message get_message(); virtual void set_origin_random_variable (RanVar *); virtual void set_product_messages (std::vector <double> *);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -