revive unstable discrete. testCSP still fails, but looks like the solution is valid. See comments in file.

release/4.3a0
Duy-Nguyen Ta 2013-10-11 17:42:30 +00:00
parent 9639660685
commit bf11f93cee
13 changed files with 61 additions and 26 deletions

View File

@ -143,9 +143,6 @@ namespace gtsam {
shared_ptr combine(const Ordering& keys, ADT::Binary op) const; shared_ptr combine(const Ordering& keys, ADT::Binary op) const;
/** Test whether the factor is empty */
virtual bool empty() const { return size() == 0; }
// /** // /**
// * @brief Permutes the keys in Potentials and DiscreteFactor // * @brief Permutes the keys in Potentials and DiscreteFactor
// * // *

View File

@ -97,10 +97,6 @@ Potentials::ADT DiscreteConditional::choose(const Values& parentsValues) const {
/* ******************************************************************************** */ /* ******************************************************************************** */
void DiscreteConditional::solveInPlace(Values& values) const { void DiscreteConditional::solveInPlace(Values& values) const {
// TODO: Abhijit asks: is this really the fastest way? He thinks it is. // TODO: Abhijit asks: is this really the fastest way? He thinks it is.
print("Me: ");
values.print("values:");
cout << "nrFrontals/nrParents: " << nrFrontals() << "/" << nrParents() << endl;
cout << "first frontal: " << firstFrontalKey() << endl;
ADT pFS = choose(values); // P(F|S=parentsValues) ADT pFS = choose(values); // P(F|S=parentsValues)
// Initialize // Initialize

View File

@ -83,7 +83,7 @@ public:
} }
/** Test whether the factor is empty */ /** Test whether the factor is empty */
virtual bool empty() const = 0; virtual bool empty() const { return size() == 0; }
/// @} /// @}
/// @name Standard Interface /// @name Standard Interface

View File

@ -3,7 +3,7 @@
set (gtsam_unstable_subdirs set (gtsam_unstable_subdirs
base base
geometry geometry
#discrete discrete
dynamics dynamics
nonlinear nonlinear
slam slam
@ -43,7 +43,7 @@ endforeach(subdir)
set(gtsam_unstable_srcs set(gtsam_unstable_srcs
${base_srcs} ${base_srcs}
${geometry_srcs} ${geometry_srcs}
#${discrete_srcs} ${discrete_srcs}
${dynamics_srcs} ${dynamics_srcs}
${nonlinear_srcs} ${nonlinear_srcs}
${slam_srcs} ${slam_srcs}

View File

@ -37,6 +37,18 @@ namespace gtsam {
virtual void print(const std::string& s = "", virtual void print(const std::string& s = "",
const IndexFormatter& formatter = DefaultIndexFormatter) const; const IndexFormatter& formatter = DefaultIndexFormatter) const;
/// equals
bool equals(const DiscreteFactor& other, double tol) const {
if(!dynamic_cast<const AllDiff*>(&other))
return false;
else {
const AllDiff& f(static_cast<const AllDiff&>(other));
return cardinalities_.size() == f.cardinalities_.size()
&& std::equal(cardinalities_.begin(), cardinalities_.end(),
f.cardinalities_.begin());
}
}
/// Calculate value = expensive ! /// Calculate value = expensive !
virtual double operator()(const Values& values) const; virtual double operator()(const Values& values) const;

View File

@ -39,6 +39,16 @@ namespace gtsam {
<< formatter(keys_[1]) << std::endl; << formatter(keys_[1]) << std::endl;
} }
/// equals
bool equals(const DiscreteFactor& other, double tol) const {
if(!dynamic_cast<const BinaryAllDiff*>(&other))
return false;
else {
const BinaryAllDiff& f(static_cast<const BinaryAllDiff&>(other));
return (cardinality0_==f.cardinality0_) && (cardinality1_==f.cardinality1_);
}
}
/// Calculate value /// Calculate value
virtual double operator()(const Values& values) const { virtual double operator()(const Values& values) const {
return (double) (values.at(keys_[0]) != values.at(keys_[1])); return (double) (values.at(keys_[0]) != values.at(keys_[1]));

View File

@ -7,7 +7,6 @@
#include <gtsam_unstable/discrete/Domain.h> #include <gtsam_unstable/discrete/Domain.h>
#include <gtsam_unstable/discrete/CSP.h> #include <gtsam_unstable/discrete/CSP.h>
#include <gtsam/discrete/DiscreteSequentialSolver.h>
#include <gtsam/base/Testable.h> #include <gtsam/base/Testable.h>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
@ -17,15 +16,14 @@ namespace gtsam {
/// Find the best total assignment - can be expensive /// Find the best total assignment - can be expensive
CSP::sharedValues CSP::optimalAssignment() const { CSP::sharedValues CSP::optimalAssignment() const {
DiscreteSequentialSolver solver(*this); DiscreteBayesNet::shared_ptr chordal = this->eliminateSequential();
DiscreteBayesNet::shared_ptr chordal = solver.eliminate(); sharedValues mpe = chordal->optimize();
sharedValues mpe = optimize(*chordal);
return mpe; return mpe;
} }
void CSP::runArcConsistency(size_t cardinality, size_t nrIterations, bool print) const { void CSP::runArcConsistency(size_t cardinality, size_t nrIterations, bool print) const {
// Create VariableIndex // Create VariableIndex
VariableIndexOrdered index(*this); VariableIndex index(*this);
// index.print(); // index.print();
size_t n = index.size(); size_t n = index.size();
@ -46,7 +44,7 @@ namespace gtsam {
// keep track of which domains changed // keep track of which domains changed
changed[v] = false; changed[v] = false;
// loop over all factors/constraints for variable v // loop over all factors/constraints for variable v
const VariableIndexOrdered::Factors& factors = index[v]; const VariableIndex::Factors& factors = index[v];
BOOST_FOREACH(size_t f,factors) { BOOST_FOREACH(size_t f,factors) {
// if not already a singleton // if not already a singleton
if (!domains[v].isSingleton()) { if (!domains[v].isSingleton()) {

View File

@ -19,6 +19,7 @@
#include <gtsam_unstable/base/dllexport.h> #include <gtsam_unstable/base/dllexport.h>
#include <gtsam/discrete/DiscreteFactor.h> #include <gtsam/discrete/DiscreteFactor.h>
#include <boost/assign.hpp>
namespace gtsam { namespace gtsam {
@ -43,12 +44,12 @@ namespace gtsam {
/// Construct unary factor /// Construct unary factor
Constraint(Index j) : Constraint(Index j) :
DiscreteFactor(j) { DiscreteFactor(boost::assign::cref_list_of<1>(j)) {
} }
/// Construct binary factor /// Construct binary factor
Constraint(Index j1, Index j2) : Constraint(Index j1, Index j2) :
DiscreteFactor(j1, j2) { DiscreteFactor(boost::assign::cref_list_of<2>(j1)(j2)) {
} }
/// construct from container /// construct from container

View File

@ -69,6 +69,16 @@ namespace gtsam {
virtual void print(const std::string& s = "", virtual void print(const std::string& s = "",
const IndexFormatter& formatter = DefaultIndexFormatter) const; const IndexFormatter& formatter = DefaultIndexFormatter) const;
/// equals
bool equals(const DiscreteFactor& other, double tol) const {
if(!dynamic_cast<const Domain*>(&other))
return false;
else {
const Domain& f(static_cast<const Domain&>(other));
return (cardinality_==f.cardinality_) && (values_==f.values_);
}
}
bool contains(size_t value) const { bool contains(size_t value) const {
return values_.count(value)>0; return values_.count(value)>0;
} }

View File

@ -7,7 +7,6 @@
#include <gtsam_unstable/discrete/Scheduler.h> #include <gtsam_unstable/discrete/Scheduler.h>
#include <gtsam/discrete/DiscreteFactorGraph.h> #include <gtsam/discrete/DiscreteFactorGraph.h>
#include <gtsam/discrete/DiscreteSequentialSolver.h>
#include <gtsam/base/debug.h> #include <gtsam/base/debug.h>
#include <gtsam/base/timing.h> #include <gtsam/base/timing.h>
@ -257,11 +256,8 @@ namespace gtsam {
/** Eliminate, return a Bayes net */ /** Eliminate, return a Bayes net */
DiscreteBayesNet::shared_ptr Scheduler::eliminate() const { DiscreteBayesNet::shared_ptr Scheduler::eliminate() const {
gttic(my_solver);
DiscreteSequentialSolver solver(*this);
gttoc(my_solver);
gttic(my_eliminate); gttic(my_eliminate);
DiscreteBayesNet::shared_ptr chordal = solver.eliminate(); DiscreteBayesNet::shared_ptr chordal = this->eliminateSequential();
gttoc(my_eliminate); gttoc(my_eliminate);
return chordal; return chordal;
} }
@ -271,14 +267,14 @@ namespace gtsam {
DiscreteBayesNet::shared_ptr chordal = eliminate(); DiscreteBayesNet::shared_ptr chordal = eliminate();
if (ISDEBUG("Scheduler::optimalAssignment")) { if (ISDEBUG("Scheduler::optimalAssignment")) {
DiscreteBayesNet::const_reverse_iterator it = chordal->rbegin(); DiscreteBayesNet::const_iterator it = chordal->end()-1;
const Student & student = students_.front(); const Student & student = students_.front();
cout << endl; cout << endl;
(*it)->print(student.name_); (*it)->print(student.name_);
} }
gttic(my_optimize); gttic(my_optimize);
sharedValues mpe = optimize(*chordal); sharedValues mpe = chordal->optimize();
gttoc(my_optimize); gttoc(my_optimize);
return mpe; return mpe;
} }

View File

@ -45,6 +45,16 @@ namespace gtsam {
virtual void print(const std::string& s = "", virtual void print(const std::string& s = "",
const IndexFormatter& formatter = DefaultIndexFormatter) const; const IndexFormatter& formatter = DefaultIndexFormatter) const;
/// equals
bool equals(const DiscreteFactor& other, double tol) const {
if(!dynamic_cast<const SingleValue*>(&other))
return false;
else {
const SingleValue& f(static_cast<const SingleValue&>(other));
return (cardinality_==f.cardinality_) && (value_==f.value_);
}
}
/// Calculate value /// Calculate value
virtual double operator()(const Values& values) const; virtual double operator()(const Values& values) const;

View File

@ -123,6 +123,11 @@ TEST_UNSAFE( CSP, WesternUS)
(WA.first,1)(CA.first,1)(NV.first,3)(OR.first,0) (WA.first,1)(CA.first,1)(NV.first,3)(OR.first,0)
(MT.first,1)(WY.first,0)(NM.first,3)(CO.first,2) (MT.first,1)(WY.first,0)(NM.first,3)(CO.first,2)
(ID.first,2)(UT.first,1)(AZ.first,0); (ID.first,2)(UT.first,1)(AZ.first,0);
// TODO: Fix me! mpe result seems to be right. (See the printing)
// It has the same prob as the expected solution.
// Is mpe another solution, or the expected solution is unique???
cout << csp(*mpe) << " should be >= " << csp(expected) << endl;
EXPECT(assert_equal(expected,*mpe)); EXPECT(assert_equal(expected,*mpe));
EXPECT_DOUBLES_EQUAL(1, csp(*mpe), 1e-9); EXPECT_DOUBLES_EQUAL(1, csp(*mpe), 1e-9);

View File

@ -74,7 +74,7 @@ DiscreteFactorGraph createExpected() {
expected.addAllDiff(J1 & J2 & J3); expected.addAllDiff(J1 & J2 & J3);
// Mutual exclusion for students // Mutual exclusion for students
expected.addAllDiff(A & J); expected.addAllDiff(A, J);
return expected; return expected;
} }