⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 probabilisticmodel.cc.svn-base

📁 Probabilistic graphical models in matlab.
💻 SVN-BASE
📖 第 1 页 / 共 4 页
字号:
				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 'random-correlated' data tag" << endl;			return false;		}				for ( tie (u,u_end) = vertices(g); u != u_end; ++u)		{			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 'random-correlated' data tag" << endl;			return false;		}				}		return true;}bool DiscreteProbabilisticModel::unserialize_potential_tables(ifstream & input_file){		typedef graph_traits<Graph>::vertex_descriptor VertexDescriptor;	unsigned int a, b;		while (true)	{				if (!get_next_begin_token(input_file))		{			break;		}				if (!(input_file >> a >> b))		{			input_file.clear();			cout << "Error: Malformed 'potential tables' data tag" << endl;			return false;		}				else		{			VertexDescriptor u = vertex(a-1, g);			VertexDescriptor v = vertex(b-1, g);						while ( (input_file.get() != ')') && (!input_file.eof()) )			{			}						while ( (input_file.get() != '(') && (!input_file.eof()) )			{			}						if (input_file.eof())			{				cout << "Error: malformed 'potential tables' data tag" << endl;				return false;			}						vector < double> potential_table;			double c;						while (input_file >> c)			{				potential_table.push_back(c);			}						graph_traits<Graph>::edge_descriptor e = edge(u,v,g).first;						g[e] = new DiscretePotential();			g[e]->add_variable(* g[u]->get_random_variable());			g[e]->add_variable(* g[v]->get_random_variable());						g[e]->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;}double DiscreteProbabilisticModel::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)	{		error += g[*u]->get_random_variable()->compute_error_to_reference();	}		return error;}//***** End of DiscreteProbabilisticModel implementation ******////***** DiscreteFactorGraphProbabilisticModel implementation ******//// ConstructorDiscreteFactorGraphProbabilisticModel::DiscreteFactorGraphProbabilisticModel(unsigned int n): DerivedProbabilisticModel<DiscreteFactorGraph>(n), number_random_variables(0){}// Inherited methodsbool DiscreteFactorGraphProbabilisticModel::unserialize_all_random_variable_sizes(ifstream & input_file){		typedef graph_traits<Graph>::vertex_descriptor VertexDescriptor;		unsigned int size;		if(!get_next_begin_token(input_file))	{		return false;	}		if (!(input_file >> number_random_variables >> size))	{		input_file.clear();		cout << "Error: Malformed 'all random variable sizes' data tag" << endl;		return false;	}		else	{				for (unsigned int i = 1; i <= number_random_variables; ++i)		{			//cout << i << endl;			VertexDescriptor u = vertex(i-1, g);						DiscreteRandomVariable * rv;			VariableMessageNode <DiscreteRandomVariable, DiscretePotential, boost::adjacency_list_traits<vecS, vecS, undirectedS>::vertex_descriptor> * node;						rv = new DiscreteRandomVariable(i, size);			node = new VariableMessageNode<DiscreteRandomVariable, DiscretePotential, boost::adjacency_list_traits<vecS, vecS, undirectedS>::vertex_descriptor>(rv, new SingleDiscretePotential (*rv));						//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 'all random variable sizes' data tag" << endl;		return false;	}		//cout << "ok" << endl;		return true;}bool DiscreteFactorGraphProbabilisticModel::unserialize_all_potential_tables(ifstream & input_file){	graph_traits<Graph>::adjacency_iterator v, v_end;		typedef graph_traits<Graph>::vertex_descriptor VertexDescriptor;		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("all-random") == 0)	{		for ( unsigned int i = number_random_variables; i < num_vertices(g) ; ++i)		{			VertexDescriptor u = vertex(i, g);						unsigned int a = 1;						for ( tie (v,v_end) = adjacent_vertices(u,g); v!= v_end; ++v)			{				a = a * g[*v]->get_random_variable()->get_number_values();			}						potential_table.resize(a);						for (vector<double>::iterator it = potential_table.begin(); it !=  potential_table.end(); ++it)			{				*it= uniform_distribution_0_1();				}						static_cast < DiscretePotential * > (g[u]->get_potential())->setup_potential_values(potential_table);		}				return true;	}		// Here we create a one strongly diagonal potential and use it for all the potentials in the graph			if (code_string.compare("all-random-strongly-diagonal-random-noise") == 0)	{		if (!get_next_begin_token(input_file))		{			return false;		}				double strongly_diagonal_coefficient(1.0);				if (!(input_file >> strongly_diagonal_coefficient))		{			cout << "Error: malformed 'one-random-strongly-diagonal-random-noise' data tag" << endl;			return false;		}				for ( unsigned int i = number_random_variables; i < num_vertices(g) ; ++i)		{						VertexDescriptor u = vertex(i, g);						unsigned int a = 1;						for ( tie (v,v_end) = adjacent_vertices(u,g); v!= v_end; ++v)			{				a = a * g[*v]->get_random_variable()->get_number_values();			}						potential_table.resize(a);						for (vector<double>::iterator it = potential_table.begin(); it !=  potential_table.end(); ++it)			{				*it= uniform_distribution_0_1();				}						static_cast < DiscretePotential * > (g[u]->get_potential())->setup_potential_values(potential_table);						tie (v,v_end) = adjacent_vertices(u,g);						VertexDescriptor w = *v;						while (g[w]->get_random_variable()->loop_over())			{				for ( tie (v,v_end) = adjacent_vertices(u,g); v!= v_end; ++v)				{					g[*v]->get_random_variable()->set_equal_to_variable( * g[w]->get_random_variable());					g[u]->get_potential()->set_variable_value ( * g[*v]->get_random_variable());				}								static_cast < DiscretePotential * > (g[u]->get_potential())->modify_potential_value( uniform_distribution_0_1()*strongly_diagonal_coefficient);			}					}				input_file.clear();				while ( (input_file.get() != ')') && (!input_file.eof()) )		{		}				if (input_file.eof())		{			cout << "Error: malformed 'all-random-strongly-diagonal-random-noise' data tag" << endl;			return false;		}					return true;	}		if (code_string.compare("all-random-strongly-diagonal-uniform-noise") == 0)	{		if (!get_next_begin_token(input_file))		{			return false;		}				double strongly_diagonal_coefficient(1.0);				if (!(input_file >> strongly_diagonal_coefficient))		{			cout << "Error: malformed 'all-random-strongly-diagonal-uniform-noise' data tag" << endl;			return false;		}				for ( unsigned int i = number_random_variables; i < num_vertices(g) ; ++i)		{		//cout << "Potential " << i+1 << endl;						VertexDescriptor u = vertex(i, g);			unsigned int a = 1;						for ( tie (v,v_end) = adjacent_vertices(u,g); v!= v_end; ++v)			{				a = a * g[*v]->get_random_variable()->get_number_values();			}						potential_table.resize(a);						for (vector<double>::iterator it = potential_table.begin(); it !=  potential_table.end(); ++it)			{				*it= 1.0;				}						static_cast < DiscretePotential * > (g[u]->get_potential())->setup_potential_values(potential_table);						tie (v,v_end) = adjacent_vertices(u,g);						VertexDescriptor w = *v;						while (g[w]->get_random_variable()->loop_over())			{				for ( tie (v,v_end) = adjacent_vertices(u,g); v!= v_end; ++v)				{					g[*v]->get_random_variable()->set_equal_to_variable( * g[w]->get_random_variable());					g[u]->get_potential()->set_variable_value ( * g[*v]->get_random_variable());				}												static_cast < DiscretePotential * > (g[u]->get_potential())->modify_potential_value( uniform_distribution_0_1()*strongly_diagonal_coefficient);			}					}				input_file.clear();				while ( (input_file.get() != ')') && (!input_file.eof()) )		{		}				if (input_file.eof())		{			cout << "Error: malformed 'all-random-strongly-diagonal-uniform-noise' data tag" << endl;			return false;		}					return true;	}		if (code_string.compare("all-uniform-strongly-diagonal-uniform-noise") == 0)	{		if (!get_next_begin_token(input_file))		{			return false;		}				double strongly_diagonal_coefficient(1.0);				if (!(input_file >> strongly_diagonal_coefficient))		{			cout << "Error: malformed 'one-uniform-strongly-diagonal-uniform-noise' data tag" << endl;			return false;		}				for ( unsigned int i = number_random_variables; i < num_vertices(g) ; ++i)		{			VertexDescriptor u = vertex(i, g);						unsigned int a = 1;						for ( tie (v,v_end) = adjacent_vertices(u,g); v!= v_end; ++v)			{				a = a * g[*v]->get_random_variable()->get_number_values();			}						potential_table.resize(a);						for (vector<double>::iterator it = potential_table.begin(); it !=  potential_table.end(); ++it)			{				*it= 1.0;				}						static_cast < DiscretePotential * > (g[u]->get_potential())->setup_potential_values(potential_table);						tie (v,v_end) = adjacent_vertices(u,g);						VertexDescriptor w = *v;						while (g[w]->get_random_variable()->loop_over())			{				for ( tie (v,v_end) = adjacent_vertices(u,g); v!= v_end; ++v)				{					g[*v]->get_random_variable()->set_equal_to_variable( * g[w]->get_random_variable());					g[u]->get_potential()->set_variable_value ( * g[*v]->get_random_variable());				}												static_cast < DiscretePotential * > (g[u]->get_potential())->modify_potential_value( strongly_diagonal_coefficient);			}					}				input_file.clear();				while ( (input_file.get() != ')') && (!input_file.eof()) )		{		}				if (input_file.eof())		{			cout << "Error: malformed 'all-uniform-strongly-diagonal-uniform-noise' data tag" << endl;			return false;		}					return true;	}		return true;	}bool DiscreteFactorGraphProbabilisticModel::unserialize_structure(ifstream & input_file){	unsigned int a, b;		string code_string;		if (!get_next_begin_token(input_file))	{		return false;	}		getline(input_file, code_string, ')');		if (code_string.compare("random") == 0)	{		while ( (input_file.get() != '(')  && (!input_file.eof()) )		{		}				if (input_file.eof())		{			cout << "Error, no parameters given to random" << endl;			return false;		}				if (!(input_file >> a >> b))		{			input_file.clear();			cout << "Error: Malformed 'structure' data tag" << endl;			return false;		}		else		{			number_random_variables = a;						create_random_graph(b);		}	}			input_file.clear();		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_random_variable_sizes(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 'RV size' data tag" << endl;			return false;		}		else		{			++number_random_variables;						VertexDescriptor u = vertex(a-1, g);						DiscreteRandomVariable * rv;			VariableMessageNode <DiscreteRandomVariable, DiscretePotential, boost::adjacency_list_traits<vecS, vecS, undirectedS>::vertex_descriptor> * node;						rv = new DiscreteRandomVariable(a, b);			node = new VariableMessageNode<DiscreteRandomVariable, DiscretePotential, boost::adjacency_list_traits<vecS, vecS, undirectedS>::vertex_descriptor>(rv, new SingleDiscretePotential (*rv));			

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -