| 
									
										
										
										
											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 { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ofstream of(s.c_str()); | 
					
						
							|  |  |  | 		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(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		char filename[100]; | 
					
						
							|  |  |  | 		sscanf(s.c_str(), "%s.dot", filename); | 
					
						
							|  |  |  | 		string cmd = boost::str(boost::format("neato -s -n -Tpdf %s -o %s.pdf") % s % filename); | 
					
						
							|  |  |  | 		system(cmd.c_str()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 13:39:13 +08:00
										 |  |  | 	/* ************************************************************************* */ | 
					
						
							|  |  |  | } |