📄 mrf_graph_test.cc.svn-base
字号:
#ifdef NOTUSED#include <iostream>#include <GraphAlgorithms.h>#include <RandomVariable.h>#include <Potential.h>#include <Node.h>using namespace std;using namespace boost;void create_mrf_graph_test(){ cout << "Testing an MRF Graph..." << endl; // Creating the Graph 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 > Graph; Graph G; typedef boost::graph_traits<Graph>::vertex_descriptor VertexDescriptor; DiscretePotential * cp = new ConstantPotential(); DiscreteRandomVariable * rv_1 = new DiscreteRandomVariable(1,2); DiscreteRandomVariable * rv_2 = new DiscreteRandomVariable(2,2); DiscreteRandomVariable * rv_3 = new DiscreteRandomVariable(3,2); DiscreteRandomVariable * rv_4 = new DiscreteRandomVariable(4,2); DiscreteRandomVariable * rv_5 = new DiscreteRandomVariable(5,2); DiscreteRandomVariable * rv_6 = new DiscreteRandomVariable(6,2); typedef MRFMessageNode < DiscreteRandomVariable, DiscretePotential, boost::adjacency_list_traits <vecS, vecS, undirectedS>::vertex_descriptor > MRFDiscreteNode; MRFDiscreteNode * v_1 = new MRFDiscreteNode (rv_1, cp); MRFDiscreteNode * v_2 = new MRFDiscreteNode (rv_2, cp); MRFDiscreteNode * v_3 = new MRFDiscreteNode (rv_3, cp); MRFDiscreteNode * v_4 = new MRFDiscreteNode (rv_4, cp); MRFDiscreteNode * v_5 = new MRFDiscreteNode(rv_5, cp); MRFDiscreteNode * v_6 = new MRFDiscreteNode (rv_6, cp); /* No pointers version MRFMessageNode<DiscreteRandomVariable, Potential, boost::graph_traits<HelperGraph>::vertex_descriptor> v_1 (rv_1, cp); MRFMessageNode<DiscreteRandomVariable, Potential, boost::graph_traits<HelperGraph>::vertex_descriptor> v_2 (rv_2, cp); MRFMessageNode<DiscreteRandomVariable, Potential, boost::graph_traits<HelperGraph>::vertex_descriptor> v_3 (rv_3, cp); MRFMessageNode<DiscreteRandomVariable, Potential, boost::graph_traits<HelperGraph>::vertex_descriptor> v_4 (rv_4, cp); MRFMessageNode<DiscreteRandomVariable, Potential, boost::graph_traits<HelperGraph>::vertex_descriptor> v_5 (rv_5, cp); MRFMessageNode<DiscreteRandomVariable, Potential, boost::graph_traits<HelperGraph>::vertex_descriptor> v_6 (rv_6, cp); */ VertexDescriptor vd_1 = add_vertex(v_1, G); VertexDescriptor vd_2 = add_vertex(v_2, G); VertexDescriptor vd_3 = add_vertex(v_3, G); VertexDescriptor vd_4 = add_vertex(v_4, G); VertexDescriptor vd_5 = add_vertex(v_5, G); VertexDescriptor vd_6 = add_vertex(v_6, G); // As a sidenote: we could maybe construct these Potentials when we add the edges in the Graph, since it already contains the // informations about which variables should be linked to the Potential DiscretePotential * p_1 = new DiscretePotential(); p_1->add_variable(*rv_1); p_1->add_variable(*rv_2); DiscretePotential * p_2 = new DiscretePotential(); p_2->add_variable(*rv_2); p_2->add_variable(*rv_3); DiscretePotential * p_3 = new DiscretePotential(); p_3->add_variable(*rv_3); p_3->add_variable(*rv_4); DiscretePotential * p_4 = new DiscretePotential(); p_4->add_variable(*rv_3); p_4->add_variable(*rv_5); DiscretePotential * p_5 = new DiscretePotential(); p_5->add_variable(*rv_5); p_5->add_variable(*rv_6); vector <double> values_vec; values_vec.push_back (100); values_vec.push_back (4); values_vec.push_back (100); values_vec.push_back (5); p_1->setup_potential_values (values_vec); values_vec[0] = 8; values_vec[1] = 3; values_vec[2] = 7; values_vec[3] = 8; p_2->setup_potential_values (values_vec); values_vec[0] = 1; values_vec[1] = 5; values_vec[2] = 6; values_vec[3] = 12; p_3->setup_potential_values (values_vec); values_vec[0] = 9; values_vec[1] = 2; values_vec[2] = 4; values_vec[3] = 3; p_4->setup_potential_values (values_vec); values_vec[0] = 7; values_vec[1] = 5; values_vec[2] = 7; values_vec[3] = 1; p_5->setup_potential_values (values_vec); // Yes... it does seem really stupid to do that twice, and may introduce errors... G[add_edge(vd_1, vd_2, G).first]= p_1; G[add_edge(vd_2, vd_3, G).first]= p_2; G[add_edge(vd_3, vd_4, G).first]= p_3; G[add_edge(vd_3, vd_5, G).first]= p_4; G[add_edge(vd_5, vd_6, G).first]= p_5; // Ok. Our Graph is done! //End of graph creation cout << "Starting Message Passing..." << endl; message_passing (G); rv_1->display_probabilities(); rv_2->display_probabilities(); rv_3->display_probabilities(); rv_4->display_probabilities(); rv_5->display_probabilities(); rv_6->display_probabilities(); // Warning: the Graph isn't reinitialized, and in fact this can have side effects. // Example: the variables keep a memory of their marginals, Potentials are linked to the values where they were previously... // I should implement initialization routines. cout << "Starting Tree Gibbs Sampler..." << endl; tree_gibbs_sampler (G,2,10); rv_1->display_probabilities(); rv_2->display_probabilities(); rv_3->display_probabilities(); rv_4->display_probabilities(); rv_5->display_probabilities(); rv_6->display_probabilities(); cout << "Starting Gibbs Sampler..." << endl; gibbs_sampler (G,1000,100); // Display of Results rv_1->display_probabilities(); rv_2->display_probabilities(); rv_3->display_probabilities(); rv_4->display_probabilities(); rv_5->display_probabilities(); rv_6->display_probabilities(); delete cp; delete rv_1; delete rv_2; delete rv_3; delete rv_4; delete rv_5; delete rv_6; delete p_1; delete p_2; delete p_3; delete p_4; delete p_5;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -