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