Updated copyrights
[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 #ifndef __defined_libdai_exactinf_h
24 #define __defined_libdai_exactinf_h
25
26
27 #include <dai/daialg.h>
28 #include <dai/properties.h>
29 #include <dai/factorgraph.h>
30 #include <dai/enum.h>
31
32
33 namespace dai {
34
35
36 class ExactInf : public DAIAlgFG {
37 public:
38 struct Properties {
39 size_t verbose;
40 } props;
41 /// Name of this inference method
42 static const char *Name;
43
44 private:
45 std::vector<Factor> _beliefsV;
46 std::vector<Factor> _beliefsF;
47 Real _logZ;
48
49 public:
50 /// Default constructor
51 ExactInf() : DAIAlgFG(), props(), _beliefsV(), _beliefsF(), _logZ(0) {}
52
53 /// Construct from FactorGraph fg and PropertySet opts
54 ExactInf( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), props(), _beliefsV(), _beliefsF(), _logZ() {
55 setProperties( opts );
56 construct();
57 }
58
59 /// Copy constructor
60 ExactInf( const ExactInf &x ) : DAIAlgFG(x), props(x.props), _beliefsV(x._beliefsV), _beliefsF(x._beliefsF), _logZ(x._logZ) {}
61
62 /// Clone *this (virtual copy constructor)
63 virtual ExactInf* clone() const { return new ExactInf(*this); }
64
65 /// Create (virtual default constructor)
66 virtual ExactInf* create() const { return new ExactInf(); }
67
68 /// Assignment operator
69 ExactInf& operator=( const ExactInf &x ) {
70 if( this != &x ) {
71 DAIAlgFG::operator=( x );
72 props = x.props;
73 _beliefsV = x._beliefsV;
74 _beliefsF = x._beliefsF;
75 _logZ = x._logZ;
76 }
77 return *this;
78 }
79
80 /// Identifies itself for logging purposes
81 virtual std::string identify() const;
82
83 /// Get single node belief
84 virtual Factor belief( const Var &n ) const { return beliefV( findVar( n ) ); }
85
86 /// Get general belief
87 virtual Factor belief( const VarSet &ns ) const;
88
89 /// Get all beliefs
90 virtual std::vector<Factor> beliefs() const;
91
92 /// Get log partition sum
93 virtual Real logZ() const { return _logZ; }
94
95 /// Clear messages and beliefs
96 virtual void init();
97
98 /// Clear messages and beliefs corresponding to the nodes in ns
99 virtual void init( const VarSet &/*ns*/ ) {
100 DAI_THROW(NOT_IMPLEMENTED);
101 }
102
103 /// The actual approximate inference algorithm
104 virtual double run();
105
106 /// Return maximum difference between single node beliefs in the last pass
107 virtual double maxDiff() const {
108 DAI_THROW(NOT_IMPLEMENTED);
109 return 0.0;
110 }
111
112 /// Return number of passes over the factorgraph
113 virtual size_t Iterations() const {
114 DAI_THROW(NOT_IMPLEMENTED);
115 return 0;
116 }
117
118 void construct();
119 void setProperties( const PropertySet &opts );
120 PropertySet getProperties() const;
121 std::string printProperties() const;
122
123 Factor beliefV( size_t i ) const { return _beliefsV[i]; }
124 Factor beliefF( size_t I ) const { return _beliefsF[I]; }
125 };
126
127
128 } // end of namespace dai
129
130
131 #endif