gtsam
Loading...
Searching...
No Matches
LP.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
18
19#pragma once
20
21#include <gtsam/config.h>
22
23#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
24
28
29#include <string>
30
31namespace gtsam {
32
33using namespace std;
34
36using KeyDimMap = std::map<Key, uint32_t>;
37/*
38 * Iterates through every factor in a linear graph and generates a
39 * mapping between every factor key and it's corresponding dimensionality.
40 */
41template <class LinearGraph>
42KeyDimMap collectKeyDim(const LinearGraph& linearGraph) {
43 KeyDimMap keyDimMap;
44 for (const typename LinearGraph::sharedFactor& factor : linearGraph) {
45 if (!factor) continue;
46 for (Key key : factor->keys())
47 keyDimMap[key] = factor->getDim(factor->find(key));
48 }
49 return keyDimMap;
50}
51
55struct LP {
56 using shared_ptr = std::shared_ptr<LP>;
57
58 LinearCost cost;
59 EqualityFactorGraph equalities;
60 InequalityFactorGraph inequalities;
61private:
62 mutable KeyDimMap cachedConstrainedKeyDimMap_;
63
64public:
66 bool isFeasible(const VectorValues& x) const {
67 return (equalities.error(x) == 0 && inequalities.error(x) == 0);
68 }
69
71 void print(const string& s = "") const {
72 std::cout << s << std::endl;
73 cost.print("Linear cost: ");
74 equalities.print("Linear equality factors: ");
75 inequalities.print("Linear inequality factors: ");
76 }
77
79 bool equals(const LP& other, double tol = 1e-9) const {
80 return cost.equals(other.cost) && equalities.equals(other.equalities)
81 && inequalities.equals(other.inequalities);
82 }
83
84 const KeyDimMap& constrainedKeyDimMap() const {
85 if (!cachedConstrainedKeyDimMap_.empty())
86 return cachedConstrainedKeyDimMap_;
87 // Collect key-dim map of all variables in the constraints
88 cachedConstrainedKeyDimMap_ = collectKeyDim(equalities);
89 KeyDimMap keysDim2 = collectKeyDim(inequalities);
90 cachedConstrainedKeyDimMap_.insert(keysDim2.begin(), keysDim2.end());
91 return cachedConstrainedKeyDimMap_;
92 }
93
94 Vector costGradient(Key key, const VectorValues& delta) const {
95 Vector g = Vector::Zero(delta.at(key).size());
96 Factor::const_iterator it = cost.find(key);
97 if (it != cost.end()) g = cost.getA(it).transpose();
98 return g;
99 }
100};
101
103template<> struct traits<LP> : public Testable<LP> {
104};
105
106}
107
108#endif // GTSAM_ALLOW_DEPRECATED_SINCE_V43
Factor graph of all LinearInequality factors.
Factor graph of all LinearEquality factors.
LinearCost derived from JacobianFactor to support linear cost functions c'x.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
std::map< Key, size_t > KeyDimMap
Map from variable key to dimension.
Definition MultifrontalClique.h:51
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152