4c66019b33457064ad9559d3c6d07c17494917df
[libdai.git] / src / exactinf.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 #include <dai/exactinf.h>
10 #include <sstream>
11
12
13 namespace dai {
14
15
16 using namespace std;
17
18
19 void ExactInf::setProperties( const PropertySet &opts ) {
20 if( opts.hasKey("verbose") )
21 props.verbose = opts.getStringAs<size_t>("verbose");
22 else
23 props.verbose = 0;
24 }
25
26
27 PropertySet ExactInf::getProperties() const {
28 PropertySet opts;
29 opts.set( "verbose", props.verbose );
30 return opts;
31 }
32
33
34 string ExactInf::printProperties() const {
35 stringstream s( stringstream::out );
36 s << "[";
37 s << "verbose=" << props.verbose << "]";
38 return s.str();
39 }
40
41
42 void ExactInf::construct() {
43 // clear variable beliefs and reserve space
44 _beliefsV.clear();
45 _beliefsV.reserve( nrVars() );
46 for( size_t i = 0; i < nrVars(); i++ )
47 _beliefsV.push_back( Factor( var(i) ) );
48
49 // clear factor beliefs and reserve space
50 _beliefsF.clear();
51 _beliefsF.reserve( nrFactors() );
52 for( size_t I = 0; I < nrFactors(); I++ )
53 _beliefsF.push_back( Factor( factor(I).vars() ) );
54 }
55
56
57 void ExactInf::init() {
58 for( size_t i = 0; i < nrVars(); i++ )
59 _beliefsV[i].fill( 1.0 );
60 for( size_t I = 0; I < nrFactors(); I++ )
61 _beliefsF[I].fill( 1.0 );
62 }
63
64
65 Real ExactInf::run() {
66 if( props.verbose >= 1 )
67 cerr << "Starting " << identify() << "...";
68
69 Factor P;
70 for( size_t I = 0; I < nrFactors(); I++ )
71 P *= factor(I);
72
73 Real Z = P.sum();
74 _logZ = std::log(Z);
75 for( size_t i = 0; i < nrVars(); i++ )
76 _beliefsV[i] = P.marginal(var(i));
77 for( size_t I = 0; I < nrFactors(); I++ )
78 _beliefsF[I] = P.marginal(factor(I).vars());
79
80 if( props.verbose >= 1 )
81 cerr << "finished" << endl;
82
83 return 0.0;
84 }
85
86
87 Factor ExactInf::calcMarginal( const VarSet &vs ) const {
88 Factor P;
89 for( size_t I = 0; I < nrFactors(); I++ )
90 P *= factor(I);
91 return P.marginal( vs, true );
92 }
93
94
95 std::vector<std::size_t> ExactInf::findMaximum() const {
96 Factor P;
97 for( size_t I = 0; I < nrFactors(); I++ )
98 P *= factor(I);
99 size_t linearState = P.p().argmax().first;
100
101 // convert to state
102 map<Var, size_t> state = calcState( P.vars(), linearState );
103
104 // convert to desired output data structure
105 vector<size_t> mapState;
106 mapState.reserve( nrVars() );
107 for( size_t i = 0; i < nrVars(); i++ )
108 mapState.push_back( state[var(i)] );
109
110 return mapState;
111 }
112
113
114 vector<Factor> ExactInf::beliefs() const {
115 vector<Factor> result = _beliefsV;
116 result.insert( result.end(), _beliefsF.begin(), _beliefsF.end() );
117 return result;
118 }
119
120
121 Factor ExactInf::belief( const VarSet &ns ) const {
122 if( ns.size() == 0 )
123 return Factor();
124 else if( ns.size() == 1 ) {
125 return beliefV( findVar( *(ns.begin()) ) );
126 } else {
127 size_t I;
128 for( I = 0; I < nrFactors(); I++ )
129 if( factor(I).vars() >> ns )
130 break;
131 if( I == nrFactors() )
132 DAI_THROW(BELIEF_NOT_AVAILABLE);
133 return beliefF(I).marginal(ns);
134 }
135 }
136
137
138 } // end of namespace dai