2009-10-29 13:39:13 +08:00
|
|
|
/*
|
|
|
|
* SymbolicFactorGraph.cpp
|
|
|
|
*
|
|
|
|
* Created on: Oct 29, 2009
|
|
|
|
* Author: Frank Dellaert
|
|
|
|
*/
|
|
|
|
|
2010-02-13 15:09:56 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <boost/format.hpp>
|
2009-10-29 13:39:13 +08:00
|
|
|
#include <boost/foreach.hpp>
|
2010-02-13 15:09:56 +08:00
|
|
|
#include "Point2.h"
|
2009-10-30 11:48:32 +08:00
|
|
|
#include "Ordering.h"
|
2009-10-29 13:39:13 +08:00
|
|
|
#include "SymbolicFactorGraph.h"
|
2009-11-01 03:53:20 +08:00
|
|
|
#include "SymbolicBayesNet.h"
|
2009-11-12 12:56:30 +08:00
|
|
|
#include "inference-inl.h"
|
2010-02-13 15:09:56 +08:00
|
|
|
#include "LieConfig-inl.h"
|
2009-10-29 13:39:13 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace gtsam {
|
|
|
|
|
2010-02-17 07:20:39 +08:00
|
|
|
INSTANTIATE_LIE_CONFIG(Symbol, Point2)
|
|
|
|
|
2009-10-30 13:45:22 +08:00
|
|
|
// Explicitly instantiate so we don't have to include everywhere
|
|
|
|
template class FactorGraph<SymbolicFactor>;
|
|
|
|
|
2009-11-12 12:56:30 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
boost::shared_ptr<SymbolicConditional>
|
2010-01-18 03:34:57 +08:00
|
|
|
SymbolicFactorGraph::eliminateOne(const Symbol& key){
|
2009-11-12 12:56:30 +08:00
|
|
|
return gtsam::eliminateOne<SymbolicFactor,SymbolicConditional>(*this, key);
|
|
|
|
}
|
|
|
|
|
2009-10-29 13:39:13 +08:00
|
|
|
/* ************************************************************************* */
|
2009-11-09 15:04:26 +08:00
|
|
|
SymbolicBayesNet
|
2009-10-30 11:48:32 +08:00
|
|
|
SymbolicFactorGraph::eliminate(const Ordering& ordering)
|
2009-10-29 22:34:34 +08:00
|
|
|
{
|
2009-11-09 15:04:26 +08:00
|
|
|
SymbolicBayesNet bayesNet;
|
2009-10-29 22:34:34 +08:00
|
|
|
|
2010-01-18 03:34:57 +08:00
|
|
|
BOOST_FOREACH(const Symbol& key, ordering) {
|
2009-11-08 03:31:39 +08:00
|
|
|
SymbolicConditional::shared_ptr conditional =
|
2009-11-12 12:56:30 +08:00
|
|
|
gtsam::eliminateOne<SymbolicFactor,SymbolicConditional>(*this,key);
|
2009-11-09 15:04:26 +08:00
|
|
|
bayesNet.push_back(conditional);
|
2009-10-30 11:48:32 +08:00
|
|
|
}
|
2009-11-02 11:50:30 +08:00
|
|
|
return bayesNet;
|
2009-10-29 22:34:34 +08:00
|
|
|
}
|
|
|
|
|
2010-02-13 15:09:56 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
void saveGraph(const SymbolicFactorGraph& fg, const SymbolicConfig& config, const std::string& s) {
|
|
|
|
|
|
|
|
Symbol key;
|
|
|
|
Point2 pt;
|
|
|
|
float scale = 100;
|
|
|
|
|
2010-02-19 10:48:47 +08:00
|
|
|
string dotfile = s + ".dot";
|
|
|
|
ofstream of(dotfile.c_str());
|
2010-02-13 15:09:56 +08:00
|
|
|
of << "graph G{" << endl;
|
|
|
|
of << "bgcolor=\"transparent\";" << endl;
|
|
|
|
|
|
|
|
BOOST_FOREACH(boost::tie(key, pt), config){
|
|
|
|
of << (string)key << "[label=\"" << (string)key << "\"][pos=\"" << pt.x()*scale << "," << pt.y()*scale << "\"];" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
BOOST_FOREACH(const SymbolicFactorGraph::sharedFactor& factor, fg) {
|
|
|
|
index++;
|
|
|
|
Point2 center;
|
|
|
|
BOOST_FOREACH(const Symbol& key, factor->keys())
|
|
|
|
center = center + config[key];
|
|
|
|
center = Point2(center.x() / factor->keys().size(), center.y() / factor->keys().size());
|
|
|
|
of << "f" << index << "[pos=\"" << center.x()*scale << "," << center.y()*scale << "\"][shape=\"point\"];" << endl;
|
|
|
|
BOOST_FOREACH(const Symbol& key, factor->keys())
|
|
|
|
of << "f" << index << "--" << (string)key << endl;
|
|
|
|
}
|
|
|
|
of<<"}";
|
|
|
|
of.close();
|
|
|
|
|
2010-02-19 10:48:47 +08:00
|
|
|
string cmd = boost::str(boost::format("neato -s -n -Tpdf %s -o %s.pdf") % dotfile % s);
|
2010-02-13 15:09:56 +08:00
|
|
|
system(cmd.c_str());
|
|
|
|
}
|
|
|
|
|
2009-10-29 13:39:13 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
}
|