📄 probabilisticmodel.cc.svn-base
字号:
cout << "Creating a Variable Node with internal index " << u+1 << endl; g[u] = node; } while ( (input_file.get() != ')') && (!input_file.eof()) ) { } if (input_file.eof()) { cout << "Error: malformed 'link' data tag" << endl; return false; } } return true;}bool DiscreteFactorGraphProbabilisticModel::unserialize_observations(ifstream & input_file){ typedef graph_traits<Graph>::vertex_descriptor VertexDescriptor; unsigned int a, b; while (true) { char c = input_file.get(); while ( !((c == '(') || (c == '<')) && (!input_file.eof()) ) { c = input_file.get(); } if (input_file.eof()) { break; } if (c == '<') { input_file.unget(); break; } if (!(input_file >> a >> b)) { input_file.clear(); cout << "Error: Malformed 'observations' data tag" << endl; return false; } else { VertexDescriptor u = vertex(a-1, g); g[u]->get_random_variable()->observe_variable_value(b); } while ( (input_file.get() != ')') && (!input_file.eof()) ) { } if (input_file.eof()) { cout << "Error: malformed 'observations' data tag" << endl; return false; } } return true;}bool DiscreteFactorGraphProbabilisticModel::unserialize_links(ifstream & input_file){ typedef graph_traits<Graph>::vertex_descriptor VertexDescriptor; unsigned int current_potential_node(number_random_variables); string code_string; if (!get_next_begin_token(input_file)) { return false; } getline(input_file, code_string, ')'); if (code_string.compare("normal") == 0) { while (true) { if (!get_next_begin_token(input_file)) { break; } unsigned int a(0); vector <unsigned int> link_vec; while (input_file >> a) { link_vec.push_back(a); } input_file.clear(); ++current_potential_node; VertexDescriptor u = vertex(current_potential_node-1, g); // current potential node DiscretePotential * pot_ptr = new DiscretePotential(); for (vector <unsigned int>::iterator it = link_vec.begin(); it != link_vec.end(); ++it) { VertexDescriptor v = vertex(*it-1, g); pot_ptr->add_variable(* g[v]->get_random_variable()); //cout << "Adding edge: " << u << " and " << v << endl; add_edge(u,v,g); } // It is probably wrong to create the MN now when the Potential is not yet fully built cout << "Creating a Potential Node with internal index " << u+1 << endl; g[u] = new PotentialMessageNode <DiscreteRandomVariable, DiscretePotential, VertexDescriptor> (current_potential_node, pot_ptr); while ( (input_file.get() != ')') && (!input_file.eof()) ) { } if (input_file.eof()) { cout << "Error: malformed 'link' data tag" << endl; return false; } } } if (code_string.compare("qmr") == 0) { while (true) { if (!get_next_begin_token(input_file)) { break; } unsigned int a(0); vector <unsigned int> link_vec; while (input_file >> a) { link_vec.push_back(a); } input_file.clear(); ++current_potential_node; VertexDescriptor u = vertex(current_potential_node-1, g); // current potential node DiscretePotential * pot_ptr = new QMRDiscretePotential(); for (vector <unsigned int>::iterator it = link_vec.begin(); it != link_vec.end(); ++it) { VertexDescriptor v = vertex(*it-1, g); pot_ptr->add_variable(* g[v]->get_random_variable()); //cout << "Adding edge: " << u << " and " << v << endl; add_edge(u,v,g); } // It is probably wrong to create the MN now when the Potential is not yet fully built cout << "Creating a QMR Potential Node with internal index " << u+1 << endl; g[u] = new PotentialMessageNode <DiscreteRandomVariable, DiscretePotential, VertexDescriptor> (current_potential_node, pot_ptr); while ( (input_file.get() != ')') && (!input_file.eof()) ) { } if (input_file.eof()) { cout << "Error: malformed 'link' data tag" << endl; return false; } } } return true;}bool DiscreteFactorGraphProbabilisticModel::unserialize_potential_tables(ifstream & input_file){ typedef graph_traits<Graph>::vertex_descriptor VertexDescriptor; unsigned int current_potential = number_random_variables; string code_string; if (!get_next_begin_token(input_file)) { return false; } getline(input_file, code_string, ')'); if ( (code_string.compare("normal") == 0) || (code_string.compare("qmr") == 0) ) { while (true) { if (!get_next_begin_token(input_file)) { break; } ++current_potential; VertexDescriptor u = vertex(current_potential-1, g); vector < double> potential_table; double a; while (input_file >> a) { potential_table.push_back(a); } static_cast < DiscretePotential *> (g[u]->get_potential() )->setup_potential_values(potential_table); input_file.clear(); while ( (input_file.get() != ')') && (!input_file.eof()) ) { } if (input_file.eof()) { cout << "Error: malformed 'potential tables' data tag" << endl; return false; } } } return true; }/*bool DiscreteFactorGraphProbabilisticModel::unserialize_internal_potentials(std::ifstream &){ typedef graph_traits<Graph>::vertex_descriptor VertexDescriptor; unsigned int current_rv = 0; string code_string; if (!get_next_begin_token(input_file)) { return false; } getline(input_file, code_string, ')'); if ( (code_string.compare("normal") == 0) || (code_string.compare("qmr") == 0) ) { while (true) { if (!get_next_begin_token(input_file)) { break; } VertexDescriptor u = vertex(current_rv, g); vector < double> potential_table; double a; while (input_file >> a) { potential_table.push_back(a); } static_cast < DiscretePotential *> (g[u]->get_potential() )->setup_potential_values(potential_table); input_file.clear(); while ( (input_file.get() != ')') && (!input_file.eof()) ) { } if (input_file.eof()) { cout << "Error: malformed 'potential tables' data tag" << endl; return false; } ++current_rv; } } return true; }*/bool DiscreteFactorGraphProbabilisticModel::unserialize_all_internal_potentials(ifstream & input_file){ vector < double> potential_table; string code_string; if (!get_next_begin_token(input_file)) { return false; } getline(input_file, code_string, ')'); if (code_string.compare("random-correlated") == 0) { if (!get_next_begin_token(input_file)) { return false; } double coefficient(1.0); if (!(input_file >> coefficient)) { cout << "Error: malformed 'random-correlated' data tag" << endl; return false; } for ( unsigned int i = 0; i < number_random_variables; ++i) { graph_traits<DiscreteFactorGraph>::vertex_descriptor u = vertex(i,g); unsigned int a = g[u]->get_random_variable()->get_number_values(); potential_table.resize(a); fill(potential_table.begin(), potential_table.end(), 1.0); uniform_int <int> uni_int_dist(0, a-1); variate_generator <boost::lagged_fibonacci607 &, uniform_int <int> > vg (fibonnacci_number_generator, uni_int_dist); potential_table[vg()] = coefficient; static_cast <DiscretePotential *> (g[u]->get_potential())->setup_potential_values(potential_table); } input_file.clear(); while ( (input_file.get() != ')') && (!input_file.eof()) ) { } if (input_file.eof()) { cout << "Error: malformed 'random-correlated' data tag" << endl; return false; } } if (code_string.compare("special") == 0) { if (!get_next_begin_token(input_file)) { return false; } double coefficient(1.0); if (!(input_file >> coefficient)) { cout << "Error: malformed 'special' data tag" << endl; return false; } for ( unsigned int i = 0; i < number_random_variables; ++i) { graph_traits<DiscreteFactorGraph>::vertex_descriptor u = vertex(i,g); unsigned int a = g[u]->get_random_variable()->get_number_values(); potential_table.resize(a); fill(potential_table.begin(), potential_table.end(), 1.0); uniform_int <int> uni_int_dist(0, 1); variate_generator <boost::lagged_fibonacci607 &, uniform_int <int> > vg (fibonnacci_number_generator, uni_int_dist); potential_table[vg()] = coefficient; static_cast <DiscretePotential *> (g[u]->get_potential())->setup_potential_values(potential_table); } input_file.clear(); while ( (input_file.get() != ')') && (!input_file.eof()) ) { } if (input_file.eof()) { cout << "Error: malformed 'special' data tag" << endl; return false; } } return true;}double DiscreteFactorGraphProbabilisticModel::compute_error_to_reference(){ graph_traits<Graph>::vertex_iterator u, u_end; double error(0.0); for ( tie(u, u_end) = vertices(g); u != u_end; ++u) { VariableMessageNode <DiscreteRandomVariable, DiscretePotential, graph_traits<Graph>::vertex_descriptor> * vmn = dynamic_cast < VariableMessageNode <DiscreteRandomVariable, DiscretePotential, graph_traits<Graph>::vertex_descriptor> *> ( g[*u]); if (vmn != NULL) { error += vmn->get_random_variable()->compute_error_to_reference(); } } return error;}void DiscreteFactorGraphProbabilisticModel::create_random_graph(const unsigned int density){ typedef graph_traits<Graph>::vertex_descriptor VertexDescriptor; unsigned int number_potentials = num_vertices(g) - number_random_variables; cout << 2 << "-" << density << endl; uniform_int <int> uni_int_dist(2, density); variate_generator <boost::lagged_fibonacci607 &, uniform_int <int> > vg (fibonnacci_number_generator, uni_int_dist); cout << 0 << "-" << number_random_variables-1 << endl; uniform_int <int> uni_int_dist_2(0, number_random_variables-1); //vector <DiscretePotential *> pot_ptr; variate_generator <boost::lagged_fibonacci607 &, uniform_int <int> > vg_2 (fibonnacci_number_generator, uni_int_dist_2); for (unsigned int i = 0; i < number_potentials; ++i) { VertexDescriptor u = vertex(i+number_random_variables, g); // current potential node //pot_ptr.push_back(new DiscretePotential()); unsigned int a = vg(); for (unsigned int j = 0; j < a;++j) { VertexDescriptor v = vertex(vg_2(), g); if (!edge(u,v,g).second) { add_edge(u,v,g); //pot_ptr->add_variable(* g[v]->get_random_variable()); } assert(edge(u,v,g).second); } //g[u] = new PotentialMessageNode <DiscreteRandomVariable, DiscretePotential, VertexDescriptor> (number_random_variables+1+i, pot_ptr); } cout << number_random_variables << "-" << number_random_variables+number_potentials-1 << endl; uniform_int <int> uni_int_dist_3(number_random_variables, number_random_variables+number_potentials-1); variate_generator <boost::lagged_fibonacci607 &, uniform_int <int> > vg_3 (fibonnacci_number_generator, uni_int_dist_3); for (unsigned int i = 0; i < number_random_variables; ++i) { VertexDescriptor u = vertex(i, g); // current variable node if (out_degree(u,g) == 0) { VertexDescriptor v = vertex(vg_3(), g); // current variable node //static_cast <DiscretePotential * >(g[v]->get_potential())->add_variable(* g[u]->get_random_variable()); add_edge(u,v,g); } } for (unsigned int i = 0; i < number_potentials; ++i) { VertexDescriptor u = vertex(i+number_random_variables, g); // current potential node DiscretePotential * pot_ptr = new DiscretePotential(); graph_traits<Graph>::adjacency_iterator v, v_end; for (tie (v, v_end) = adjacent_vertices(u,g); v != v_end; ++v) { pot_ptr->add_variable(* g[*v]->get_random_variable()); } g[u] = new PotentialMessageNode <DiscreteRandomVariable, DiscretePotential, VertexDescriptor> (number_random_variables+1+i, pot_ptr); } // Below ensures that we only have ONE connected component in our Random Graph. /* std::vector<unsigned int> component(number_of_nodes); unsigned int number_of_components = connected_components(g, &component[0]); std::vector<unsigned int>::size_type i; vector <unsigned int> component_contents (number_of_components); for (i = 0; i != number_of_nodes; ++i) { component_contents[component[i]] = i; } for (i = 0; i < number_of_components -1 ; ++i) { t = vertex(component_contents[i],g); u = vertex(component_contents[i+1],g); add_edge (t, u, g); } assert (connected_components(g, &component[0]) == 1); */ display_graph(g);}//***** End of DiscreteFactorGraphProbabilisticModel implementation ******//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -