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

📄 discretepotential.h.svn-base

📁 Probabilistic graphical models in matlab.
💻 SVN-BASE
字号:
#ifndef DISCRETEPOTENTIAL_H#define DISCRETEPOTENTIAL_H#include <cmath>#include <Potential.h>// ******************* DiscretePotential Declaration *****************//// This class implements a Potential as a matrix, which may be high-D. No support for sparse matrices or such, so this is probably not usable if we have a Potential depending on a large number of variables.// This class can only be used with Discrete Random Variables - for now// We should put this class as an abstract class.../*class DiscretePotential: public Potential{	}*/class DiscretePotential: public Potential{	public:		// Constructors, destructor			DiscretePotential();	DiscretePotential(const std::vector <DiscreteRandomVariable *> &);		~DiscretePotential();		// Virtual functions, inherited		double get_potential_value() const;	void set_variable_value (const RandomVariable &);		// Virtual functions, defined		virtual void modify_potential_value (const double);	virtual void setup_potential_values (const std::vector <double> &);		virtual void add_variable (const DiscreteRandomVariable &);	virtual double sum_product_messages_fixed (const DiscreteRandomVariable &, const std::vector <double> &);		// Virtual functions, defined, later inherited virtual too		virtual void set_observed_variable (const RandomVariable &);	virtual void unset_observed_variable (const RandomVariable &);		// Normal functions			// Obsolete methods		void obtain_numbers(const unsigned int, unsigned int &, unsigned int &);		// Debug methods	#ifndef NDEBUG		virtual void display_table()	{		std::cout << "Displaying table: " << std::endl;				for (std::vector <double>::iterator it = potential_table.begin(); it != potential_table.end(); it++)		{			std::cout << *it << ", ";		}		std::cout << std::endl;	}	#endif		// Friends		template <class A, class B, class C> friend class PotentialMessageNode;		template <class A, class B, class C> friend class VariableMessageNode;	protected:			//double internal_sum_product_messages (const unsigned int, const std::vector <double> & );	virtual double recursive_internal_sum_product_messages_fixed (const unsigned int, const std::vector <double> & );		virtual unsigned int get_table_size() const	{		return potential_table.size();	}		//void fix_variable (const DiscreteRandomVariable &);		std::vector <unsigned int> number_of_variable_values; // this stores the dimension of the variables, per variable	std::vector <unsigned int> variable_values; // this stores the actual values of the variables	std::vector <double> potential_table; // this is the actual vector containing all the potential values		// This vector is used to make an inner product to treat potential_table as a high-D matrix, first values are:	// 1, variable_values [number_of_variable_values-1], variable_values [number_of_variable_values-1] * variable_values [number_of_variable_values-2]...		std::vector <unsigned int> help_vector; 		std::map <unsigned int, unsigned int> correspondance_table; // this table is used to link a particular variable to "its inner index" for the potential		//std::vector <unsigned int> fixed_variable_values;	std::vector <bool> fixed_variable;};inline double DiscretePotential::get_potential_value() const{	return potential_table [inner_product(variable_values.begin(), variable_values.end(), help_vector.begin(), 0)];	}inline void DiscretePotential::modify_potential_value(const double a){	//std::cout << " We modify with " << a << std::endl;	//std::cout << "prod is " << inner_product(variable_values.begin(), variable_values.end(), help_vector.begin(), 0) << std::endl;		potential_table [inner_product(variable_values.begin(), variable_values.end(), help_vector.begin(), 0)] = a;}// ******************* End of DiscretePotential Declaration **************//// ******************* ChainedSingleDiscretePotential Declaration **************//class ChainedSingleDiscretePotential: public DiscretePotential{	public:		// Constructor, destructor		ChainedSingleDiscretePotential(DiscreteRandomVariable &);	~ChainedSingleDiscretePotential();		// Inherited virtual methods		double get_potential_value() const;	void set_variable_value(const RandomVariable &);		void set_observed_variable (const RandomVariable &);	void unset_observed_variable (const RandomVariable &);		// Normal methods		void add_potential(DiscretePotential * const );	void precompute_products();		// Friends		friend class DiscreteParticleFilterProposal;	private:			DiscreteRandomVariable & single_rv;	std::list < DiscretePotential * > potential_lst;	std::vector < double > precomputed_products;	bool optimized;};// ******************* End of ChainedSingleDiscretePotential Declaration **************//// ******************* SingleDiscretePotential Declaration **************//// A special case where there is only one variable attached to the Potential. The values are discrete and determined through a tableclass SingleDiscretePotential: public DiscretePotential{	public:		// Constructor, destructor		SingleDiscretePotential(const DiscreteRandomVariable &);		~SingleDiscretePotential();		// Inherited virtual functions		double get_potential_value() const;	void set_variable_value (const RandomVariable &);		void set_observed_variable (const RandomVariable &);	void unset_observed_variable (const RandomVariable &);		void setup_potential_values (const std::vector <double> &);		// Unknown		Potential * clone();	void modify_potential_value (double);		// Obsolete stuff		double sum_product(unsigned int, unsigned int, std::vector <double> &);		// Debug methods	#ifndef NDEBUG	void display_table()	{		std::cout << "Displaying table: " << std::endl;				for (std::vector <double>::iterator it = single_potential_table.begin(); it != single_potential_table.end(); it++)		{			std::cout << *it << std::endl;		}	}	#endif	private:			unsigned int variable_value;	std::vector <double> single_potential_table;	};inline double SingleDiscretePotential::get_potential_value() const{	//std::cout << "accessed " << variable_value << std::endl;	return single_potential_table[variable_value];}inline void SingleDiscretePotential::set_variable_value (const RandomVariable & rv){		variable_value = static_cast < const DiscreteRandomVariable &> (rv).last_sampled_value;	//std::cout << "RV changed to " << variable_value  << std::endl;}inline void SingleDiscretePotential::modify_potential_value( double a){	//cout << "size: " << potential_table.size() << endl;	//cout << "args: " << i << ", " << a << endl;	single_potential_table [variable_value] = a;}// ******************* End of SingleDiscretePotential Declaration **************//// ******************* ConstantPotential Declaration **************//// A quickly made class to model a constant Potential (ie, no Potential at all...)class ConstantPotential: public DiscretePotential{public:		// Destructor		~ConstantPotential();		// Inherited virtual functions		double get_potential_value() const;	void set_variable_value(const RandomVariable &);		// To be checked		double sum_product(unsigned int, unsigned int, std::vector <double> &);		// Debug methods	#ifndef NDEBUG	void display_table() 	{		std::cout << "display_table() called on a ConstantPotential" << std::endl;	}#endif};inline void ConstantPotential::set_variable_value(const RandomVariable &) {}inline double ConstantPotential::get_potential_value() const{	return 1.0;}inline double ConstantPotential::sum_product(unsigned int, unsigned int, std::vector <double> &){	std::cout << "Error, we shouldn't call the Sum Product on a CP" << std::endl;	return 1.0;}// ******************* End of ConstantPotential Declaration **************//// ******************* UniqueDiscretePotential Declaration **************//class UniqueDiscretePotential: public DiscretePotential{	public:		// Constructors, destructor			UniqueDiscretePotential();		~UniqueDiscretePotential();		// Virtual functions, inherited		double get_potential_value() const;	//void set_variable_value (const RandomVariable &);		// Virtual functions, defined		void modify_potential_value (const double);	void setup_potential_values (const std::vector <double> &);		// Virtual functions, defined, later inherited virtual too		//virtual void set_observed_variable (const RandomVariable &);	//virtual void unset_observed_variable (const RandomVariable &);		// Normal functions		void add_variable (const DiscreteRandomVariable &);	//double sum_product_messages (const DiscreteRandomVariable &, const std::vector <double> &);	//double sum_product_messages_fixed (const DiscreteRandomVariable &, const std::vector <double> &);		// Obsolete methods		//void obtain_numbers(const unsigned int, unsigned int &, unsigned int &);		// Debug methods	#ifndef NDEBUG		virtual void display_table()	{		std::cout << "Displaying table: " << std::endl;				for (std::vector <double>::iterator it = single_potential_table.begin(); it != single_potential_table.end(); it++)		{			std::cout << *it << std::endl;		}	}	#endif		// Friends		template <class A, class B, class C> friend class PotentialMessageNode;		template <class A, class B, class C> friend class VariableMessageNode;	protected:		static std::vector <double> single_potential_table;	bool setup;		//double internal_sum_product_messages (const unsigned int, const std::vector <double> & );	double recursive_internal_sum_product_messages_fixed (const unsigned int, const std::vector <double> & );		unsigned int get_table_size() const	{		return single_potential_table.size();	}		//void fix_variable (const DiscreteRandomVariable &);		//std::vector <unsigned int> number_of_variable_values; // this stores the dimension of the variables, per variable	//std::vector <unsigned int> variable_values; // this stores the actual values of the variables	//std::vector <double> potential_table; // this is the actual vector containing all the potential values		// This vector is used to make an inner product to treat potential_table as a high-D matrix, first values are:	// 1, variable_values [number_of_variable_values-1], variable_values [number_of_variable_values-1] * variable_values [number_of_variable_values-2]...		//std::vector <unsigned int> help_vector; 		//std::map <unsigned int, unsigned int> correspondance_table; // this table is used to link a particular variable to "its inner index" for the potential		//std::vector <unsigned int> fixed_variable_values;	//std::vector <bool> fixed_variable;};inline double UniqueDiscretePotential::get_potential_value() const{	return single_potential_table [inner_product(variable_values.begin(), variable_values.end(), help_vector.begin(), 0)];	}inline void UniqueDiscretePotential::modify_potential_value(const double a){	single_potential_table [inner_product(variable_values.begin(), variable_values.end(), help_vector.begin(), 0)] = a;}//************* End of UniqueDiscretePotential ************************//class QMRDiscretePotential: public DiscretePotential{	public:		// Constructors, destructor			QMRDiscretePotential();		~QMRDiscretePotential();		// Virtual functions, inherited		double get_potential_value() const;	//void set_variable_value (const RandomVariable &);		// Virtual functions, defined		//void modify_potential_value (const double);	void setup_potential_values (const std::vector <double> &);		// Virtual functions, defined, later inherited virtual too		//virtual void set_observed_variable (const RandomVariable &);	//virtual void unset_observed_variable (const RandomVariable &);		// Normal functions		void add_variable (const DiscreteRandomVariable &);	//double sum_product_messages (const DiscreteRandomVariable &, const std::vector <double> &);	//double sum_product_messages_fixed (const DiscreteRandomVariable &, const std::vector <double> &);		// Obsolete methods		//void obtain_numbers(const unsigned int, unsigned int &, unsigned int &);		// Debug methods	#ifndef NDEBUG		virtual void display_table()	{		std::cout << "Displaying table: " << std::endl;				for (std::vector <double>::iterator it = thetha.begin(); it != thetha.end(); it++)		{			std::cout << *it << ", ";		}		std::cout << std::endl;	}	#endif		// Friends		template <class A, class B, class C> friend class PotentialMessageNode;		template <class A, class B, class C> friend class VariableMessageNode;	protected:			double thetha_0;	std::vector <double> thetha;		unsigned int table_size;	//double internal_sum_product_messages (const unsigned int, const std::vector <double> & );	double recursive_internal_sum_product_messages_fixed (const unsigned int, const std::vector <double> & );		unsigned int get_table_size() const	{		//std::cout << "TS: " << table_size << std::endl;		return table_size;	}		//void fix_variable (const DiscreteRandomVariable &);		//std::vector <unsigned int> number_of_variable_values; // this stores the dimension of the variables, per variable	//std::vector <unsigned int> variable_values; // this stores the actual values of the variables	//std::vector <double> potential_table; // this is the actual vector containing all the potential values		// This vector is used to make an inner product to treat potential_table as a high-D matrix, first values are:	// 1, variable_values [number_of_variable_values-1], variable_values [number_of_variable_values-1] * variable_values [number_of_variable_values-2]...		//std::vector <unsigned int> help_vector; 		//std::map <unsigned int, unsigned int> correspondance_table; // this table is used to link a particular variable to "its inner index" for the potential		//std::vector <unsigned int> fixed_variable_values;	//std::vector <bool> fixed_variable;};inline double QMRDiscretePotential::get_potential_value() const{	double a = inner_product(variable_values.begin(), variable_values.end(), thetha.begin(), 0.0);	return (1 - exp(-thetha_0 - a) );	}#endif

⌨️ 快捷键说明

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