gtsam  4.1.0
gtsam
LPSolver.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
23 
24 #include <limits>
25 #include <algorithm>
26 
27 namespace gtsam {
28 
30 struct LPPolicy {
33  static constexpr double maxAlpha = std::numeric_limits<double>::infinity();
34 
48  const VectorValues& xk) {
49  GaussianFactorGraph graph;
50  for (LinearCost::const_iterator it = lp.cost.begin(); it != lp.cost.end();
51  ++it) {
52  size_t dim = lp.cost.getDim(it);
53  Vector b = xk.at(*it) - lp.cost.getA(it).transpose(); // b = xk-g
54  graph.emplace_shared<JacobianFactor>(*it, Matrix::Identity(dim, dim), b);
55  }
56 
57  KeySet allKeys = lp.inequalities.keys();
58  allKeys.merge(lp.equalities.keys());
59  allKeys.merge(KeySet(lp.cost.keys()));
60  // Add corresponding factors for all variables that are not explicitly in
61  // the cost function. Gradients of the cost function wrt to these variables
62  // are zero (g=0), so b=xk
63  if (lp.cost.keys().size() != allKeys.size()) {
64  KeySet difference;
65  std::set_difference(allKeys.begin(), allKeys.end(), lp.cost.begin(),
66  lp.cost.end(),
67  std::inserter(difference, difference.end()));
68  for (Key k : difference) {
69  size_t dim = lp.constrainedKeyDimMap().at(k);
70  graph.emplace_shared<JacobianFactor>(k, Matrix::Identity(dim, dim), xk.at(k));
71  }
72  }
73  return graph;
74  }
75 };
76 
77 using LPSolver = ActiveSetSolver<LP, LPPolicy, LPInitSolver>;
78 
79 }
gtsam::GaussianFactorGraph
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition: GaussianFactorGraph.h:68
gtsam::LP::equalities
EqualityFactorGraph equalities
Linear equality constraints: cE(x) = 0.
Definition: LP.h:55
gtsam::LP
Data structure of a Linear Program.
Definition: LP.h:51
ActiveSetSolver.h
Active set method for solving LP, QP problems.
LPInitSolver.h
This finds a feasible solution for an LP problem.
gtsam::Factor::keys
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:118
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
gtsam::VectorValues
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:74
gtsam::JacobianFactor::getDim
DenseIndex getDim(const_iterator variable) const override
Return the dimension of the variable pointed to by the given key iterator todo: Remove this in favor ...
Definition: JacobianFactor.h:274
gtsam::Factor::begin
const_iterator begin() const
Iterator at beginning of involved variable keys.
Definition: Factor.h:121
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
gtsam::FastSet::merge
void merge(const FastSet &other)
insert another set: handy for MATLAB access
Definition: FastSet.h:121
gtsam::JacobianFactor
A Gaussian factor in the squared-error form.
Definition: JacobianFactor.h:91
gtsam::LPPolicy::buildCostFunction
static GaussianFactorGraph buildCostFunction(const LP &lp, const VectorValues &xk)
Create the factor ||x-xk - (-g)||^2 where xk is the current feasible solution on the constraint surfa...
Definition: LPSolver.h:47
gtsam::FastSet< Key >
LP.h
Struct used to hold a Linear Programming Problem.
gtsam::LPPolicy::maxAlpha
static constexpr double maxAlpha
Maximum alpha for line search x'=xk + alpha*p, where p is the cost gradient For LP,...
Definition: LPSolver.h:33
gtsam::Factor::const_iterator
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition: Factor.h:67
gtsam::LPPolicy
Policy for ActivetSetSolver to solve Linear Programming.
Definition: LPSolver.h:30
gtsam::FactorGraph::keys
KeySet keys() const
Potentially slow function to return all keys involved, sorted, as a set.
Definition: FactorGraph-inst.h:76
gtsam::LP::cost
LinearCost cost
Linear cost factor.
Definition: LP.h:54
gtsam::Factor::end
const_iterator end() const
Iterator at end of involved variable keys.
Definition: Factor.h:124
gtsam::JacobianFactor::getA
constABlock getA(const_iterator variable) const
Get a view of the A matrix for the variable pointed to by the given key iterator.
Definition: JacobianFactor.h:298
gtsam::LP::inequalities
InequalityFactorGraph inequalities
Linear inequality constraints: cI(x) <= 0.
Definition: LP.h:56
gtsam::VectorValues::at
Vector & at(Key j)
Read/write access to the vector value with key j, throws std::out_of_range if j does not exist,...
Definition: VectorValues.h:137