1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
5 This file is part of libDAI.
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <boost/program_options.hpp>
32 #include <dai/alldai.h>
37 namespace po
= boost::program_options
;
56 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) {
58 if( name
== "LDPC" ) {
59 double zero
[2] = {1.0, 0.0};
61 for( size_t i
= 0; i
< fg
.nrVars(); i
++ )
62 q
.push_back( Factor(Var(i
,2), zero
) );
70 obj
= newInfAlg( name
, fg
, opts
);
79 string
identify() const {
81 return obj
->identify();
86 vector
<Factor
> allBeliefs() {
87 vector
<Factor
> result
;
88 for( size_t i
= 0; i
< obj
->fg().nrVars(); i
++ )
89 result
.push_back( obj
->belief( obj
->fg().var(i
) ) );
103 } catch( Exception
&e
) {
104 if( e
.code() == Exception::NOT_IMPLEMENTED
)
111 maxdiff
= obj
->maxDiff();
113 } catch( Exception
&e
) {
114 if( e
.code() == Exception::NOT_IMPLEMENTED
)
121 iters
= obj
->Iterations();
123 } catch( Exception
&e
) {
124 if( e
.code() == Exception::NOT_IMPLEMENTED
)
134 void calcErrs( const TestDAI
&x
) {
136 err
.reserve( q
.size() );
137 for( size_t i
= 0; i
< q
.size(); i
++ )
138 err
.push_back( dist( q
[i
], x
.q
[i
], Prob::DISTTV
) );
141 void calcErrs( const vector
<Factor
> &x
) {
143 err
.reserve( q
.size() );
144 for( size_t i
= 0; i
< q
.size(); i
++ )
145 err
.push_back( dist( q
[i
], x
[i
], Prob::DISTTV
) );
149 return( *max_element( err
.begin(), err
.end() ) );
153 return( accumulate( err
.begin(), err
.end(), 0.0 ) / err
.size() );
158 pair
<string
, PropertySet
> parseMethodRaw( const string
&s
) {
159 string::size_type pos
= s
.find_first_of('[');
162 if( pos
== string::npos
) {
165 name
= s
.substr(0,pos
);
168 ss
<< s
.substr(pos
,s
.length());
171 return make_pair(name
,opts
);
175 pair
<string
, PropertySet
> parseMethod( const string
&_s
, const map
<string
,string
> & aliases
) {
176 // break string into method[properties]
177 pair
<string
,PropertySet
> ps
= parseMethodRaw(_s
);
180 // as long as 'method' is an alias, update:
181 while( aliases
.find(ps
.first
) != aliases
.end() && !looped
) {
182 string astr
= aliases
.find(ps
.first
)->second
;
183 pair
<string
,PropertySet
> aps
= parseMethodRaw(astr
);
184 if( aps
.first
== ps
.first
)
186 // override aps properties by ps properties
187 aps
.second
.Set( ps
.second
);
190 // repeat until method name == alias name ('looped'), or
191 // there is no longer an alias 'method'
194 // check whether name is valid
196 for( ; strlen( DAINames
[n
] ) != 0; n
++ )
197 if( ps
.first
== DAINames
[n
] )
199 if( strlen( DAINames
[n
] ) == 0 && (ps
.first
!= "LDPC") )
200 DAI_THROWE(UNKNOWN_DAI_ALGORITHM
,string("Unknown DAI algorithm \"") + ps
.first
+ string("\" in \"") + _s
+ string("\""));
206 double clipdouble( double x
, double minabs
) {
207 if( fabs(x
) < minabs
)
214 int main( int argc
, char *argv
[] ) {
217 vector
<string
> methods
;
221 bool marginals
= false;
222 bool report_iters
= true;
223 bool report_time
= true;
225 po::options_description
opts_required("Required options");
226 opts_required
.add_options()
227 ("filename", po::value
< string
>(&filename
), "Filename of FactorGraph")
228 ("methods", po::value
< vector
<string
> >(&methods
)->multitoken(), "DAI methods to test")
231 po::options_description
opts_optional("Allowed options");
232 opts_optional
.add_options()
233 ("help", "produce help message")
234 ("aliases", po::value
< string
>(&aliases
), "Filename for aliases")
235 ("tol", po::value
< double >(&tol
), "Override tolerance")
236 ("maxiter", po::value
< size_t >(&maxiter
), "Override maximum number of iterations")
237 ("verbose", po::value
< size_t >(&verbose
), "Override verbosity")
238 ("marginals", po::value
< bool >(&marginals
), "Output single node marginals?")
239 ("report-time", po::value
< bool >(&report_time
), "Report calculation time")
240 ("report-iters", po::value
< bool >(&report_iters
), "Report iterations needed")
243 po::options_description cmdline_options
;
244 cmdline_options
.add(opts_required
).add(opts_optional
);
246 po::variables_map vm
;
247 po::store(po::parse_command_line(argc
, argv
, cmdline_options
), vm
);
250 if( vm
.count("help") || !(vm
.count("filename") && vm
.count("methods")) ) {
251 cout
<< "Reads factorgraph <filename.fg> and performs the approximate" << endl
;
252 cout
<< "inference algorithms <method*>, reporting calculation time, max and average" << endl
;
253 cout
<< "error and relative logZ error (comparing with the results of" << endl
;
254 cout
<< "<method0>, the base method, for which one can use JTREE_HUGIN)." << endl
<< endl
;
255 cout
<< opts_required
<< opts_optional
<< endl
;
257 cout
<< "This is a debugging (unoptimised) build of libDAI." << endl
;
264 map
<string
,string
> Aliases
;
265 if( !aliases
.empty() ) {
267 infile
.open (aliases
.c_str());
268 if (infile
.is_open()) {
271 getline(infile
,line
);
274 if( (!line
.empty()) && (line
[0] != '#') ) {
275 string::size_type pos
= line
.find(':',0);
276 if( pos
== string::npos
)
277 DAI_THROWE(RUNTIME_ERROR
,"Invalid alias");
279 string::size_type posl
= line
.substr(0, pos
).find_last_not_of(" \t");
280 string key
= line
.substr(0, posl
+ 1);
281 string::size_type posr
= line
.substr(pos
+ 1, line
.length()).find_first_not_of(" \t");
282 string val
= line
.substr(pos
+ 1 + posr
, line
.length());
289 DAI_THROWE(RUNTIME_ERROR
,"Error opening aliases file");
293 fg
.ReadFromFile( filename
.c_str() );
298 cout
.setf( ios_base::scientific
);
301 cout
<< "# " << filename
<< endl
;
303 cout
<< left
<< "# METHOD" << "\t";
305 cout
<< right
<< "SECONDS " << "\t";
307 cout
<< "ITERS" << "\t";
308 cout
<< "MAX ERROR" << "\t";
309 cout
<< "AVG ERROR" << "\t";
310 cout
<< "LOGZ ERROR" << "\t";
311 cout
<< "MAXDIFF" << "\t";
314 for( size_t m
= 0; m
< methods
.size(); m
++ ) {
315 pair
<string
, PropertySet
> meth
= parseMethod( methods
[m
], Aliases
);
317 if( vm
.count("tol") )
318 meth
.second
.Set("tol",tol
);
319 if( vm
.count("maxiter") )
320 meth
.second
.Set("maxiter",maxiter
);
321 if( vm
.count("verbose") )
322 meth
.second
.Set("verbose",verbose
);
323 TestDAI
piet(fg
, meth
.first
, meth
.second
);
332 cout
<< left
<< methods
[m
] << "\t";
334 cout
<< right
<< piet
.time
<< "\t";
336 if( piet
.has_iters
) {
337 cout
<< piet
.iters
<< "\t";
344 cout
.setf( ios_base::scientific
);
347 double me
= clipdouble( piet
.maxErr(), 1e-9 );
350 double ae
= clipdouble( piet
.avgErr(), 1e-9 );
353 if( piet
.has_logZ
) {
354 cout
.setf( ios::showpos
);
355 double le
= clipdouble( piet
.logZ
/ logZ0
- 1.0, 1e-9 );
357 cout
.unsetf( ios::showpos
);
361 if( piet
.has_maxdiff
) {
362 double md
= clipdouble( piet
.maxdiff
, 1e-9 );
374 for( size_t i
= 0; i
< piet
.q
.size(); i
++ )
375 cout
<< "# " << piet
.q
[i
] << endl
;
380 } catch( string
&s
) {
381 cerr
<< "Exception: " << s
<< endl
;