764d787806b8aa26d600cd4ef44b19b2927e4f57
[libdai.git] / include / dai / exactinf.h
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 */
7
8
9 /// \file
10 /// \brief Defines ExactInf class, which can be used for exact inference on small factor graphs.
11
12
13 #ifndef __defined_libdai_exactinf_h
14 #define __defined_libdai_exactinf_h
15
16
17 #include <dai/daialg.h>
18 #include <dai/properties.h>
19 #include <dai/factorgraph.h>
20 #include <dai/enum.h>
21
22
23 namespace dai {
24
25
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;
39
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;
47
48 public:
49 /// \name Constructors/destructors
50 //@{
51 /// Default constructor
52 ExactInf() : DAIAlgFG(), props(), _beliefsV(), _beliefsF(), _logZ(0) {}
53
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 //@}
63
64 /// \name General InfAlg interface
65 //@{
66 virtual ExactInf* clone() const { return new ExactInf(*this); }
67 virtual ExactInf* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new ExactInf( fg, opts ); }
68 virtual std::string name() const { return "EXACT"; }
69 virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
70 virtual Factor belief( const VarSet &vs ) const;
71 virtual Factor beliefV( size_t i ) const { return _beliefsV[i]; }
72 virtual Factor beliefF( size_t I ) const { return _beliefsF[I]; }
73 virtual std::vector<Factor> beliefs() const;
74 virtual Real logZ() const { return _logZ; }
75 /** \note The complexity of this calculation is exponential in the number of variables.
76 */
77 std::vector<std::size_t> findMaximum() const;
78 virtual void init();
79 virtual void init( const VarSet &/*ns*/ ) {}
80 virtual Real run();
81 virtual Real maxDiff() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
82 virtual size_t Iterations() const { DAI_THROW(NOT_IMPLEMENTED); return 0; }
83 virtual void setProperties( const PropertySet &opts );
84 virtual PropertySet getProperties() const;
85 virtual std::string printProperties() const;
86 //@}
87
88 /// \name Additional interface specific for ExactInf
89 //@{
90 /// Calculates marginal probability distribution for variables \a vs
91 /** \note The complexity of this calculation is exponential in the number of variables.
92 */
93 Factor calcMarginal( const VarSet &vs ) const;
94 //@}
95
96 private:
97 /// Helper function for constructors
98 void construct();
99 };
100
101
102 } // end of namespace dai
103
104
105 #endif