Various cleanups
[libdai.git] / src / exactinf.cpp
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
4
5 This file is part of libDAI.
6
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.
11
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.
16
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
20 */
21
22
23 #include <dai/exactinf.h>
24 #include <sstream>
25
26
27 namespace dai {
28
29
30 using namespace std;
31
32
33 const char *ExactInf::Name = "EXACT";
34
35
36 void ExactInf::setProperties( const PropertySet &opts ) {
37 assert( opts.hasKey("verbose") );
38
39 props.verbose = opts.getStringAs<size_t>("verbose");
40 }
41
42
43 PropertySet ExactInf::getProperties() const {
44 PropertySet opts;
45 opts.Set( "verbose", props.verbose );
46 return opts;
47 }
48
49
50 string ExactInf::printProperties() const {
51 stringstream s( stringstream::out );
52 s << "[";
53 s << "verbose=" << props.verbose << "]";
54 return s.str();
55 }
56
57
58 void ExactInf::construct() {
59 // clear variable beliefs and reserve space
60 _beliefsV.clear();
61 _beliefsV.reserve( nrVars() );
62 for( size_t i = 0; i < nrVars(); i++ )
63 _beliefsV.push_back( Factor( var(i) ) );
64
65 // clear factor beliefs and reserve space
66 _beliefsF.clear();
67 _beliefsF.reserve( nrFactors() );
68 for( size_t I = 0; I < nrFactors(); I++ )
69 _beliefsF.push_back( Factor( factor(I).vars() ) );
70 }
71
72
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 );
78 }
79
80
81 double ExactInf::run() {
82 if( props.verbose >= 1 )
83 cerr << "Starting " << identify() << "...";
84
85 Factor P;
86 for( size_t I = 0; I < nrFactors(); I++ )
87 P *= factor(I);
88
89 Real Z = P.sum();
90 _logZ = std::log(Z);
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());
95
96 if( props.verbose >= 1 )
97 cerr << "finished" << endl;
98
99 return 0.0;
100 }
101
102
103 vector<Factor> ExactInf::beliefs() const {
104 vector<Factor> result = _beliefsV;
105 result.insert( result.end(), _beliefsF.begin(), _beliefsF.end() );
106 return result;
107 }
108
109
110 Factor ExactInf::belief( const VarSet &ns ) const {
111 if( ns.size() == 0 )
112 return Factor();
113 else if( ns.size() == 1 ) {
114 return belief( *(ns.begin()) );
115 } else {
116 size_t I;
117 for( I = 0; I < nrFactors(); I++ )
118 if( factor(I).vars() >> ns )
119 break;
120 assert( I != nrFactors() );
121 return beliefF(I).marginal(ns);
122 }
123 }
124
125
126 string ExactInf::identify() const {
127 return string(Name) + printProperties();
128 }
129
130
131 } // end of namespace dai