1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
4 This file is part of libDAI.
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.
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.
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
27 #include <dai/diffs.h>
37 const char *MF::Name
= "MF";
40 bool MF::checkProperties() {
41 if( !HasProperty("tol") )
43 if (!HasProperty("maxiter") )
45 if (!HasProperty("verbose") )
48 ConvertPropertyTo
<double>("tol");
49 ConvertPropertyTo
<size_t>("maxiter");
50 ConvertPropertyTo
<size_t>("verbose");
59 _beliefs
.reserve( nrVars() );
62 for( size_t i
= 0; i
< nrVars(); ++i
)
63 _beliefs
.push_back(Factor(var(i
)));
67 string
MF::identify() const {
68 stringstream
result (stringstream::out
);
69 result
<< Name
<< GetProperties();
75 assert( checkProperties() );
77 for( vector
<Factor
>::iterator qi
= _beliefs
.begin(); qi
!= _beliefs
.end(); qi
++ )
86 cout
<< "Starting " << identify() << "...";
88 size_t pass_size
= _beliefs
.size();
89 Diffs
diffs(pass_size
* 3, 1.0);
92 for( t
=0; t
< (MaxIter()*pass_size
) && diffs
.maxDiff() > Tol(); t
++ ) {
93 // choose random Var i
94 size_t i
= (size_t) (nrVars() * rnd_uniform());
98 foreach( const Neighbor
&I
, nbV(i
) ) {
100 foreach( const Neighbor
&j
, nbF(I
) ) // for all j in I \ i
103 piet
= factor(I
).log0();
105 piet
= piet
.part_sum(var(i
));
110 jan
.normalize( _normtype
);
112 if( jan
.hasNaNs() ) {
113 cout
<< "MF::run(): ERROR: jan has NaNs!" << endl
;
117 diffs
.push( dist( jan
, _beliefs
[i
], Prob::DISTLINF
) );
122 updateMaxDiff( diffs
.maxDiff() );
124 if( Verbose() >= 1 ) {
125 if( diffs
.maxDiff() > Tol() ) {
128 cout
<< "MF::run: WARNING: not converged within " << MaxIter() << " passes (" << toc() - tic
<< " clocks)...final maxdiff:" << diffs
.maxDiff() << endl
;
132 cout
<< "converged in " << t
/ pass_size
<< " passes (" << toc() - tic
<< " clocks)." << endl
;
136 return diffs
.maxDiff();
140 Factor
MF::beliefV (size_t i
) const {
143 piet
.normalize( Prob::NORMPROB
);
148 Factor
MF::belief (const VarSet
&ns
) const {
150 return belief( *(ns
.begin()) );
152 assert( ns
.size() == 1 );
158 Factor
MF::belief (const Var
&n
) const {
159 return( beliefV( findVar( n
) ) );
163 vector
<Factor
> MF::beliefs() const {
164 vector
<Factor
> result
;
165 for( size_t i
= 0; i
< nrVars(); i
++ )
166 result
.push_back( beliefV(i
) );
171 Complex
MF::logZ() const {
174 for(size_t i
=0; i
< nrVars(); i
++ )
175 sum
-= beliefV(i
).entropy();
176 for(size_t I
=0; I
< nrFactors(); I
++ ) {
178 foreach( const Neighbor
&j
, nbF(I
) ) // for all j in I
180 henk
.normalize( Prob::NORMPROB
);
182 piet
= factor(I
).log0();
184 sum
-= Complex( piet
.totalSum() );
191 void MF::init( const VarSet
&ns
) {
192 for( size_t i
= 0; i
< nrVars(); i
++ ) {
193 if( ns
.contains(var(i
) ) )
194 _beliefs
[i
].fill( 1.0 );
199 } // end of namespace dai