This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/CLPBN/clpbn/bp/HorusCli.cpp

172 lines
4.3 KiB
C++
Raw Normal View History

#include <cstdlib>
#include <iostream>
#include <sstream>
#include "FactorGraph.h"
2011-12-12 15:29:51 +00:00
#include "VarElimSolver.h"
2012-04-05 23:00:48 +01:00
#include "BpSolver.h"
2011-12-12 15:29:51 +00:00
#include "CbpSolver.h"
using namespace std;
2011-12-12 15:29:51 +00:00
void processArguments (FactorGraph&, int, const char* []);
2012-04-10 15:00:18 +01:00
void runSolver (const FactorGraph&, const VarIds&);
const string USAGE = "usage: \
2012-04-10 20:43:08 +01:00
./hcli ve|bp|cbp NETWORK_FILE [VARIABLE | OBSERVED_VARIABLE=EVIDENCE]..." ;
int
main (int argc, const char* argv[])
2011-12-12 15:29:51 +00:00
{
2012-04-10 20:43:08 +01:00
if (argc <= 1) {
cerr << "error: no solver specified" << endl;
cerr << "error: no graphical model specified" << endl;
cerr << USAGE << endl;
exit (0);
}
2012-04-10 20:43:08 +01:00
if (argc <= 2) {
cerr << "error: no graphical model specified" << endl;
cerr << USAGE << endl;
exit (0);
}
string solver (argv[1]);
if (solver == "ve") {
Globals::infAlgorithm = InfAlgorithms::VE;
} else if (solver == "bp") {
Globals::infAlgorithm = InfAlgorithms::BP;
} else if (solver == "cbp") {
Globals::infAlgorithm = InfAlgorithms::CBP;
} else {
cerr << "error: unknow solver `" << solver << "'" << endl ;
cerr << USAGE << endl;
exit(0);
}
string fileName (argv[2]);
2012-04-10 15:00:18 +01:00
string extension = fileName.substr (
fileName.find_last_of ('.') + 1);
FactorGraph fg;
if (extension == "uai") {
2012-04-10 20:43:08 +01:00
fg.readFromUaiFormat (fileName.c_str());
2011-12-12 15:29:51 +00:00
} else if (extension == "fg") {
2012-04-10 20:43:08 +01:00
fg.readFromLibDaiFormat (fileName.c_str());
} else {
cerr << "error: the graphical model must be defined either " ;
cerr << "in a UAI or libDAI file" << endl;
exit (0);
}
processArguments (fg, argc, argv);
return 0;
}
void
2011-12-12 15:29:51 +00:00
processArguments (FactorGraph& fg, int argc, const char* argv[])
{
2012-04-05 23:00:48 +01:00
VarIds queryIds;
2012-04-10 20:43:08 +01:00
for (int i = 3; i < argc; i++) {
const string& arg = argv[i];
if (arg.find ('=') == std::string::npos) {
if (!Util::isInteger (arg)) {
cerr << "error: `" << arg << "' " ;
cerr << "is not a valid variable id" ;
cerr << endl;
exit (0);
}
2011-12-12 15:29:51 +00:00
VarId vid;
stringstream ss;
ss << arg;
ss >> vid;
2012-04-05 23:00:48 +01:00
VarNode* queryVar = fg.getVarNode (vid);
if (queryVar) {
2012-04-05 23:00:48 +01:00
queryIds.push_back (vid);
} else {
cerr << "error: there isn't a variable with " ;
cerr << "`" << vid << "' as id" ;
cerr << endl;
exit (0);
}
} else {
size_t pos = arg.find ('=');
if (arg.substr (0, pos).empty()) {
cerr << "error: missing left argument" << endl;
cerr << USAGE << endl;
exit (0);
}
if (arg.substr (pos + 1).empty()) {
cerr << "error: missing right argument" << endl;
cerr << USAGE << endl;
exit (0);
}
if (!Util::isInteger (arg.substr (0, pos))) {
cerr << "error: `" << arg.substr (0, pos) << "' " ;
cerr << "is not a variable id" ;
cerr << endl;
exit (0);
}
2011-12-12 15:29:51 +00:00
VarId vid;
stringstream ss;
ss << arg.substr (0, pos);
ss >> vid;
2012-04-05 23:00:48 +01:00
VarNode* var = fg.getVarNode (vid);
if (var) {
if (!Util::isInteger (arg.substr (pos + 1))) {
cerr << "error: `" << arg.substr (pos + 1) << "' " ;
cerr << "is not a state index" ;
cerr << endl;
exit (0);
}
int stateIndex;
stringstream ss;
ss << arg.substr (pos + 1);
ss >> stateIndex;
2011-12-12 15:29:51 +00:00
if (var->isValidState (stateIndex)) {
var->setEvidence (stateIndex);
} else {
cerr << "error: `" << stateIndex << "' " ;
cerr << "is not a valid state index for variable " ;
2011-12-12 15:29:51 +00:00
cerr << "`" << var->varId() << "'" ;
cerr << endl;
exit (0);
}
} else {
cerr << "error: there isn't a variable with " ;
cerr << "`" << vid << "' as id" ;
cerr << endl;
exit (0);
}
}
}
2012-04-10 15:00:18 +01:00
runSolver (fg, queryIds);
}
void
runSolver (const FactorGraph& fg, const VarIds& queryIds)
{
2011-12-12 15:29:51 +00:00
Solver* solver = 0;
2012-03-31 23:27:37 +01:00
switch (Globals::infAlgorithm) {
2011-12-12 15:29:51 +00:00
case InfAlgorithms::VE:
solver = new VarElimSolver (fg);
break;
2012-04-05 23:00:48 +01:00
case InfAlgorithms::BP:
solver = new BpSolver (fg);
2011-12-12 15:29:51 +00:00
break;
case InfAlgorithms::CBP:
solver = new CbpSolver (fg);
break;
default:
assert (false);
}
2012-04-05 23:00:48 +01:00
if (queryIds.size() == 0) {
solver->printAllPosterioris();
} else {
2012-04-10 15:00:18 +01:00
solver->printAnswer (queryIds);
}
delete solver;
}