Added ExactInf class for doing brute force exact inference...
[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 void ExactInf::create() {
50 // clear variable beliefs and reserve space
51 _beliefsV.clear();
52 _beliefsV.reserve( nrVars() );
53 for( size_t i = 0; i < nrVars(); i++ )
54 _beliefsV.push_back( Factor( var(i) ) );
55
56 // clear factor beliefs and reserve space
57 _beliefsF.clear();
58 _beliefsF.reserve( nrFactors() );
59 for( size_t I = 0; I < nrFactors(); I++ )
60 _beliefsF.push_back( Factor( factor(I).vars() ) );
61 }
62
63
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 );
69 }
70
71
72 double ExactInf::run() {
73 if( props.verbose >= 1 )
74 cout << "Starting " << identify() << "...";
75
76 Factor P;
77 for( size_t I = 0; I < nrFactors(); I++ )
78 P *= factor(I);
79
80 Real Z = P.totalSum();
81 _logZ = std::log(Z);
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());
86
87 if( props.verbose >= 1 )
88 cout << "finished" << endl;
89
90 return 0.0;
91 }
92
93
94 vector<Factor> ExactInf::beliefs() const {
95 vector<Factor> result = _beliefsV;
96 result.insert( result.end(), _beliefsF.begin(), _beliefsF.end() );
97 return result;
98 }
99
100
101 Factor ExactInf::belief( const VarSet &ns ) const {
102 if( ns.size() == 0 )
103 return Factor();
104 else if( ns.size() == 1 ) {
105 return belief( *(ns.begin()) );
106 } else {
107 size_t I;
108 for( I = 0; I < nrFactors(); I++ )
109 if( factor(I).vars() >> ns )
110 break;
111 assert( I != nrFactors() );
112 return beliefF(I).marginal(ns);
113 }
114 }
115
116
117 string ExactInf::identify() const {
118 stringstream result (stringstream::out);
119 result << Name << getProperties();
120 return result.str();
121 }
122
123
124 } // end of namespace dai