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
23 #include <dai/exactinf.h>
33 const char *ExactInf::Name
= "EXACT";
36 void ExactInf::setProperties( const PropertySet
&opts
) {
37 assert( opts
.hasKey("verbose") );
39 props
.verbose
= opts
.getStringAs
<size_t>("verbose");
43 PropertySet
ExactInf::getProperties() const {
45 opts
.Set( "verbose", props
.verbose
);
50 string
ExactInf::printProperties() const {
51 stringstream
s( stringstream::out
);
53 s
<< "verbose=" << props
.verbose
<< "]";
58 void ExactInf::construct() {
59 // clear variable beliefs and reserve space
61 _beliefsV
.reserve( nrVars() );
62 for( size_t i
= 0; i
< nrVars(); i
++ )
63 _beliefsV
.push_back( Factor( var(i
) ) );
65 // clear factor beliefs and reserve space
67 _beliefsF
.reserve( nrFactors() );
68 for( size_t I
= 0; I
< nrFactors(); I
++ )
69 _beliefsF
.push_back( Factor( factor(I
).vars() ) );
73 void ExactInf::init() {
74 for( size_t i
= 0; i
< nrVars(); i
++ )
75 _beliefsV
[i
].fill( 1.0 );
76 for( size_t I
= 0; I
< nrFactors(); I
++ )
77 _beliefsF
[I
].fill( 1.0 );
81 double ExactInf::run() {
82 if( props
.verbose
>= 1 )
83 cerr
<< "Starting " << identify() << "...";
86 for( size_t I
= 0; I
< nrFactors(); I
++ )
91 for( size_t i
= 0; i
< nrVars(); i
++ )
92 _beliefsV
[i
] = P
.marginal(var(i
));
93 for( size_t I
= 0; I
< nrFactors(); I
++ )
94 _beliefsF
[I
] = P
.marginal(factor(I
).vars());
96 if( props
.verbose
>= 1 )
97 cerr
<< "finished" << endl
;
103 vector
<Factor
> ExactInf::beliefs() const {
104 vector
<Factor
> result
= _beliefsV
;
105 result
.insert( result
.end(), _beliefsF
.begin(), _beliefsF
.end() );
110 Factor
ExactInf::belief( const VarSet
&ns
) const {
113 else if( ns
.size() == 1 ) {
114 return belief( *(ns
.begin()) );
117 for( I
= 0; I
< nrFactors(); I
++ )
118 if( factor(I
).vars() >> ns
)
120 assert( I
!= nrFactors() );
121 return beliefF(I
).marginal(ns
);
126 string
ExactInf::identify() const {
127 return string(Name
) + printProperties();
131 } // end of namespace dai