Added Exceptions framework (and more)
[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/exceptions.h>
32
33
34 namespace dai {
35
36
37 class sub_nb;
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
54 size_t N;
55
56 std::vector<double> Mag;
57
58 public:
59 ENUM2(UpdateType,FULL,LINEAR)
60 ENUM3(InitType,RESPPROP,CLAMPING,EXACT)
61
62 UpdateType Updates() const { return GetPropertyAs<UpdateType>("updates"); }
63 InitType Inits() const { return GetPropertyAs<InitType>("inits"); }
64
65 MR( const FactorGraph & fg, const Properties &opts );
66 void init(size_t Nin, double *_w, double *_th);
67 void makekindex();
68 void read_files();
69 void init_cor();
70 double init_cor_resp();
71 void solvemcav();
72 void solveM();
73 double run();
74 Factor belief( const Var &n ) const;
75 Factor belief( const VarSet &/*ns*/ ) const {
76 DAI_THROW(NOT_IMPLEMENTED);
77 return Factor();
78 }
79 std::vector<Factor> beliefs() const;
80 Complex logZ() const {
81 DAI_THROW(NOT_IMPLEMENTED);
82 return 0.0;
83 }
84 void init() { assert( checkProperties() ); }
85 static const char *Name;
86 std::string identify() const;
87 double _tJ(size_t i, sub_nb A);
88
89 double Omega(size_t i, size_t _j, size_t _l);
90 double T(size_t i, sub_nb A);
91 double T(size_t i, size_t _j);
92 double Gamma(size_t i, size_t _j, size_t _l1, size_t _l2);
93 double Gamma(size_t i, size_t _l1, size_t _l2);
94
95 double appM(size_t i, sub_nb A);
96 void sum_subs(size_t j, sub_nb A, double *sum_even, double *sum_odd);
97
98 double sign(double a) { return (a >= 0) ? 1.0 : -1.0; }
99 MR* clone() const { return new MR(*this); }
100
101 bool checkProperties();
102
103 };
104
105
106 // represents a subset of nb[i] as a binary number
107 // the elements of a subset should be thought of as indices in nb[i]
108 class sub_nb {
109 private:
110 size_t s;
111 size_t bits;
112
113 public:
114 // construct full subset containing nr_elmt elements
115 sub_nb(size_t nr_elmt) {
116 #ifdef DAI_DEBUG
117 assert( nr_elmt < sizeof(size_t) / sizeof(char) * 8 );
118 #endif
119 bits = nr_elmt;
120 s = (1U << bits) - 1;
121 }
122
123 // copy constructor
124 sub_nb( const sub_nb & x ) : s(x.s), bits(x.bits) {}
125
126 // assignment operator
127 sub_nb & operator=( const sub_nb &x ) {
128 if( this != &x ) {
129 s = x.s;
130 bits = x.bits;
131 }
132 return *this;
133 }
134
135 // returns number of elements
136 size_t size() {
137 size_t size = 0;
138 for(size_t bit = 0; bit < bits; bit++)
139 if( s & (1U << bit) )
140 size++;
141 return size;
142 }
143
144 // increases s by one (for enumeration in lexicographical order)
145 sub_nb operator++() {
146 s++;
147 if( s >= (1U << bits) )
148 s = 0;
149 return *this;
150 }
151
152 // return i'th element of this subset
153 size_t operator[](size_t i) {
154 size_t bit;
155 for(bit = 0; bit < bits; bit++ )
156 if( s & (1U << bit) ) {
157 if( i == 0 )
158 break;
159 else
160 i--;
161 }
162 #ifdef DAI_DEBUG
163 assert( bit < bits );
164 #endif
165 return bit;
166 }
167
168 // add index _j to this subset
169 sub_nb &operator +=(size_t _j) {
170 s |= (1U << _j);
171 return *this;
172 }
173
174 // return copy with index _j
175 sub_nb operator+(size_t _j) {
176 sub_nb x = *this;
177 x += _j;
178 return x;
179 }
180
181 // delete index _j from this subset
182 sub_nb &operator -=(size_t _j) {
183 s &= ~(1U << _j);
184 return *this;
185 }
186
187 // return copy without index _j
188 sub_nb operator-(size_t _j) {
189 sub_nb x = *this;
190 x -= _j;
191 return x;
192 }
193
194 // empty this subset
195 sub_nb & clear() {
196 s = 0;
197 return *this;
198 }
199
200 // returns true if subset is empty
201 bool empty() { return (s == 0); }
202
203 // return 1 if _j is contained, 0 otherwise ("is element of")
204 size_t operator&(size_t _j) { return s & (1U << _j); }
205
206 friend std::ostream& operator<< (std::ostream& os, const sub_nb x) {
207 if( x.bits == 0 )
208 os << "empty";
209 else {
210 for(size_t bit = x.bits; bit > 0; bit-- )
211 if( x.s & (1U << (bit-1)) )
212 os << "1";
213 else
214 os << "0";
215 }
216 return os;
217 }
218 };
219
220
221 } // end of namespace dai
222
223
224 #endif