[REFACTOR] Replace BOOST_FOREACH with c++ foreach loop

[BUGFIX] Fixed failing unit test due to assert.
release/4.3a0
ivan 2016-06-13 20:35:17 -04:00
parent 6bafe9932e
commit bf68fc6b4b
12 changed files with 68 additions and 65 deletions

View File

@ -45,7 +45,8 @@ namespace gtsam {
* *
*/ */
int ActiveSetSolver::identifyLeavingConstraint( int ActiveSetSolver::identifyLeavingConstraint(
const InequalityFactorGraph& workingSet, const VectorValues& lambdas) const { const InequalityFactorGraph& workingSet,
const VectorValues& lambdas) const {
int worstFactorIx = -1; int worstFactorIx = -1;
// preset the maxLambda to 0.0: if lambda is <= 0.0, the constraint is // preset the maxLambda to 0.0: if lambda is <= 0.0, the constraint is
// either // either
@ -78,11 +79,12 @@ return worstFactorIx;
GaussianFactorGraph::shared_ptr ActiveSetSolver::buildDualGraph( GaussianFactorGraph::shared_ptr ActiveSetSolver::buildDualGraph(
const InequalityFactorGraph& workingSet, const VectorValues& delta) const { const InequalityFactorGraph& workingSet, const VectorValues& delta) const {
GaussianFactorGraph::shared_ptr dualGraph(new GaussianFactorGraph()); GaussianFactorGraph::shared_ptr dualGraph(new GaussianFactorGraph());
BOOST_FOREACH (Key key, constrainedKeys_) { for (Key key : constrainedKeys_) {
// Each constrained key becomes a factor in the dual graph // Each constrained key becomes a factor in the dual graph
JacobianFactor::shared_ptr dualFactor = JacobianFactor::shared_ptr dualFactor = createDualFactor(key, workingSet,
createDualFactor(key, workingSet, delta); delta);
if (!dualFactor->empty()) dualGraph->push_back(dualFactor); if (!dualFactor->empty())
dualGraph->push_back(dualFactor);
} }
return dualGraph; return dualGraph;
} }

View File

@ -53,7 +53,7 @@ public:
*/ */
TermsContainer Aterms; TermsContainer Aterms;
if (variableIndex.find(key) != variableIndex.end()) { if (variableIndex.find(key) != variableIndex.end()) {
BOOST_FOREACH (size_t factorIx, variableIndex[key]) { for(size_t factorIx: variableIndex[key]) {
typename FACTOR::shared_ptr factor = graph.at(factorIx); typename FACTOR::shared_ptr factor = graph.at(factorIx);
if (!factor->active()) continue; if (!factor->active()) continue;
Matrix Ai = factor->getA(factor->find(key)).transpose(); Matrix Ai = factor->getA(factor->find(key)).transpose();

View File

@ -36,7 +36,7 @@ public:
*/ */
double error(const VectorValues& x) const { double error(const VectorValues& x) const {
double total_error = 0.; double total_error = 0.;
BOOST_FOREACH(const sharedFactor& factor, *this){ for(const sharedFactor& factor: *this){
if(factor) if(factor)
total_error += factor->error(x); total_error += factor->error(x);
} }

View File

@ -47,7 +47,7 @@ public:
* Compute error of a guess. * Compute error of a guess.
* Infinity error if it violates an inequality; zero otherwise. */ * Infinity error if it violates an inequality; zero otherwise. */
double error(const VectorValues& x) const { double error(const VectorValues& x) const {
BOOST_FOREACH(const sharedFactor& factor, *this) { for(const sharedFactor& factor: *this) {
if(factor) if(factor)
if (factor->error(x) > 0) if (factor->error(x) > 0)
return std::numeric_limits<double>::infinity(); return std::numeric_limits<double>::infinity();

View File

@ -161,7 +161,8 @@ InequalityFactorGraph LPSolver::identifyActiveConstraints(
InequalityFactorGraph workingSet; InequalityFactorGraph workingSet;
for (const LinearInequality::shared_ptr &factor : inequalities) { for (const LinearInequality::shared_ptr &factor : inequalities) {
LinearInequality::shared_ptr workingFactor(new LinearInequality(*factor)); LinearInequality::shared_ptr workingFactor(new LinearInequality(*factor));
GTSAM_PRINT(*workingFactor);
GTSAM_PRINT(initialValues);
double error = workingFactor->error(initialValues); double error = workingFactor->error(initialValues);
// TODO: find a feasible initial point for LPSolver. // TODO: find a feasible initial point for LPSolver.
// For now, we just throw an exception // For now, we just throw an exception

View File

@ -43,9 +43,10 @@ public:
template<class LinearGraph> template<class LinearGraph>
KeyDimMap collectKeysDim(const LinearGraph &linearGraph) const { KeyDimMap collectKeysDim(const LinearGraph &linearGraph) const {
KeyDimMap keysDim; KeyDimMap keysDim;
BOOST_FOREACH(const typename LinearGraph::sharedFactor &factor, linearGraph) { for (const typename LinearGraph::sharedFactor &factor : linearGraph) {
if (!factor) continue; if (!factor)
BOOST_FOREACH(Key key, factor->keys()) continue;
for (Key key : factor->keys())
keysDim[key] = factor->getDim(factor->find(key)); keysDim[key] = factor->getDim(factor->find(key));
} }
return keysDim; return keysDim;

View File

@ -184,7 +184,7 @@ pair<VectorValues, VectorValues> QPSolver::optimize() const {
//Make an LP with any linear cost function. It doesn't matter for initialization. //Make an LP with any linear cost function. It doesn't matter for initialization.
LP initProblem; LP initProblem;
Key newKey = 0; // make an unrelated key for a random variable cost Key newKey = 0; // make an unrelated key for a random variable cost
BOOST_FOREACH(Key key, qp_.cost.getKeyDimMap() | boost::adaptors::map_keys) for (Key key : qp_.cost.getKeyDimMap() | boost::adaptors::map_keys)
if (newKey < key) if (newKey < key)
newKey = key; newKey = key;
newKey++; newKey++;

View File

@ -94,7 +94,7 @@ TEST(LPInitSolver, infinite_loop_multi_var) {
VectorValues starter; VectorValues starter;
starter.insert(X, kZero); starter.insert(X, kZero);
starter.insert(Y, kZero); starter.insert(Y, kZero);
starter.insert(Z, Vector::Constant(2, 2.0)); starter.insert(Z, Vector::Constant(1, 2.0));
VectorValues results, duals; VectorValues results, duals;
boost::tie(results, duals) = solver.optimize(starter); boost::tie(results, duals) = solver.optimize(starter);
VectorValues expected; VectorValues expected;

View File

@ -149,7 +149,6 @@ TEST(QPSolver, indentifyActiveConstraints) {
expectedSolution.insert(X(1), kZero); expectedSolution.insert(X(1), kZero);
expectedSolution.insert(X(2), kZero); expectedSolution.insert(X(2), kZero);
CHECK(assert_equal(expectedSolution, solution, 1e-100)); CHECK(assert_equal(expectedSolution, solution, 1e-100));
} }
/* ************************************************************************* */ /* ************************************************************************* */

View File

@ -37,7 +37,7 @@ bool LinearConstraintSQP::isPrimalFeasible(const LinearConstraintNLPState& state
/* ************************************************************************* */ /* ************************************************************************* */
bool LinearConstraintSQP::isDualFeasible(const VectorValues& duals) const { bool LinearConstraintSQP::isDualFeasible(const VectorValues& duals) const {
BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, lcnlp_.linearInequalities) { for(const NonlinearFactor::shared_ptr& factor: lcnlp_.linearInequalities) {
ConstrainedFactor::shared_ptr inequality ConstrainedFactor::shared_ptr inequality
= boost::dynamic_pointer_cast<ConstrainedFactor>(factor); = boost::dynamic_pointer_cast<ConstrainedFactor>(factor);
@ -66,7 +66,7 @@ bool LinearConstraintSQP::checkConvergence(const LinearConstraintNLPState& state
/* ************************************************************************* */ /* ************************************************************************* */
VectorValues LinearConstraintSQP::initializeDuals() const { VectorValues LinearConstraintSQP::initializeDuals() const {
VectorValues duals; VectorValues duals;
BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, lcnlp_.linearEqualities){ for(const NonlinearFactor::shared_ptr& factor: lcnlp_.linearEqualities){
ConstrainedFactor::shared_ptr constraint ConstrainedFactor::shared_ptr constraint
= boost::dynamic_pointer_cast<ConstrainedFactor>(factor); = boost::dynamic_pointer_cast<ConstrainedFactor>(factor);
duals.insert(constraint->dualKey(), Vector::Zero(factor->dim())); duals.insert(constraint->dualKey(), Vector::Zero(factor->dim()));
@ -92,7 +92,7 @@ LinearConstraintNLPState LinearConstraintSQP::iterate(
VectorValues delta, duals; VectorValues delta, duals;
QPSolver qpSolver(qp); QPSolver qpSolver(qp);
VectorValues zeroInitialValues; VectorValues zeroInitialValues;
BOOST_FOREACH(const Values::ConstKeyValuePair& key_value, state.values) for(const Values::ConstKeyValuePair& key_value: state.values)
zeroInitialValues.insert(key_value.key, Vector::Zero(key_value.value.dim())); zeroInitialValues.insert(key_value.key, Vector::Zero(key_value.value.dim()));
boost::tie(delta, duals) = qpSolver.optimize(zeroInitialValues, state.duals, boost::tie(delta, duals) = qpSolver.optimize(zeroInitialValues, state.duals,

View File

@ -27,7 +27,7 @@ EqualityFactorGraph::shared_ptr LinearEqualityFactorGraph::linearize(
const Values& linearizationPoint) const { const Values& linearizationPoint) const {
EqualityFactorGraph::shared_ptr linearGraph( EqualityFactorGraph::shared_ptr linearGraph(
new EqualityFactorGraph()); new EqualityFactorGraph());
BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, *this){ for(const NonlinearFactor::shared_ptr& factor: *this){
JacobianFactor::shared_ptr jacobian = boost::dynamic_pointer_cast<JacobianFactor>( JacobianFactor::shared_ptr jacobian = boost::dynamic_pointer_cast<JacobianFactor>(
factor->linearize(linearizationPoint)); factor->linearize(linearizationPoint));
ConstrainedFactor::shared_ptr constraint = boost::dynamic_pointer_cast<ConstrainedFactor>(factor); ConstrainedFactor::shared_ptr constraint = boost::dynamic_pointer_cast<ConstrainedFactor>(factor);
@ -38,7 +38,7 @@ EqualityFactorGraph::shared_ptr LinearEqualityFactorGraph::linearize(
/* ************************************************************************* */ /* ************************************************************************* */
bool LinearEqualityFactorGraph::checkFeasibility(const Values& values, double tol) const { bool LinearEqualityFactorGraph::checkFeasibility(const Values& values, double tol) const {
BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, *this){ for(const NonlinearFactor::shared_ptr& factor: *this){
NoiseModelFactor::shared_ptr noiseModelFactor NoiseModelFactor::shared_ptr noiseModelFactor
= boost::dynamic_pointer_cast<NoiseModelFactor>(factor); = boost::dynamic_pointer_cast<NoiseModelFactor>(factor);
Vector error = noiseModelFactor->unwhitenedError(values); Vector error = noiseModelFactor->unwhitenedError(values);

View File

@ -26,7 +26,7 @@ namespace gtsam {
InequalityFactorGraph::shared_ptr LinearInequalityFactorGraph::linearize( InequalityFactorGraph::shared_ptr LinearInequalityFactorGraph::linearize(
const Values& linearizationPoint) const { const Values& linearizationPoint) const {
InequalityFactorGraph::shared_ptr linearGraph(new InequalityFactorGraph()); InequalityFactorGraph::shared_ptr linearGraph(new InequalityFactorGraph());
BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, *this) { for(const NonlinearFactor::shared_ptr& factor: *this) {
JacobianFactor::shared_ptr jacobian = boost::dynamic_pointer_cast<JacobianFactor>( JacobianFactor::shared_ptr jacobian = boost::dynamic_pointer_cast<JacobianFactor>(
factor->linearize(linearizationPoint)); factor->linearize(linearizationPoint));
ConstrainedFactor::shared_ptr constraint ConstrainedFactor::shared_ptr constraint
@ -40,7 +40,7 @@ InequalityFactorGraph::shared_ptr LinearInequalityFactorGraph::linearize(
bool LinearInequalityFactorGraph::checkFeasibilityAndComplimentary( bool LinearInequalityFactorGraph::checkFeasibilityAndComplimentary(
const Values& values, const VectorValues& dualValues, double tol) const { const Values& values, const VectorValues& dualValues, double tol) const {
BOOST_FOREACH(const NonlinearFactor::shared_ptr& factor, *this) { for(const NonlinearFactor::shared_ptr& factor: *this) {
NoiseModelFactor::shared_ptr noiseModelFactor NoiseModelFactor::shared_ptr noiseModelFactor
= boost::dynamic_pointer_cast<NoiseModelFactor>(factor); = boost::dynamic_pointer_cast<NoiseModelFactor>(factor);
Vector error = noiseModelFactor->unwhitenedError(values); Vector error = noiseModelFactor->unwhitenedError(values);