Updated copyrights
[libdai.git] / include / dai / mr.h
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
4
5 This file is part of libDAI.
6
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.
11
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.
16
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
20 */
21
22
23 #ifndef __defined_libdai_mr_h
24 #define __defined_libdai_mr_h
25
26
27 #include <vector>
28 #include <string>
29 #include <dai/factorgraph.h>
30 #include <dai/daialg.h>
31 #include <dai/enum.h>
32 #include <dai/properties.h>
33 #include <dai/exceptions.h>
34 #include <boost/dynamic_bitset.hpp>
35
36
37 namespace dai {
38
39
40 class MR : public DAIAlgFG {
41 private:
42 bool supported; // is the underlying factor graph supported?
43
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;
51
52 static const size_t kmax = 31;
53 typedef boost::dynamic_bitset<> sub_nb;
54
55 size_t N;
56
57 std::vector<double> Mag;
58
59 double _maxdiff;
60 size_t _iters;
61
62 public:
63 struct Properties {
64 size_t verbose;
65 double tol;
66 DAI_ENUM(UpdateType,FULL,LINEAR)
67 DAI_ENUM(InitType,RESPPROP,CLAMPING,EXACT)
68 UpdateType updates;
69 InitType inits;
70 } props;
71 static const char *Name;
72
73 public:
74 /// Default constructor
75 MR() : DAIAlgFG(), supported(), con(), nb(), tJ(), theta(), M(), kindex(), cors(), N(), Mag(), _maxdiff(), _iters(), props() {}
76
77 /// Construct from FactorGraph fg and PropertySet opts
78 MR( const FactorGraph &fg, const PropertySet &opts );
79
80 /// Copy constructor
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) {}
82
83 /// Clone *this (virtual copy constructor)
84 virtual MR* clone() const { return new MR(*this); }
85
86 /// Create (virtual default constructor)
87 virtual MR* create() const { return new MR(); }
88
89 /// Assignment operator
90 MR& operator=( const MR &x ) {
91 if( this != &x ) {
92 DAIAlgFG::operator=(x);
93 supported = x.supported;
94 con = x.con;
95 nb = x.nb;
96 tJ = x.tJ;
97 theta = x.theta;
98 M = x.M;
99 kindex = x.kindex;
100 cors = x.cors;
101 N = x.N;
102 Mag = x.Mag;
103 _maxdiff = x._maxdiff;
104 _iters = x._iters;
105 props = x.props;
106 }
107 return *this;
108 }
109
110 /// Identifies itself for logging purposes
111 virtual std::string identify() const;
112
113 /// Get single node belief
114 virtual Factor belief( const Var &n ) const;
115
116 /// Get general belief
117 virtual Factor belief( const VarSet &/*ns*/ ) const {
118 DAI_THROW(NOT_IMPLEMENTED);
119 return Factor();
120 }
121
122 /// Get all beliefs
123 virtual std::vector<Factor> beliefs() const;
124
125 /// Get log partition sum
126 virtual Real logZ() const {
127 DAI_THROW(NOT_IMPLEMENTED);
128 return 0.0;
129 }
130
131 /// Clear messages and beliefs
132 virtual void init() {}
133
134 /// Clear messages and beliefs corresponding to the nodes in ns
135 virtual void init( const VarSet &/*ns*/ ) {
136 DAI_THROW(NOT_IMPLEMENTED);
137 }
138
139 /// The actual approximate inference algorithm
140 virtual double run();
141
142 /// Return maximum difference between single node beliefs in the last pass
143 virtual double maxDiff() const { return _maxdiff; }
144
145 /// Return number of passes over the factorgraph
146 virtual size_t Iterations() const { return _iters; }
147
148
149 void init(size_t Nin, double *_w, double *_th);
150 void makekindex();
151 void init_cor();
152 double init_cor_resp();
153 void solvemcav();
154 void solveM();
155
156 double _tJ(size_t i, sub_nb A);
157
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);
163
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);
166
167 double sign(double a) { return (a >= 0) ? 1.0 : -1.0; }
168
169 void setProperties( const PropertySet &opts );
170 PropertySet getProperties() const;
171 std::string printProperties() const;
172 };
173
174
175 } // end of namespace dai
176
177
178 #endif