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
22 #include <dai/exactinf.h>
32 const char *ExactInf::Name
= "EXACT";
35 void ExactInf::setProperties( const PropertySet
&opts
) {
36 assert( opts
.hasKey("verbose") );
38 props
.verbose
= opts
.getStringAs
<size_t>("verbose");
42 PropertySet
ExactInf::getProperties() const {
44 opts
.Set( "verbose", props
.verbose
);
49 void ExactInf::create() {
50 // clear variable beliefs and reserve space
52 _beliefsV
.reserve( nrVars() );
53 for( size_t i
= 0; i
< nrVars(); i
++ )
54 _beliefsV
.push_back( Factor( var(i
) ) );
56 // clear factor beliefs and reserve space
58 _beliefsF
.reserve( nrFactors() );
59 for( size_t I
= 0; I
< nrFactors(); I
++ )
60 _beliefsF
.push_back( Factor( factor(I
).vars() ) );
64 void ExactInf::init() {
65 for( size_t i
= 0; i
< nrVars(); i
++ )
66 _beliefsV
[i
].fill( 1.0 );
67 for( size_t I
= 0; I
< nrFactors(); I
++ )
68 _beliefsF
[I
].fill( 1.0 );
72 double ExactInf::run() {
73 if( props
.verbose
>= 1 )
74 cout
<< "Starting " << identify() << "...";
77 for( size_t I
= 0; I
< nrFactors(); I
++ )
80 Real Z
= P
.totalSum();
82 for( size_t i
= 0; i
< nrVars(); i
++ )
83 _beliefsV
[i
] = P
.marginal(var(i
));
84 for( size_t I
= 0; I
< nrFactors(); I
++ )
85 _beliefsF
[I
] = P
.marginal(factor(I
).vars());
87 if( props
.verbose
>= 1 )
88 cout
<< "finished" << endl
;
94 vector
<Factor
> ExactInf::beliefs() const {
95 vector
<Factor
> result
= _beliefsV
;
96 result
.insert( result
.end(), _beliefsF
.begin(), _beliefsF
.end() );
101 Factor
ExactInf::belief( const VarSet
&ns
) const {
104 else if( ns
.size() == 1 ) {
105 return belief( *(ns
.begin()) );
108 for( I
= 0; I
< nrFactors(); I
++ )
109 if( factor(I
).vars() >> ns
)
111 assert( I
!= nrFactors() );
112 return beliefF(I
).marginal(ns
);
117 string
ExactInf::identify() const {
118 stringstream
result (stringstream::out
);
119 result
<< Name
<< getProperties();
124 } // end of namespace dai