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

📄 probabilisticmodel.cc.svn-base

📁 Probabilistic graphical models in matlab.
💻 SVN-BASE
📖 第 1 页 / 共 4 页
字号:
		else		{			a = 0;			graph_traits<Graph>::vertex_iterator u, u_end;						for (tie (u, u_end) = vertices(g); u != u_end; ++u)			{				++a;								DiscreteRandomVariable * rv;				MRFMessageNode <DiscreteRandomVariable, DiscretePotential, boost::adjacency_list_traits<vecS, vecS, undirectedS>::vertex_descriptor> * node;								rv = new DiscreteRandomVariable(a, b);				node = new MRFMessageNode<DiscreteRandomVariable, DiscretePotential, boost::adjacency_list_traits<vecS, vecS, undirectedS>::vertex_descriptor>(rv, new SingleDiscretePotential (*rv));								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 DiscreteProbabilisticModel::unserialize_random_variable_sizes(ifstream & input_file){		typedef graph_traits<Graph>::vertex_descriptor VertexDescriptor;		unsigned int a, b;		while (true)	{		if (!get_next_begin_token(input_file))		{			return false;		}				if (!(input_file >> a >> b))		{			input_file.clear();			cout << "Error: Malformed 'link' data tag" << endl;			return false;		}		else		{						VertexDescriptor u = vertex(a-1, g);						DiscreteRandomVariable * rv;			MRFMessageNode <DiscreteRandomVariable, DiscretePotential, boost::adjacency_list_traits<vecS, vecS, undirectedS>::vertex_descriptor> * node;						rv = new DiscreteRandomVariable(a, b);			node = new MRFMessageNode<DiscreteRandomVariable, DiscretePotential, boost::adjacency_list_traits<vecS, vecS, undirectedS>::vertex_descriptor>(rv, new SingleDiscretePotential (*rv));						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 DiscreteProbabilisticModel::unserialize_observations(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 '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 DiscreteProbabilisticModel::unserialize_links(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 'link' data tag" << endl;			return false;		}				else		{			VertexDescriptor u = vertex(a-1, g);			VertexDescriptor v = vertex(b-1, g);						graph_traits<Graph>::edge_descriptor e = add_edge(u,v,g).first;			}				while ( (input_file.get() != ')') && (!input_file.eof()) )		{		}						if (input_file.eof())		{			cout << "Error: malformed 'link' data tag" << endl;			return false;		}			}		return true;}bool DiscreteProbabilisticModel::unserialize_all_potential_tables(ifstream & input_file){			graph_traits<Graph>::edge_iterator e, e_end;		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 ( tie (e,e_end) = edges(g); e != e_end; ++e)		{						unsigned int a = g[source(*e, g)]->get_random_variable()->get_number_values();			unsigned int b = g[target(*e, g)]->get_random_variable()->get_number_values();						potential_table.resize(a*b);						for (vector<double>::iterator it = potential_table.begin(); it !=  potential_table.end(); ++it)			{				*it= uniform_distribution_0_1();				}						g[*e] = new DiscretePotential();			g[*e]->add_variable(* g[source(*e, g)]->get_random_variable());			g[*e]->add_variable(* g[target(*e, g)]->get_random_variable());						g[*e]->setup_potential_values(potential_table);		}				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 ( tie (e,e_end) = edges(g); e != e_end; ++e)		{						unsigned int a = g[source(*e, g)]->get_random_variable()->get_number_values();			unsigned int b = g[target(*e, g)]->get_random_variable()->get_number_values();						if (a != b)			{				cout << "The potential here is not a squared  matrix, I cannot make it strongly diagonal with noise." << endl;				return false;			}						potential_table.resize(a*b);						for (unsigned int i = 0; i < potential_table.size(); ++i)			{				if ( (i % a) == ( i/a ) )				{					potential_table[i] = uniform_distribution_0_1()*strongly_diagonal_coefficient;				}								else				{					potential_table[i] = 1.0;				}							}						g[*e] = new DiscretePotential();			g[*e]->add_variable(* g[source(*e, g)]->get_random_variable());			g[*e]->add_variable(* g[target(*e, g)]->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 'one-random-strongly-diagonal-random-noise' data tag in 'all potential tables' tag" << endl;			return false;		}					return true;	}		if (code_string.compare("normal") == 0)	{		if (!get_next_begin_token(input_file))		{			return false;		}				double a;				while (input_file >> a)		{			potential_table.push_back(a);		}				input_file.clear();				while ( (input_file.get() != ')') && (!input_file.eof()) )		{		}				if (input_file.eof())		{			cout << "Error: malformed 'normal' data tag in 'all potential tables' tag" << endl;			return false;		}			}		// Following will create one random potential and use it for all the potentials in the graph		if (code_string.compare("one-random") == 0)	{		tie (e,e_end) = edges(g);				unsigned int a = g[source(*e, g)]->get_random_variable()->get_number_values();		unsigned int b = g[target(*e, g)]->get_random_variable()->get_number_values();				potential_table.resize(a*b);				for (vector<double>::iterator it = potential_table.begin(); it !=  potential_table.end(); ++it)		{			*it= uniform_distribution_0_1();			}			}		// Here we create a one strongly diagonal potential and use it for all the potentials in the graph		if (code_string.compare("one-random-strongly-diagonal-random-noise") == 0)	{		tie (e,e_end) = edges(g);				unsigned int a = g[source(*e, g)]->get_random_variable()->get_number_values();		unsigned int b = g[target(*e, g)]->get_random_variable()->get_number_values();				if (a != b)		{			cout << "The potential here is not a squared  matrix, I cannot make it strongly diagonal with noise." << endl;			return false;		}				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 in 'all potential tables' tag" << endl;			return false;		}				potential_table.resize(a*b);				for (unsigned int i = 0; i < potential_table.size(); ++i)		{			if ( (i % a) == ( i/a ) )			{				potential_table[i] = uniform_distribution_0_1()*strongly_diagonal_coefficient;			}						else			{				potential_table[i] = uniform_distribution_0_1();			}					}						input_file.clear();				while ( (input_file.get() != ')') && (!input_file.eof()) )		{		}				if (input_file.eof())		{			cout << "Error: malformed 'one-random-strongly-diagonal-random-noise' data tag in 'all potential tables' tag" << endl;			return false;		}				}		if (code_string.compare("one-random-strongly-diagonal-uniform-noise") == 0)	{		tie (e,e_end) = edges(g);				unsigned int a = g[source(*e, g)]->get_random_variable()->get_number_values();		unsigned int b = g[target(*e, g)]->get_random_variable()->get_number_values();				if (a != b)		{			cout << "The potential here is not a squared  matrix, I cannot make it strongly diagonal with noise." << endl;			return false;		}				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-uniform-noise' data tag in 'all potential tables' tag" << endl;			return false;		}				potential_table.resize(a*b);				for (unsigned int i = 0; i < potential_table.size(); ++i)		{			if ( (i % a) == ( i/a ) )			{				potential_table[i] = uniform_distribution_0_1()*strongly_diagonal_coefficient;			}						else			{				potential_table[i] = 1.0;			}					}				/*		 for (unsigned int i = 0; i < potential_table.size(); ++i)		 {			 cout << potential_table[i] << " ";		 }		 */						input_file.clear();				while ( (input_file.get() != ')') && (!input_file.eof()) )		{		}				if (input_file.eof())		{			cout << "Error: malformed 'one-random-strongly-diagonal-uniform-noise' data tag in 'all potential tables' tag" << endl;			return false;		}				}		if (code_string.compare("one-uniform-strongly-diagonal-uniform-noise") == 0)	{		tie (e,e_end) = edges(g);				unsigned int a = g[source(*e, g)]->get_random_variable()->get_number_values();		unsigned int b = g[target(*e, g)]->get_random_variable()->get_number_values();				if (a != b)		{			cout << "The potential here is not a squared  matrix, I cannot make it strongly diagonal with noise." << endl;			return false;		}				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;		}				potential_table.resize(a*b);				for (unsigned int i = 0; i < potential_table.size(); ++i)		{			if ( (i % a) == ( i/a ) )			{				potential_table[i] = strongly_diagonal_coefficient;			}						else			{				potential_table[i] = 1.0;			}					}				input_file.clear();				while ( (input_file.get() != ')') && (!input_file.eof()) )		{		}				if (input_file.eof())		{			cout << "Error: malformed 'one-uniform-strongly-diagonal-uniform-noise' data tag" << endl;			return false;		}				}		for ( tie (e,e_end) = edges(g); e != e_end; ++e)	{		g[*e] = new UniqueDiscretePotential();		g[*e]->add_variable(* g[source(*e, g)]->get_random_variable());		g[*e]->add_variable(* g[target(*e, g)]->get_random_variable());				//cout << "Edge between " << source(*e, g) << " and " << target(*e, g) << endl;				g[*e]->setup_potential_values(potential_table);	}		//cout << "Done with potential tables" << endl;	return true;}bool DiscreteProbabilisticModel::unserialize_all_internal_potentials(ifstream & input_file){			graph_traits<Graph>::vertex_iterator u, u_end;		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 ( 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, 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()) )		{		}

⌨️ 快捷键说明

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