Merge branch 'master' of git@git.tuebingen.mpg.de:libdai
[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 create();
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 {
78 return new ExactInf();
79 }
80 */
81 /// Return maximum difference between single node
82 /// beliefs for two consecutive iterations
83 virtual double maxDiff() const {
84 DAI_THROW(NOT_IMPLEMENTED);
85 return 0.0;
86 }
87
88 /// Identifies itself for logging purposes
89 virtual std::string identify() const;
90
91 /// Get single node belief
92 virtual Factor belief( const Var &n ) const {
93 return beliefV( findVar( n ) );
94 }
95
96 /// Get general belief
97 virtual Factor belief( const VarSet &n ) const;
98
99 /// Get all beliefs
100 virtual std::vector<Factor> beliefs() const;
101
102 /// Get log partition sum
103 virtual Real logZ() const {
104 return _logZ;
105 }
106
107 /// Clear messages and beliefs
108 virtual void init();
109
110 /// Clear messages and beliefs corresponding to the nodes in ns
111 virtual void init( const VarSet &/*ns*/ ) {}
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 create();
120 void undoProbs( const VarSet &ns ) { FactorGraph::undoProbs(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