@@ -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. */
120101inline 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. */
127108inline 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 */
139152vector_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 */
147176size_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 {
171210LagrangianMetrics 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
0 commit comments