From 67cb0806fe9bb69840e64509d2af34e58b4a7fb1 Mon Sep 17 00:00:00 2001 From: Alex Cunningham Date: Thu, 17 May 2012 05:26:01 +0000 Subject: [PATCH] Removed old code from current release branch --- gtsam/unstable/base/RefCounted.cpp | 20 -- gtsam/unstable/base/RefCounted.h | 97 --------- gtsam/unstable/discrete/PotentialTable.cpp | 173 --------------- gtsam/unstable/discrete/PotentialTable.h | 106 --------- .../unstable/discrete/TypedDiscreteFactor.cpp | 128 ----------- gtsam/unstable/discrete/TypedDiscreteFactor.h | 79 ------- .../discrete/TypedDiscreteFactorGraph.cpp | 78 ------- .../discrete/TypedDiscreteFactorGraph.h | 61 ------ gtsam/unstable/discrete/parseUAI.cpp | 168 --------------- gtsam/unstable/discrete/parseUAI.h | 33 --- .../discrete/tests/testPotentialTable.cpp | 164 -------------- .../tests/testTypedDiscreteFactor.cpp | 64 ------ .../tests/testTypedDiscreteFactorGraph.cpp | 204 ------------------ .../tests/testTypedDiscreteVariable.cpp | 80 ------- 14 files changed, 1455 deletions(-) delete mode 100644 gtsam/unstable/base/RefCounted.cpp delete mode 100644 gtsam/unstable/base/RefCounted.h delete mode 100644 gtsam/unstable/discrete/PotentialTable.cpp delete mode 100644 gtsam/unstable/discrete/PotentialTable.h delete mode 100644 gtsam/unstable/discrete/TypedDiscreteFactor.cpp delete mode 100644 gtsam/unstable/discrete/TypedDiscreteFactor.h delete mode 100644 gtsam/unstable/discrete/TypedDiscreteFactorGraph.cpp delete mode 100644 gtsam/unstable/discrete/TypedDiscreteFactorGraph.h delete mode 100644 gtsam/unstable/discrete/parseUAI.cpp delete mode 100644 gtsam/unstable/discrete/parseUAI.h delete mode 100644 gtsam/unstable/discrete/tests/testPotentialTable.cpp delete mode 100644 gtsam/unstable/discrete/tests/testTypedDiscreteFactor.cpp delete mode 100644 gtsam/unstable/discrete/tests/testTypedDiscreteFactorGraph.cpp delete mode 100644 gtsam/unstable/discrete/tests/testTypedDiscreteVariable.cpp diff --git a/gtsam/unstable/base/RefCounted.cpp b/gtsam/unstable/base/RefCounted.cpp deleted file mode 100644 index ff3981149..000000000 --- a/gtsam/unstable/base/RefCounted.cpp +++ /dev/null @@ -1,20 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * @file RefCounted.cpp - * @brief Simple reference-counted base class - * @author Frank Dellaert - * @date Mar 29, 2011 - */ - -#include - diff --git a/gtsam/unstable/base/RefCounted.h b/gtsam/unstable/base/RefCounted.h deleted file mode 100644 index ffa475935..000000000 --- a/gtsam/unstable/base/RefCounted.h +++ /dev/null @@ -1,97 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * @file RefCounted.h - * @brief Simple reference-counted base class - * @author Frank Dellaert - * @date Mar 29, 2011 - */ - -#include - -// Forward Declarations -namespace gtsam { - struct RefCounted; -} - -namespace boost { - void intrusive_ptr_add_ref(const gtsam::RefCounted * p); - void intrusive_ptr_release(const gtsam::RefCounted * p); -} - -namespace gtsam { - - /** - * Simple reference counted class inspired by - * http://www.codeproject.com/KB/stl/boostsmartptr.aspx - */ - struct RefCounted { - private: - mutable long references_; - friend void ::boost::intrusive_ptr_add_ref(const RefCounted * p); - friend void ::boost::intrusive_ptr_release(const RefCounted * p); - public: - RefCounted() : - references_(0) { - } - virtual ~RefCounted() { - } - }; - -} // namespace gtsam - -// Intrusive Pointer free functions -#ifndef DEBUG_REFCOUNT - -namespace boost { - - // increment reference count of object *p - inline void intrusive_ptr_add_ref(const gtsam::RefCounted * p) { - ++(p->references_); - } - - // decrement reference count, and delete object when reference count reaches 0 - inline void intrusive_ptr_release(const gtsam::RefCounted * p) { - if (--(p->references_) == 0) - delete p; - } - -} // namespace boost - -#else - -#include - - namespace gtsam { - static long GlobalRefCount = 0; - } - - namespace boost { - inline void intrusive_ptr_add_ref(const gtsam::RefCounted * p) { - ++(p->references_); - gtsam::GlobalRefCount++; - std::cout << "add_ref " << p << " " << p->references_ << // - " " << gtsam::GlobalRefCount << std::endl; - } - - inline void intrusive_ptr_release(const gtsam::RefCounted * p) { - gtsam::GlobalRefCount--; - std::cout << "release " << p << " " << (p->references_ - 1) << // - " " << gtsam::GlobalRefCount << std::endl; - if (--(p->references_) == 0) - delete p; - } - - } // namespace boost - -#endif - diff --git a/gtsam/unstable/discrete/PotentialTable.cpp b/gtsam/unstable/discrete/PotentialTable.cpp deleted file mode 100644 index 41677134e..000000000 --- a/gtsam/unstable/discrete/PotentialTable.cpp +++ /dev/null @@ -1,173 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * Potentials.cpp - * - * @date Feb 21, 2011 - * @author Duy-Nguyen Ta - */ - -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -namespace gtsam { - - /* ************************************************************************* */ - void PotentialTable::Iterator::operator++() { - // note size_t is unsigned and i>=0 is always true, so strange-looking loop: - for (size_t i = size(); i--; ) { - if (++at(i) < cardinalities_[i]) - return; - else - at(i) = 0; - } - } - - /* ************************************************************************* */ - size_t PotentialTable::computeTableSize( - const std::vector& cardinalities) { - size_t tableSize = 1; - BOOST_FOREACH(const size_t c, cardinalities) - tableSize *= c; - return tableSize; - } - - /* ************************************************************************* */ - PotentialTable::PotentialTable(const std::vector& cs) : - cardinalities_(cs), table_(computeTableSize(cs)) { - generateKeyFactors(); - } - - /* ************************************************************************* */ - PotentialTable::PotentialTable(const std::vector& cardinalities, - const Table& table) : cardinalities_(cardinalities),table_(table) { - generateKeyFactors(); - } - - /* ************************************************************************* */ - PotentialTable::PotentialTable(const std::vector& cardinalities, - const std::string& tableString) : cardinalities_(cardinalities) { - parse(tableString); - generateKeyFactors(); - } - - /* ************************************************************************* */ - bool PotentialTable::equals(const PotentialTable& other, double tol) const { - //TODO: compare potentials in a more general sense with arbitrary order of keys??? - if ((cardinalities_ == other.cardinalities_) && (table_.size() - == other.table_.size()) && (keyFactors_ == other.keyFactors_)) { - for (size_t i = 0; i < table_.size(); i++) { - if (fabs(table_[i] - other.table_[i]) > tol) { - return false; - } - return true; - } - } - return false; - } - - /* ************************************************************************* */ - void PotentialTable::print(const std::string& s) const { - cout << s << endl; - for (size_t i = 0; i < cardinalities_.size(); i++) - cout << boost::format("[%d,%d]") % cardinalities_[i] % keyFactors_[i] << " "; - cout << endl; - Iterator assignment(cardinalities_); - for (size_t idx = 0; idx < table_.size(); ++idx, ++assignment) { - for (size_t k = 0; k < assignment.size(); k++) - cout << assignment[k] << "\t\t"; - cout << table_[idx] << endl; - } - } - - /* ************************************************************************* */ - const double& PotentialTable::operator()(const Assignment& var) const { - return table_[tableIndexFromAssignment(var)]; - } - - /* ************************************************************************* */ - const double& PotentialTable::operator[](const size_t index) const { - return table_.at(index); - } - - - /* ************************************************************************* */ - void PotentialTable::setPotential(const PotentialTable::Assignment& asg, const double potential) { - size_t idx = tableIndexFromAssignment(asg); - assert(idx (iss), istream_iterator (), - back_inserter(table_)); - -#ifndef NDEBUG - size_t expectedSize = computeTableSize(cardinalities_); - if (table_.size() != expectedSize) throw invalid_argument( - boost::str( - boost::format( - "String specification \"%s\" for table only contains %d doubles instead of %d") - % tableString % table_.size() % expectedSize)); -#endif - } - -} // namespace diff --git a/gtsam/unstable/discrete/PotentialTable.h b/gtsam/unstable/discrete/PotentialTable.h deleted file mode 100644 index 2cb2a7499..000000000 --- a/gtsam/unstable/discrete/PotentialTable.h +++ /dev/null @@ -1,106 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * Potentials.h - * - * @date Feb 21, 2011 - * @author Duy-Nguyen Ta - */ - -#ifndef POTENTIALS_H_ -#define POTENTIALS_H_ - -#include -#include -#include -#include -#include -#include - -namespace gtsam -{ -/** - * PotentialTable holds the real-valued potentials for Factors or Conditionals - */ -class PotentialTable { -public: - typedef std::vector Table; // container type for potentials f(x1,x2,..) - typedef std::vector Cardinalities; // just a typedef - typedef std::vector Assignment; // just a typedef - - /** - * An assignment that can be incemented - */ - struct Iterator: std::vector { - Cardinalities cardinalities_; - Iterator(const Cardinalities& cs):cardinalities_(cs) { - for(size_t i=0;i cardinalities_; // cardinalities of variables - Table table_; // Potential values of all instantiations of the variables, following the variables' order in vector Keys. - std::vector keyFactors_; // factors to multiply a key's assignment with, to access the potential table - - void generateKeyFactors(); - void parse(const std::string& tableString); - -public: - - /** compute table size from variable cardinalities */ - static size_t computeTableSize(const std::vector& cardinalities); - - /** construct an empty potential */ - PotentialTable() {} - - /** Dangerous empty n-ary potential. */ - PotentialTable(const std::vector& cardinalities); - - /** n-ary potential. */ - PotentialTable(const std::vector& cardinalities, - const Table& table); - - /** n-ary potential. */ - PotentialTable(const std::vector& cardinalities, - const std::string& tableString); - - /** return iterator to first element */ - Iterator begin() const { return Iterator(cardinalities_);} - - /** equality */ - bool equals(const PotentialTable& other, double tol = 1e-9) const; - - /** print */ - void print(const std::string& s = "Potential Table: ") const; - - /** return cardinality of a variable */ - size_t cardinality(size_t var) const { return cardinalities_[var]; } - size_t tableSize() const { return table_.size(); } - - /** accessors to potential values in the table given the assignment */ - const double& operator()(const Assignment& var) const; - const double& operator[](const size_t index) const; - - void setPotential(const Assignment& asg, const double potential); - void setPotential(const size_t tableIndex, const double potential); - - /** convert between assignment and where it is in the table */ - size_t tableIndexFromAssignment(const Assignment& var) const; - Assignment assignmentFromTableIndex(const size_t i) const; -}; - - -} // namespace - -#endif /* POTENTIALS_H_ */ diff --git a/gtsam/unstable/discrete/TypedDiscreteFactor.cpp b/gtsam/unstable/discrete/TypedDiscreteFactor.cpp deleted file mode 100644 index 769c0f095..000000000 --- a/gtsam/unstable/discrete/TypedDiscreteFactor.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * @file TypedDiscreteFactor.cpp - * @brief - * @author Duy-Nguyen Ta - * @date Mar 5, 2011 - */ - -#include -#include -#include -#include - -using namespace std; - -namespace gtsam { - - /* ******************************************************************************** */ - TypedDiscreteFactor::TypedDiscreteFactor(const Indices& keys, - const string& table) : - Factor (keys.begin(), keys.end()), potentials_(keys, table) { - } - - /* ******************************************************************************** */ - TypedDiscreteFactor::TypedDiscreteFactor(const Indices& keys, - const vector& table) : - Factor (keys.begin(), keys.end()), potentials_(keys, table) { - //#define DEBUG_FACTORS -#ifdef DEBUG_FACTORS - static size_t count = 0; - string dotfile = (boost::format("Factor-%03d") % ++count).str(); - potentials_.dot(dotfile); - if (count == 57) potentials_.print("57"); -#endif - } - - /* ************************************************************************* */ - double TypedDiscreteFactor::operator()(const Values& values) const { - return potentials_(values); - } - - /* ************************************************************************* */ - void TypedDiscreteFactor::print(const string&s) const { - Factor::print(s); - potentials_.print(); - } - - /* ************************************************************************* */ - bool TypedDiscreteFactor::equals(const TypedDiscreteFactor& other, double tol) const { - return potentials_.equals(other.potentials_, tol); - } - - /* ******************************************************************************** */ - DiscreteFactor::shared_ptr TypedDiscreteFactor::toDiscreteFactor( - const KeyOrdering& ordering) const { - throw std::runtime_error("broken"); - //return boost::make_shared(keys(), ordering, potentials_); - } - -#ifdef OLD -DiscreteFactor TypedDiscreteFactor::toDiscreteFactor( - const KeyOrdering& ordering, const ProblemType problemType) const { - { - static bool debug = false; - - // instantiate vector keys and column index in order - DiscreteFactor::ColumnIndex orderColumnIndex; - vector keys; - BOOST_FOREACH(const KeyOrdering::value_type& ord, ordering) - { - if (debug) cout << "Key: " << ord.first; - - // find the key with ord.first in this factor - vector::const_iterator it = std::find(keys_.begin(), - keys_.end(), ord.first); - - // if found - if (it != keys_.end()) { - if (debug) cout << "it found: " << (*it) << ", index: " - << ord.second << endl; - - keys.push_back(ord.second); // push back the ordering index - orderColumnIndex[ord.second] = columnIndex_.at(ord.first.name()); - - if (debug) cout << "map " << ord.second << " with name: " - << ord.first.name() << " to " << columnIndex_.at( - ord.first.name()) << endl; - } - } - - DiscreteFactor f(keys, potentials_, orderColumnIndex, problemType); - return f; - } - - /* ******************************************************************************** */ - std::vector TypedDiscreteFactor::init(const Indices& keys) { - vector cardinalities; - for (size_t j = 0; j < keys.size(); j++) { - Index key = keys[j]; - keys_.push_back(key); - columnIndex_[key.name()] = j; - cardinalities.push_back(key.cardinality()); - } - return cardinalities; - } - - /* ******************************************************************************** */ - double TypedDiscreteFactor::potential(const TypedValues& values) const { - vector assignment(values.size()); - BOOST_FOREACH(const TypedValues::value_type& val, values) - if (columnIndex_.find(val.first) != columnIndex_.end()) assignment[columnIndex_.at( - val.first)] = val.second; - return potentials_(assignment); - } - -#endif - -} // namespace diff --git a/gtsam/unstable/discrete/TypedDiscreteFactor.h b/gtsam/unstable/discrete/TypedDiscreteFactor.h deleted file mode 100644 index 209d4b51f..000000000 --- a/gtsam/unstable/discrete/TypedDiscreteFactor.h +++ /dev/null @@ -1,79 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * @file TypedDiscreteFactor.h - * @brief - * @author Duy-Nguyen Ta - * @date Mar 5, 2011 - */ - -#pragma once - -#include -#include -#include -#include - -namespace gtsam { - - /** - * A factor on discrete variables with string keys - */ - class TypedDiscreteFactor: public Factor { - - typedef AlgebraicDecisionDiagram ADD; - - /** potentials of the factor */ - ADD potentials_; - - public: - - /** A map from keys to values */ - typedef ADD::Assignment Values; - - /** Constructor from keys and string table */ - TypedDiscreteFactor(const Indices& keys, const std::string& table); - - /** Constructor from keys and doubles */ - TypedDiscreteFactor(const Indices& keys, - const std::vector& table); - - /** Evaluate */ - double operator()(const Values& values) const; - - // Testable - bool equals(const TypedDiscreteFactor& other, double tol = 1e-9) const; - void print(const std::string& s = "DiscreteFactor: ") const; - - DiscreteFactor::shared_ptr toDiscreteFactor(const KeyOrdering& ordering) const; - -#ifdef OLD - /** map each variable name to its column index in the potential table */ - typedef std::map Index2IndexMap; - Index2IndexMap columnIndex_; - - /** Initialize keys, column index, and return cardinalities */ - std::vector init(const Indices& keys); - - public: - - /** Default constructor */ - TypedDiscreteFactor() {} - - /** Evaluate potential of a given assignment of values */ - double potential(const TypedValues& values) const; - -#endif - - }; // TypedDiscreteFactor - -} // namespace diff --git a/gtsam/unstable/discrete/TypedDiscreteFactorGraph.cpp b/gtsam/unstable/discrete/TypedDiscreteFactorGraph.cpp deleted file mode 100644 index c03f92aeb..000000000 --- a/gtsam/unstable/discrete/TypedDiscreteFactorGraph.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * @file TypedDiscreteFactorGraph.cpp - * @brief - * @author Duy-Nguyen Ta - * @date Mar 1, 2011 - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -namespace gtsam { - - /* ************************************************************************* */ - TypedDiscreteFactorGraph::TypedDiscreteFactorGraph() { - } - - /* ************************************************************************* */ - TypedDiscreteFactorGraph::TypedDiscreteFactorGraph(const string& filename) { - bool success = parseUAI(filename, *this); - if (!success) throw runtime_error( - "TypedDiscreteFactorGraph constructor from filename failed"); - } - - /* ************************************************************************* */ - void TypedDiscreteFactorGraph::add// - (const Indices& keys, const string& table) { - push_back(boost::shared_ptr// - (new TypedDiscreteFactor(keys, table))); - } - - /* ************************************************************************* */ - void TypedDiscreteFactorGraph::add// - (const Indices& keys, const vector& table) { - push_back(boost::shared_ptr// - (new TypedDiscreteFactor(keys, table))); - } - - /* ************************************************************************* */ - void TypedDiscreteFactorGraph::print(const string s) { - cout << s << endl; - cout << "Factors: " << endl; - BOOST_FOREACH(const sharedFactor factor, factors_) - factor->print(); - } - - /* ************************************************************************* */ - double TypedDiscreteFactorGraph::operator()( - const TypedDiscreteFactor::Values& values) const { - // Loop over all factors and multiply their probabilities - double p = 1.0; - BOOST_FOREACH(const sharedFactor& factor, *this) - p *= (*factor)(values); - return p; - } - -/* ************************************************************************* */ - -} diff --git a/gtsam/unstable/discrete/TypedDiscreteFactorGraph.h b/gtsam/unstable/discrete/TypedDiscreteFactorGraph.h deleted file mode 100644 index a8fb88ff9..000000000 --- a/gtsam/unstable/discrete/TypedDiscreteFactorGraph.h +++ /dev/null @@ -1,61 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * @file TypedDiscreteFactorGraph.h - * @brief Factor graph with typed factors (with Index keys) - * @author Duy-Nguyen Ta - * @author Frank Dellaert - * @date Mar 1, 2011 - */ - -#pragma once - -#include -#include -#include -#include - -namespace gtsam { - - /** - * Typed discrete factor graph, where keys are strings - */ - class TypedDiscreteFactorGraph: public FactorGraph { - - public: - - /** - * Default constructor - */ - TypedDiscreteFactorGraph(); - - /** - * Constructor from file - * For now assumes in .uai format from UAI'08 Probablistic Inference Evaluation - * See http://graphmod.ics.uci.edu/uai08/FileFormat - */ - TypedDiscreteFactorGraph(const std::string& filename); - - // Add factors without shared pointer ugliness - void add(const Indices& keys, const std::string& table); - void add(const Indices& keys, const std::vector& table); - - /** print */ - void print(const std::string s); - - /** Evaluate potential of a given assignment of values */ - double operator()(const TypedDiscreteFactor::Values& values) const; - - }; // TypedDiscreteFactorGraph - - -} // namespace diff --git a/gtsam/unstable/discrete/parseUAI.cpp b/gtsam/unstable/discrete/parseUAI.cpp deleted file mode 100644 index 95e66a82c..000000000 --- a/gtsam/unstable/discrete/parseUAI.cpp +++ /dev/null @@ -1,168 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * parseUAI.cpp - * @brief: parse UAI 2008 format - * @date March 5, 2011 - * @author Duy-Nguyen Ta - * @author Frank Dellaert - */ - -//#define PARSE -#ifdef PARSE -#include -#include // for parsing -#include // for ref -#include -#include -#include -#include - -#include - -using namespace std; -namespace qi = boost::spirit::qi; - -namespace gtsam { - - /* ************************************************************************* */ - // Keys are the vars of variables connected to a factor - // subclass of Indices with special constructor - struct Keys: public Indices { - Keys() { - } - // Pick correct vars based on indices - Keys(const Indices& vars, const vector& indices) { - BOOST_FOREACH(int i, indices) - push_back(vars[i]); - } - }; - - /* ************************************************************************* */ - // The UAI grammar is defined in a class - // Spirit local variables are used, see - // http://boost-spirit.com/home/2010/01/21/what-are-rule-bound-semantic-actions - /* ************************************************************************* */ - struct Grammar { - - // declare all parsers as instance variables - typedef vector Table; - typedef boost::spirit::istream_iterator It; - qi::rule uai, preamble, type, vars, factors, tables; - qi::rule > keys; - qi::rule > table; - - // Variables filled by preamble parser - size_t nrVars_, nrFactors_; - Indices vars_; - vector factors_; - - // Variables filled by tables parser - vector tables_; - - // The constructor defines the parser rules (declared below) - // To debug, just say debug(rule) after defining the rule - Grammar() { - using boost::phoenix::val; - using boost::phoenix::ref; - using boost::phoenix::construct; - using namespace boost::spirit::qi; - - //--------------- high level parsers with side-effects :-( ----------------- - - // A uai file consists of preamble followed by tables - uai = preamble >> tables; - - // The preamble defines the variables and factors - // The parser fills in the first set of variables above, - // including the vector of factor "Neighborhoods" - preamble = type >> vars >> int_[ref(nrFactors_) = _1] >> factors; - - // type string, does not seem to matter - type = lit("BAYES") | lit("MARKOV"); - - // vars parses "3 2 2 3" and synthesizes a Keys class, in this case - // containing Indices {v0,2}, {v1,2}, and {v2,3} - vars = int_[ref(nrVars_) = _1] >> (repeat(ref(nrVars_))[int_]) // - [ref(vars_) = construct (_1)]; - - // Parse a list of Neighborhoods and fill factors_ - factors = (repeat(ref(nrFactors_))[keys])// - [ref(factors_) = _1]; - - // The tables parser fills in the tables_ - tables = (repeat(ref(nrFactors_))[table])// - [ref(tables_) = _1]; - - //----------- basic parsers with synthesized attributes :-) ----------------- - - // keys parses strings like "2 1 2", indicating - // a binary factor (2) on variables v1 and v2. - // It returns a Keys class as attribute - keys = int_[_a = _1] >> repeat(_a)[int_] // - [_val = construct (ref(vars_), _1)]; - - // The tables are a list of doubles preceded by a count, e.g. "4 1.0 2.0 3.0 4.0" - // The table parser returns a PotentialTable::Table attribute - table = int_[_a = _1] >> repeat(_a)[double_] // - [_val = construct
(_1)]; - } - - // Add the factors to the graph - void addFactorsToGraph(TypedDiscreteFactorGraph& graph) { - assert(factors_.size()==nrFactors_); - assert(tables_.size()==nrFactors_); - for (size_t i = 0; i < nrFactors_; i++) - graph.add(factors_[i], tables_[i]); - } - - }; - - /* ************************************************************************* */ - bool parseUAI(const std::string& filename, TypedDiscreteFactorGraph& graph) { - - // open file, disable skipping of whitespace - std::ifstream in(filename.c_str()); - if (!in) { - cerr << "Could not open " << filename << endl; - return false; - } - - in.unsetf(std::ios::skipws); - - // wrap istream into iterator - boost::spirit::istream_iterator first(in); - boost::spirit::istream_iterator last; - - // Parse and add factors into the graph - Grammar grammar; - bool success = qi::phrase_parse(first, last, grammar.uai, qi::space); - if (success) grammar.addFactorsToGraph(graph); - - return success; - } -/* ************************************************************************* */ - -}// gtsam -#else - -#include - -namespace gtsam { - -/** Dummy version of function - otherwise, missing symbol */ -bool parseUAI(const std::string& filename, TypedDiscreteFactorGraph& graph) { - return false; -} - -} // \namespace gtsam -#endif diff --git a/gtsam/unstable/discrete/parseUAI.h b/gtsam/unstable/discrete/parseUAI.h deleted file mode 100644 index 7e1010ff5..000000000 --- a/gtsam/unstable/discrete/parseUAI.h +++ /dev/null @@ -1,33 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * parseUAI.h - * @brief: parse UAI 2008 format - * @date March 5, 2011 - * @author Duy-Nguyen Ta - * @author Frank Dellaert - */ - -#include -#include - -namespace gtsam { - - /** - * Constructor from file - * For now assumes in .uai format from UAI'08 Probablistic Inference Evaluation - * See http://graphmod.ics.uci.edu/uai08/FileFormat - */ - bool parseUAI(const std::string& filename, - gtsam::TypedDiscreteFactorGraph& graph); - -} // gtsam diff --git a/gtsam/unstable/discrete/tests/testPotentialTable.cpp b/gtsam/unstable/discrete/tests/testPotentialTable.cpp deleted file mode 100644 index 2a549b608..000000000 --- a/gtsam/unstable/discrete/tests/testPotentialTable.cpp +++ /dev/null @@ -1,164 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * @file testPotentialTable.cpp - * @brief Develop recursive potential operations - * @author Frank Dellaert - * @date Mar 6, 2011 - */ - -#include -#include // for operator += -using namespace boost::assign; - -#include -#include - -using namespace std; -using namespace gtsam; - -/* ******************************************************************************** */ -TEST( PotentialTable, Iterator) -{ - PotentialTable::Cardinalities cs; - cs += 2, 3; - PotentialTable::Iterator it(cs); - LONGS_EQUAL(0,it[0]); - LONGS_EQUAL(0,it[1]); - ++it; - LONGS_EQUAL(0,it[0]); - LONGS_EQUAL(1,it[1]); - ++it; - LONGS_EQUAL(0,it[0]); - LONGS_EQUAL(2,it[1]); - ++it; - LONGS_EQUAL(1,it[0]); - LONGS_EQUAL(0,it[1]); - ++it; - LONGS_EQUAL(1,it[0]); - LONGS_EQUAL(1,it[1]); - ++it; - LONGS_EQUAL(1,it[0]); - LONGS_EQUAL(2,it[1]); - ++it; - LONGS_EQUAL(0,it[0]); - LONGS_EQUAL(0,it[1]); -} - -/* ******************************************************************************** */ -#include - -TEST( PotentialTable, unordered_map) -{ - boost::unordered_map x; - x[false] = 7; -} - -/* ******************************************************************************** */ - -struct Factor { - vector table_; - vector keys_; - bool operator==(const Factor& f) const { - return table_ == f.table_ && keys_ == f.keys_; - } -}; - -Factor operator*(const double& s, const Factor& f) { - Factor r = f; - BOOST_FOREACH(double& ri, r.table_) - ri *= s; - return r; -} - -Factor operator*(const Factor& f, const double& s) { - Factor r = f; - BOOST_FOREACH(double& ri, r.table_) - ri *= s; - return r; -} - -Factor operator*(const Factor& f1, const Factor& f2) { - Factor r; - - // base case 1, both tables start with same key - if (f1.keys_.front() == f2.keys_.front()) { - } - - return r; -} - -/* ******************************************************************************** */ -// f(5)*f(5) = f0*f0 @ f1*f1 -TEST( PotentialTable, baseCase1a) -{ - Factor f1, f2, expected; - f1.table_ += 00, 01; - f2.table_ += 20, 21; - f1.keys_ += 5; - f2.keys_ += 5; - expected.table_ += 00 * 20, 01 * 21; - expected.keys_ += 5; - CHECK(f1*f2==expected) -} - -/* ******************************************************************************** */ -// f(0,1)*f(0) = f0(1)*f0 @ f1(1)*f1 -TEST( PotentialTable, baseCase1b) -{ - Factor f1, f2, expected; - f1.table_ += 00, 01, 10, 11; - f2.table_ += 20, 21; - f1.keys_ += 0, 1; - f2.keys_ += 0; - expected.table_ += 00 * 20, 00 * 21, 01 * 20, 01 * 21, 10 * 20, 10 * 21, 11 - * 20, 11 * 21; - expected.keys_ += 0, 1, 2; - CHECK(f1*f2==expected) -} - -/* ******************************************************************************** */ -// f0(1)*f(2) = f00*f(2) @ f01*f(2) -TEST( PotentialTable, baseCase2) -{ - Factor f1, f2, expected; - f1.table_ += 00, 01; - f2.table_ += 20, 21; - f1.keys_ += 1; - f2.keys_ += 2; - expected.table_ += 00 * 20, 00 * 21, 01 * 20, 01 * 21; - expected.keys_ += 1, 2; - CHECK(f1*f2==expected) -} - -/* ******************************************************************************** */ -// f(0,1)*f(2) = f0(1)*f(2) @ f1(1)*f(2) -// f0(1)*f(2) = f00*f(2) @ f01*f(2) -TEST( PotentialTable, multiplication) -{ - Factor f1, f2, expected; - f1.table_ += 00, 01, 10, 11; - f2.table_ += 20, 21; - f1.keys_ += 0, 1; - f2.keys_ += 2; - expected.table_ += 00 * 20, 00 * 21, 01 * 20, 01 * 21, 10 * 20, 10 * 21, 11 - * 20, 11 * 21; - expected.keys_ += 0, 1, 2; - CHECK(f1*f2==expected) -} - -/* ************************************************************************* */ -int main() { - TestResult tr; - return TestRegistry::runAllTests(tr); -} -/* ************************************************************************* */ diff --git a/gtsam/unstable/discrete/tests/testTypedDiscreteFactor.cpp b/gtsam/unstable/discrete/tests/testTypedDiscreteFactor.cpp deleted file mode 100644 index 16798cd2b..000000000 --- a/gtsam/unstable/discrete/tests/testTypedDiscreteFactor.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * @file testTypedDiscreteFactor.cpp - * @brief Typed f1s use discrete keys - * @author Duy-Nguyen Ta - * @date Mar 5, 2011 - */ - -#include -#include -#include -#include -using namespace boost::assign; - -using namespace std; -using namespace gtsam; - -/* ******************************************************************************** */ -// initialize some common test values -DiscreteKey v0("v0"), v1("v1"), v2("v2", 3); -TypedDiscreteFactor::Values values; - -void init() { - values[v0] = 0; - values[v1] = 0; - values[v2] = 1; -} - -/* ******************************************************************************** */ -TEST( TypedDiscreteFactor, constructors) -{ - TypedDiscreteFactor f1(v1 & v2, "0.210 0.333 0.457 0.811 0.000 0.189"); - EXPECT_LONGS_EQUAL(2, f1.size()); - // f1.print(); - - double expectedP001 = 0.333; - EXPECT_DOUBLES_EQUAL(expectedP001, f1(values), 1e-9); - - vector ys; - ys += 0.210, 0.333, 0.457, 0.811, 0.000, 0.189; - TypedDiscreteFactor f2(v1 & v2, ys); - - EXPECT(assert_equal(f1, f2, 1e-9)); - EXPECT_LONGS_EQUAL(2, f1.size()); - EXPECT_DOUBLES_EQUAL(expectedP001, f2(values), 1e-9); -} - -/* ************************************************************************* */ -int main() { - init(); - TestResult tr; - return TestRegistry::runAllTests(tr); -} -/* ************************************************************************* */ diff --git a/gtsam/unstable/discrete/tests/testTypedDiscreteFactorGraph.cpp b/gtsam/unstable/discrete/tests/testTypedDiscreteFactorGraph.cpp deleted file mode 100644 index 70aee2c48..000000000 --- a/gtsam/unstable/discrete/tests/testTypedDiscreteFactorGraph.cpp +++ /dev/null @@ -1,204 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * testTypedDiscreteFactorGraph.cpp - * @brief test readable factor graphs - * @author Duy-Nguyen Ta - * @author Frank Dellaert - * @date Feb 14, 2011 - */ - -#include -#include -#include - -using namespace std; -using namespace gtsam; - -/* ******************************************************************************** */ -// initialize some common test values -DiscreteKey v0("v0"), v1("v1"), v2("v2", 3); -TypedDiscreteFactor::Values values; - -void init() { - values[v0] = 0; - values[v1] = 0; - values[v2] = 1; -} - -string path("../../../gtsam/discrete/tests/data/"); - -/* ************************************************************************* */ -TEST( TypedDiscreteFactorGraph, parseUAI) -{ - // This reads in a small factor graph on discrete variables v0, v1, and v2 - TypedDiscreteFactorGraph graph(path + "UAI/sampleMARKOV.uai"); - - // GTSAM_PRINT(graph); - - LONGS_EQUAL(3,graph.nrFactors()) - - double expectedP001 = 0.436 * 0.128 * 0.333; - double actualP001 = graph(values); - DOUBLES_EQUAL(expectedP001,actualP001,1e-9) -} - -/* ************************************************************************* */ -TEST( TypedDiscreteFactorGraph, parseUAI1) -{ - // This reads in a big graph from UAI 2008 - TypedDiscreteFactorGraph graph(path + "UAI/uai08_test1.uai"); - // GTSAM_PRINT(graph); - LONGS_EQUAL(54,graph.nrFactors()) -} - -/* ************************************************************************* */ -TEST( TypedDiscreteFactorGraph, parseUAI2) -{ - // This reads in a big graph from UAI 2008 - TypedDiscreteFactorGraph graph(path + "UAI/uai08_test2.uai"); - // GTSAM_PRINT(graph); - LONGS_EQUAL(21,graph.nrFactors()) -} - -/* ************************************************************************* */ -TEST( TypedDiscreteFactorGraph, parseUAI3) -{ - // This reads in a big graph from UAI 2008 - TypedDiscreteFactorGraph graph(path + "UAI/uai08_test3.uai"); - // GTSAM_PRINT(graph); - LONGS_EQUAL(13,graph.nrFactors()) -} - -///* ******************************************************************************** */ -//// global test data -// -//string graphFilename(path + "UAI/uai08_test2.uai"); -//string evidFilename (path + "UAI/uai08_test2.uai.evid"); -// -///** -// * [Cluster] Ordering splitted from libDAI -// * {x9}, {x10}, {x11}, {x12}, {x13}, {x14}, {x15}, {x16}, {x17}, {x18}, {x19}, {x20}, -// * {x0, x1, x8}, {x2, x6, x7}, {x4, x6, x8}, -// * {x1, x5, x7}, {x1, x3, x7, x8}, {x3, x6, x7, x8}) -// * -// */ -//size_t ordering[21] = {9,10,11,12,13,14,15,16,17,18,19,20,0,2,4,5,1,3,6,7,8}; -//vector vOrdering; -// -//// Container for all data read from files. -//TypedDiscreteFactorGraph container; -// -//// The factor graph generated from the data, after assigning the elimination ordering -//// for each variable -//DiscreteFactorGraph::shared_ptr graph; -// -///* ******************************************************************************** */ -//// Initialize all test data -//void initTestData() -//{ -// container.readFromFile_UAI(graphFilename); -// container.readEvidence_UAI(evidFilename); -// for (size_t i = 0; i<21; i++) vOrdering.push_back(ordering[i]); -// container.setOrdering(vOrdering); -// graph = container.generateFactorGraph(); -//} -// -// -///* ******************************************************************************** */ -//// Test reading .fg file from libDAI -//TEST( TypedDiscreteFactorGraph, readFG) -//{ -// TypedDiscreteFactorGraph graph; -// graph.readFromFile_FG(path + "FG/alarm.fg"); -//// graph.print(); -//} -// -///* ******************************************************************************** */ -//TEST( TypedDiscreteFactorGraph, testSequentialSolver) -//{ -//// tic(0, "Sequential Solver"); -// -// boost::shared_ptr actualMPE -// = DiscreteSequentialSolver(*graph).optimize(); -// BOOST_FOREACH(const PotentialTable::MapAssignment::value_type asg, *actualMPE) -// cout << vOrdering[asg.first] << ": " << asg.second << endl; -// -//// toc(0, "Sequential Solver"); -// tictoc_print(); -// -//} -// -///* ******************************************************************************** */ -//void backSubstitute(const BayesTree::sharedClique currentClique, -// PotentialTable::MapAssignment& assignments) { -// -// // solve the bayes net in the current node -// DiscreteBayesNet::const_reverse_iterator it = currentClique->rbegin(); -// for (; it!=currentClique->rend(); ++it) { -// size_t val = (*it)->solve(assignments); // Solve for that variable -// assignments[(*it)->key()] = val; // store result in partial solution -// } -// -// // solve the bayes nets in the child nodes -// BOOST_FOREACH(const BayesTree::sharedClique& child, -// currentClique->children()) { -// backSubstitute(child, assignments); -// } -//} -// -// -//void optimizeMultiFrontal(DiscreteFactorGraph::shared_ptr graph) { -// -// VariableIndex::shared_ptr structure(new VariableIndex(*graph)); -// JunctionTree::shared_ptr junctionTree(new JunctionTree(*graph, *structure)); -// BayesTree::sharedClique rootClique = junctionTree->eliminate(); -// -//// toc(1, "GJT eliminate"); -// -//// tictoc_print(); -// -//// // Allocate solution vector -//// tic(2, "allocate VectorValues"); -//// vector dims(rootClique->back()->key() + 1, 0); -//// countDims(rootClique, dims); -//// VectorValues result(dims); -//// toc(2, "allocate VectorValues"); -//// -//// // back-substitution -//// tic(3, "back-substitute"); -//// btreeBackSubstitute(rootClique, result); -//// toc(3, "back-substitute"); -//// return result; -//} -// -// -///* ******************************************************************************** */ -// -//TEST( TypedDiscreteFactorGraph, multifrontalSolver) -//{ -// tic(0, "Multifrontal Solver"); -// optimizeMultiFrontal(graph); -// -// toc(0, "Multifrontal Solver"); -// tictoc_print(); -//} - - -/* ************************************************************************* */ -int main() { /*initTestData(); */ - init(); - TestResult tr; - return TestRegistry::runAllTests(tr); -} -/* ************************************************************************* */ - diff --git a/gtsam/unstable/discrete/tests/testTypedDiscreteVariable.cpp b/gtsam/unstable/discrete/tests/testTypedDiscreteVariable.cpp deleted file mode 100644 index 1a264f00c..000000000 --- a/gtsam/unstable/discrete/tests/testTypedDiscreteVariable.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * GTSAM Copyright 2010, Georgia Tech Research Corporation, - * Atlanta, Georgia 30332-0415 - * All Rights Reserved - * Authors: Frank Dellaert, et al. (see THANKS for the full author list) - - * See LICENSE for the license information - - * -------------------------------------------------------------------------- */ - -/* - * testTypedDiscreteVariable.cpp - * - * @date March 2, 2011 - * @author Duy-Nguyen Ta - */ - -#include - -using namespace std; - -/* ******************************************************************************** */ - -class DiscreteVariable { - int v_; -public: - DiscreteVariable(int v) : - v_(v) { - } - bool operator ==(const DiscreteVariable& other) const { - return v_ == other.v_; - } - bool operator !=(const DiscreteVariable& other) const { - return v_ != other.v_; - } -}; - -class Color: public DiscreteVariable { -public: - enum Value { - red, green, blue - }; - Color(Value v) : - DiscreteVariable(v) { - } -}; - -class Flavor: public DiscreteVariable { -public: - enum Value { - sweet, sour - }; - Flavor(Value v) : - DiscreteVariable(v) { - } -}; - -//TEST( TypedDiscreteFactorGraph, simple) -//{ -// Color v1(Color::red), v2(Color::green); -// CHECK(v1!=v2); -// CHECK(v1==v1); -// -// // Declare a bunch of keys -// DiscreteKey C("Color"); -// DiscreteKey F("Flavor"); -// -// // Create a factor saying red is associated with sweet, -// // green with sour, blue with both -// TypedDiscreteFactor factor(C, F, "1 0 0 1 1 1"); -//} - -/* ************************************************************************* */ -int main() { - TestResult tr; - return TestRegistry::runAllTests(tr); -} -/* ************************************************************************* */ -