1 /* This file is part of libDAI - http://www.libdai.org/
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.
7 * Copyright (C) 2006-2010 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
13 #include <dai/alldai.h>
14 #include <dai/properties.h>
15 #include <dai/exceptions.h>
24 InfAlg
*newInfAlg( const std::string
&name
, const FactorGraph
&fg
, const PropertySet
&opts
) {
25 if( name
== ExactInf::Name
)
26 return new ExactInf (fg
, opts
);
28 if( name
== BP::Name
)
29 return new BP (fg
, opts
);
32 if( name
== FBP::Name
)
33 return new FBP (fg
, opts
);
36 if( name
== TRWBP::Name
)
37 return new TRWBP (fg
, opts
);
40 if( name
== MF::Name
)
41 return new MF (fg
, opts
);
44 if( name
== HAK::Name
)
45 return new HAK (fg
, opts
);
48 if( name
== LC::Name
)
49 return new LC (fg
, opts
);
51 #ifdef DAI_WITH_TREEEP
52 if( name
== TreeEP::Name
)
53 return new TreeEP (fg
, opts
);
56 if( name
== JTree::Name
)
57 return new JTree (fg
, opts
);
60 if( name
== MR::Name
)
61 return new MR (fg
, opts
);
64 if( name
== Gibbs::Name
)
65 return new Gibbs (fg
, opts
);
68 if( name
== CBP::Name
)
69 return new CBP (fg
, opts
);
71 DAI_THROWE(UNKNOWN_DAI_ALGORITHM
,"Unknown libDAI algorithm: " + name
);
75 InfAlg
*newInfAlgFromString( const std::string
&nameOpts
, const FactorGraph
&fg
) {
76 pair
<string
,PropertySet
> no
= parseNameProperties( nameOpts
);
77 return newInfAlg( no
.first
, fg
, no
.second
);
81 InfAlg
*newInfAlgFromString( const std::string
&nameOpts
, const FactorGraph
&fg
, const std::map
<std::string
,std::string
> &aliases
) {
82 pair
<string
,PropertySet
> no
= parseNameProperties( nameOpts
, aliases
);
83 return newInfAlg( no
.first
, fg
, no
.second
);
87 std::pair
<std::string
, PropertySet
> parseNameProperties( const std::string
&s
) {
88 string::size_type pos
= s
.find_first_of('[');
91 if( pos
== string::npos
) {
94 name
= s
.substr(0,pos
);
97 ss
<< s
.substr(pos
,s
.length());
100 return make_pair(name
,opts
);
104 std::pair
<std::string
, PropertySet
> parseNameProperties( const std::string
&s
, const std::map
<std::string
,std::string
> &aliases
) {
105 // break string into method[properties]
106 pair
<string
,PropertySet
> ps
= parseNameProperties(s
);
109 // as long as 'method' is an alias, update:
110 while( aliases
.find(ps
.first
) != aliases
.end() && !looped
) {
111 string astr
= aliases
.find(ps
.first
)->second
;
112 pair
<string
,PropertySet
> aps
= parseNameProperties(astr
);
113 if( aps
.first
== ps
.first
)
115 // override aps properties by ps properties
116 aps
.second
.Set( ps
.second
);
119 // repeat until method name == alias name ('looped'), or
120 // there is no longer an alias 'method'
127 std::map
<std::string
,std::string
> readAliasesFile( const std::string
&filename
) {
129 map
<string
,string
> result
;
131 infile
.open( filename
.c_str() );
132 if( infile
.is_open() ) {
135 getline( infile
,line
);
138 if( (!line
.empty()) && (line
[0] != '#') ) {
139 string::size_type pos
= line
.find(':',0);
140 if( pos
== string::npos
)
141 DAI_THROWE(INVALID_ALIAS
,"Invalid alias '" + line
+ "'");
143 string::size_type posl
= line
.substr(0, pos
).find_last_not_of(" \t");
144 string key
= line
.substr(0, posl
+ 1);
145 string::size_type posr
= line
.substr(pos
+ 1, line
.length()).find_first_not_of(" \t");
146 string val
= line
.substr(pos
+ 1 + posr
, line
.length());
153 DAI_THROWE(CANNOT_READ_FILE
,"Error opening aliases file " + filename
);
158 } // end of namespace dai