fix a bug in exporting to uai format

This commit is contained in:
Tiago Gomes 2012-05-28 17:00:46 +01:00
parent 3ee176c65b
commit 149f3dab26
2 changed files with 24 additions and 32 deletions

View File

@ -322,29 +322,24 @@ FactorGraph::exportToUaiFormat (const char* fileName) const
}
out << "MARKOV" << endl;
out << varNodes_.size() << endl;
for (size_t i = 0; i < varNodes_.size(); i++) {
out << varNodes_[i]->range() << " " ;
VarNodes sortedVns = varNodes_;
std::sort (sortedVns.begin(), sortedVns.end(), sortByVarId());
for (size_t i = 0; i < sortedVns.size(); i++) {
out << ((i != 0) ? " " : "") << sortedVns[i]->range();
}
out << endl << facNodes_.size() << endl;
for (size_t i = 0; i < facNodes_.size(); i++) {
VarIds args = facNodes_[i]->factor().arguments();
out << args.size() << " " << Util::elementsToString (args) << endl;
}
out << endl;
out << facNodes_.size() << endl;
for (size_t i = 0; i < facNodes_.size(); i++) {
const VarNodes& factorVars = facNodes_[i]->neighbors();
out << factorVars.size();
for (size_t j = 0; j < factorVars.size(); j++) {
out << " " << factorVars[j]->getIndex();
}
out << endl;
}
for (size_t i = 0; i < facNodes_.size(); i++) {
Params params = facNodes_[i]->factor().params();
if (Globals::logDomain) {
Util::exp (params);
}
out << endl << params.size() << endl << " " ;
for (size_t j = 0; j < params.size(); j++) {
out << params[j] << " " ;
}
out << endl;
out << params.size() << endl << " " ;
out << Util::elementsToString (params) << endl << endl;
}
out.close();
}
@ -363,14 +358,8 @@ FactorGraph::exportToLibDaiFormat (const char* fileName) const
for (size_t i = 0; i < facNodes_.size(); i++) {
Factor f (facNodes_[i]->factor());
out << f.nrArguments() << endl;
for (size_t j = 0; j < f.nrArguments(); j++) {
out << f.argument (j) << " " ;
}
out << endl;
for (size_t j = 0; j < f.nrArguments(); j++) {
out << f.range (j) << " " ;
}
out << endl;
out << Util::elementsToString (f.arguments()) << endl;
out << Util::elementsToString (f.ranges()) << endl;
VarIds args = f.arguments();
std::reverse (args.begin(), args.end());
f.reorderArguments (args);

View File

@ -32,6 +32,7 @@ class VarNode : public Var
};
class FacNode
{
public:
@ -60,14 +61,6 @@ class FacNode
};
struct CompVarId
{
bool operator() (const Var* v1, const Var* v2) const
{
return v1->varId() < v2->varId();
}
};
class FactorGraph
{
@ -141,5 +134,15 @@ class FactorGraph
VarMap varMap_;
};
struct sortByVarId
{
bool operator()(VarNode* vn1, VarNode* vn2) {
return vn1->varId() < vn2->varId();
}
};
#endif // HORUS_FACTORGRAPH_H