68f6b52c1d9f65a039685795ac619471bb968b8c
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
12 /// \file
13 /// \brief Defines ExactInf class, which can be used for exact inference on small factor graphs.
16 #ifndef __defined_libdai_exactinf_h
17 #define __defined_libdai_exactinf_h
20 #include <dai/daialg.h>
21 #include <dai/properties.h>
22 #include <dai/factorgraph.h>
23 #include <dai/enum.h>
26 namespace dai {
29 /// Exact inference algorithm using brute force enumeration (mainly useful for testing purposes)
30 /** Inference is done simply by multiplying all factors together into one large factor,
31 * and then calculating marginals and partition sum from the product.
32 * \note This inference method can easily exhaust all available memory; in that case, one
33 * may try the JTree class instead.
34 */
35 class ExactInf : public DAIAlgFG {
36 public:
37 /// Parameters of this inference algorithm
38 struct Properties {
39 /// Verbosity
40 size_t verbose;
41 } props;
43 /// Name of this inference algorithm
44 static const char *Name;
46 private:
47 /// All single variable marginals
48 std::vector<Factor> _beliefsV;
49 /// All factor variable marginals
50 std::vector<Factor> _beliefsF;
51 /// Logarithm of partition sum
52 Real _logZ;
54 public:
55 /// \name Constructors/destructors
56 //@{
57 /// Default constructor
58 ExactInf() : DAIAlgFG(), props(), _beliefsV(), _beliefsF(), _logZ(0) {}
60 /// Construct from FactorGraph \a fg and PropertySet \a opts
61 ExactInf( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), props(), _beliefsV(), _beliefsF(), _logZ() {
62 setProperties( opts );
63 construct();
64 }
65 //@}
67 /// \name General InfAlg interface
68 //@{
69 virtual ExactInf* clone() const { return new ExactInf(*this); }
70 virtual std::string identify() const;
71 virtual Factor belief( const Var &n ) const { return beliefV( findVar( n ) ); }
72 virtual Factor belief( const VarSet &ns ) const;
73 virtual Factor beliefV( size_t i ) const { return _beliefsV[i]; }
74 virtual Factor beliefF( size_t I ) const { return _beliefsF[I]; }
75 virtual std::vector<Factor> beliefs() const;
76 virtual Real logZ() const { return _logZ; }
77 virtual void init();
78 virtual void init( const VarSet &/*ns*/ ) { DAI_THROW(NOT_IMPLEMENTED); }
79 virtual Real run();
80 virtual Real maxDiff() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
81 virtual size_t Iterations() const { DAI_THROW(NOT_IMPLEMENTED); return 0; }
82 //@}
84 /// \name Managing parameters (which are stored in ExactInf::props)
85 //@{
86 /// Set parameters of this inference algorithm.
87 /** The parameters are set according to \a opts.
88 * The values can be stored either as std::string or as the type of the corresponding ExactInf::props member.
89 */
90 void setProperties( const PropertySet &opts );
91 /// Returns parameters of this inference algorithm converted into a PropertySet.
92 PropertySet getProperties() const;
93 /// Returns parameters of this inference algorithm formatted as a string in the format "[key1=val1,key2=val2,...,keyn=valn]".
94 std::string printProperties() const;
95 //@}
97 private:
98 /// Helper function for constructors
99 void construct();
100 };
103 } // end of namespace dai
106 #endif