Replaced complex numbers by real numbers
[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/exceptions.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 ENUM6(CavityType,FULL,PAIR,PAIR2,PAIRINT,PAIRCUM,UNIFORM)
48 ENUM3(UpdateType,SEQFIX,SEQRND,NONE)
49
50 CavityType Cavity() const { return GetPropertyAs<CavityType>("cavity"); }
51 UpdateType Updates() const { return GetPropertyAs<UpdateType>("updates"); }
52 bool reInit() const { return GetPropertyAs<bool>("reinit"); }
53
54 /// Default constructor
55 LC() : DAIAlgFG() {};
56 /// Copy constructor
57 LC(const LC & x) : DAIAlgFG(x), _pancakes(x._pancakes), _cavitydists(x._cavitydists), _phis(x._phis), _beliefs(x._beliefs) {};
58 /// Clone function
59 LC* clone() const { return new LC(*this); }
60 /// Construct LC object from a FactorGraph and parameters
61 LC(const FactorGraph & fg, const Properties &opts);
62 /// Assignment operator
63 LC& operator=(const LC & x) {
64 if( this != &x ) {
65 DAIAlgFG::operator=(x);
66 _pancakes = x._pancakes;
67 _cavitydists = x._cavitydists;
68 _phis = x._phis;
69 _beliefs = x._beliefs;
70 }
71 return *this;
72 }
73
74 static const char *Name;
75 double CalcCavityDist( size_t i, const std::string &name, const Properties &opts );
76 double InitCavityDists( const std::string &name, const Properties &opts );
77 long SetCavityDists( std::vector<Factor> &Q );
78
79 void init();
80 Factor NewPancake (size_t i, size_t _I, bool & hasNaNs);
81 double run();
82
83 std::string identify() const;
84 Factor belief (const Var &n) const { return( _beliefs[findVar(n)] ); }
85 Factor belief (const VarSet &/*ns*/) const {
86 DAI_THROW(NOT_IMPLEMENTED);
87 return Factor();
88 }
89 std::vector<Factor> beliefs() const { return _beliefs; }
90 Real logZ() const {
91 DAI_THROW(NOT_IMPLEMENTED);
92 return 0.0;
93 }
94 void CalcBelief (size_t i);
95 const Factor & belief (size_t i) const { return _beliefs[i]; };
96 const Factor & pancake (size_t i) const { return _pancakes[i]; };
97 const Factor & cavitydist (size_t i) const { return _cavitydists[i]; };
98
99 void clamp( const Var &/*n*/, size_t /*i*/ ) {
100 DAI_THROW(NOT_IMPLEMENTED);
101 }
102 void undoProbs( const VarSet &/*ns*/ ) {
103 DAI_THROW(NOT_IMPLEMENTED);
104 }
105 void saveProbs( const VarSet &/*ns*/ ) {
106 DAI_THROW(NOT_IMPLEMENTED);
107 }
108 virtual void makeCavity(const Var & /*n*/) {
109 DAI_THROW(NOT_IMPLEMENTED);
110 }
111 bool checkProperties();
112 };
113
114
115 } // end of namespace dai
116
117
118 #endif