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
;
52 DAI_ENUM(CavityType
,FULL
,PAIR
,PAIR2
,PAIRINT
,PAIRCUM
,UNIFORM
)
54 DAI_ENUM(UpdateType
,SEQFIX
,SEQRND
,NONE
)
56 std::string cavainame
;
57 PropertySet cavaiopts
;
63 /// Default constructor
64 LC() : DAIAlgFG(), _pancakes(), _cavitydists(), _phis(), _beliefs(), props(), maxdiff() {}
66 LC(const LC
& x
) : DAIAlgFG(x
), _pancakes(x
._pancakes
), _cavitydists(x
._cavitydists
), _phis(x
._phis
), _beliefs(x
._beliefs
), props(x
.props
), maxdiff(x
.maxdiff
) {}
68 LC
* clone() const { return new LC(*this); }
69 /// Create (virtual constructor)
70 virtual LC
* create() const { return new LC(); }
71 /// Construct LC object from a FactorGraph and parameters
72 LC( const FactorGraph
& fg
, const PropertySet
&opts
);
73 /// Assignment operator
74 LC
& operator=(const LC
& x
) {
76 DAIAlgFG::operator=(x
);
77 _pancakes
= x
._pancakes
;
78 _cavitydists
= x
._cavitydists
;
80 _beliefs
= x
._beliefs
;
87 static const char *Name
;
88 double CalcCavityDist( size_t i
, const std::string
&name
, const PropertySet
&opts
);
89 double InitCavityDists( const std::string
&name
, const PropertySet
&opts
);
90 long SetCavityDists( std::vector
<Factor
> &Q
);
93 /// Clear messages and beliefs corresponding to the nodes in ns
94 virtual void init( const VarSet
&/*ns*/ ) {
95 DAI_THROW(NOT_IMPLEMENTED
);
97 Factor
NewPancake (size_t i
, size_t _I
, bool & hasNaNs
);
100 std::string
identify() const;
101 Factor
belief (const Var
&n
) const { return( _beliefs
[findVar(n
)] ); }
102 Factor
belief (const VarSet
&/*ns*/) const {
103 DAI_THROW(NOT_IMPLEMENTED
);
106 std::vector
<Factor
> beliefs() const { return _beliefs
; }
108 DAI_THROW(NOT_IMPLEMENTED
);
111 void CalcBelief (size_t i
);
112 const Factor
&belief (size_t i
) const { return _beliefs
[i
]; };
113 const Factor
&pancake (size_t i
) const { return _pancakes
[i
]; };
114 const Factor
&cavitydist (size_t i
) const { return _cavitydists
[i
]; };
116 void clamp( const Var
&/*n*/, size_t /*i*/ ) {
117 DAI_THROW(NOT_IMPLEMENTED
);
119 void restoreFactors( const VarSet
&/*ns*/ ) {
120 DAI_THROW(NOT_IMPLEMENTED
);
122 void backupFactors( const VarSet
&/*ns*/ ) {
123 DAI_THROW(NOT_IMPLEMENTED
);
125 virtual void makeCavity(const Var
& /*n*/) {
126 DAI_THROW(NOT_IMPLEMENTED
);
128 void setProperties( const PropertySet
&opts
);
129 PropertySet
getProperties() const;
130 std::string
printProperties() const;
131 double maxDiff() const { return maxdiff
; }
135 } // end of namespace dai