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