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 string
ExactInf::printProperties() const {
50 stringstream
s( stringstream::out
);
52 s
<< "verbose=" << props
.verbose
<< "]";
57 void ExactInf::create() {
58 // clear variable beliefs and reserve space
60 _beliefsV
.reserve( nrVars() );
61 for( size_t i
= 0; i
< nrVars(); i
++ )
62 _beliefsV
.push_back( Factor( var(i
) ) );
64 // clear factor beliefs and reserve space
66 _beliefsF
.reserve( nrFactors() );
67 for( size_t I
= 0; I
< nrFactors(); I
++ )
68 _beliefsF
.push_back( Factor( factor(I
).vars() ) );
72 void ExactInf::init() {
73 for( size_t i
= 0; i
< nrVars(); i
++ )
74 _beliefsV
[i
].fill( 1.0 );
75 for( size_t I
= 0; I
< nrFactors(); I
++ )
76 _beliefsF
[I
].fill( 1.0 );
80 double ExactInf::run() {
81 if( props
.verbose
>= 1 )
82 cout
<< "Starting " << identify() << "...";
85 for( size_t I
= 0; I
< nrFactors(); I
++ )
88 Real Z
= P
.totalSum();
90 for( size_t i
= 0; i
< nrVars(); i
++ )
91 _beliefsV
[i
] = P
.marginal(var(i
));
92 for( size_t I
= 0; I
< nrFactors(); I
++ )
93 _beliefsF
[I
] = P
.marginal(factor(I
).vars());
95 if( props
.verbose
>= 1 )
96 cout
<< "finished" << endl
;
102 vector
<Factor
> ExactInf::beliefs() const {
103 vector
<Factor
> result
= _beliefsV
;
104 result
.insert( result
.end(), _beliefsF
.begin(), _beliefsF
.end() );
109 Factor
ExactInf::belief( const VarSet
&ns
) const {
112 else if( ns
.size() == 1 ) {
113 return belief( *(ns
.begin()) );
116 for( I
= 0; I
< nrFactors(); I
++ )
117 if( factor(I
).vars() >> ns
)
119 assert( I
!= nrFactors() );
120 return beliefF(I
).marginal(ns
);
125 string
ExactInf::identify() const {
126 return string(Name
) + printProperties();
130 } // end of namespace dai