1 /* This file is part of libDAI - http://www.libdai.org/
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
11 #include <dai/alldai.h>
12 #include <dai/properties.h>
13 #include <dai/exceptions.h>
22 class _builtinInfAlgs
: public std::map
<std::string
, InfAlg
*> {
25 operator[]( ExactInf().name() ) = new ExactInf
;
27 operator[]( BP().name() ) = new BP
;
30 operator[]( FBP().name() ) = new FBP
;
33 operator[]( TRWBP().name() ) = new TRWBP
;
36 operator[]( MF().name() ) = new MF
;
39 operator[]( HAK().name() ) = new HAK
;
42 operator[]( LC().name() ) = new LC
;
44 #ifdef DAI_WITH_TREEEP
45 operator[]( TreeEP().name() ) = new TreeEP
;
48 operator[]( JTree().name() ) = new JTree
;
51 operator[]( MR().name() ) = new MR
;
54 operator[]( Gibbs().name() ) = new Gibbs
;
57 operator[]( CBP().name() ) = new CBP
;
59 #ifdef DAI_WITH_DECMAP
60 operator[]( DecMAP().name() ) = new DecMAP
;
63 operator[]( GLC().name() ) = new GLC
;
69 for( iterator it
= begin(); it
!= end(); it
++ )
75 static _builtinInfAlgs allBuiltinInfAlgs
;
78 std::map
<std::string
, InfAlg
*>& builtinInfAlgs() {
79 return allBuiltinInfAlgs
;
83 std::set
<std::string
> builtinInfAlgNames() {
84 std::set
<std::string
> algNames
;
85 for( _builtinInfAlgs::const_iterator it
= allBuiltinInfAlgs
.begin(); it
!= allBuiltinInfAlgs
.end(); it
++ )
86 algNames
.insert( it
->first
);
91 InfAlg
*newInfAlg( const std::string
&name
, const FactorGraph
&fg
, const PropertySet
&opts
) {
92 _builtinInfAlgs::const_iterator i
= allBuiltinInfAlgs
.find( name
);
93 if( i
== allBuiltinInfAlgs
.end() )
94 DAI_THROWE(UNKNOWN_DAI_ALGORITHM
, "Unknown inference algorithm: " + name
);
95 InfAlg
*ia
= i
->second
;
96 return ia
->construct( fg
, opts
);
100 InfAlg
*newInfAlgFromString( const std::string
&nameOpts
, const FactorGraph
&fg
) {
101 pair
<string
,PropertySet
> no
= parseNameProperties( nameOpts
);
102 return newInfAlg( no
.first
, fg
, no
.second
);
106 InfAlg
*newInfAlgFromString( const std::string
&nameOpts
, const FactorGraph
&fg
, const std::map
<std::string
,std::string
> &aliases
) {
107 pair
<string
,PropertySet
> no
= parseNameProperties( nameOpts
, aliases
);
108 return newInfAlg( no
.first
, fg
, no
.second
);
112 std::pair
<std::string
, PropertySet
> parseNameProperties( const std::string
&s
) {
113 string::size_type pos
= s
.find_first_of('[');
116 if( pos
== string::npos
) {
119 name
= s
.substr(0,pos
);
122 ss
<< s
.substr(pos
,s
.length());
125 return make_pair(name
,opts
);
129 std::pair
<std::string
, PropertySet
> parseNameProperties( const std::string
&s
, const std::map
<std::string
,std::string
> &aliases
) {
130 // break string into method[properties]
131 pair
<string
,PropertySet
> ps
= parseNameProperties(s
);
134 // as long as 'method' is an alias, update:
135 while( aliases
.find(ps
.first
) != aliases
.end() && !looped
) {
136 string astr
= aliases
.find(ps
.first
)->second
;
137 pair
<string
,PropertySet
> aps
= parseNameProperties(astr
);
138 if( aps
.first
== ps
.first
)
140 // override aps properties by ps properties
141 aps
.second
.set( ps
.second
);
144 // repeat until method name == alias name ('looped'), or
145 // there is no longer an alias 'method'
152 std::map
<std::string
,std::string
> readAliasesFile( const std::string
&filename
) {
154 map
<string
,string
> result
;
156 infile
.open( filename
.c_str() );
157 if( infile
.is_open() ) {
160 getline( infile
,line
);
163 if( (!line
.empty()) && (line
[0] != '#') ) {
164 string::size_type pos
= line
.find(':',0);
165 if( pos
== string::npos
)
166 DAI_THROWE(INVALID_ALIAS
,"Invalid alias '" + line
+ "'");
168 string::size_type posl
= line
.substr(0, pos
).find_last_not_of(" \t");
169 string key
= line
.substr(0, posl
+ 1);
170 string::size_type posr
= line
.substr(pos
+ 1, line
.length()).find_first_not_of(" \t");
171 string val
= line
.substr(pos
+ 1 + posr
, line
.length());
178 DAI_THROWE(CANNOT_READ_FILE
,"Error opening aliases file " + filename
);
183 } // end of namespace dai