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

View File

@ -32,6 +32,7 @@ class VarNode : public Var
}; };
class FacNode class FacNode
{ {
public: 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 class FactorGraph
{ {
@ -141,5 +134,15 @@ class FactorGraph
VarMap varMap_; VarMap varMap_;
}; };
struct sortByVarId
{
bool operator()(VarNode* vn1, VarNode* vn2) {
return vn1->varId() < vn2->varId();
}
};
#endif // HORUS_FACTORGRAPH_H #endif // HORUS_FACTORGRAPH_H