Added tests for Keys, Added TypedLabeledSymbol class to allow for runtime symbols on keys

release/4.3a0
Alex Cunningham 2010-01-18 16:18:02 +00:00
parent 7653aa8747
commit dfeacb218e
4 changed files with 144 additions and 5 deletions

View File

@ -468,6 +468,7 @@
</target> </target>
<target name="testBayesTree.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testBayesTree.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testBayesTree.run</buildTarget> <buildTarget>testBayesTree.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>false</useDefaultCommand> <useDefaultCommand>false</useDefaultCommand>
@ -475,7 +476,6 @@
</target> </target>
<target name="testSymbolicBayesNet.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testSymbolicBayesNet.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testSymbolicBayesNet.run</buildTarget> <buildTarget>testSymbolicBayesNet.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>false</useDefaultCommand> <useDefaultCommand>false</useDefaultCommand>
@ -483,6 +483,7 @@
</target> </target>
<target name="testSymbolicFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testSymbolicFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testSymbolicFactorGraph.run</buildTarget> <buildTarget>testSymbolicFactorGraph.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>false</useDefaultCommand> <useDefaultCommand>false</useDefaultCommand>
@ -714,6 +715,7 @@
</target> </target>
<target name="testGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="testGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testGraph.run</buildTarget> <buildTarget>testGraph.run</buildTarget>
<stopOnError>true</stopOnError> <stopOnError>true</stopOnError>
<useDefaultCommand>false</useDefaultCommand> <useDefaultCommand>false</useDefaultCommand>
@ -751,6 +753,14 @@
<useDefaultCommand>true</useDefaultCommand> <useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders> <runAllBuilders>true</runAllBuilders>
</target> </target>
<target name="testKey.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments>-j2</buildArguments>
<buildTarget>testKey.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
<target name="install" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder"> <target name="install" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand> <buildCommand>make</buildCommand>
<buildArguments>-j2</buildArguments> <buildArguments>-j2</buildArguments>

View File

