1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
4 This file is part of libDAI.
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.
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.
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
22 #ifndef __defined_libdai_exactinf_h
23 #define __defined_libdai_exactinf_h
26 #include <dai/daialg.h>
27 #include <dai/properties.h>
28 #include <dai/factorgraph.h>
35 class ExactInf
: public DAIAlgFG
{
40 /// Name of this inference method
41 static const char *Name
;
44 std::vector
<Factor
> _beliefsV
;
45 std::vector
<Factor
> _beliefsF
;
49 /// Default constructor
50 ExactInf() : DAIAlgFG(), props(), _beliefsV(), _beliefsF(), _logZ(0) {}
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
);
59 ExactInf( const ExactInf
&x
) : DAIAlgFG(x
), props(x
.props
), _beliefsV(x
._beliefsV
), _beliefsF(x
._beliefsF
), _logZ(x
._logZ
) {}
61 /// Clone *this (virtual copy constructor)
62 virtual ExactInf
* clone() const { return new ExactInf(*this); }
64 /// Create (virtual default constructor)
65 virtual ExactInf
* create() const { return new ExactInf(); }
67 /// Assignment operator
68 ExactInf
& operator=( const ExactInf
&x
) {
70 DAIAlgFG::operator=( x
);
72 _beliefsV
= x
._beliefsV
;
73 _beliefsF
= x
._beliefsF
;
79 /// Identifies itself for logging purposes
80 virtual std::string
identify() const;
82 /// Get single node belief
83 virtual Factor
belief( const Var
&n
) const { return beliefV( findVar( n
) ); }
85 /// Get general belief
86 virtual Factor
belief( const VarSet
&ns
) const;
89 virtual std::vector
<Factor
> beliefs() const;
91 /// Get log partition sum
92 virtual Real
logZ() const { return _logZ
; }
94 /// Clear messages and beliefs
97 /// Clear messages and beliefs corresponding to the nodes in ns
98 virtual void init( const VarSet
&/*ns*/ ) {
99 DAI_THROW(NOT_IMPLEMENTED
);
102 /// The actual approximate inference algorithm
103 virtual double run();
105 /// Return maximum difference between single node beliefs in the last pass
106 virtual double maxDiff() const {
107 DAI_THROW(NOT_IMPLEMENTED
);
111 /// Return number of passes over the factorgraph
112 virtual size_t Iterations() const {
113 DAI_THROW(NOT_IMPLEMENTED
);
118 void setProperties( const PropertySet
&opts
);
119 PropertySet
getProperties() const;
120 std::string
printProperties() const;
122 Factor
beliefV( size_t i
) const { return _beliefsV
[i
]; }
123 Factor
beliefF( size_t I
) const { return _beliefsF
[I
]; }
127 } // end of namespace dai