Merged tests/*, matlab/*, utils/* from SVN head...
[libdai.git] / include / dai / lc.h
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
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.
10
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.
15
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
19 */
20
21
22 #ifndef __defined_libdai_lc_h
23 #define __defined_libdai_lc_h
24
25
26 #include <string>
27 #include <dai/daialg.h>
28 #include <dai/enum.h>
29 #include <dai/factorgraph.h>
30 #include <dai/properties.h>
31 #include <dai/exceptions.h>
32
33
34 namespace dai {
35
36
37 class LC : public DAIAlgFG {
38 protected:
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;
43
44 /// Single variable beliefs
45 std::vector<Factor> _beliefs;
46
47 public:
48 struct Properties {
49 size_t verbose;
50 size_t maxiter;
51 double tol;
52 DAI_ENUM(CavityType,FULL,PAIR,PAIR2,UNIFORM)
53 CavityType cavity;
54 DAI_ENUM(UpdateType,SEQFIX,SEQRND,NONE)
55 UpdateType updates;
56 std::string cavainame;
57 PropertySet cavaiopts;
58 bool reinit;
59 } props;
60 double maxdiff;
61
62 public:
63 /// Default constructor
64 LC() : DAIAlgFG(), _pancakes(), _cavitydists(), _phis(), _beliefs(), props(), maxdiff() {}
65 /// Copy constructor
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) {}
67 /// Clone function
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) {
75 if( this != &x ) {
76 DAIAlgFG::operator=(x);
77 _pancakes = x._pancakes;
78 _cavitydists = x._cavitydists;
79 _phis = x._phis;
80 _beliefs = x._beliefs;
81 props = x.props;
82 maxdiff = x.maxdiff;
83 }
84 return *this;
85 }
86
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 );
91
92 void init();
93 /// Clear messages and beliefs corresponding to the nodes in ns
94 virtual void init( const VarSet &/*ns*/ ) {
95 DAI_THROW(NOT_IMPLEMENTED);
96 }
97 Factor NewPancake (size_t i, size_t _I, bool & hasNaNs);
98 double run();
99
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);
104 return Factor();
105 }
106 std::vector<Factor> beliefs() const { return _beliefs; }
107 Real logZ() const {
108 DAI_THROW(NOT_IMPLEMENTED);
109 return 0.0;
110 }
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]; };
115
116 void clamp( const Var &/*n*/, size_t /*i*/ ) {
117 DAI_THROW(NOT_IMPLEMENTED);
118 }
119 void restoreFactors( const VarSet &/*ns*/ ) {
120 DAI_THROW(NOT_IMPLEMENTED);
121 }
122 void backupFactors( const VarSet &/*ns*/ ) {
123 DAI_THROW(NOT_IMPLEMENTED);
124 }
125 virtual void makeCavity(const Var & /*n*/) {
126 DAI_THROW(NOT_IMPLEMENTED);
127 }
128 void setProperties( const PropertySet &opts );
129 PropertySet getProperties() const;
130 std::string printProperties() const;
131 double maxDiff() const { return maxdiff; }
132 };
133
134
135 } // end of namespace dai
136
137
138 #endif