Replaced Name members by name() virtual functions (fixing a bug in matlab/dai.cpp)
[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-2010 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 void ExactInf::setProperties( const PropertySet &opts ) {
23 if( opts.hasKey("verbose") )
24 props.verbose = opts.getStringAs<size_t>("verbose");
25 else
26 props.verbose = 0;
27 }
28
29
30 PropertySet ExactInf::getProperties() const {
31 PropertySet opts;
32 opts.set( "verbose", props.verbose );
33 return opts;
34 }
35
36
37 string ExactInf::printProperties() const {
38 stringstream s( stringstream::out );
39 s << "[";
40 s << "verbose=" << props.verbose << "]";
41 return s.str();
42 }
43
44
45 void ExactInf::construct() {
46 // clear variable beliefs and reserve space
47 _beliefsV.clear();
48 _beliefsV.reserve( nrVars() );
49 for( size_t i = 0; i < nrVars(); i++ )
50 _beliefsV.push_back( Factor( var(i) ) );
51
52 // clear factor beliefs and reserve space
53 _beliefsF.clear();
54 _beliefsF.reserve( nrFactors() );
55 for( size_t I = 0; I < nrFactors(); I++ )
56 _beliefsF.push_back( Factor( factor(I).vars() ) );
57 }
58
59
60 void ExactInf::init() {
61 for( size_t i = 0; i < nrVars(); i++ )
62 _beliefsV[i].fill( 1.0 );
63 for( size_t I = 0; I < nrFactors(); I++ )
64 _beliefsF[I].fill( 1.0 );
65 }
66
67
68 Real ExactInf::run() {
69 if( props.verbose >= 1 )
70 cerr << "Starting " << identify() << "...";
71
72 Factor P;
73 for( size_t I = 0; I < nrFactors(); I++ )
74 P *= factor(I);
75
76 Real Z = P.sum();
77 _logZ = std::log(Z);
78 for( size_t i = 0; i < nrVars(); i++ )
79 _beliefsV[i] = P.marginal(var(i));
80 for( size_t I = 0; I < nrFactors(); I++ )
81 _beliefsF[I] = P.marginal(factor(I).vars());
82
83 if( props.verbose >= 1 )
84 cerr << "finished" << endl;
85
86 return 0.0;
87 }
88
89
90 Factor ExactInf::calcMarginal( const VarSet &vs ) const {
91 Factor P;
92 for( size_t I = 0; I < nrFactors(); I++ )
93 P *= factor(I);
94 return P.marginal( vs, true );
95 }
96
97
98 std::vector<std::size_t> ExactInf::findMaximum() const {
99 Factor P;
100 for( size_t I = 0; I < nrFactors(); I++ )
101 P *= factor(I);
102 size_t linearState = P.p().argmax().first;
103
104 // convert to state
105 map<Var, size_t> state = calcState( P.vars(), linearState );
106
107 // convert to desired output data structure
108 vector<size_t> mapState;
109 mapState.reserve( nrVars() );
110 for( size_t i = 0; i < nrVars(); i++ )
111 mapState.push_back( state[var(i)] );
112
113 return mapState;
114 }
115
116
117 vector<Factor> ExactInf::beliefs() const {
118 vector<Factor> result = _beliefsV;
119 result.insert( result.end(), _beliefsF.begin(), _beliefsF.end() );
120 return result;
121 }
122
123
124 Factor ExactInf::belief( const VarSet &ns ) const {
125 if( ns.size() == 0 )
126 return Factor();
127 else if( ns.size() == 1 ) {
128 return beliefV( findVar( *(ns.begin()) ) );
129 } else {
130 size_t I;
131 for( I = 0; I < nrFactors(); I++ )
132 if( factor(I).vars() >> ns )
133 break;
134 if( I == nrFactors() )
135 DAI_THROW(BELIEF_NOT_AVAILABLE);
136 return beliefF(I).marginal(ns);
137 }
138 }
139
140
141 } // end of namespace dai