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