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_lc_h
23 #define __defined_libdai_lc_h
27 #include <dai/daialg.h>
29 #include <dai/factorgraph.h>
30 #include <dai/properties.h>
31 #include <dai/exceptions.h>
37 class LC
: public DAIAlgFG
{
39 std::vector
<Factor
> _pancakes
; // used by all LC types (psi_I is stored in the pancake)
40 std::vector
<Factor
> _cavitydists
; // used by all LC types to store the approximate cavity distribution
41 /// _phis[i][_I] corresponds to \f$ \phi^{\setminus i}_I(x_{I \setminus i}) \f$
42 std::vector
<std::vector
<Factor
> > _phis
;
44 /// Single variable beliefs
45 std::vector
<Factor
> _beliefs
;
47 /// Maximum difference encountered so far
49 /// Number of iterations needed
59 DAI_ENUM(CavityType
,FULL
,PAIR
,PAIR2
,UNIFORM
)
61 DAI_ENUM(UpdateType
,SEQFIX
,SEQRND
,NONE
)
63 std::string cavainame
; // FIXME: needs assignment operator?
64 PropertySet cavaiopts
; // FIXME: needs assignment operator?
66 /// Name of this inference method
67 static const char *Name
;
70 /// Default constructor
71 LC() : DAIAlgFG(), _pancakes(), _cavitydists(), _phis(), _beliefs(), _maxdiff(), _iters(), props() {}
73 /// Construct from FactorGraph fg and PropertySet opts
74 LC( const FactorGraph
&fg
, const PropertySet
&opts
);
77 LC( const LC
&x
) : DAIAlgFG(x
), _pancakes(x
._pancakes
), _cavitydists(x
._cavitydists
), _phis(x
._phis
), _beliefs(x
._beliefs
), _maxdiff(x
._maxdiff
), _iters(x
._iters
), props(x
.props
) {}
79 /// Clone *this (virtual copy constructor)
80 virtual LC
* clone() const { return new LC(*this); }
82 /// Create (virtual default constructor)
83 virtual LC
* create() const { return new LC(); }
85 /// Assignment operator
86 LC
& operator=( const LC
&x
) {
88 DAIAlgFG::operator=( x
);
89 _pancakes
= x
._pancakes
;
90 _cavitydists
= x
._cavitydists
;
92 _beliefs
= x
._beliefs
;
93 _maxdiff
= x
._maxdiff
;
100 /// Identifies itself for logging purposes
101 virtual std::string
identify() const;
103 /// Get single node belief
104 virtual Factor
belief( const Var
&n
) const { return( _beliefs
[findVar(n
)] ); }
106 /// Get general belief
107 virtual Factor
belief( const VarSet
&/*ns*/ ) const {
108 DAI_THROW(NOT_IMPLEMENTED
);
113 virtual std::vector
<Factor
> beliefs() const { return _beliefs
; }
115 /// Get log partition sum
116 virtual Real
logZ() const {
117 DAI_THROW(NOT_IMPLEMENTED
);
121 /// Clear messages and beliefs
124 /// Clear messages and beliefs corresponding to the nodes in ns
125 virtual void init( const VarSet
&/*ns*/ ) { init(); }
127 /// The actual approximate inference algorithm
128 virtual double run();
130 /// Return maximum difference between single node beliefs in the last pass
131 virtual double maxDiff() const { return _maxdiff
; }
133 /// Return number of passes over the factorgraph
134 virtual size_t Iterations() const { return _iters
; }
136 double CalcCavityDist( size_t i
, const std::string
&name
, const PropertySet
&opts
);
137 double InitCavityDists( const std::string
&name
, const PropertySet
&opts
);
138 long SetCavityDists( std::vector
<Factor
> &Q
);
140 Factor
NewPancake (size_t i
, size_t _I
, bool & hasNaNs
);
142 void CalcBelief (size_t i
);
143 const Factor
&belief (size_t i
) const { return _beliefs
[i
]; };
144 const Factor
&pancake (size_t i
) const { return _pancakes
[i
]; };
145 const Factor
&cavitydist (size_t i
) const { return _cavitydists
[i
]; };
147 void setProperties( const PropertySet
&opts
);
148 PropertySet
getProperties() const;
149 std::string
printProperties() const;
153 } // end of namespace dai