Merge branch 'joris'
[libdai.git] / include / dai / exactinf.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 /// \file
24 /// \brief Defines ExactInf class
25 /// \todo Improve documentation
26
27
28 #ifndef __defined_libdai_exactinf_h
29 #define __defined_libdai_exactinf_h
30
31
32 #include <dai/daialg.h>
33 #include <dai/properties.h>
34 #include <dai/factorgraph.h>
35 #include <dai/enum.h>
36
37
38 namespace dai {
39
40
41 /// Exact inference algorithm using brute force enumeration (mainly useful for testing purposes)
42 class ExactInf : public DAIAlgFG {
43 public:
44 /// Parameters of this inference algorithm
45 struct Properties {
46 /// Verbosity
47 size_t verbose;
48 } props;
49
50 /// Name of this inference algorithm
51 static const char *Name;
52
53 private:
54 std::vector<Factor> _beliefsV;
55 std::vector<Factor> _beliefsF;
56 Real _logZ;
57
58 public:
59 /// Default constructor
60 ExactInf() : DAIAlgFG(), props(), _beliefsV(), _beliefsF(), _logZ(0) {}
61
62 /// Copy constructor
63 ExactInf( const ExactInf &x ) : DAIAlgFG(x), props(x.props), _beliefsV(x._beliefsV), _beliefsF(x._beliefsF), _logZ(x._logZ) {}
64
65 /// Assignment operator
66 ExactInf& operator=( const ExactInf &x ) {
67 if( this != &x ) {
68 DAIAlgFG::operator=( x );
69 props = x.props;
70 _beliefsV = x._beliefsV;
71 _beliefsF = x._beliefsF;
72 _logZ = x._logZ;
73 }
74 return *this;
75 }
76
77 /// Construct from FactorGraph fg and PropertySet opts
78 ExactInf( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), props(), _beliefsV(), _beliefsF(), _logZ() {
79 setProperties( opts );
80 construct();
81 }
82
83
84 /// @name General InfAlg interface
85 //@{
86 virtual ExactInf* clone() const { return new ExactInf(*this); }
87 virtual ExactInf* create() const { return new ExactInf(); }
88 virtual std::string identify() const;
89 virtual Factor belief( const Var &n ) const { return beliefV( findVar( n ) ); }
90 virtual Factor belief( const VarSet &ns ) const;
91 virtual std::vector<Factor> beliefs() const;
92 virtual Real logZ() const { return _logZ; }
93 virtual void init();
94 virtual void init( const VarSet &/*ns*/ ) { DAI_THROW(NOT_IMPLEMENTED); }
95 virtual double run();
96 virtual double maxDiff() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
97 virtual size_t Iterations() const { DAI_THROW(NOT_IMPLEMENTED); return 0; }
98 //@}
99
100
101 /// @name Additional interface specific for ExactInf
102 //@{
103 Factor beliefV( size_t i ) const { return _beliefsV[i]; }
104 Factor beliefF( size_t I ) const { return _beliefsF[I]; }
105 //@}
106
107 private:
108 void construct();
109 void setProperties( const PropertySet &opts );
110 PropertySet getProperties() const;
111 std::string printProperties() const;
112 };
113
114
115 } // end of namespace dai
116
117
118 #endif