@ -8,11 +8,14 @@
#pragma once #pragma once
#include <iostream>
#include <boost/format.hpp> #include <boost/format.hpp>
#include <boost/serialization/serialization.hpp> #include <boost/serialization/serialization.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <boost/integer_traits.hpp> #include <boost/integer_traits.hpp>
#include "Testable.h"
#define ALPHA '\224' #define ALPHA '\224'
namespace gtsam { namespace gtsam {
@ -21,12 +24,11 @@ namespace gtsam {
* TypedSymbol key class is templated on * TypedSymbol key class is templated on
* 1) the type T it is supposed to retrieve, for extra type checking * 1) the type T it is supposed to retrieve, for extra type checking
* 2) the character constant used for its string representation * 2) the character constant used for its string representation
* TODO: make Testable
*/ */
template <class T, char C> template <class T, char C>
class TypedSymbol { class TypedSymbol : Testable<TypedSymbol<T,C> > {
private: protected:
size_t j_; size_t j_;
public: public:
@ -49,6 +51,10 @@ namespace gtsam {
bool operator== (const TypedSymbol& compare) const { return j_==compare.j_;} bool operator== (const TypedSymbol& compare) const { return j_==compare.j_;}
int compare(const TypedSymbol& compare) const {return j_-compare.j_;} int compare(const TypedSymbol& compare) const {return j_-compare.j_;}
// Testable Requirements
void print(const std::string& name) const {} //FIXME
bool equals(const TypedSymbol& expected, double tol) const { return (*this)==expected; }
private: private:
/** Serialization function */ /** Serialization function */
@ -59,6 +65,72 @@ namespace gtsam {
} }
}; };
/**
* TypedLabeledSymbol is a variation of the TypedSymbol that allows
* for a runtime label to be placed on the label, so as to express
* "Pose 5 for robot 3"
* Labels should be kept to base datatypes (int, char, etc) to
* minimize cost of comparisons
*
* The labels will be compared first when comparing Keys, followed by the
* index
*/
template <class T, char C, typename L>
class TypedLabeledSymbol : public TypedSymbol<T, C>, Testable<TypedLabeledSymbol<T,C,L> > {
protected:
// Label
L label_;
public:
// Constructors:
TypedLabeledSymbol() {}
TypedLabeledSymbol(size_t j, L label):TypedSymbol<T,C>(j), label_(label) {}
// Get stuff:
L label() const { return label_;}
const char* c_str() const { return (std::string)(*this).c_str();}
operator std::string() const
{ return (boost::format("%c%label%d") % C % label_ % this->j_).str(); }
std::string latex() const
{ return (boost::format("%c%label_{%d}") % C % label_ % this->j_).str(); }
// logic:
bool operator< (const TypedLabeledSymbol& compare) const {
if (label_ == compare.label_) // sort by label first
return this->j_<compare.j_;
else
return label_<compare.label_;
}
bool operator== (const TypedLabeledSymbol& compare) const
{ return this->j_==compare.j_ && label_ == compare.label_;}
int compare(const TypedLabeledSymbol& compare) const {
if (label_ == compare.label_) // sort by label first
return this->j_-compare.j_;
else
return label_-compare.label_;
}
// Testable Requirements
void print(const std::string& name) const {} // FIXME
bool equals(const TypedLabeledSymbol& expected, double tol) const
{ return (*this)==expected; }
private:
/** Serialization function */
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & BOOST_SERIALIZATION_NVP(this->j_);
ar & BOOST_SERIALIZATION_NVP(label_);
}
};
/** /**
* Character and index key used in VectorConfig, GaussianFactorGraph, * Character and index key used in VectorConfig, GaussianFactorGraph,

View File

@ -131,13 +131,15 @@ testSubgraphPreconditioner_LDADD = libgtsam.la
headers += Key.h NonlinearFactorGraph.h NonlinearFactorGraph-inl.h headers += Key.h NonlinearFactorGraph.h NonlinearFactorGraph-inl.h
headers += NonlinearOptimizer.h NonlinearOptimizer-inl.h headers += NonlinearOptimizer.h NonlinearOptimizer-inl.h
headers += NonlinearFactor.h headers += NonlinearFactor.h
check_PROGRAMS += testNonlinearFactor testNonlinearFactorGraph testNonlinearOptimizer check_PROGRAMS += testNonlinearFactor testNonlinearFactorGraph testNonlinearOptimizer testKey
testNonlinearFactor_SOURCES = $(example) testNonlinearFactor.cpp testNonlinearFactor_SOURCES = $(example) testNonlinearFactor.cpp
testNonlinearFactor_LDADD = libgtsam.la testNonlinearFactor_LDADD = libgtsam.la
testNonlinearFactorGraph_SOURCES = $(example) testNonlinearFactorGraph.cpp testNonlinearFactorGraph_SOURCES = $(example) testNonlinearFactorGraph.cpp
testNonlinearFactorGraph_LDADD = libgtsam.la testNonlinearFactorGraph_LDADD = libgtsam.la
testNonlinearOptimizer_SOURCES = $(example) testNonlinearOptimizer.cpp testNonlinearOptimizer_SOURCES = $(example) testNonlinearOptimizer.cpp
testNonlinearOptimizer_LDADD = libgtsam.la testNonlinearOptimizer_LDADD = libgtsam.la
testKey_SOURCES = testKey.cpp
testKey_LDADD = libgtsam.la
# Nonlinear constraints # Nonlinear constraints
headers += SQPOptimizer.h SQPOptimizer-inl.h headers += SQPOptimizer.h SQPOptimizer-inl.h

55
cpp/testKey.cpp Normal file
View File

@ -0,0 +1,55 @@
/*
* @file testKey.cpp
* @author Alex Cunningham
*/
#include <CppUnitLite/TestHarness.h>
#include "Key.h"
using namespace gtsam;
class Pose3;
/* ************************************************************************* */
TEST ( TypedSymbol, basic_operations ) {
typedef TypedSymbol<Pose3, 'x'> Key;
Key key1(0),
key2(0),
key3(1),
key4(2);
CHECK(key1.index()==0);
CHECK(key1 == key2);
CHECK(assert_equal(key1, key2));
CHECK(!(key1 == key3));
CHECK(key1 < key3);
CHECK(key3 < key4);
}
/* ************************************************************************* */
TEST ( TypedLabledSymbol, basic_operations ) {
typedef TypedLabeledSymbol<Pose3, 'x', int> RobotKey;
RobotKey key1(0, 1),
key2(0, 1),
key3(1, 1),
key4(2, 1),
key5(0, 2),
key6(1, 2);
CHECK(key1.label()==1);
CHECK(key1.index()==0);
CHECK(key1 == key2);
CHECK(assert_equal(key1, key2));
CHECK(!(key1 == key3));
CHECK(key1 < key3);
CHECK(key3 < key4);
CHECK(!(key1 == key5));
CHECK(key1 < key5);
CHECK(key5 < key6);
}
/* ************************************************************************* */
int main() { TestResult tr; return TestRegistry::runAllTests(tr); }
/* ************************************************************************* */