optimalAssignment -> optimize. Not deprecating as in unstable.

release/4.3a0
Frank Dellaert 2022-01-21 14:47:28 -05:00
parent e713897235
commit b17fcfb64f
10 changed files with 17 additions and 59 deletions

View File

@ -14,18 +14,6 @@ using namespace std;
namespace gtsam { namespace gtsam {
/// Find the best total assignment - can be expensive
DiscreteValues CSP::optimalAssignment() const {
DiscreteBayesNet::shared_ptr chordal = this->eliminateSequential();
return chordal->optimize();
}
/// Find the best total assignment - can be expensive
DiscreteValues CSP::optimalAssignment(const Ordering& ordering) const {
DiscreteBayesNet::shared_ptr chordal = this->eliminateSequential(ordering);
return chordal->optimize();
}
bool CSP::runArcConsistency(const VariableIndex& index, bool CSP::runArcConsistency(const VariableIndex& index,
Domains* domains) const { Domains* domains) const {
bool changed = false; bool changed = false;

View File

@ -43,12 +43,6 @@ class GTSAM_UNSTABLE_EXPORT CSP : public DiscreteFactorGraph {
// return result; // return result;
// } // }
/// Find the best total assignment - can be expensive.
DiscreteValues optimalAssignment() const;
/// Find the best total assignment, with given ordering - can be expensive.
DiscreteValues optimalAssignment(const Ordering& ordering) const;
// /* // /*
// * Perform loopy belief propagation // * Perform loopy belief propagation
// * True belief propagation would check for each value in domain // * True belief propagation would check for each value in domain

View File

@ -255,23 +255,6 @@ DiscreteBayesNet::shared_ptr Scheduler::eliminate() const {
return chordal; return chordal;
} }
/** Find the best total assignment - can be expensive */
DiscreteValues Scheduler::optimalAssignment() const {
DiscreteBayesNet::shared_ptr chordal = eliminate();
if (ISDEBUG("Scheduler::optimalAssignment")) {
DiscreteBayesNet::const_iterator it = chordal->end() - 1;
const Student& student = students_.front();
cout << endl;
(*it)->print(student.name_);
}
gttic(my_optimize);
DiscreteValues mpe = chordal->optimize();
gttoc(my_optimize);
return mpe;
}
/** find the assignment of students to slots with most possible committees */ /** find the assignment of students to slots with most possible committees */
DiscreteValues Scheduler::bestSchedule() const { DiscreteValues Scheduler::bestSchedule() const {
DiscreteValues best; DiscreteValues best;

View File

@ -147,9 +147,6 @@ class GTSAM_UNSTABLE_EXPORT Scheduler : public CSP {
/** Eliminate, return a Bayes net */ /** Eliminate, return a Bayes net */
DiscreteBayesNet::shared_ptr eliminate() const; DiscreteBayesNet::shared_ptr eliminate() const;
/** Find the best total assignment - can be expensive */
DiscreteValues optimalAssignment() const;
/** find the assignment of students to slots with most possible committees */ /** find the assignment of students to slots with most possible committees */
DiscreteValues bestSchedule() const; DiscreteValues bestSchedule() const;

View File

@ -122,7 +122,7 @@ void runLargeExample() {
// SETDEBUG("timing-verbose", true); // SETDEBUG("timing-verbose", true);
SETDEBUG("DiscreteConditional::DiscreteConditional", true); SETDEBUG("DiscreteConditional::DiscreteConditional", true);
gttic(large); gttic(large);
auto MPE = scheduler.optimalAssignment(); auto MPE = scheduler.optimize();
gttoc(large); gttoc(large);
tictoc_finishedIteration(); tictoc_finishedIteration();
tictoc_print(); tictoc_print();

View File

@ -143,7 +143,7 @@ void runLargeExample() {
} }
#else #else
gttic(large); gttic(large);
auto MPE = scheduler.optimalAssignment(); auto MPE = scheduler.optimize();
gttoc(large); gttoc(large);
tictoc_finishedIteration(); tictoc_finishedIteration();
tictoc_print(); tictoc_print();

View File

@ -167,7 +167,7 @@ void runLargeExample() {
} }
#else #else
gttic(large); gttic(large);
auto MPE = scheduler.optimalAssignment(); auto MPE = scheduler.optimize();
gttoc(large); gttoc(large);
tictoc_finishedIteration(); tictoc_finishedIteration();
tictoc_print(); tictoc_print();

View File

@ -132,7 +132,7 @@ TEST(CSP, allInOne) {
EXPECT(assert_equal(expectedProduct, product)); EXPECT(assert_equal(expectedProduct, product));
// Solve // Solve
auto mpe = csp.optimalAssignment(); auto mpe = csp.optimize();
DiscreteValues expected; DiscreteValues expected;
insert(expected)(ID.first, 1)(UT.first, 0)(AZ.first, 1); insert(expected)(ID.first, 1)(UT.first, 0)(AZ.first, 1);
EXPECT(assert_equal(expected, mpe)); EXPECT(assert_equal(expected, mpe));
@ -172,22 +172,18 @@ TEST(CSP, WesternUS) {
csp.addAllDiff(WY, CO); csp.addAllDiff(WY, CO);
csp.addAllDiff(CO, NM); csp.addAllDiff(CO, NM);
DiscreteValues mpe;
insert(mpe)(0, 2)(1, 3)(2, 2)(3, 1)(4, 1)(5, 3)(6, 3)(7, 2)(8, 0)(9, 1)(10, 0);
// Create ordering according to example in ND-CSP.lyx // Create ordering according to example in ND-CSP.lyx
Ordering ordering; Ordering ordering;
ordering += Key(0), Key(1), Key(2), Key(3), Key(4), Key(5), Key(6), Key(7), ordering += Key(0), Key(1), Key(2), Key(3), Key(4), Key(5), Key(6), Key(7),
Key(8), Key(9), Key(10); Key(8), Key(9), Key(10);
// Solve using that ordering:
auto mpe = csp.optimalAssignment(ordering);
// GTSAM_PRINT(mpe);
DiscreteValues expected;
insert(expected)(WA.first, 1)(CA.first, 1)(NV.first, 3)(OR.first, 0)(
MT.first, 1)(WY.first, 0)(NM.first, 3)(CO.first, 2)(ID.first, 2)(
UT.first, 1)(AZ.first, 0);
// TODO: Fix me! mpe result seems to be right. (See the printing) // Solve using that ordering:
// It has the same prob as the expected solution. auto actualMPE = csp.optimize(ordering);
// Is mpe another solution, or the expected solution is unique???
EXPECT(assert_equal(expected, mpe)); EXPECT(assert_equal(mpe, actualMPE));
EXPECT_DOUBLES_EQUAL(1, csp(mpe), 1e-9); EXPECT_DOUBLES_EQUAL(1, csp(mpe), 1e-9);
// Write out the dual graph for hmetis // Write out the dual graph for hmetis
@ -227,7 +223,7 @@ TEST(CSP, ArcConsistency) {
EXPECT_DOUBLES_EQUAL(1, csp(valid), 1e-9); EXPECT_DOUBLES_EQUAL(1, csp(valid), 1e-9);
// Solve // Solve
auto mpe = csp.optimalAssignment(); auto mpe = csp.optimize();
DiscreteValues expected; DiscreteValues expected;
insert(expected)(ID.first, 1)(UT.first, 0)(AZ.first, 2); insert(expected)(ID.first, 1)(UT.first, 0)(AZ.first, 2);
EXPECT(assert_equal(expected, mpe)); EXPECT(assert_equal(expected, mpe));

View File

@ -122,7 +122,7 @@ TEST(schedulingExample, test) {
// Do exact inference // Do exact inference
gttic(small); gttic(small);
auto MPE = s.optimalAssignment(); auto MPE = s.optimize();
gttoc(small); gttoc(small);
// print MPE, commented out as unit tests don't print // print MPE, commented out as unit tests don't print

View File

@ -100,7 +100,7 @@ class Sudoku : public CSP {
/// solve and print solution /// solve and print solution
void printSolution() const { void printSolution() const {
auto MPE = optimalAssignment(); auto MPE = optimize();
printAssignment(MPE); printAssignment(MPE);
} }
@ -126,7 +126,7 @@ TEST(Sudoku, small) {
0, 1, 0, 0); 0, 1, 0, 0);
// optimize and check // optimize and check
auto solution = csp.optimalAssignment(); auto solution = csp.optimize();
DiscreteValues expected; DiscreteValues expected;
insert(expected)(csp.key(0, 0), 0)(csp.key(0, 1), 1)(csp.key(0, 2), 2)( insert(expected)(csp.key(0, 0), 0)(csp.key(0, 1), 1)(csp.key(0, 2), 2)(
csp.key(0, 3), 3)(csp.key(1, 0), 2)(csp.key(1, 1), 3)(csp.key(1, 2), 0)( csp.key(0, 3), 3)(csp.key(1, 0), 2)(csp.key(1, 1), 3)(csp.key(1, 2), 0)(
@ -148,7 +148,7 @@ TEST(Sudoku, small) {
EXPECT_LONGS_EQUAL(16, new_csp.size()); EXPECT_LONGS_EQUAL(16, new_csp.size());
// Check that solution // Check that solution
auto new_solution = new_csp.optimalAssignment(); auto new_solution = new_csp.optimize();
// csp.printAssignment(new_solution); // csp.printAssignment(new_solution);
EXPECT(assert_equal(expected, new_solution)); EXPECT(assert_equal(expected, new_solution));
} }
@ -250,7 +250,7 @@ TEST(Sudoku, AJC_3star_Feb8_2012) {
EXPECT_LONGS_EQUAL(81, new_csp.size()); EXPECT_LONGS_EQUAL(81, new_csp.size());
// Check that solution // Check that solution
auto solution = new_csp.optimalAssignment(); auto solution = new_csp.optimize();
// csp.printAssignment(solution); // csp.printAssignment(solution);
EXPECT_LONGS_EQUAL(6, solution.at(key99)); EXPECT_LONGS_EQUAL(6, solution.at(key99));
} }