1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
4 This file is part of libDAI.
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.
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.
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
29 #include <boost/program_options.hpp>
31 #include <dai/alldai.h>
36 namespace po
= boost::program_options
;
55 TestDAI( const FactorGraph
&fg
, const string
&_name
, const PropertySet
&opts
) : obj(NULL
), name(_name
), err(), q(), logZ(0.0), maxdiff(0.0), time(0), iters(0U), has_logZ(false), has_maxdiff(false), has_iters(false) {
57 if( name
== "LDPC" ) {
58 double zero
[2] = {1.0, 0.0};
60 for( size_t i
= 0; i
< fg
.nrVars(); i
++ )
61 q
.push_back( Factor(Var(i
,2), zero
) );
69 obj
= newInfAlg( name
, fg
, opts
);
80 return obj
->identify();
85 vector
<Factor
> allBeliefs() {
86 vector
<Factor
> result
;
87 for( size_t i
= 0; i
< obj
->fg().nrVars(); i
++ )
88 result
.push_back( obj
->belief( obj
->fg().var(i
) ) );
101 } catch( Exception
&e
) {
105 maxdiff
= obj
->maxDiff();
107 } catch( Exception
&e
) {
116 void calcErrs( const TestDAI
&x
) {
118 err
.reserve( q
.size() );
119 for( size_t i
= 0; i
< q
.size(); i
++ )
120 err
.push_back( dist( q
[i
], x
.q
[i
], Prob::DISTTV
) );
123 void calcErrs( const vector
<Factor
> &x
) {
125 err
.reserve( q
.size() );
126 for( size_t i
= 0; i
< q
.size(); i
++ )
127 err
.push_back( dist( q
[i
], x
[i
], Prob::DISTTV
) );
131 return( *max_element( err
.begin(), err
.end() ) );
135 return( accumulate( err
.begin(), err
.end(), 0.0 ) / err
.size() );
140 pair
<string
, PropertySet
> parseMethod( const string
&_s
, const map
<string
,string
> & aliases
) {
141 // s = first part of _s, until '['
142 string::size_type pos
= _s
.find_first_of('[');
144 if( pos
== string::npos
)
147 s
= _s
.substr(0,pos
);
149 // if the first part is an alias, substitute
150 if( aliases
.find(s
) != aliases
.end() )
151 s
= aliases
.find(s
)->second
;
153 // attach second part, merging properties if necessary
154 if( pos
!= string::npos
) {
155 if( s
.at(s
.length()-1) == ']' ) {
156 s
= s
.erase(s
.length()-1,1) + ',' + _s
.substr(pos
+1);
158 s
= s
+ _s
.substr(pos
);
161 pair
<string
, PropertySet
> result
;
162 string
& name
= result
.first
;
163 PropertySet
& opts
= result
.second
;
165 pos
= s
.find_first_of('[');
166 if( pos
== string::npos
)
167 throw "Malformed method";
168 name
= s
.substr( 0, pos
);
170 for( ; strlen( DAINames
[n
] ) != 0; n
++ )
171 if( name
== DAINames
[n
] )
173 if( strlen( DAINames
[n
] ) == 0 && (name
!= "LDPC") )
174 DAI_THROW(UNKNOWN_DAI_ALGORITHM
);
177 ss
<< s
.substr(pos
,s
.length());
184 double clipdouble( double x
, double minabs
) {
185 if( fabs(x
) < minabs
)
192 int main( int argc
, char *argv
[] ) {
196 vector
<string
> methods
;
200 bool marginals
= false;
201 bool report_time
= true;
203 po::options_description
opts_required("Required options");
204 opts_required
.add_options()
205 ("filename", po::value
< string
>(&filename
), "Filename of FactorGraph")
206 ("methods", po::value
< vector
<string
> >(&methods
)->multitoken(), "DAI methods to test")
209 po::options_description
opts_optional("Allowed options");
210 opts_optional
.add_options()
211 ("help", "produce help message")
212 ("aliases", po::value
< string
>(&aliases
), "Filename for aliases")
213 ("tol", po::value
< double >(&tol
), "Override tolerance")
214 ("maxiter", po::value
< size_t >(&maxiter
), "Override maximum number of iterations")
215 ("verbose", po::value
< size_t >(&verbose
), "Override verbosity")
216 ("marginals", po::value
< bool >(&marginals
), "Output single node marginals?")
217 ("report-time", po::value
< bool >(&report_time
), "Report calculation time")
220 po::options_description cmdline_options
;
221 cmdline_options
.add(opts_required
).add(opts_optional
);
223 po::variables_map vm
;
224 po::store(po::parse_command_line(argc
, argv
, cmdline_options
), vm
);
227 if( vm
.count("help") || !(vm
.count("filename") && vm
.count("methods")) ) {
228 cout
<< "Reads factorgraph <filename.fg> and performs the approximate" << endl
;
229 cout
<< "inference algorithms <method*>, reporting calculation time, max and average" << endl
;
230 cout
<< "error and relative logZ error (comparing with the results of" << endl
;
231 cout
<< "<method0>, the base method, for which one can use JTREE_HUGIN)." << endl
<< endl
;
232 cout
<< opts_required
<< opts_optional
<< endl
;
237 map
<string
,string
> Aliases
;
238 if( !aliases
.empty() ) {
240 infile
.open (aliases
.c_str());
241 if (infile
.is_open()) {
244 getline(infile
,line
);
247 if( (!line
.empty()) && (line
[0] != '#') ) {
248 string::size_type pos
= line
.find(':',0);
249 if( pos
== string::npos
)
250 throw "Invalid alias";
252 string::size_type posl
= line
.substr(0, pos
).find_last_not_of(" \t");
253 string key
= line
.substr(0, posl
+ 1);
254 string::size_type posr
= line
.substr(pos
+ 1, line
.length()).find_first_not_of(" \t");
255 string val
= line
.substr(pos
+ 1 + posr
, line
.length());
262 throw "Error opening aliases file";
266 fg
.ReadFromFile( filename
.c_str() );
271 cout
<< "# " << filename
<< endl
;
273 cout
<< left
<< "# METHOD" << " ";
276 cout
<< right
<< "SECONDS" << " ";
279 cout
<< "MAX ERROR" << " ";
281 cout
<< "AVG ERROR" << " ";
283 cout
<< "LOGZ ERROR" << " ";
285 cout
<< "MAXDIFF" << " ";
287 cout
<< "ITERS" << endl
;
289 for( size_t m
= 0; m
< methods
.size(); m
++ ) {
290 pair
<string
, PropertySet
> meth
= parseMethod( methods
[m
], Aliases
);
292 if( vm
.count("tol") )
293 meth
.second
.Set("tol",tol
);
294 if( vm
.count("maxiter") )
295 meth
.second
.Set("maxiter",maxiter
);
296 if( vm
.count("verbose") )
297 meth
.second
.Set("verbose",verbose
);
298 TestDAI
piet(fg
, meth
.first
, meth
.second
);
307 // cout << left << piet.identify() << " ";
308 cout
<< left
<< methods
[m
] << " ";
311 cout
<< right
<< piet
.time
<< " ";
315 cout
.setf( ios_base::scientific
);
318 double me
= clipdouble( piet
.maxErr(), 1e-9 );
321 double ae
= clipdouble( piet
.avgErr(), 1e-9 );
324 if( piet
.has_logZ
) {
325 double le
= clipdouble( piet
.logZ
/ logZ0
- 1.0, 1e-9 );
331 if( piet
.has_maxdiff
) {
332 double md
= clipdouble( piet
.maxdiff
, 1e-9 );
342 if( piet
.has_iters
) {
343 cout
<< piet
.iters
<< " " << endl
;
351 for( size_t i
= 0; i
< piet
.q
.size(); i
++ )
352 cout
<< "# " << piet
.q
[i
] << endl
;
355 } catch(const char *e
) {
356 cerr
<< "Exception: " << e
<< endl
;
358 } catch(exception
& e
) {
359 cerr
<< "Exception: " << e
.what() << endl
;
363 cerr
<< "Exception of unknown type!" << endl
;