1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
9 /// \file
10 /// \brief Defines ExactInf class, which can be used for exact inference on small factor graphs.
13 #ifndef __defined_libdai_exactinf_h
14 #define __defined_libdai_exactinf_h
17 #include <dai/daialg.h>
18 #include <dai/properties.h>
19 #include <dai/factorgraph.h>
20 #include <dai/enum.h>
23 namespace dai {
26 /// Exact inference algorithm using brute force enumeration (mainly useful for testing purposes)
27 /** Inference is done simply by multiplying all factors together into one large factor,
28 * and then calculating marginals and partition sum from the product.
29 * \note This inference method can easily exhaust all available memory; in that case, one
30 * may try the JTree class instead.
31 */
32 class ExactInf : public DAIAlgFG {
33 public:
34 /// Parameters for ExactInf
35 struct Properties {
36 /// Verbosity (amount of output sent to stderr)
37 size_t verbose;
38 } props;
40 private:
41 /// All single variable marginals
42 std::vector<Factor> _beliefsV;
43 /// All factor variable marginals
44 std::vector<Factor> _beliefsF;
45 /// Logarithm of partition sum
46 Real _logZ;
48 public:
49 /// \name Constructors/destructors
50 //@{
51 /// Default constructor
52 ExactInf() : DAIAlgFG(), props(), _beliefsV(), _beliefsF(), _logZ(0) {}
54 /// Construct from FactorGraph \a fg and PropertySet \a opts
55 /** \param fg Factor graph.
56 * \param opts Parameters @see Properties
57 */
58 ExactInf( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), props(), _beliefsV(), _beliefsF(), _logZ() {
59 setProperties( opts );
60 construct();
61 }
62 //@}
64 /// \name General InfAlg interface
65 //@{
66 virtual ExactInf* clone() const { return new ExactInf(*this); }
67 virtual std::string name() const { return "EXACT"; }
68 virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
69 virtual Factor belief( const VarSet &vs ) const;
70 virtual Factor beliefV( size_t i ) const { return _beliefsV[i]; }
71 virtual Factor beliefF( size_t I ) const { return _beliefsF[I]; }
72 virtual std::vector<Factor> beliefs() const;
73 virtual Real logZ() const { return _logZ; }
74 /** \note The complexity of this calculation is exponential in the number of variables.
75 */
76 std::vector<std::size_t> findMaximum() const;
77 virtual void init();
78 virtual void init( const VarSet &/*ns*/ ) {}
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 virtual void setProperties( const PropertySet &opts );
83 virtual PropertySet getProperties() const;
84 virtual std::string printProperties() const;
85 //@}
87 /// \name Additional interface specific for ExactInf
88 //@{
89 /// Calculates marginal probability distribution for variables \a vs
90 /** \note The complexity of this calculation is exponential in the number of variables.
91 */
92 Factor calcMarginal( const VarSet &vs ) const;
93 //@}
95 private:
96 /// Helper function for constructors
97 void construct();
98 };
101 } // end of namespace dai
104 #endif