Fixed regression in scripts/regenerate-properties
[libdai.git] / src / exactinf.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 #include <dai/exactinf.h>
13 #include <sstream>
14
15
16 namespace dai {
17
18
19 using namespace std;
20
21
22 const char *ExactInf::Name = "EXACT";
23
24
25 void ExactInf::setProperties( const PropertySet &opts ) {
26 DAI_ASSERT( opts.hasKey("verbose") );
27
28 props.verbose = opts.getStringAs<size_t>("verbose");
29 }
30
31
32 PropertySet ExactInf::getProperties() const {
33 PropertySet opts;
34 opts.Set( "verbose", props.verbose );
35 return opts;
36 }
37
38
39 string ExactInf::printProperties() const {
40 stringstream s( stringstream::out );
41 s << "[";
42 s << "verbose=" << props.verbose << "]";
43 return s.str();
44 }
45
46
47 void ExactInf::construct() {
48 // clear variable beliefs and reserve space
49 _beliefsV.clear();
50 _beliefsV.reserve( nrVars() );
51 for( size_t i = 0; i < nrVars(); i++ )
52 _beliefsV.push_back( Factor( var(i) ) );
53
54 // clear factor beliefs and reserve space
55 _beliefsF.clear();
56 _beliefsF.reserve( nrFactors() );
57 for( size_t I = 0; I < nrFactors(); I++ )
58 _beliefsF.push_back( Factor( factor(I).vars() ) );
59 }
60
61
62 void ExactInf::init() {
63 for( size_t i = 0; i < nrVars(); i++ )
64 _beliefsV[i].fill( 1.0 );
65 for( size_t I = 0; I < nrFactors(); I++ )
66 _beliefsF[I].fill( 1.0 );
67 }
68
69
70 Real ExactInf::run() {
71 if( props.verbose >= 1 )
72 cerr << "Starting " << identify() << "...";
73
74 Factor P;
75 for( size_t I = 0; I < nrFactors(); I++ )
76 P *= factor(I);
77
78 Real Z = P.sum();
79 _logZ = std::log(Z);
80 for( size_t i = 0; i < nrVars(); i++ )
81 _beliefsV[i] = P.marginal(var(i));
82 for( size_t I = 0; I < nrFactors(); I++ )
83 _beliefsF[I] = P.marginal(factor(I).vars());
84
85 if( props.verbose >= 1 )
86 cerr << "finished" << endl;
87
88 return 0.0;
89 }
90
91
92 Factor ExactInf::calcMarginal( const VarSet &vs ) const {
93 Factor P;
94 for( size_t I = 0; I < nrFactors(); I++ )
95 P *= factor(I);
96 return P.marginal( vs, true );
97 }
98
99
100 vector<Factor> ExactInf::beliefs() const {
101 vector<Factor> result = _beliefsV;
102 result.insert( result.end(), _beliefsF.begin(), _beliefsF.end() );
103 return result;
104 }
105
106
107 Factor ExactInf::belief( const VarSet &ns ) const {
108 if( ns.size() == 0 )
109 return Factor();
110 else if( ns.size() == 1 ) {
111 return belief( *(ns.begin()) );
112 } else {
113 size_t I;
114 for( I = 0; I < nrFactors(); I++ )
115 if( factor(I).vars() >> ns )
116 break;
117 DAI_ASSERT( I != nrFactors() );
118 return beliefF(I).marginal(ns);
119 }
120 }
121
122
123 string ExactInf::identify() const {
124 return string(Name) + printProperties();
125 }
126
127
128 } // end of namespace dai