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
5 This file is part of libDAI.
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.
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.
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
28 #include <dai/diffs.h>
30 #include <dai/alldai.h>
39 const char *LC::Name
= "LC";
42 void LC::setProperties( const PropertySet
&opts
) {
43 assert( opts
.hasKey("tol") );
44 assert( opts
.hasKey("maxiter") );
45 assert( opts
.hasKey("verbose") );
46 assert( opts
.hasKey("cavity") );
47 assert( opts
.hasKey("updates") );
49 props
.tol
= opts
.getStringAs
<double>("tol");
50 props
.maxiter
= opts
.getStringAs
<size_t>("maxiter");
51 props
.verbose
= opts
.getStringAs
<size_t>("verbose");
52 props
.cavity
= opts
.getStringAs
<Properties::CavityType
>("cavity");
53 props
.updates
= opts
.getStringAs
<Properties::UpdateType
>("updates");
54 if( opts
.hasKey("cavainame") )
55 props
.cavainame
= opts
.getStringAs
<string
>("cavainame");
56 if( opts
.hasKey("cavaiopts") )
57 props
.cavaiopts
= opts
.getStringAs
<PropertySet
>("cavaiopts");
58 if( opts
.hasKey("reinit") )
59 props
.reinit
= opts
.getStringAs
<bool>("reinit");
60 if( opts
.hasKey("damping") )
61 props
.damping
= opts
.getStringAs
<double>("damping");
67 PropertySet
LC::getProperties() const {
69 opts
.Set( "tol", props
.tol
);
70 opts
.Set( "maxiter", props
.maxiter
);
71 opts
.Set( "verbose", props
.verbose
);
72 opts
.Set( "cavity", props
.cavity
);
73 opts
.Set( "updates", props
.updates
);
74 opts
.Set( "cavainame", props
.cavainame
);
75 opts
.Set( "cavaiopts", props
.cavaiopts
);
76 opts
.Set( "reinit", props
.reinit
);
77 opts
.Set( "damping", props
.damping
);
82 string
LC::printProperties() const {
83 stringstream
s( stringstream::out
);
85 s
<< "tol=" << props
.tol
<< ",";
86 s
<< "maxiter=" << props
.maxiter
<< ",";
87 s
<< "verbose=" << props
.verbose
<< ",";
88 s
<< "cavity=" << props
.cavity
<< ",";
89 s
<< "updates=" << props
.updates
<< ",";
90 s
<< "cavainame=" << props
.cavainame
<< ",";
91 s
<< "cavaiopts=" << props
.cavaiopts
<< ",";
92 s
<< "reinit=" << props
.reinit
<< ",";
93 s
<< "damping=" << props
.damping
<< "]";
98 LC::LC( const FactorGraph
& fg
, const PropertySet
&opts
) : DAIAlgFG(fg
), _pancakes(), _cavitydists(), _phis(), _beliefs(), _maxdiff(0.0), _iters(0), props() {
99 setProperties( opts
);
102 _pancakes
.resize( nrVars() );
104 // create cavitydists
105 for( size_t i
=0; i
< nrVars(); i
++ )
106 _cavitydists
.push_back(Factor( delta(i
) ));
109 _phis
.reserve( nrVars() );
110 for( size_t i
= 0; i
< nrVars(); i
++ ) {
111 _phis
.push_back( vector
<Factor
>() );
112 _phis
[i
].reserve( nbV(i
).size() );
113 foreach( const Neighbor
&I
, nbV(i
) )
114 _phis
[i
].push_back( Factor( factor(I
).vars() / var(i
) ) );
118 _beliefs
.reserve( nrVars() );
119 for( size_t i
=0; i
< nrVars(); i
++ )
120 _beliefs
.push_back(Factor(var(i
)));
124 string
LC::identify() const {
125 return string(Name
) + printProperties();
129 void LC::CalcBelief (size_t i
) {
130 _beliefs
[i
] = _pancakes
[i
].marginal(var(i
));
134 double LC::CalcCavityDist (size_t i
, const std::string
&name
, const PropertySet
&opts
) {
138 if( props
.verbose
>= 2 )
139 cout
<< "Initing cavity " << var(i
) << "(" << delta(i
).size() << " vars, " << nrStates(delta(i
)) << " states)" << endl
;
141 if( props
.cavity
== Properties::CavityType::UNIFORM
)
142 Bi
= Factor(delta(i
));
144 InfAlg
*cav
= newInfAlg( name
, *this, opts
);
145 cav
->makeCavity( i
);
147 if( props
.cavity
== Properties::CavityType::FULL
)
148 Bi
= calcMarginal( *cav
, cav
->fg().delta(i
), props
.reinit
);
149 else if( props
.cavity
== Properties::CavityType::PAIR
)
150 Bi
= calcMarginal2ndO( *cav
, cav
->fg().delta(i
), props
.reinit
);
151 else if( props
.cavity
== Properties::CavityType::PAIR2
) {
152 vector
<Factor
> pairbeliefs
= calcPairBeliefsNew( *cav
, cav
->fg().delta(i
), props
.reinit
);
153 for( size_t ij
= 0; ij
< pairbeliefs
.size(); ij
++ )
154 Bi
*= pairbeliefs
[ij
];
156 maxdiff
= cav
->maxDiff();
160 _cavitydists
[i
] = Bi
;
166 double LC::InitCavityDists( const std::string
&name
, const PropertySet
&opts
) {
169 if( props
.verbose
>= 1 ) {
170 cout
<< Name
<< "::InitCavityDists: ";
171 if( props
.cavity
== Properties::CavityType::UNIFORM
)
172 cout
<< "Using uniform initial cavity distributions" << endl
;
173 else if( props
.cavity
== Properties::CavityType::FULL
)
174 cout
<< "Using full " << name
<< opts
<< "...";
175 else if( props
.cavity
== Properties::CavityType::PAIR
)
176 cout
<< "Using pairwise " << name
<< opts
<< "...";
177 else if( props
.cavity
== Properties::CavityType::PAIR2
)
178 cout
<< "Using pairwise(new) " << name
<< opts
<< "...";
181 double maxdiff
= 0.0;
182 for( size_t i
= 0; i
< nrVars(); i
++ ) {
183 double md
= CalcCavityDist(i
, name
, opts
);
188 if( props
.verbose
>= 1 ) {
189 cout
<< Name
<< "::InitCavityDists used " << toc() - tic
<< " seconds." << endl
;
196 long LC::SetCavityDists( std::vector
<Factor
> &Q
) {
197 if( props
.verbose
>= 1 )
198 cout
<< Name
<< "::SetCavityDists: Setting initial cavity distributions" << endl
;
199 if( Q
.size() != nrVars() )
201 for( size_t i
= 0; i
< nrVars(); i
++ ) {
202 if( _cavitydists
[i
].vars() != Q
[i
].vars() ) {
205 _cavitydists
[i
] = Q
[i
];
212 for( size_t i
= 0; i
< nrVars(); ++i
)
213 foreach( const Neighbor
&I
, nbV(i
) )
214 if( props
.updates
== Properties::UpdateType::SEQRND
)
215 _phis
[i
][I
.iter
].randomize();
217 _phis
[i
][I
.iter
].fill(1.0);
221 Factor
LC::NewPancake (size_t i
, size_t _I
, bool & hasNaNs
) {
222 size_t I
= nbV(i
)[_I
];
223 Factor piet
= _pancakes
[i
];
225 // recalculate _pancake[i]
226 VarSet Ivars
= factor(I
).vars();
228 for( VarSet::const_iterator k
= Ivars
.begin(); k
!= Ivars
.end(); k
++ )
230 A_I
*= (_pancakes
[findVar(*k
)] * factor(I
).inverse()).partSum( Ivars
/ var(i
) );
231 if( Ivars
.size() > 1 )
232 A_I
^= (1.0 / (Ivars
.size() - 1));
233 Factor A_Ii
= (_pancakes
[i
] * factor(I
).inverse() * _phis
[i
][_I
].inverse()).partSum( Ivars
/ var(i
) );
234 Factor quot
= A_I
.divided_by(A_Ii
);
235 if( props
.damping
!= 0.0 )
236 quot
= (quot
^(1.0 - props
.damping
)) * (_phis
[i
][_I
]^props
.damping
);
238 piet
*= quot
.divided_by( _phis
[i
][_I
] ).normalized();
239 _phis
[i
][_I
] = quot
.normalized();
243 if( piet
.hasNaNs() ) {
244 cout
<< Name
<< "::NewPancake(" << i
<< ", " << _I
<< "): has NaNs!" << endl
;
253 if( props
.verbose
>= 1 )
254 cout
<< "Starting " << identify() << "...";
255 if( props
.verbose
>= 2 )
259 Diffs
diffs(nrVars(), 1.0);
261 double md
= InitCavityDists( props
.cavainame
, props
.cavaiopts
);
265 for( size_t i
= 0; i
< nrVars(); i
++ ) {
266 _pancakes
[i
] = _cavitydists
[i
];
268 foreach( const Neighbor
&I
, nbV(i
) ) {
269 _pancakes
[i
] *= factor(I
);
270 if( props
.updates
== Properties::UpdateType::SEQRND
)
271 _pancakes
[i
] *= _phis
[i
][I
.iter
];
274 _pancakes
[i
].normalize();
279 vector
<Factor
> old_beliefs
;
280 for(size_t i
=0; i
< nrVars(); i
++ )
281 old_beliefs
.push_back(belief(i
));
283 bool hasNaNs
= false;
284 for( size_t i
=0; i
< nrVars(); i
++ )
285 if( _pancakes
[i
].hasNaNs() ) {
290 cout
<< Name
<< "::run: initial _pancakes has NaNs!" << endl
;
294 size_t nredges
= nrEdges();
295 vector
<Edge
> update_seq
;
296 update_seq
.reserve( nredges
);
297 for( size_t i
= 0; i
< nrVars(); ++i
)
298 foreach( const Neighbor
&I
, nbV(i
) )
299 update_seq
.push_back( Edge( i
, I
.iter
) );
301 // do several passes over the network until maximum number of iterations has
302 // been reached or until the maximum belief difference is smaller than tolerance
303 for( _iters
=0; _iters
< props
.maxiter
&& diffs
.maxDiff() > props
.tol
; _iters
++ ) {
304 // Sequential updates
305 if( props
.updates
== Properties::UpdateType::SEQRND
)
306 random_shuffle( update_seq
.begin(), update_seq
.end() );
308 for( size_t t
=0; t
< nredges
; t
++ ) {
309 size_t i
= update_seq
[t
].first
;
310 size_t _I
= update_seq
[t
].second
;
311 _pancakes
[i
] = NewPancake( i
, _I
, hasNaNs
);
317 // compare new beliefs with old ones
318 for(size_t i
=0; i
< nrVars(); i
++ ) {
319 diffs
.push( dist( belief(i
), old_beliefs
[i
], Prob::DISTLINF
) );
320 old_beliefs
[i
] = belief(i
);
323 if( props
.verbose
>= 3 )
324 cout
<< Name
<< "::run: maxdiff " << diffs
.maxDiff() << " after " << _iters
+1 << " passes" << endl
;
327 if( diffs
.maxDiff() > _maxdiff
)
328 _maxdiff
= diffs
.maxDiff();
330 if( props
.verbose
>= 1 ) {
331 if( diffs
.maxDiff() > props
.tol
) {
332 if( props
.verbose
== 1 )
334 cout
<< Name
<< "::run: WARNING: not converged within " << props
.maxiter
<< " passes (" << toc() - tic
<< " seconds)...final maxdiff:" << diffs
.maxDiff() << endl
;
336 if( props
.verbose
>= 2 )
337 cout
<< Name
<< "::run: ";
338 cout
<< "converged in " << _iters
<< " passes (" << toc() - tic
<< " seconds)." << endl
;
342 return diffs
.maxDiff();
346 } // end of namespace dai