Merge branch 'master' of git@git.tuebingen.mpg.de:libdai
[libdai.git] / include / dai / mr.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_mr_h
23 #define __defined_libdai_mr_h
24
25
26 #include <vector>
27 #include <string>
28 #include <dai/factorgraph.h>
29 #include <dai/daialg.h>
30 #include <dai/enum.h>
31 #include <dai/properties.h>
32 #include <dai/exceptions.h>
33
34
35 namespace dai {
36
37
38 class sub_nb;
39
40
41 class MR : public DAIAlgFG {
42 private:
43 bool supported; // is the underlying factor graph supported?
44
45 std::vector<size_t> con; // con[i] = connectivity of spin i
46 std::vector<std::vector<size_t> > nb; // nb[i] are the neighbours of spin i
47 std::vector<std::vector<double> > tJ; // tJ[i][_j] is the tanh of the interaction between spin i and its neighbour nb[i][_j]
48 std::vector<double> theta; // theta[i] is the local field on spin i
49 std::vector<std::vector<double> > M; // M[i][_j] is M^{(i)}_j
50 std::vector<std::vector<size_t> > kindex; // the _j'th neighbour of spin i has spin i as its kindex[i][_j]'th neighbour
51 std::vector<std::vector<std::vector<double> > > cors;
52
53 static const size_t kmax = 31;
54
55 size_t N;
56
57 std::vector<double> Mag;
58
59 public:
60 struct Properties {
61 size_t verbose;
62 double tol;
63 DAI_ENUM(UpdateType,FULL,LINEAR)
64 DAI_ENUM(InitType,RESPPROP,CLAMPING,EXACT)
65 UpdateType updates;
66 InitType inits;
67 } props;
68 double maxdiff;
69
70 public:
71 MR( const FactorGraph & fg, const PropertySet &opts );
72 void init(size_t Nin, double *_w, double *_th);
73 void makekindex();
74 void read_files();
75 void init_cor();
76 double init_cor_resp();
77 void solvemcav();
78 void solveM();
79 double run();
80 Factor belief( const Var &n ) const;
81 Factor belief( const VarSet &/*ns*/ ) const {
82 DAI_THROW(NOT_IMPLEMENTED);
83 return Factor();
84 }
85 std::vector<Factor> beliefs() const;
86 Real logZ() const {
87 DAI_THROW(NOT_IMPLEMENTED);
88 return 0.0;
89 }
90 void init() {}
91 static const char *Name;
92 std::string identify() const;
93 double _tJ(size_t i, sub_nb A);
94
95 double Omega(size_t i, size_t _j, size_t _l);
96 double T(size_t i, sub_nb A);
97 double T(size_t i, size_t _j);
98 double Gamma(size_t i, size_t _j, size_t _l1, size_t _l2);
99 double Gamma(size_t i, size_t _l1, size_t _l2);
100
101 double appM(size_t i, sub_nb A);
102 void sum_subs(size_t j, sub_nb A, double *sum_even, double *sum_odd);
103
104 double sign(double a) { return (a >= 0) ? 1.0 : -1.0; }
105 MR* clone() const { return new MR(*this); }
106
107 void setProperties( const PropertySet &opts );
108 PropertySet getProperties() const;
109 std::string printProperties() const;
110 double maxDiff() const { return maxdiff; }
111 };
112
113
114 // represents a subset of nb[i] as a binary number
115 // the elements of a subset should be thought of as indices in nb[i]
116 class sub_nb {
117 private:
118 size_t s;
119 size_t bits;
120
121 public:
122 // construct full subset containing nr_elmt elements
123 sub_nb(size_t nr_elmt) {
124 #ifdef DAI_DEBUG
125 assert( nr_elmt < sizeof(size_t) / sizeof(char) * 8 );
126 #endif
127 bits = nr_elmt;
128 s = (1U << bits) - 1;
129 }
130
131 // copy constructor
132 sub_nb( const sub_nb & x ) : s(x.s), bits(x.bits) {}
133
134 // assignment operator
135 sub_nb & operator=( const sub_nb &x ) {
136 if( this != &x ) {
137 s = x.s;
138 bits = x.bits;
139 }
140 return *this;
141 }
142
143 // returns number of elements
144 size_t size() {
145 size_t size = 0;
146 for(size_t bit = 0; bit < bits; bit++)
147 if( s & (1U << bit) )
148 size++;
149 return size;
150 }
151
152 // increases s by one (for enumeration in lexicographical order)
153 sub_nb operator++() {
154 s++;
155 if( s >= (1U << bits) )
156 s = 0;
157 return *this;
158 }
159
160 // return i'th element of this subset
161 size_t operator[](size_t i) {
162 size_t bit;
163 for(bit = 0; bit < bits; bit++ )
164 if( s & (1U << bit) ) {
165 if( i == 0 )
166 break;
167 else
168 i--;
169 }
170 #ifdef DAI_DEBUG
171 assert( bit < bits );
172 #endif
173 return bit;
174 }
175
176 // add index _j to this subset
177 sub_nb &operator +=(size_t _j) {
178 s |= (1U << _j);
179 return *this;
180 }
181
182 // return copy with index _j
183 sub_nb operator+(size_t _j) {
184 sub_nb x = *this;
185 x += _j;
186 return x;
187 }
188
189 // delete index _j from this subset
190 sub_nb &operator -=(size_t _j) {
191 s &= ~(1U << _j);
192 return *this;
193 }
194
195 // return copy without index _j
196 sub_nb operator-(size_t _j) {
197 sub_nb x = *this;
198 x -= _j;
199 return x;
200 }
201
202 // empty this subset
203 sub_nb & clear() {
204 s = 0;
205 return *this;
206 }
207
208 // returns true if subset is empty
209 bool empty() { return (s == 0); }
210
211 // return 1 if _j is contained, 0 otherwise ("is element of")
212 size_t operator&(size_t _j) { return s & (1U << _j); }
213
214 friend std::ostream& operator<< (std::ostream& os, const sub_nb x) {
215 if( x.bits == 0 )
216 os << "empty";
217 else {
218 for(size_t bit = x.bits; bit > 0; bit-- )
219 if( x.s & (1U << (bit-1)) )
220 os << "1";
221 else
222 os << "0";
223 }
224 return os;
225 }
226 };
227
228
229 } // end of namespace dai
230
231
232 #endif