Removed stuff from InfAlg, moved it to individual inference algorithms
[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
32
33 namespace dai {
34
35
36 class LC : public DAIAlgFG {
37 protected:
38 std::vector<Factor> _pancakes; // used by all LC types (psi_I is stored in the pancake)
39 std::vector<Factor> _cavitydists; // used by all LC types to store the approximate cavity distribution
40 /// _phis[i][_I] corresponds to \f$ \phi^{\setminus i}_I(x_{I \setminus i}) \f$
41 std::vector<std::vector<Factor> > _phis;
42
43 /// Single variable beliefs
44 std::vector<Factor> _beliefs;
45
46 public:
47 struct Properties {
48 size_t verbose;
49 size_t maxiter;
50 double tol;
51 ENUM6(CavityType,FULL,PAIR,PAIR2,PAIRINT,PAIRCUM,UNIFORM)
52 CavityType cavity;
53 ENUM3(UpdateType,SEQFIX,SEQRND,NONE)
54 UpdateType updates;
55 std::string cavainame;
56 PropertySet cavaiopts;
57 bool reinit;
58 } props;
59 double maxdiff;
60
61 public:
62 /// Default constructor
63 LC() : DAIAlgFG(), _pancakes(), _cavitydists(), _phis(), _beliefs(), props(), maxdiff() {}
64 /// Copy constructor
65 LC(const LC & x) : DAIAlgFG(x), _pancakes(x._pancakes), _cavitydists(x._cavitydists), _phis(x._phis), _beliefs(x._beliefs), props(x.props), maxdiff(x.maxdiff) {}
66 /// Clone function
67 LC* clone() const { return new LC(*this); }
68 /// Construct LC object from a FactorGraph and parameters
69 LC( const FactorGraph & fg, const PropertySet &opts );
70 /// Assignment operator
71 LC& operator=(const LC & x) {
72 if( this != &x ) {
73 DAIAlgFG::operator=(x);
74 _pancakes = x._pancakes;
75 _cavitydists = x._cavitydists;
76 _phis = x._phis;
77 _beliefs = x._beliefs;
78 props = x.props;
79 maxdiff = x.maxdiff;
80 }
81 return *this;
82 }
83
84 static const char *Name;
85 double CalcCavityDist( size_t i, const std::string &name, const PropertySet &opts );
86 double InitCavityDists( const std::string &name, const PropertySet &opts );
87 long SetCavityDists( std::vector<Factor> &Q );
88
89 void init();
90 Factor NewPancake (size_t i, size_t _I, bool & hasNaNs);
91 double run();
92
93 std::string identify() const;
94 Factor belief (const Var &n) const { return( _beliefs[findVar(n)] ); }
95 Factor belief (const VarSet &/*ns*/) const { assert( 0 == 1 ); }
96 std::vector<Factor> beliefs() const { return _beliefs; }
97 Complex logZ() const { return NAN; }
98 void CalcBelief (size_t i);
99 const Factor &belief (size_t i) const { return _beliefs[i]; };
100 const Factor &pancake (size_t i) const { return _pancakes[i]; };
101 const Factor &cavitydist (size_t i) const { return _cavitydists[i]; };
102
103 void clamp( const Var &/*n*/, size_t /*i*/ ) { assert( 0 == 1 ); }
104 void undoProbs( const VarSet &/*ns*/ ) { assert( 0 == 1 ); }
105 void saveProbs( const VarSet &/*ns*/ ) { assert( 0 == 1 ); }
106 virtual void makeCavity(const Var & /*n*/) { assert( 0 == 1 ); }
107
108 void setProperties( const PropertySet &opts );
109 PropertySet getProperties() const;
110 double maxDiff() const { return maxdiff; }
111 };
112
113
114 } // end of namespace dai
115
116
117 #endif