Merged mf.* from SVN head (which implements damping)...
[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
41 private:
42 std::vector<Factor> _beliefsV;
43 std::vector<Factor> _beliefsF;
44 Real _logZ;
45
46 public:
47 /// Default constructor
48 ExactInf() : DAIAlgFG(), props(), _beliefsV(), _beliefsF(), _logZ(0) {}
49
50 /// Copy constructor
51 ExactInf( const ExactInf &x ) : DAIAlgFG(x), props(x.props), _beliefsV(x._beliefsV), _beliefsF(x._beliefsF), _logZ(x._logZ) {}
52
53 /// Clone (virtual copy constructor)
54 virtual ExactInf* clone() const {
55 return new ExactInf(*this);
56 }
57
58 /// Construct from FactorGraph fg and PropertySet opts
59 ExactInf( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), props(), _beliefsV(), _beliefsF(), _logZ() {
60 setProperties( opts );
61 construct();
62 }
63
64 /// Assignment operator
65 ExactInf& operator=( const ExactInf &x ) {
66 if( this != &x ) {
67 DAIAlgFG::operator=( x );
68 props = x.props;
69 _beliefsV = x._beliefsV;
70 _beliefsF = x._beliefsF;
71 _logZ = x._logZ;
72 }
73 return *this;
74 }
75
76 /// Create (virtual constructor)
77 virtual ExactInf* create() const { return new ExactInf(); }
78
79 /// Return maximum difference between single node
80 /// beliefs for two consecutive iterations
81 virtual double maxDiff() const {
82 DAI_THROW(NOT_IMPLEMENTED);
83 return 0.0;
84 }
85
86 /// Identifies itself for logging purposes
87 virtual std::string identify() const;
88
89 /// Get single node belief
90 virtual Factor belief( const Var &n ) const {
91 return beliefV( findVar( n ) );
92 }
93
94 /// Get general belief
95 virtual Factor belief( const VarSet &n ) const;
96
97 /// Get all beliefs
98 virtual std::vector<Factor> beliefs() const;
99
100 /// Get log partition sum
101 virtual Real logZ() const {
102 return _logZ;
103 }
104
105 /// Clear messages and beliefs
106 virtual void init();
107
108 /// Clear messages and beliefs corresponding to the nodes in ns
109 virtual void init( const VarSet &/*ns*/ ) {
110 DAI_THROW(NOT_IMPLEMENTED);
111 }
112
113 /// The actual approximate inference algorithm
114 virtual double run();
115
116 /// Name of this inference method
117 static const char *Name;
118
119 void construct();
120 void restoreFactors( const VarSet &ns ) { FactorGraph::restoreFactors(ns); init(ns); }
121 void setProperties( const PropertySet &opts );
122 PropertySet getProperties() const;
123 std::string printProperties() const;
124
125 Factor beliefV( size_t i ) const {
126 return _beliefsV[i];
127 }
128
129 Factor beliefF( size_t I ) const {
130 return _beliefsF[I];
131 }
132 };
133
134
135 } // end of namespace dai
136
137
138 #endif