1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
4 This file is part of libDAI.
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.
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.
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
22 #ifndef __defined_libdai_bp_h
23 #define __defined_libdai_bp_h
27 #include <dai/daialg.h>
28 #include <dai/factorgraph.h>
35 class BP
: public DAIAlgFG
{
37 typedef std::vector
<size_t> ind_t
;
45 std::vector
<std::vector
<EdgeProp
> > edges
;
48 ENUM4(UpdateType
,SEQFIX
,SEQRND
,SEQMAX
,PARALL
)
49 UpdateType
Updates() const { return GetPropertyAs
<UpdateType
>("updates"); }
51 // default constructor
54 BP(const BP
& x
) : DAIAlgFG(x
), edges(x
.edges
) {};
55 BP
* clone() const { return new BP(*this); }
56 // construct BP object from FactorGraph
57 BP(const FactorGraph
& fg
, const Properties
&opts
) : DAIAlgFG(fg
, opts
) {
58 assert( checkProperties() );
61 // assignment operator
62 BP
& operator=(const BP
& x
) {
64 DAIAlgFG::operator=(x
);
70 static const char *Name
;
72 Prob
& message(size_t i
, size_t _I
) { return edges
[i
][_I
].message
; }
73 const Prob
& message(size_t i
, size_t _I
) const { return edges
[i
][_I
].message
; }
74 Prob
& newMessage(size_t i
, size_t _I
) { return edges
[i
][_I
].newMessage
; }
75 const Prob
& newMessage(size_t i
, size_t _I
) const { return edges
[i
][_I
].newMessage
; }
76 ind_t
& index(size_t i
, size_t _I
) { return edges
[i
][_I
].index
; }
77 const ind_t
& index(size_t i
, size_t _I
) const { return edges
[i
][_I
].index
; }
78 double & residual(size_t i
, size_t _I
) { return edges
[i
][_I
].residual
; }
79 const double & residual(size_t i
, size_t _I
) const { return edges
[i
][_I
].residual
; }
80 void findMaxResidual( size_t &i
, size_t &_I
);
82 std::string
identify() const;
85 void calcNewMessage( size_t i
, size_t _I
);
87 Factor
beliefV (size_t i
) const;
88 Factor
beliefF (size_t I
) const;
89 Factor
belief (const Var
&n
) const;
90 Factor
belief (const VarSet
&n
) const;
91 std::vector
<Factor
> beliefs() const;
94 void init( const VarSet
&ns
);
95 void undoProbs( const VarSet
&ns
) { FactorGraph::undoProbs(ns
); init(ns
); }
96 bool checkProperties();
100 } // end of namespace dai