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
{
42 std::vector
<Factor
> _beliefsV
;
43 std::vector
<Factor
> _beliefsF
;
47 /// Default constructor
48 ExactInf() : DAIAlgFG(), props(), _beliefsV(), _beliefsF(), _logZ(0) {}
51 ExactInf( const ExactInf
&x
) : DAIAlgFG(x
), props(x
.props
), _beliefsV(x
._beliefsV
), _beliefsF(x
._beliefsF
), _logZ(x
._logZ
) {}
53 /// Clone (virtual copy constructor)
54 virtual ExactInf
* clone() const {
55 return new ExactInf(*this);
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
);
64 /// Assignment operator
65 ExactInf
& operator=( const ExactInf
&x
) {
67 DAIAlgFG::operator=( x
);
69 _beliefsV
= x
._beliefsV
;
70 _beliefsF
= x
._beliefsF
;
76 /// Create (virtual constructor)
77 virtual ExactInf
* create() const {
78 return new ExactInf();
81 /// Return maximum difference between single node
82 /// beliefs for two consecutive iterations
83 virtual double maxDiff() const {
84 DAI_THROW(NOT_IMPLEMENTED
);
88 /// Identifies itself for logging purposes
89 virtual std::string
identify() const;
91 /// Get single node belief
92 virtual Factor
belief( const Var
&n
) const {
93 return beliefV( findVar( n
) );
96 /// Get general belief
97 virtual Factor
belief( const VarSet
&n
) const;
100 virtual std::vector
<Factor
> beliefs() const;
102 /// Get log partition sum
103 virtual Real
logZ() const {
107 /// Clear messages and beliefs
110 /// Clear messages and beliefs corresponding to the nodes in ns
111 virtual void init( const VarSet
&/*ns*/ ) {
112 DAI_THROW(NOT_IMPLEMENTED
);
115 /// The actual approximate inference algorithm
116 virtual double run();
118 /// Name of this inference method
119 static const char *Name
;
122 void restoreFactors( const VarSet
&ns
) { FactorGraph::restoreFactors(ns
); init(ns
); }
123 void setProperties( const PropertySet
&opts
);
124 PropertySet
getProperties() const;
125 std::string
printProperties() const;
127 Factor
beliefV( size_t i
) const {
131 Factor
beliefF( size_t I
) const {
137 } // end of namespace dai