From 149f3dab26db393bf867d06e4c100799cf67d5ad Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Mon, 28 May 2012 17:00:46 +0100 Subject: [PATCH] fix a bug in exporting to uai format --- packages/CLPBN/horus/FactorGraph.cpp | 37 ++++++++++------------------ packages/CLPBN/horus/FactorGraph.h | 19 ++++++++------ 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/packages/CLPBN/horus/FactorGraph.cpp b/packages/CLPBN/horus/FactorGraph.cpp index 78e146068..2987410b6 100644 --- a/packages/CLPBN/horus/FactorGraph.cpp +++ b/packages/CLPBN/horus/FactorGraph.cpp @@ -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); diff --git a/packages/CLPBN/horus/FactorGraph.h b/packages/CLPBN/horus/FactorGraph.h index b0abfd147..5346221b1 100644 --- a/packages/CLPBN/horus/FactorGraph.h +++ b/packages/CLPBN/horus/FactorGraph.h @@ -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