1 /* This file is part of libDAI - http://www.libdai.org/
2 *
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]
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 for ExactInf
38 struct Properties {
39 /// Verbosity (amount of output sent to stderr)
40 size_t verbose;
41 } props;
43 private:
44 /// All single variable marginals
45 std::vector<Factor> _beliefsV;
46 /// All factor variable marginals
47 std::vector<Factor> _beliefsF;
48 /// Logarithm of partition sum
49 Real _logZ;
51 public:
52 /// \name Constructors/destructors
53 //@{
54 /// Default constructor
55 ExactInf() : DAIAlgFG(), props(), _beliefsV(), _beliefsF(), _logZ(0) {}
57 /// Construct from FactorGraph \a fg and PropertySet \a opts
58 /** \param fg Factor graph.
59 * \param opts Parameters @see Properties
60 */
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 name() const { return "EXACT"; }
71 virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
72 virtual Factor belief( const VarSet &vs ) 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 /** \note The complexity of this calculation is exponential in the number of variables.
78 */
79 std::vector<std::size_t> findMaximum() const;
80 virtual void init();
81 virtual void init( const VarSet &/*ns*/ ) {}
82 virtual Real run();
83 virtual Real maxDiff() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
84 virtual size_t Iterations() const { DAI_THROW(NOT_IMPLEMENTED); return 0; }
85 virtual void setProperties( const PropertySet &opts );
86 virtual PropertySet getProperties() const;
87 virtual std::string printProperties() const;
88 //@}
90 /// \name Additional interface specific for ExactInf
91 //@{
92 /// Calculates marginal probability distribution for variables \a vs
93 /** \note The complexity of this calculation is exponential in the number of variables.
94 */
95 Factor calcMarginal( const VarSet &vs ) const;
96 //@}
98 private:
99 /// Helper function for constructors
100 void construct();
101 };
104 } // end of namespace dai
107 #endif