Skip to content

Commit 0725c23

Browse files
Merged in feature/constraint_array (pull request #646)
Feature/constraint array Approved-by: Jean-Pierre Sleiman Approved-by: Sotaro Katayama
2 parents e3ce64c + 348ded2 commit 0725c23

File tree

62 files changed

+2434
-922
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2434
-922
lines changed

ocs2_core/include/ocs2_core/constraint/StateConstraintCollection.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class StateConstraintCollection : public Collection<StateConstraint> {
5252
/** Returns the number of active constraints at given time. */
5353
virtual size_t getNumConstraints(scalar_t time) const;
5454

55-
/** Get the constraint vector value */
56-
virtual vector_t getValue(scalar_t time, const vector_t& state, const PreComputation& preComp) const;
55+
/** Get an array of all constraints */
56+
virtual vector_array_t getValue(scalar_t time, const vector_t& state, const PreComputation& preComp) const;
5757

5858
/** Get the constraint linear approximation */
5959
virtual VectorFunctionLinearApproximation getLinearApproximation(scalar_t time, const vector_t& state,
@@ -68,4 +68,4 @@ class StateConstraintCollection : public Collection<StateConstraint> {
6868
StateConstraintCollection(const StateConstraintCollection& other);
6969
};
7070

71-
} // namespace ocs2
71+
} // namespace ocs2

ocs2_core/include/ocs2_core/constraint/StateInputConstraintCollection.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class StateInputConstraintCollection : public Collection<StateInputConstraint> {
5252
/** Returns the number of active constraints at given time. */
5353
virtual size_t getNumConstraints(scalar_t time) const;
5454

55-
/** Get the constraint vector value */
56-
virtual vector_t getValue(scalar_t time, const vector_t& state, const vector_t& input, const PreComputation& preComp) const;
55+
/** Get an array of all constraints */
56+
virtual vector_array_t getValue(scalar_t time, const vector_t& state, const vector_t& input, const PreComputation& preComp) const;
5757

5858
/** Get the constraint linear approximation */
5959
virtual VectorFunctionLinearApproximation getLinearApproximation(scalar_t time, const vector_t& state, const vector_t& input,
@@ -68,4 +68,4 @@ class StateInputConstraintCollection : public Collection<StateInputConstraint> {
6868
StateInputConstraintCollection(const StateInputConstraintCollection& other);
6969
};
7070

71-
} // namespace ocs2
71+
} // namespace ocs2

ocs2_core/include/ocs2_core/integration/TrapezoidalIntegration.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,32 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

3030
#pragma once
3131

32+
#include <cassert>
3233
#include <vector>
3334

3435
namespace ocs2 {
3536

36-
template <typename SCALAR_T>
37-
SCALAR_T trapezoidalIntegration(const std::vector<SCALAR_T>& timeTrajectory, const std::vector<SCALAR_T>& valueTrajectory) {
37+
/**
38+
* Compute the trapezoidal integration of a trajectory of VALUE_T given the time stamp timeTrajectory and initial value initialValue.
39+
*
40+
* @note It requires that the VALUE_T has overwrite operator+(VALUE_T, VALUE_T) and define VALUE_T::operator*(SCALAR_T)
41+
*/
42+
template <typename SCALAR_T, typename VALUE_T>
43+
VALUE_T trapezoidalIntegration(const std::vector<SCALAR_T>& timeTrajectory, const std::vector<VALUE_T>& valueTrajectory,
44+
VALUE_T initialValue) {
45+
assert(timeTrajectory.size() == valueTrajectory.size());
46+
3847
if (timeTrajectory.size() < 2) {
39-
return 0.0;
48+
return initialValue;
4049
}
4150

42-
SCALAR_T areaUnderCurve = 0.0;
4351
for (size_t k = 1; k < timeTrajectory.size(); k++) {
44-
areaUnderCurve += 0.5 * (valueTrajectory[k] + valueTrajectory[k - 1]) * (timeTrajectory[k] - timeTrajectory[k - 1]);
52+
auto temp = valueTrajectory[k - 1] + valueTrajectory[k];
53+
temp *= (0.5 * (timeTrajectory[k] - timeTrajectory[k - 1]));
54+
initialValue += temp;
4555
} // end of k loop
4656

47-
return areaUnderCurve;
57+
return initialValue;
4858
}
4959

5060
} // namespace ocs2

ocs2_core/include/ocs2_core/loopshaping/constraint/LoopshapingStateConstraint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class LoopshapingStateConstraint final : public StateConstraintCollection {
4747

4848
LoopshapingStateConstraint* clone() const override { return new LoopshapingStateConstraint(*this); }
4949

50-
vector_t getValue(scalar_t time, const vector_t& state, const PreComputation& preComp) const override;
50+
vector_array_t getValue(scalar_t time, const vector_t& state, const PreComputation& preComp) const override;
5151
VectorFunctionLinearApproximation getLinearApproximation(scalar_t time, const vector_t& state,
5252
const PreComputation& preComp) const override;
5353
VectorFunctionQuadraticApproximation getQuadraticApproximation(scalar_t time, const vector_t& state,

ocs2_core/include/ocs2_core/loopshaping/constraint/LoopshapingStateInputConstraint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class LoopshapingStateInputConstraint : public StateInputConstraintCollection {
4141
public:
4242
~LoopshapingStateInputConstraint() override = default;
4343

44-
vector_t getValue(scalar_t time, const vector_t& state, const vector_t& input, const PreComputation& preComp) const override;
44+
vector_array_t getValue(scalar_t time, const vector_t& state, const vector_t& input, const PreComputation& preComp) const override;
4545

4646
protected:
4747
LoopshapingStateInputConstraint(const StateInputConstraintCollection& systemConstraint,

ocs2_core/include/ocs2_core/model_data/Metrics.h

Lines changed: 91 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -53,83 +53,96 @@ struct LagrangianMetricsConstRef {
5353
};
5454

5555
/**
56-
* The collection of cost, equality constraints, and LagrangianMetrics structure for all possible constraint terms (handled by
57-
* Lagrangian method) in a particular time point.
58-
* cost : The total cost in a particular time point.
59-
* stateEqConstraint : A vector of all active state equality constraints.
60-
* stateInputEqConstraint : A vector of all active state-input equality constraints.
61-
* stateEqLagrangian : An array of state equality constraint terms handled by Lagrangian method.
62-
* stateIneqLagrangian : An array of state inequality constraint terms handled by Lagrangian method.
63-
* stateInputEqLagrangian : An array of state-input equality constraint terms handled by Lagrangian method.
64-
* stateInputIneqLagrangian : An array of state-input inequality constraint terms handled by Lagrangian method.
56+
* The collection of cost, dynamics violation, constraints, and LagrangianMetrics structure for all possible constraint
57+
* terms (handled by Lagrangian method) in a point of time.
58+
* cost : The total cost in a particular time point.
59+
* dynamicsViolation : The vector of dynamics violation.
60+
* stateEqConstraint : An array of all state equality constraints.
61+
* stateInputEqConstraint : An array of all state-input equality constraints.
62+
* stateIneqConstraint : An array of all state inequality constraints.
63+
* stateInputIneqConstraint : An array of all state-input inequality constraints.
64+
* stateEqLagrangian : An array of state equality constraint terms handled by Lagrangian method.
65+
* stateIneqLagrangian : An array of state inequality constraint terms handled by Lagrangian method.
66+
* stateInputEqLagrangian : An array of state-input equality constraint terms handled by Lagrangian method.
67+
* stateInputIneqLagrangian : An array of state-input inequality constraint terms handled by Lagrangian method.
6568
*/
66-
struct MetricsCollection {
69+
struct Metrics {
6770
// Cost
6871
scalar_t cost;
6972

73+
// Dynamics violation
74+
vector_t dynamicsViolation;
75+
7076
// Equality constraints
71-
vector_t stateEqConstraint;
72-
vector_t stateInputEqConstraint;
77+
vector_array_t stateEqConstraint;
78+
vector_array_t stateInputEqConstraint;
7379

7480
// Inequality constraints
75-
vector_t stateIneqConstraint;
76-
vector_t stateInputIneqConstraint;
81+
vector_array_t stateIneqConstraint;
82+
vector_array_t stateInputIneqConstraint;
7783

7884
// Lagrangians
7985
std::vector<LagrangianMetrics> stateEqLagrangian;
8086
std::vector<LagrangianMetrics> stateIneqLagrangian;
8187
std::vector<LagrangianMetrics> stateInputEqLagrangian;
8288
std::vector<LagrangianMetrics> stateInputIneqLagrangian;
8389

84-
/** Exchanges the values of MetricsCollection */
85-
void swap(MetricsCollection& other) {
86-
// Cost
87-
std::swap(cost, other.cost);
88-
// Equality constraints
89-
stateEqConstraint.swap(other.stateEqConstraint);
90-
stateInputEqConstraint.swap(other.stateInputEqConstraint);
91-
// Inequality constraints
92-
stateIneqConstraint.swap(other.stateIneqConstraint);
93-
stateInputIneqConstraint.swap(other.stateInputIneqConstraint);
94-
// Lagrangians
95-
stateEqLagrangian.swap(other.stateEqLagrangian);
96-
stateIneqLagrangian.swap(other.stateIneqLagrangian);
97-
stateInputEqLagrangian.swap(other.stateInputEqLagrangian);
98-
stateInputIneqLagrangian.swap(other.stateInputIneqLagrangian);
99-
}
90+
/** Exchanges the values of Metrics */
91+
void swap(Metrics& other);
10092

101-
/** Clears the value of the MetricsCollection */
102-
void clear() {
103-
// Cost
104-
cost = 0.0;
105-
// Equality constraints
106-
stateEqConstraint = vector_t();
107-
stateInputEqConstraint = vector_t();
108-
// Inequality constraints
109-
stateIneqConstraint = vector_t();
110-
stateInputIneqConstraint = vector_t();
111-
// Lagrangians
112-
stateEqLagrangian.clear();
113-
stateIneqLagrangian.clear();
114-
stateInputEqLagrangian.clear();
115-
stateInputIneqLagrangian.clear();
116-
}
93+
/** Clears the value of the Metrics */
94+
void clear();
95+
96+
/** Returns true if *this is approximately equal to other, within the precision determined by prec. */
97+
bool isApprox(const Metrics& other, scalar_t prec = 1e-8) const;
11798
};
11899

119-
/** Sums penalties of an array of LagrangianMetrics */
100+
/** Sums penalties of an array of LagrangianMetrics. */
120101
inline scalar_t sumPenalties(const std::vector<LagrangianMetrics>& metricsArray) {
121102
scalar_t s = 0.0;
122103
std::for_each(metricsArray.begin(), metricsArray.end(), [&s](const LagrangianMetrics& m) { s += m.penalty; });
123104
return s;
124105
}
125106

126-
/** Computes the sum of squared norm of constraints of an array of LagrangianMetrics */
107+
/** Computes the sum of squared norm of constraints of an array of LagrangianMetrics. */
127108
inline scalar_t constraintsSquaredNorm(const std::vector<LagrangianMetrics>& metricsArray) {
128109
scalar_t s = 0.0;
129110
std::for_each(metricsArray.begin(), metricsArray.end(), [&s](const LagrangianMetrics& m) { s += m.constraint.squaredNorm(); });
130111
return s;
131112
}
132113

114+
/** Computes the sum of squared norm of a vector of equality constraints violation. */
115+
inline scalar_t getEqConstraintsSSE(const vector_t& eqConstraint) {
116+
if (eqConstraint.size() == 0) {
117+
return 0.0;
118+
} else {
119+
return eqConstraint.squaredNorm();
120+
}
121+
}
122+
123+
/** Computes the sum of squared norm of a vector of inequality constraints violation. */
124+
inline scalar_t getIneqConstraintsSSE(const vector_t& ineqConstraint) {
125+
if (ineqConstraint.size() == 0) {
126+
return 0.0;
127+
} else {
128+
return ineqConstraint.cwiseMin(0.0).squaredNorm();
129+
}
130+
}
131+
132+
/** Computes the sum of squared norm of an array of equality constraints violation. */
133+
inline scalar_t getEqConstraintsSSE(const vector_array_t& eqConstraint) {
134+
scalar_t s = 0.0;
135+
std::for_each(eqConstraint.begin(), eqConstraint.end(), [&s](const vector_t& v) { s += getEqConstraintsSSE(v); });
136+
return s;
137+
}
138+
139+
/** Computes the sum of squared norm of an array of inequality constraints violation. */
140+
inline scalar_t getIneqConstraintsSSE(const vector_array_t& ineqConstraint) {
141+
scalar_t s = 0.0;
142+
std::for_each(ineqConstraint.begin(), ineqConstraint.end(), [&s](const vector_t& v) { s += getIneqConstraintsSSE(v); });
143+
return s;
144+
}
145+
133146
/**
134147
* Serializes an array of LagrangianMetrics structures associated to an array of constraint terms.
135148
*
@@ -138,14 +151,40 @@ inline scalar_t constraintsSquaredNorm(const std::vector<LagrangianMetrics>& met
138151
*/
139152
vector_t toVector(const std::vector<LagrangianMetrics>& termsLagrangianMetrics);
140153

154+
/**
155+
* Serializes an array of constraint terms.
156+
*
157+
* @ param [in] constraintArray : An array of constraint terms.
158+
* @return Serialized vector of the format : (..., constraintArray[i], ...).
159+
*/
160+
vector_t toVector(const vector_array_t& constraintArray);
161+
141162
/**
142163
* Gets the size of constraint terms.
143164
*
165+
* @ param [in] constraintArray : An array of constraint terms.
166+
* @return An array of constraint terms size. It has the same size as the input array.
167+
*/
168+
size_array_t getSizes(const vector_array_t& constraintArray);
169+
170+
/**
171+
* Gets the size of constraint Lagrangian terms.
172+
*
144173
* @ param [in] termsLagrangianMetrics : LagrangianMetrics associated to an array of constraint terms.
145174
* @return An array of constraint terms size. It has the same size as the input array.
146175
*/
147176
size_array_t getSizes(const std::vector<LagrangianMetrics>& termsLagrangianMetrics);
148177

178+
/**
179+
* Deserializes the vector to an array of constraint terms.
180+
*
181+
* @param [in] termsSize : An array of constraint terms size. It as the same size as the output array.
182+
* @param [in] vec : Serialized array of constraint terms of the format :
183+
* (..., constraintArray[i], ...)
184+
* @return An array of constraint terms.
185+
*/
186+
vector_array_t toConstraintArray(const size_array_t& termsSize, const vector_t& vec);
187+
149188
/**
150189
* Deserializes the vector to an array of LagrangianMetrics structures based on size of constraint terms.
151190
*
@@ -171,13 +210,13 @@ namespace LinearInterpolation {
171210
LagrangianMetrics interpolate(const index_alpha_t& indexAlpha, const std::vector<LagrangianMetricsConstRef>& dataArray);
172211

173212
/**
174-
* Linearly interpolates a trajectory of MetricsCollection.
213+
* Linearly interpolates a trajectory of Metrics.
175214
*
176215
* @param [in] indexAlpha : index and interpolation coefficient (alpha) pair.
177-
* @param [in] dataArray : A trajectory of MetricsCollection.
178-
* @return The interpolated MetricsCollection at indexAlpha.
216+
* @param [in] dataArray : A trajectory of Metrics.
217+
* @return The interpolated Metrics at indexAlpha.
179218
*/
180-
MetricsCollection interpolate(const index_alpha_t& indexAlpha, const std::vector<MetricsCollection>& dataArray);
219+
Metrics interpolate(const index_alpha_t& indexAlpha, const std::vector<Metrics>& dataArray);
181220

182221
} // namespace LinearInterpolation
183222
} // namespace ocs2

ocs2_core/src/constraint/StateConstraintCollection.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,13 @@ size_t StateConstraintCollection::getNumConstraints(scalar_t time) const {
6262
/******************************************************************************************************/
6363
/******************************************************************************************************/
6464
/******************************************************************************************************/
65-
vector_t StateConstraintCollection::getValue(scalar_t time, const vector_t& state, const PreComputation& preComp) const {
66-
vector_t constraintValues;
67-
constraintValues.resize(getNumConstraints(time));
68-
69-
// append vectors of constraint values from each constraintTerm
70-
size_t i = 0;
71-
for (const auto& constraintTerm : this->terms_) {
72-
if (constraintTerm->isActive(time)) {
73-
const auto constraintTermValues = constraintTerm->getValue(time, state, preComp);
74-
constraintValues.segment(i, constraintTermValues.rows()) = constraintTermValues;
75-
i += constraintTermValues.rows();
65+
vector_array_t StateConstraintCollection::getValue(scalar_t time, const vector_t& state, const PreComputation& preComp) const {
66+
vector_array_t constraintValues(this->terms_.size());
67+
for (size_t i = 0; i < this->terms_.size(); ++i) {
68+
if (this->terms_[i]->isActive(time)) {
69+
constraintValues[i] = this->terms_[i]->getValue(time, state, preComp);
7670
}
77-
}
78-
71+
} // end of i loop
7972
return constraintValues;
8073
}
8174

ocs2_core/src/constraint/StateInputConstraintCollection.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,14 @@ size_t StateInputConstraintCollection::getNumConstraints(scalar_t time) const {
6363
/******************************************************************************************************/
6464
/******************************************************************************************************/
6565
/******************************************************************************************************/
66-
vector_t StateInputConstraintCollection::getValue(scalar_t time, const vector_t& state, const vector_t& input,
67-
const PreComputation& preComp) const {
68-
vector_t constraintValues;
69-
constraintValues.resize(getNumConstraints(time));
70-
71-
// append vectors of constraint values from each constraintTerm
72-
size_t i = 0;
73-
for (const auto& constraintTerm : this->terms_) {
74-
if (constraintTerm->isActive(time)) {
75-
const auto constraintTermValues = constraintTerm->getValue(time, state, input, preComp);
76-
constraintValues.segment(i, constraintTermValues.rows()) = constraintTermValues;
77-
i += constraintTermValues.rows();
66+
vector_array_t StateInputConstraintCollection::getValue(scalar_t time, const vector_t& state, const vector_t& input,
67+
const PreComputation& preComp) const {
68+
vector_array_t constraintValues(this->terms_.size());
69+
for (size_t i = 0; i < this->terms_.size(); ++i) {
70+
if (this->terms_[i]->isActive(time)) {
71+
constraintValues[i] = this->terms_[i]->getValue(time, state, input, preComp);
7872
}
79-
}
80-
73+
} // end of i loop
8174
return constraintValues;
8275
}
8376

ocs2_core/src/loopshaping/constraint/LoopshapingStateConstraint.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ namespace ocs2 {
3838
/******************************************************************************************************/
3939
/******************************************************************************************************/
4040
/******************************************************************************************************/
41-
vector_t LoopshapingStateConstraint::getValue(scalar_t t, const vector_t& x, const PreComputation& preComp) const {
41+
vector_array_t LoopshapingStateConstraint::getValue(scalar_t t, const vector_t& x, const PreComputation& preComp) const {
4242
if (this->empty()) {
43-
return vector_t::Zero(0);
43+
return vector_array_t();
4444
}
4545

4646
const LoopshapingPreComputation& preCompLS = cast<LoopshapingPreComputation>(preComp);

ocs2_core/src/loopshaping/constraint/LoopshapingStateInputConstraint.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ namespace ocs2 {
3838
/******************************************************************************************************/
3939
/******************************************************************************************************/
4040
/******************************************************************************************************/
41-
vector_t LoopshapingStateInputConstraint::getValue(scalar_t t, const vector_t& x, const vector_t& u, const PreComputation& preComp) const {
41+
vector_array_t LoopshapingStateInputConstraint::getValue(scalar_t t, const vector_t& x, const vector_t& u,
42+
const PreComputation& preComp) const {
4243
if (this->empty()) {
43-
return vector_t::Zero(0);
44+
return vector_array_t();
4445
}
4546

4647
const LoopshapingPreComputation& preCompLS = cast<LoopshapingPreComputation>(preComp);

0 commit comments

Comments
 (0)