Merge branch 'master' of git@git.tuebingen.mpg.de:libdai
[libdai.git] / src / mf.cpp
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 #include <iostream>
23 #include <sstream>
24 #include <map>
25 #include <set>
26 #include <dai/mf.h>
27 #include <dai/diffs.h>
28 #include <dai/util.h>
29
30
31 namespace dai {
32
33
34 using namespace std;
35
36
37 const char *MF::Name = "MF";
38
39
40 void MF::setProperties( const PropertySet &opts ) {
41 assert( opts.hasKey("tol") );
42 assert( opts.hasKey("maxiter") );
43 assert( opts.hasKey("verbose") );
44
45 props.tol = opts.getStringAs<double>("tol");
46 props.maxiter = opts.getStringAs<size_t>("maxiter");
47 props.verbose = opts.getStringAs<size_t>("verbose");
48 }
49
50
51 PropertySet MF::getProperties() const {
52 PropertySet opts;
53 opts.Set( "tol", props.tol );
54 opts.Set( "maxiter", props.maxiter );
55 opts.Set( "verbose", props.verbose );
56 return opts;
57 }
58
59
60 void MF::create() {
61 // clear beliefs
62 _beliefs.clear();
63 _beliefs.reserve( nrVars() );
64
65 // create beliefs
66 for( size_t i = 0; i < nrVars(); ++i )
67 _beliefs.push_back(Factor(var(i)));
68 }
69
70
71 string MF::identify() const {
72 stringstream result (stringstream::out);
73 result << Name << getProperties();
74 return result.str();
75 }
76
77
78 void MF::init() {
79 for( vector<Factor>::iterator qi = _beliefs.begin(); qi != _beliefs.end(); qi++ )
80 qi->fill(1.0);
81 }
82
83
84 double MF::run() {
85 double tic = toc();
86
87 if( props.verbose >= 1 )
88 cout << "Starting " << identify() << "...";
89
90 size_t pass_size = _beliefs.size();
91 Diffs diffs(pass_size * 3, 1.0);
92
93 size_t t=0;
94 for( t=0; t < (props.maxiter*pass_size) && diffs.maxDiff() > props.tol; t++ ) {
95 // choose random Var i
96 size_t i = (size_t) (nrVars() * rnd_uniform());
97
98 Factor jan;
99 Factor piet;
100 foreach( const Neighbor &I, nbV(i) ) {
101 Factor henk;
102 foreach( const Neighbor &j, nbF(I) ) // for all j in I \ i
103 if( j != i )
104 henk *= _beliefs[j];
105 piet = factor(I).log0();
106 piet *= henk;
107 piet = piet.part_sum(var(i));
108 piet = piet.exp();
109 jan *= piet;
110 }
111
112 jan.normalize( Prob::NORMPROB );
113
114 if( jan.hasNaNs() ) {
115 cout << "MF::run(): ERROR: jan has NaNs!" << endl;
116 return 1.0;
117 }
118
119 diffs.push( dist( jan, _beliefs[i], Prob::DISTLINF ) );
120
121 _beliefs[i] = jan;
122 }
123
124 if( diffs.maxDiff() > maxdiff )
125 maxdiff = diffs.maxDiff();
126
127 if( props.verbose >= 1 ) {
128 if( diffs.maxDiff() > props.tol ) {
129 if( props.verbose == 1 )
130 cout << endl;
131 cout << "MF::run: WARNING: not converged within " << props.maxiter << " passes (" << toc() - tic << " clocks)...final maxdiff:" << diffs.maxDiff() << endl;
132 } else {
133 if( props.verbose >= 2 )
134 cout << "MF::run: ";
135 cout << "converged in " << t / pass_size << " passes (" << toc() - tic << " clocks)." << endl;
136 }
137 }
138
139 return diffs.maxDiff();
140 }
141
142
143 Factor MF::beliefV (size_t i) const {
144 Factor piet;
145 piet = _beliefs[i];
146 piet.normalize( Prob::NORMPROB );
147 return(piet);
148 }
149
150
151 Factor MF::belief (const VarSet &ns) const {
152 if( ns.size() == 1 )
153 return belief( *(ns.begin()) );
154 else {
155 assert( ns.size() == 1 );
156 return Factor();
157 }
158 }
159
160
161 Factor MF::belief (const Var &n) const {
162 return( beliefV( findVar( n ) ) );
163 }
164
165
166 vector<Factor> MF::beliefs() const {
167 vector<Factor> result;
168 for( size_t i = 0; i < nrVars(); i++ )
169 result.push_back( beliefV(i) );
170 return result;
171 }
172
173
174 Real MF::logZ() const {
175 Real sum = 0.0;
176
177 for(size_t i=0; i < nrVars(); i++ )
178 sum -= beliefV(i).entropy();
179 for(size_t I=0; I < nrFactors(); I++ ) {
180 Factor henk;
181 foreach( const Neighbor &j, nbF(I) ) // for all j in I
182 henk *= _beliefs[j];
183 henk.normalize( Prob::NORMPROB );
184 Factor piet;
185 piet = factor(I).log0();
186 piet *= henk;
187 sum -= piet.totalSum();
188 }
189
190 return -sum;
191 }
192
193
194 void MF::init( const VarSet &ns ) {
195 for( size_t i = 0; i < nrVars(); i++ ) {
196 if( ns.contains(var(i) ) )
197 _beliefs[i].fill( 1.0 );
198 }
199 }
200
201
202 } // end of namespace dai