Partial adoption of contributions by Giuseppe:
[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
39 std::vector<_ind_t> _indices;
40 std::vector<Prob> _messages, _newmessages;
41
42 public:
43 ENUM4(UpdateType,SEQFIX,SEQRND,SEQMAX,PARALL)
44 UpdateType Updates() const { return GetPropertyAs<UpdateType>("updates"); }
45
46 // default constructor
47 BP() : DAIAlgFG() {};
48 // copy constructor
49 BP(const BP & x) : DAIAlgFG(x), _indices(x._indices), _messages(x._messages), _newmessages(x._newmessages) {};
50 BP* clone() const { return new BP(*this); }
51 // construct BP object from FactorGraph
52 BP(const FactorGraph & fg, const Properties &opts) : DAIAlgFG(fg, opts) {
53 assert( checkProperties() );
54 Regenerate();
55 }
56 // assignment operator
57 BP & operator=(const BP & x) {
58 if(this!=&x) {
59 DAIAlgFG::operator=(x);
60 _messages = x._messages;
61 _newmessages = x._newmessages;
62 _indices = x._indices;
63 }
64 return *this;
65 }
66
67 static const char *Name;
68
69 Prob & message(size_t i1, size_t i2) { return( _messages[VV2E(i1,i2)] ); }
70 const Prob & message(size_t i1, size_t i2) const { return( _messages[VV2E(i1,i2)] ); }
71 Prob & newMessage(size_t i1, size_t i2) { return( _newmessages[VV2E(i1,i2)] ); }
72 const Prob & newMessage(size_t i1, size_t i2) const { return( _newmessages[VV2E(i1,i2)] ); }
73 _ind_t & index(size_t i1, size_t i2) { return( _indices[VV2E(i1,i2)] ); }
74 const _ind_t & index(size_t i1, size_t i2) const { return( _indices[VV2E(i1,i2)] ); }
75
76 std::string identify() const;
77 void Regenerate();
78 void init();
79 void calcNewMessage(size_t iI);
80 double run();
81 Factor belief1 (size_t i) const;
82 Factor belief2 (size_t I) const;
83 Factor belief (const Var &n) const;
84 Factor belief (const VarSet &n) const;
85 std::vector<Factor> beliefs() const;
86 Complex logZ() const;
87
88 void init( const VarSet &ns );
89 void undoProbs( const VarSet &ns ) { FactorGraph::undoProbs(ns); init(ns); }
90 bool checkProperties();
91 };
92
93
94 } // end of namespace dai
95
96
97 #endif