1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
5 This file is part of libDAI.
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef __defined_libdai_mr_h
24 #define __defined_libdai_mr_h
29 #include <dai/factorgraph.h>
30 #include <dai/daialg.h>
32 #include <dai/properties.h>
33 #include <dai/exceptions.h>
34 #include <boost/dynamic_bitset.hpp>
40 class MR
: public DAIAlgFG
{
42 bool supported
; // is the underlying factor graph supported?
44 std::vector
<size_t> con
; // con[i] = connectivity of spin i
45 std::vector
<std::vector
<size_t> > nb
; // nb[i] are the neighbours of spin i
46 std::vector
<std::vector
<double> > tJ
; // tJ[i][_j] is the tanh of the interaction between spin i and its neighbour nb[i][_j]
47 std::vector
<double> theta
; // theta[i] is the local field on spin i
48 std::vector
<std::vector
<double> > M
; // M[i][_j] is M^{(i)}_j
49 std::vector
<std::vector
<size_t> > kindex
; // the _j'th neighbour of spin i has spin i as its kindex[i][_j]'th neighbour
50 std::vector
<std::vector
<std::vector
<double> > > cors
;
52 static const size_t kmax
= 31;
53 typedef boost::dynamic_bitset
<> sub_nb
;
57 std::vector
<double> Mag
;
66 DAI_ENUM(UpdateType
,FULL
,LINEAR
)
67 DAI_ENUM(InitType
,RESPPROP
,CLAMPING
,EXACT
)
71 static const char *Name
;
74 /// Default constructor
75 MR() : DAIAlgFG(), supported(), con(), nb(), tJ(), theta(), M(), kindex(), cors(), N(), Mag(), _maxdiff(), _iters(), props() {}
77 /// Construct from FactorGraph fg and PropertySet opts
78 MR( const FactorGraph
&fg
, const PropertySet
&opts
);
81 MR( const MR
&x
) : DAIAlgFG(x
), supported(x
.supported
), con(x
.con
), nb(x
.nb
), tJ(x
.tJ
), theta(x
.theta
), M(x
.M
), kindex(x
.kindex
), cors(x
.cors
), N(x
.N
), Mag(x
.Mag
), _maxdiff(x
._maxdiff
), _iters(x
._iters
), props(x
.props
) {}
83 /// Clone *this (virtual copy constructor)
84 virtual MR
* clone() const { return new MR(*this); }
86 /// Create (virtual default constructor)
87 virtual MR
* create() const { return new MR(); }
89 /// Assignment operator
90 MR
& operator=( const MR
&x
) {
92 DAIAlgFG::operator=(x
);
93 supported
= x
.supported
;
103 _maxdiff
= x
._maxdiff
;
110 /// Identifies itself for logging purposes
111 virtual std::string
identify() const;
113 /// Get single node belief
114 virtual Factor
belief( const Var
&n
) const;
116 /// Get general belief
117 virtual Factor
belief( const VarSet
&/*ns*/ ) const {
118 DAI_THROW(NOT_IMPLEMENTED
);
123 virtual std::vector
<Factor
> beliefs() const;
125 /// Get log partition sum
126 virtual Real
logZ() const {
127 DAI_THROW(NOT_IMPLEMENTED
);
131 /// Clear messages and beliefs
132 virtual void init() {}
134 /// Clear messages and beliefs corresponding to the nodes in ns
135 virtual void init( const VarSet
&/*ns*/ ) {
136 DAI_THROW(NOT_IMPLEMENTED
);
139 /// The actual approximate inference algorithm
140 virtual double run();
142 /// Return maximum difference between single node beliefs in the last pass
143 virtual double maxDiff() const { return _maxdiff
; }
145 /// Return number of passes over the factorgraph
146 virtual size_t Iterations() const { return _iters
; }
149 void init(size_t Nin
, double *_w
, double *_th
);
152 double init_cor_resp();
156 double _tJ(size_t i
, sub_nb A
);
158 double Omega(size_t i
, size_t _j
, size_t _l
);
159 double T(size_t i
, sub_nb A
);
160 double T(size_t i
, size_t _j
);
161 double Gamma(size_t i
, size_t _j
, size_t _l1
, size_t _l2
);
162 double Gamma(size_t i
, size_t _l1
, size_t _l2
);
164 double appM(size_t i
, sub_nb A
);
165 void sum_subs(size_t j
, sub_nb A
, double *sum_even
, double *sum_odd
);
167 double sign(double a
) { return (a
>= 0) ? 1.0 : -1.0; }
169 void setProperties( const PropertySet
&opts
);
170 PropertySet
getProperties() const;
171 std::string
printProperties() const;
175 } // end of namespace dai