diff --git a/examples/DiscreteBayesNetExample.cpp b/examples/DiscreteBayesNetExample.cpp index 5dca116c3..febc1e128 100644 --- a/examples/DiscreteBayesNetExample.cpp +++ b/examples/DiscreteBayesNetExample.cpp @@ -56,8 +56,8 @@ int main(int argc, char **argv) { DiscreteBayesNet::shared_ptr chordal = fg.eliminateSequential(ordering); // solve - DiscreteFactor::sharedValues mpe = chordal->optimize(); - GTSAM_PRINT(*mpe); + auto mpe = chordal->optimize(); + GTSAM_PRINT(mpe); // We can also build a Bayes tree (directed junction tree). // The elimination order above will do fine: @@ -70,14 +70,14 @@ int main(int argc, char **argv) { // solve again, now with evidence DiscreteBayesNet::shared_ptr chordal2 = fg.eliminateSequential(ordering); - DiscreteFactor::sharedValues mpe2 = chordal2->optimize(); - GTSAM_PRINT(*mpe2); + auto mpe2 = chordal2->optimize(); + GTSAM_PRINT(mpe2); // We can also sample from it cout << "\n10 samples:" << endl; for (size_t i = 0; i < 10; i++) { - DiscreteFactor::sharedValues sample = chordal2->sample(); - GTSAM_PRINT(*sample); + auto sample = chordal2->sample(); + GTSAM_PRINT(sample); } return 0; } diff --git a/examples/DiscreteBayesNet_FG.cpp b/examples/DiscreteBayesNet_FG.cpp index 121df4bef..69283a1be 100644 --- a/examples/DiscreteBayesNet_FG.cpp +++ b/examples/DiscreteBayesNet_FG.cpp @@ -33,11 +33,11 @@ using namespace gtsam; int main(int argc, char **argv) { // Define keys and a print function Key C(1), S(2), R(3), W(4); - auto print = [=](DiscreteFactor::sharedValues values) { - cout << boolalpha << "Cloudy = " << static_cast((*values)[C]) - << " Sprinkler = " << static_cast((*values)[S]) - << " Rain = " << boolalpha << static_cast((*values)[R]) - << " WetGrass = " << static_cast((*values)[W]) << endl; + auto print = [=](const DiscreteFactor::Values& values) { + cout << boolalpha << "Cloudy = " << static_cast(values.at(C)) + << " Sprinkler = " << static_cast(values.at(S)) + << " Rain = " << boolalpha << static_cast(values.at(R)) + << " WetGrass = " << static_cast(values.at(W)) << endl; }; // We assume binary state variables @@ -85,7 +85,7 @@ int main(int argc, char **argv) { } // "Most Probable Explanation", i.e., configuration with largest value - DiscreteFactor::sharedValues mpe = graph.eliminateSequential()->optimize(); + auto mpe = graph.eliminateSequential()->optimize(); cout << "\nMost Probable Explanation (MPE):" << endl; print(mpe); @@ -97,7 +97,7 @@ int main(int argc, char **argv) { // solve again, now with evidence DiscreteBayesNet::shared_ptr chordal = graph.eliminateSequential(); - DiscreteFactor::sharedValues mpe_with_evidence = chordal->optimize(); + auto mpe_with_evidence = chordal->optimize(); cout << "\nMPE given C=0:" << endl; print(mpe_with_evidence); @@ -113,7 +113,7 @@ int main(int argc, char **argv) { // We can also sample from it cout << "\n10 samples:" << endl; for (size_t i = 0; i < 10; i++) { - DiscreteFactor::sharedValues sample = chordal->sample(); + auto sample = chordal->sample(); print(sample); } return 0; diff --git a/examples/HMMExample.cpp b/examples/HMMExample.cpp index ee861e381..b46baf4e0 100644 --- a/examples/HMMExample.cpp +++ b/examples/HMMExample.cpp @@ -66,14 +66,14 @@ int main(int argc, char **argv) { chordal->print("Eliminated"); // solve - DiscreteFactor::sharedValues mpe = chordal->optimize(); - GTSAM_PRINT(*mpe); + auto mpe = chordal->optimize(); + GTSAM_PRINT(mpe); // We can also sample from it cout << "\n10 samples:" << endl; for (size_t k = 0; k < 10; k++) { - DiscreteFactor::sharedValues sample = chordal->sample(); - GTSAM_PRINT(*sample); + auto sample = chordal->sample(); + GTSAM_PRINT(sample); } // Or compute the marginals. This re-eliminates the FG into a Bayes tree diff --git a/examples/UGM_chain.cpp b/examples/UGM_chain.cpp index 3a885a844..ababef022 100644 --- a/examples/UGM_chain.cpp +++ b/examples/UGM_chain.cpp @@ -70,8 +70,8 @@ int main(int argc, char** argv) { // "Decoding", i.e., configuration with largest value // We use sequential variable elimination DiscreteBayesNet::shared_ptr chordal = graph.eliminateSequential(); - DiscreteFactor::sharedValues optimalDecoding = chordal->optimize(); - optimalDecoding->print("\nMost Probable Explanation (optimalDecoding)\n"); + auto optimalDecoding = chordal->optimize(); + optimalDecoding.print("\nMost Probable Explanation (optimalDecoding)\n"); // "Inference" Computing marginals for each node // Here we'll make use of DiscreteMarginals class, which makes use of diff --git a/examples/UGM_small.cpp b/examples/UGM_small.cpp index 27a6205a3..f4f3f1fd0 100644 --- a/examples/UGM_small.cpp +++ b/examples/UGM_small.cpp @@ -63,8 +63,8 @@ int main(int argc, char** argv) { // "Decoding", i.e., configuration with largest value (MPE) // We use sequential variable elimination DiscreteBayesNet::shared_ptr chordal = graph.eliminateSequential(); - DiscreteFactor::sharedValues optimalDecoding = chordal->optimize(); - optimalDecoding->print("\noptimalDecoding"); + auto optimalDecoding = chordal->optimize(); + GTSAM_PRINT(optimalDecoding); // "Inference" Computing marginals cout << "\nComputing Node Marginals .." << endl; diff --git a/gtsam/discrete/DiscreteBayesNet.cpp b/gtsam/discrete/DiscreteBayesNet.cpp index 84a80c565..71c50c477 100644 --- a/gtsam/discrete/DiscreteBayesNet.cpp +++ b/gtsam/discrete/DiscreteBayesNet.cpp @@ -54,20 +54,20 @@ namespace gtsam { } /* ************************************************************************* */ - DiscreteFactor::sharedValues DiscreteBayesNet::optimize() const { + DiscreteFactor::Values DiscreteBayesNet::optimize() const { // solve each node in turn in topological sort order (parents first) - DiscreteFactor::sharedValues result(new DiscreteFactor::Values()); + DiscreteFactor::Values result; for (auto conditional: boost::adaptors::reverse(*this)) - conditional->solveInPlace(*result); + conditional->solveInPlace(&result); return result; } /* ************************************************************************* */ - DiscreteFactor::sharedValues DiscreteBayesNet::sample() const { + DiscreteFactor::Values DiscreteBayesNet::sample() const { // sample each node in turn in topological sort order (parents first) - DiscreteFactor::sharedValues result(new DiscreteFactor::Values()); + DiscreteFactor::Values result; for (auto conditional: boost::adaptors::reverse(*this)) - conditional->sampleInPlace(*result); + conditional->sampleInPlace(&result); return result; } diff --git a/gtsam/discrete/DiscreteBayesNet.h b/gtsam/discrete/DiscreteBayesNet.h index d5ba30584..e89645658 100644 --- a/gtsam/discrete/DiscreteBayesNet.h +++ b/gtsam/discrete/DiscreteBayesNet.h @@ -83,10 +83,10 @@ namespace gtsam { /** * Solve the DiscreteBayesNet by back-substitution */ - DiscreteFactor::sharedValues optimize() const; + DiscreteFactor::Values optimize() const; /** Do ancestral sampling */ - DiscreteFactor::sharedValues sample() const; + DiscreteFactor::Values sample() const; ///@} diff --git a/gtsam/discrete/DiscreteConditional.cpp b/gtsam/discrete/DiscreteConditional.cpp index ac7c58405..e7ef4de19 100644 --- a/gtsam/discrete/DiscreteConditional.cpp +++ b/gtsam/discrete/DiscreteConditional.cpp @@ -117,9 +117,9 @@ 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. - ADT pFS = choose(values); // P(F|S=parentsValues) + ADT pFS = choose(*values); // P(F|S=parentsValues) // Initialize Values mpe; @@ -145,16 +145,16 @@ void DiscreteConditional::solveInPlace(Values& values) const { //set values (inPlace) to mpe for(Key j: frontals()) { - values[j] = mpe[j]; + (*values)[j] = mpe[j]; } } /* ******************************************************************************** */ -void DiscreteConditional::sampleInPlace(Values& values) const { +void DiscreteConditional::sampleInPlace(Values* values) const { assert(nrFrontals() == 1); Key j = (firstFrontalKey()); - size_t sampled = sample(values); // Sample variable - values[j] = sampled; // store result in partial solution + size_t sampled = sample(*values); // Sample variable given parents + (*values)[j] = sampled; // store result in partial solution } /* ******************************************************************************** */ diff --git a/gtsam/discrete/DiscreteConditional.h b/gtsam/discrete/DiscreteConditional.h index 8299fab2c..191a80fb0 100644 --- a/gtsam/discrete/DiscreteConditional.h +++ b/gtsam/discrete/DiscreteConditional.h @@ -45,7 +45,6 @@ public: /** A map from keys to values.. * TODO: Again, do we need this??? */ typedef Assignment Values; - typedef boost::shared_ptr sharedValues; /// @name Standard Constructors /// @{ @@ -133,10 +132,10 @@ public: /// @{ /// solve a conditional, in place - void solveInPlace(Values& parentsValues) const; + void solveInPlace(Values* parentsValues) const; /// sample in place, stores result in partial solution - void sampleInPlace(Values& parentsValues) const; + void sampleInPlace(Values* parentsValues) const; /// @} diff --git a/gtsam/discrete/DiscreteFactor.h b/gtsam/discrete/DiscreteFactor.h index 6b0919507..cd883c59c 100644 --- a/gtsam/discrete/DiscreteFactor.h +++ b/gtsam/discrete/DiscreteFactor.h @@ -51,7 +51,6 @@ public: * the new class DiscreteValue, as the varible's type (domain) */ typedef Assignment Values; - typedef boost::shared_ptr sharedValues; public: diff --git a/gtsam/discrete/DiscreteFactorGraph.cpp b/gtsam/discrete/DiscreteFactorGraph.cpp index e41968d6b..4ff0e339e 100644 --- a/gtsam/discrete/DiscreteFactorGraph.cpp +++ b/gtsam/discrete/DiscreteFactorGraph.cpp @@ -94,7 +94,7 @@ namespace gtsam { // } /* ************************************************************************* */ - DiscreteFactor::sharedValues DiscreteFactorGraph::optimize() const + DiscreteFactorGraph::Values DiscreteFactorGraph::optimize() const { gttic(DiscreteFactorGraph_optimize); return BaseEliminateable::eliminateSequential()->optimize(); diff --git a/gtsam/discrete/DiscreteFactorGraph.h b/gtsam/discrete/DiscreteFactorGraph.h index 3ea9c3cdd..329d015e9 100644 --- a/gtsam/discrete/DiscreteFactorGraph.h +++ b/gtsam/discrete/DiscreteFactorGraph.h @@ -74,7 +74,6 @@ public: /** A map from keys to values */ typedef KeyVector Indices; typedef Assignment Values; - typedef boost::shared_ptr sharedValues; /** Default constructor */ DiscreteFactorGraph() {} @@ -142,7 +141,7 @@ public: * the dense elimination function specified in \c function, * followed by back-substitution resulting from elimination. Is equivalent * to calling graph.eliminateSequential()->optimize(). */ - DiscreteFactor::sharedValues optimize() const; + Values optimize() const; // /** Permute the variables in the factors */ diff --git a/gtsam/discrete/tests/testDiscreteBayesNet.cpp b/gtsam/discrete/tests/testDiscreteBayesNet.cpp index 2b440e5a0..f6159c0c6 100644 --- a/gtsam/discrete/tests/testDiscreteBayesNet.cpp +++ b/gtsam/discrete/tests/testDiscreteBayesNet.cpp @@ -104,12 +104,12 @@ TEST(DiscreteBayesNet, Asia) { EXPECT(assert_equal(expected2, *chordal->back())); // solve - DiscreteFactor::sharedValues actualMPE = chordal->optimize(); + auto actualMPE = chordal->optimize(); DiscreteFactor::Values expectedMPE; insert(expectedMPE)(Asia.first, 0)(Dyspnea.first, 0)(XRay.first, 0)( Tuberculosis.first, 0)(Smoking.first, 0)(Either.first, 0)( LungCancer.first, 0)(Bronchitis.first, 0); - EXPECT(assert_equal(expectedMPE, *actualMPE)); + EXPECT(assert_equal(expectedMPE, actualMPE)); // add evidence, we were in Asia and we have dyspnea fg.add(Asia, "0 1"); @@ -117,12 +117,12 @@ TEST(DiscreteBayesNet, Asia) { // solve again, now with evidence DiscreteBayesNet::shared_ptr chordal2 = fg.eliminateSequential(ordering); - DiscreteFactor::sharedValues actualMPE2 = chordal2->optimize(); + auto actualMPE2 = chordal2->optimize(); DiscreteFactor::Values expectedMPE2; insert(expectedMPE2)(Asia.first, 1)(Dyspnea.first, 1)(XRay.first, 0)( Tuberculosis.first, 0)(Smoking.first, 1)(Either.first, 0)( LungCancer.first, 0)(Bronchitis.first, 1); - EXPECT(assert_equal(expectedMPE2, *actualMPE2)); + EXPECT(assert_equal(expectedMPE2, actualMPE2)); // now sample from it DiscreteFactor::Values expectedSample; @@ -130,8 +130,8 @@ TEST(DiscreteBayesNet, Asia) { insert(expectedSample)(Asia.first, 1)(Dyspnea.first, 1)(XRay.first, 1)( Tuberculosis.first, 0)(Smoking.first, 1)(Either.first, 1)( LungCancer.first, 1)(Bronchitis.first, 0); - DiscreteFactor::sharedValues actualSample = chordal2->sample(); - EXPECT(assert_equal(expectedSample, *actualSample)); + auto actualSample = chordal2->sample(); + EXPECT(assert_equal(expectedSample, actualSample)); } /* ************************************************************************* */ diff --git a/gtsam/discrete/tests/testDiscreteFactorGraph.cpp b/gtsam/discrete/tests/testDiscreteFactorGraph.cpp index 1defd5acf..6b7a43c1c 100644 --- a/gtsam/discrete/tests/testDiscreteFactorGraph.cpp +++ b/gtsam/discrete/tests/testDiscreteFactorGraph.cpp @@ -169,8 +169,8 @@ TEST( DiscreteFactorGraph, test) // Test optimization DiscreteFactor::Values expectedValues; insert(expectedValues)(0, 0)(1, 0)(2, 0); - DiscreteFactor::sharedValues actualValues = graph.optimize(); - EXPECT(assert_equal(expectedValues, *actualValues)); + auto actualValues = graph.optimize(); + EXPECT(assert_equal(expectedValues, actualValues)); } /* ************************************************************************* */ @@ -186,11 +186,11 @@ TEST( DiscreteFactorGraph, testMPE) // graph.product().print(); // DiscreteSequentialSolver(graph).eliminate()->print(); - DiscreteFactor::sharedValues actualMPE = graph.optimize(); + auto actualMPE = graph.optimize(); DiscreteFactor::Values expectedMPE; insert(expectedMPE)(0, 0)(1, 1)(2, 1); - EXPECT(assert_equal(expectedMPE, *actualMPE)); + EXPECT(assert_equal(expectedMPE, actualMPE)); } /* ************************************************************************* */ @@ -216,8 +216,8 @@ TEST( DiscreteFactorGraph, testMPE_Darwiche09book_p244) // Use the solver machinery. DiscreteBayesNet::shared_ptr chordal = graph.eliminateSequential(); - DiscreteFactor::sharedValues actualMPE = chordal->optimize(); - EXPECT(assert_equal(expectedMPE, *actualMPE)); + auto actualMPE = chordal->optimize(); + EXPECT(assert_equal(expectedMPE, actualMPE)); // DiscreteConditional::shared_ptr root = chordal->back(); // EXPECT_DOUBLES_EQUAL(0.4, (*root)(*actualMPE), 1e-9); @@ -244,8 +244,8 @@ ETree::shared_ptr eTree = ETree::Create(graph, structure); // eliminate normally and check solution DiscreteBayesNet::shared_ptr bayesNet = eTree->eliminate(&EliminateDiscrete); // bayesNet->print(">>>>>>>>>>>>>> Bayes Net <<<<<<<<<<<<<<<<<<"); -DiscreteFactor::sharedValues actualMPE = optimize(*bayesNet); -EXPECT(assert_equal(expectedMPE, *actualMPE)); +auto actualMPE = optimize(*bayesNet); +EXPECT(assert_equal(expectedMPE, actualMPE)); // Approximate and check solution // DiscreteBayesNet::shared_ptr approximateNet = eTree->approximate(); diff --git a/gtsam/linear/linear.i b/gtsam/linear/linear.i index c74161f26..cf65b1a38 100644 --- a/gtsam/linear/linear.i +++ b/gtsam/linear/linear.i @@ -302,6 +302,7 @@ virtual class JacobianFactor : gtsam::GaussianFactor { void print(string s = "", const gtsam::KeyFormatter& keyFormatter = gtsam::DefaultKeyFormatter) const; void printKeys(string s) const; + gtsam::KeyVector& keys() const; bool equals(const gtsam::GaussianFactor& lf, double tol) const; size_t size() const; Vector unweighted_error(const gtsam::VectorValues& c) const; @@ -514,9 +515,9 @@ virtual class GaussianBayesNet { size_t size() const; // FactorGraph derived interface - // size_t size() const; gtsam::GaussianConditional* at(size_t idx) const; gtsam::KeySet keys() const; + gtsam::KeyVector keyVector() const; bool exists(size_t idx) const; void saveGraph(const string& s) const; diff --git a/gtsam_unstable/discrete/CSP.cpp b/gtsam_unstable/discrete/CSP.cpp index 8c974f4fd..1b38d20d6 100644 --- a/gtsam_unstable/discrete/CSP.cpp +++ b/gtsam_unstable/discrete/CSP.cpp @@ -14,17 +14,15 @@ using namespace std; namespace gtsam { /// Find the best total assignment - can be expensive -CSP::sharedValues CSP::optimalAssignment() const { +CSP::Values CSP::optimalAssignment() const { DiscreteBayesNet::shared_ptr chordal = this->eliminateSequential(); - sharedValues mpe = chordal->optimize(); - return mpe; + return chordal->optimize(); } /// Find the best total assignment - can be expensive -CSP::sharedValues CSP::optimalAssignment(const Ordering& ordering) const { +CSP::Values CSP::optimalAssignment(const Ordering& ordering) const { DiscreteBayesNet::shared_ptr chordal = this->eliminateSequential(ordering); - sharedValues mpe = chordal->optimize(); - return mpe; + return chordal->optimize(); } bool CSP::runArcConsistency(const VariableIndex& index, diff --git a/gtsam_unstable/discrete/CSP.h b/gtsam_unstable/discrete/CSP.h index d94913682..0d4e0b177 100644 --- a/gtsam_unstable/discrete/CSP.h +++ b/gtsam_unstable/discrete/CSP.h @@ -22,7 +22,6 @@ class GTSAM_UNSTABLE_EXPORT CSP : public DiscreteFactorGraph { public: /** A map from keys to values */ typedef Assignment Values; - typedef boost::shared_ptr sharedValues; public: /// Add a unary constraint, allowing only a single value @@ -47,10 +46,10 @@ class GTSAM_UNSTABLE_EXPORT CSP : public DiscreteFactorGraph { // } /// Find the best total assignment - can be expensive. - sharedValues optimalAssignment() const; + Values optimalAssignment() const; /// Find the best total assignment, with given ordering - can be expensive. - sharedValues optimalAssignment(const Ordering& ordering) const; + Values optimalAssignment(const Ordering& ordering) const; // /* // * Perform loopy belief propagation diff --git a/gtsam_unstable/discrete/Scheduler.cpp b/gtsam_unstable/discrete/Scheduler.cpp index 415f92e62..6210f8037 100644 --- a/gtsam_unstable/discrete/Scheduler.cpp +++ b/gtsam_unstable/discrete/Scheduler.cpp @@ -202,16 +202,16 @@ void Scheduler::print(const string& s, const KeyFormatter& formatter) const { } // print /** Print readable form of assignment */ -void Scheduler::printAssignment(sharedValues assignment) const { +void Scheduler::printAssignment(const Values& assignment) const { // Not intended to be general! Assumes very particular ordering ! cout << endl; for (size_t s = 0; s < nrStudents(); s++) { Key j = 3 * maxNrStudents_ + s; - size_t slot = assignment->at(j); + size_t slot = assignment.at(j); cout << studentName(s) << " slot: " << slotName_[slot] << endl; Key base = 3 * s; for (size_t area = 0; area < 3; area++) { - size_t faculty = assignment->at(base + area); + size_t faculty = assignment.at(base + area); cout << setw(12) << studentArea(s, area) << ": " << facultyName_[faculty] << endl; } @@ -220,8 +220,8 @@ void Scheduler::printAssignment(sharedValues assignment) const { } /** Special print for single-student case */ -void Scheduler::printSpecial(sharedValues assignment) const { - Values::const_iterator it = assignment->begin(); +void Scheduler::printSpecial(const Values& assignment) const { + Values::const_iterator it = assignment.begin(); for (size_t area = 0; area < 3; area++, it++) { size_t f = it->second; cout << setw(12) << studentArea(0, area) << ": " << facultyName_[f] << endl; @@ -230,12 +230,12 @@ void Scheduler::printSpecial(sharedValues assignment) const { } /** Accumulate faculty stats */ -void Scheduler::accumulateStats(sharedValues assignment, +void Scheduler::accumulateStats(const Values& assignment, vector& stats) const { for (size_t s = 0; s < nrStudents(); s++) { Key base = 3 * s; for (size_t area = 0; area < 3; area++) { - size_t f = assignment->at(base + area); + size_t f = assignment.at(base + area); assert(f < stats.size()); stats[f]++; } // area @@ -256,7 +256,7 @@ DiscreteBayesNet::shared_ptr Scheduler::eliminate() const { } /** Find the best total assignment - can be expensive */ -Scheduler::sharedValues Scheduler::optimalAssignment() const { +Scheduler::Values Scheduler::optimalAssignment() const { DiscreteBayesNet::shared_ptr chordal = eliminate(); if (ISDEBUG("Scheduler::optimalAssignment")) { @@ -267,22 +267,21 @@ Scheduler::sharedValues Scheduler::optimalAssignment() const { } gttic(my_optimize); - sharedValues mpe = chordal->optimize(); + Values mpe = chordal->optimize(); gttoc(my_optimize); return mpe; } /** find the assignment of students to slots with most possible committees */ -Scheduler::sharedValues Scheduler::bestSchedule() const { - sharedValues best; +Scheduler::Values Scheduler::bestSchedule() const { + Values best; throw runtime_error("bestSchedule not implemented"); return best; } /** find the corresponding most desirable committee assignment */ -Scheduler::sharedValues Scheduler::bestAssignment( - sharedValues bestSchedule) const { - sharedValues best; +Scheduler::Values Scheduler::bestAssignment(const Values& bestSchedule) const { + Values best; throw runtime_error("bestAssignment not implemented"); return best; } diff --git a/gtsam_unstable/discrete/Scheduler.h b/gtsam_unstable/discrete/Scheduler.h index faf131f5c..08e866efd 100644 --- a/gtsam_unstable/discrete/Scheduler.h +++ b/gtsam_unstable/discrete/Scheduler.h @@ -134,26 +134,26 @@ class GTSAM_UNSTABLE_EXPORT Scheduler : public CSP { const KeyFormatter& formatter = DefaultKeyFormatter) const override; /** Print readable form of assignment */ - void printAssignment(sharedValues assignment) const; + void printAssignment(const Values& assignment) const; /** Special print for single-student case */ - void printSpecial(sharedValues assignment) const; + void printSpecial(const Values& assignment) const; /** Accumulate faculty stats */ - void accumulateStats(sharedValues assignment, + void accumulateStats(const Values& assignment, std::vector& stats) const; /** Eliminate, return a Bayes net */ DiscreteBayesNet::shared_ptr eliminate() const; /** Find the best total assignment - can be expensive */ - sharedValues optimalAssignment() const; + Values optimalAssignment() const; /** find the assignment of students to slots with most possible committees */ - sharedValues bestSchedule() const; + Values bestSchedule() const; /** find the corresponding most desirable committee assignment */ - sharedValues bestAssignment(sharedValues bestSchedule) const; + Values bestAssignment(const Values& bestSchedule) const; }; // Scheduler diff --git a/gtsam_unstable/discrete/examples/schedulingExample.cpp b/gtsam_unstable/discrete/examples/schedulingExample.cpp index e9f63b2d8..3f270e45d 100644 --- a/gtsam_unstable/discrete/examples/schedulingExample.cpp +++ b/gtsam_unstable/discrete/examples/schedulingExample.cpp @@ -122,7 +122,7 @@ void runLargeExample() { // SETDEBUG("timing-verbose", true); SETDEBUG("DiscreteConditional::DiscreteConditional", true); gttic(large); - DiscreteFactor::sharedValues MPE = scheduler.optimalAssignment(); + auto MPE = scheduler.optimalAssignment(); gttoc(large); tictoc_finishedIteration(); tictoc_print(); @@ -225,7 +225,7 @@ void sampleSolutions() { // now, sample schedules for (size_t n = 0; n < 500; n++) { vector stats(19, 0); - vector samples; + vector samples; for (size_t i = 0; i < 7; i++) { samples.push_back(samplers[i]->sample()); schedulers[i].accumulateStats(samples[i], stats); @@ -331,7 +331,7 @@ void accomodateStudent() { // sample schedules for (size_t n = 0; n < 10; n++) { - Scheduler::sharedValues sample0 = chordal->sample(); + auto sample0 = chordal->sample(); scheduler.printAssignment(sample0); } } diff --git a/gtsam_unstable/discrete/examples/schedulingQuals12.cpp b/gtsam_unstable/discrete/examples/schedulingQuals12.cpp index 1fc4a1459..5ed5766d5 100644 --- a/gtsam_unstable/discrete/examples/schedulingQuals12.cpp +++ b/gtsam_unstable/discrete/examples/schedulingQuals12.cpp @@ -129,7 +129,7 @@ void runLargeExample() { tictoc_finishedIteration(); tictoc_print(); for (size_t i=0;i<100;i++) { - DiscreteFactor::sharedValues assignment = chordal->sample(); + auto assignment = chordal->sample(); vector stats(scheduler.nrFaculty()); scheduler.accumulateStats(assignment, stats); size_t max = *max_element(stats.begin(), stats.end()); @@ -143,7 +143,7 @@ void runLargeExample() { } #else gttic(large); - DiscreteFactor::sharedValues MPE = scheduler.optimalAssignment(); + auto MPE = scheduler.optimalAssignment(); gttoc(large); tictoc_finishedIteration(); tictoc_print(); @@ -234,7 +234,7 @@ void sampleSolutions() { // now, sample schedules for (size_t n = 0; n < 500; n++) { vector stats(19, 0); - vector samples; + vector samples; for (size_t i = 0; i < NRSTUDENTS; i++) { samples.push_back(samplers[i]->sample()); schedulers[i].accumulateStats(samples[i], stats); diff --git a/gtsam_unstable/discrete/examples/schedulingQuals13.cpp b/gtsam_unstable/discrete/examples/schedulingQuals13.cpp index 95b64f289..2da0eb9b1 100644 --- a/gtsam_unstable/discrete/examples/schedulingQuals13.cpp +++ b/gtsam_unstable/discrete/examples/schedulingQuals13.cpp @@ -153,7 +153,7 @@ void runLargeExample() { tictoc_finishedIteration(); tictoc_print(); for (size_t i=0;i<100;i++) { - DiscreteFactor::sharedValues assignment = sample(*chordal); + auto assignment = sample(*chordal); vector stats(scheduler.nrFaculty()); scheduler.accumulateStats(assignment, stats); size_t max = *max_element(stats.begin(), stats.end()); @@ -167,7 +167,7 @@ void runLargeExample() { } #else gttic(large); - DiscreteFactor::sharedValues MPE = scheduler.optimalAssignment(); + auto MPE = scheduler.optimalAssignment(); gttoc(large); tictoc_finishedIteration(); tictoc_print(); @@ -259,7 +259,7 @@ void sampleSolutions() { // now, sample schedules for (size_t n = 0; n < 10000; n++) { vector stats(nrFaculty, 0); - vector samples; + vector samples; for (size_t i = 0; i < NRSTUDENTS; i++) { samples.push_back(samplers[i]->sample()); schedulers[i].accumulateStats(samples[i], stats); diff --git a/gtsam_unstable/discrete/tests/testCSP.cpp b/gtsam_unstable/discrete/tests/testCSP.cpp index 63069d710..4a3b22561 100644 --- a/gtsam_unstable/discrete/tests/testCSP.cpp +++ b/gtsam_unstable/discrete/tests/testCSP.cpp @@ -132,11 +132,11 @@ TEST(CSP, allInOne) { EXPECT(assert_equal(expectedProduct, product)); // Solve - CSP::sharedValues mpe = csp.optimalAssignment(); + auto mpe = csp.optimalAssignment(); CSP::Values expected; insert(expected)(ID.first, 1)(UT.first, 0)(AZ.first, 1); - EXPECT(assert_equal(expected, *mpe)); - EXPECT_DOUBLES_EQUAL(1, csp(*mpe), 1e-9); + EXPECT(assert_equal(expected, mpe)); + EXPECT_DOUBLES_EQUAL(1, csp(mpe), 1e-9); } /* ************************************************************************* */ @@ -176,10 +176,9 @@ TEST(CSP, WesternUS) { Ordering ordering; ordering += Key(0), Key(1), Key(2), Key(3), Key(4), Key(5), Key(6), Key(7), Key(8), Key(9), Key(10); - // Solve using that ordering: - CSP::sharedValues mpe = csp.optimalAssignment(ordering); - // GTSAM_PRINT(*mpe); + auto mpe = csp.optimalAssignment(ordering); + // GTSAM_PRINT(mpe); CSP::Values 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)( @@ -188,8 +187,8 @@ TEST(CSP, WesternUS) { // 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??? - EXPECT(assert_equal(expected, *mpe)); - EXPECT_DOUBLES_EQUAL(1, csp(*mpe), 1e-9); + EXPECT(assert_equal(expected, mpe)); + EXPECT_DOUBLES_EQUAL(1, csp(mpe), 1e-9); // Write out the dual graph for hmetis #ifdef DUAL @@ -228,11 +227,11 @@ TEST(CSP, ArcConsistency) { EXPECT_DOUBLES_EQUAL(1, csp(valid), 1e-9); // Solve - CSP::sharedValues mpe = csp.optimalAssignment(); + auto mpe = csp.optimalAssignment(); CSP::Values expected; insert(expected)(ID.first, 1)(UT.first, 0)(AZ.first, 2); - EXPECT(assert_equal(expected, *mpe)); - EXPECT_DOUBLES_EQUAL(1, csp(*mpe), 1e-9); + EXPECT(assert_equal(expected, mpe)); + EXPECT_DOUBLES_EQUAL(1, csp(mpe), 1e-9); // ensure arc-consistency, i.e., narrow domains... Domains domains; diff --git a/gtsam_unstable/discrete/tests/testScheduler.cpp b/gtsam_unstable/discrete/tests/testScheduler.cpp index 4eb86fe1f..7822cbd38 100644 --- a/gtsam_unstable/discrete/tests/testScheduler.cpp +++ b/gtsam_unstable/discrete/tests/testScheduler.cpp @@ -122,7 +122,7 @@ TEST(schedulingExample, test) { // Do exact inference gttic(small); - DiscreteFactor::sharedValues MPE = s.optimalAssignment(); + auto MPE = s.optimalAssignment(); gttoc(small); // print MPE, commented out as unit tests don't print @@ -133,13 +133,13 @@ TEST(schedulingExample, test) { // find the assignment of students to slots with most possible committees // Commented out as not implemented yet - // sharedValues bestSchedule = s.bestSchedule(); - // GTSAM_PRINT(*bestSchedule); + // auto bestSchedule = s.bestSchedule(); + // GTSAM_PRINT(bestSchedule); // find the corresponding most desirable committee assignment // Commented out as not implemented yet - // sharedValues bestAssignment = s.bestAssignment(bestSchedule); - // GTSAM_PRINT(*bestAssignment); + // auto bestAssignment = s.bestAssignment(bestSchedule); + // GTSAM_PRINT(bestAssignment); } /* ************************************************************************* */ diff --git a/gtsam_unstable/discrete/tests/testSudoku.cpp b/gtsam_unstable/discrete/tests/testSudoku.cpp index ee307fd5b..a8546bc2f 100644 --- a/gtsam_unstable/discrete/tests/testSudoku.cpp +++ b/gtsam_unstable/discrete/tests/testSudoku.cpp @@ -88,11 +88,11 @@ class Sudoku : public CSP { } /// Print readable form of assignment - void printAssignment(const DiscreteFactor::sharedValues& assignment) const { + void printAssignment(const DiscreteFactor::Values& assignment) const { for (size_t i = 0; i < n_; i++) { for (size_t j = 0; j < n_; j++) { Key k = key(i, j); - cout << 1 + assignment->at(k) << " "; + cout << 1 + assignment.at(k) << " "; } cout << endl; } @@ -100,7 +100,7 @@ class Sudoku : public CSP { /// solve and print solution void printSolution() const { - DiscreteFactor::sharedValues MPE = optimalAssignment(); + auto MPE = optimalAssignment(); printAssignment(MPE); } @@ -126,14 +126,14 @@ TEST_UNSAFE(Sudoku, small) { 0, 1, 0, 0); // optimize and check - CSP::sharedValues solution = csp.optimalAssignment(); + auto solution = csp.optimalAssignment(); CSP::Values expected; 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(1, 3), 1)(csp.key(2, 0), 3)(csp.key(2, 1), 2)(csp.key(2, 2), 1)( csp.key(2, 3), 0)(csp.key(3, 0), 1)(csp.key(3, 1), 0)(csp.key(3, 2), 3)( csp.key(3, 3), 2); - EXPECT(assert_equal(expected, *solution)); + EXPECT(assert_equal(expected, solution)); // csp.printAssignment(solution); // Do BP (AC1)