Replaced complex numbers by real numbers
[libdai.git] / include / dai / bp.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_bp_h
23 #define __defined_libdai_bp_h
24
25
26 #include <string>
27 #include <dai/daialg.h>
28 #include <dai/factorgraph.h>
29 #include <dai/enum.h>
30
31
32 namespace dai {
33
34
35 class BP : public DAIAlgFG {
36 protected:
37 typedef std::vector<size_t> ind_t;
38 struct EdgeProp {
39 ind_t index;
40 Prob message;
41 Prob newMessage;
42 double residual;
43 };
44
45 std::vector<std::vector<EdgeProp> > edges;
46 bool logDomain;
47
48 public:
49 ENUM4(UpdateType,SEQFIX,SEQRND,SEQMAX,PARALL)
50 UpdateType Updates() const { return GetPropertyAs<UpdateType>("updates"); }
51
52 // default constructor
53 BP() : DAIAlgFG(), edges(), logDomain(false) {};
54 // copy constructor
55 BP(const BP & x) : DAIAlgFG(x), edges(x.edges), logDomain(x.logDomain) {};
56 BP* clone() const { return new BP(*this); }
57 // construct BP object from FactorGraph
58 BP(const FactorGraph & fg, const Properties &opts) : DAIAlgFG(fg, opts), edges(), logDomain(false) {
59 assert( checkProperties() );
60 create();
61 }
62 // assignment operator
63 BP & operator=(const BP & x) {
64 if(this!=&x) {
65 DAIAlgFG::operator=(x);
66 edges = x.edges;
67 logDomain = x.logDomain;
68 }
69 return *this;
70 }
71
72 static const char *Name;
73
74 Prob & message(size_t i, size_t _I) { return edges[i][_I].message; }
75 const Prob & message(size_t i, size_t _I) const { return edges[i][_I].message; }
76 Prob & newMessage(size_t i, size_t _I) { return edges[i][_I].newMessage; }
77 const Prob & newMessage(size_t i, size_t _I) const { return edges[i][_I].newMessage; }
78 ind_t & index(size_t i, size_t _I) { return edges[i][_I].index; }
79 const ind_t & index(size_t i, size_t _I) const { return edges[i][_I].index; }
80 double & residual(size_t i, size_t _I) { return edges[i][_I].residual; }
81 const double & residual(size_t i, size_t _I) const { return edges[i][_I].residual; }
82 void findMaxResidual( size_t &i, size_t &_I );
83
84 std::string identify() const;
85 void create();
86 void init();
87 void calcNewMessage( size_t i, size_t _I );
88 double run();
89 Factor beliefV (size_t i) const;
90 Factor beliefF (size_t I) const;
91 Factor belief (const Var &n) const;
92 Factor belief (const VarSet &n) const;
93 std::vector<Factor> beliefs() const;
94 Real logZ() const;
95
96 void init( const VarSet &ns );
97 void undoProbs( const VarSet &ns ) { FactorGraph::undoProbs(ns); init(ns); }
98 bool checkProperties();
99 };
100
101
102 } // end of namespace dai
103
104
105 #endif