246b4331ff914105f1ac6f1922c4d4da93d38b4e
[libdai.git] / include / dai / exactinf.h
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #ifndef __defined_libdai_exactinf_h
23 #define __defined_libdai_exactinf_h
24
25
26 #include <dai/daialg.h>
27 #include <dai/properties.h>
28 #include <dai/factorgraph.h>
29 #include <dai/enum.h>
30
31
32 namespace dai {
33
34
35 class ExactInf : public DAIAlgFG {
36 public:
37 struct Properties {
38 size_t verbose;
39 } props;
40 /// Name of this inference method
41 static const char *Name;
42
43 private:
44 std::vector<Factor> _beliefsV;
45 std::vector<Factor> _beliefsF;
46 Real _logZ;
47
48 public:
49 /// Default constructor
50 ExactInf() : DAIAlgFG(), props(), _beliefsV(), _beliefsF(), _logZ(0) {}
51
52 /// Construct from FactorGraph fg and PropertySet opts
53 ExactInf( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), props(), _beliefsV(), _beliefsF(), _logZ() {
54 setProperties( opts );
55 construct();
56 }
57
58 /// Copy constructor
59 ExactInf( const ExactInf &x ) : DAIAlgFG(x), props(x.props), _beliefsV(x._beliefsV), _beliefsF(x._beliefsF), _logZ(x._logZ) {}
60
61 /// Clone *this (virtual copy constructor)
62 virtual ExactInf* clone() const { return new ExactInf(*this); }
63
64 /// Create (virtual default constructor)
65 virtual ExactInf* create() const { return new ExactInf(); }
66
67 /// Assignment operator
68 ExactInf& operator=( const ExactInf &x ) {
69 if( this != &x ) {
70 DAIAlgFG::operator=( x );
71 props = x.props;
72 _beliefsV = x._beliefsV;
73 _beliefsF = x._beliefsF;
74 _logZ = x._logZ;
75 }
76 return *this;
77 }
78
79 /// Identifies itself for logging purposes
80 virtual std::string identify() const;
81
82 /// Get single node belief
83 virtual Factor belief( const Var &n ) const { return beliefV( findVar( n ) ); }
84
85 /// Get general belief
86 virtual Factor belief( const VarSet &ns ) const;
87
88 /// Get all beliefs
89 virtual std::vector<Factor> beliefs() const;
90
91 /// Get log partition sum
92 virtual Real logZ() const { return _logZ; }
93
94 /// Clear messages and beliefs
95 virtual void init();
96
97 /// Clear messages and beliefs corresponding to the nodes in ns
98 virtual void init( const VarSet &/*ns*/ ) {
99 DAI_THROW(NOT_IMPLEMENTED);
100 }
101
102 /// The actual approximate inference algorithm
103 virtual double run();
104
105 /// Return maximum difference between single node beliefs in the last pass
106 virtual double maxDiff() const {
107 DAI_THROW(NOT_IMPLEMENTED);
108 return 0.0;
109 }
110
111 /// Return number of passes over the factorgraph
112 virtual size_t Iterations() const {
113 DAI_THROW(NOT_IMPLEMENTED);
114 return 0;
115 }
116
117 void construct();
118 void setProperties( const PropertySet &opts );
119 PropertySet getProperties() const;
120 std::string printProperties() const;
121
122 Factor beliefV( size_t i ) const { return _beliefsV[i]; }
123 Factor beliefF( size_t I ) const { return _beliefsF[I]; }
124 };
125
126
127 } // end of namespace dai
128
129
130 #endif