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