robotoc
robotoc - efficient ROBOT Optimal Control solvers
Loading...
Searching...
No Matches
constraints_impl.hxx
Go to the documentation of this file.
1#ifndef ROBOTOC_CONSTRAINTS_IMPL_HXX_
2#define ROBOTOC_CONSTRAINTS_IMPL_HXX_
3
5
6#include <cassert>
7#include <cmath>
8
9namespace robotoc {
10namespace constraintsimpl {
11
12template <typename ConstraintComponentBaseTypePtr>
13inline void clear(std::vector<ConstraintComponentBaseTypePtr>& constraints) {
14 constraints.clear();
15}
16
17
18template <typename ConstraintComponentBaseTypePtr>
20 const std::vector<ConstraintComponentBaseTypePtr>& constraints,
21 std::vector<ConstraintComponentData>& data) {
22 data.clear();
23 for (const auto& constraint : constraints) {
24 auto component_data = ConstraintComponentData(constraint->dimc(),
25 constraint->getBarrierParam());
26 constraint->allocateExtraData(component_data);
27 data.push_back(component_data);
28 }
29}
30
31
32template <typename ConstraintComponentBaseTypePtr, typename ContactStatusType>
33inline bool isFeasible(
34 const std::vector<ConstraintComponentBaseTypePtr>& constraints,
35 Robot& robot, const ContactStatusType& contact_status,
36 std::vector<ConstraintComponentData>& data, const SplitSolution& s) {
37 assert(constraints.size() == data.size());
38 for (int i=0; i<constraints.size(); ++i) {
39 assert(data[i].dimc() == constraints[i]->dimc());
40 assert(data[i].checkDimensionalConsistency());
41 bool feasible = constraints[i]->isFeasible(robot, contact_status, data[i], s);
42 if (!feasible) {
43 return false;
44 }
45 }
46 return true;
47}
48
49
50template <typename ConstraintComponentBaseTypePtr, typename ContactStatusType>
51inline void setSlackAndDual(
52 const std::vector<ConstraintComponentBaseTypePtr>& constraints,
53 Robot& robot, const ContactStatusType& contact_status,
54 std::vector<ConstraintComponentData>& data, const SplitSolution& s) {
55 assert(constraints.size() == data.size());
56 for (int i=0; i<constraints.size(); ++i) {
57 assert(data[i].dimc() == constraints[i]->dimc());
58 assert(data[i].checkDimensionalConsistency());
59 constraints[i]->setSlack(robot, contact_status, data[i], s);
60 constraints[i]->setSlackAndDualPositive(data[i]);
61 }
62}
63
64
65template <typename ConstraintComponentBaseTypePtr, typename ContactStatusType>
66inline void evalConstraint(
67 const std::vector<ConstraintComponentBaseTypePtr>& constraints,
68 Robot& robot, const ContactStatusType& contact_status,
69 std::vector<ConstraintComponentData>& data, const SplitSolution& s) {
70 assert(constraints.size() == data.size());
71 for (int i=0; i<constraints.size(); ++i) {
72 assert(data[i].dimc() == constraints[i]->dimc());
73 assert(data[i].checkDimensionalConsistency());
74 data[i].residual.setZero();
75 data[i].cmpl.setZero();
76 constraints[i]->evalConstraint(robot, contact_status, data[i], s);
77 }
78}
79
80
81template <typename ConstraintComponentBaseTypePtr, typename ContactStatusType>
83 const std::vector<ConstraintComponentBaseTypePtr>& constraints,
84 Robot& robot, const ContactStatusType& contact_status,
85 std::vector<ConstraintComponentData>& data, const SplitSolution& s,
86 SplitKKTResidual& kkt_residual) {
87 assert(constraints.size() == data.size());
88 for (int i=0; i<constraints.size(); ++i) {
89 assert(data[i].dimc() == constraints[i]->dimc());
90 assert(data[i].checkDimensionalConsistency());
91 data[i].residual.setZero();
92 data[i].cmpl.setZero();
93 constraints[i]->evalConstraint(robot, contact_status, data[i], s);
94 constraints[i]->evalDerivatives(robot, contact_status, data[i], s,
95 kkt_residual);
96 }
97}
98
99
100template <typename ConstraintComponentBaseTypePtr, typename ContactStatusType>
102 const std::vector<ConstraintComponentBaseTypePtr>& constraints,
103 const ContactStatusType& contact_status,
104 std::vector<ConstraintComponentData>& data, SplitKKTMatrix& kkt_matrix,
105 SplitKKTResidual& kkt_residual) {
106 assert(constraints.size() == data.size());
107 for (int i=0; i<constraints.size(); ++i) {
108 assert(data[i].dimc() == constraints[i]->dimc());
109 assert(data[i].checkDimensionalConsistency());
110 constraints[i]->condenseSlackAndDual(contact_status, data[i], kkt_matrix,
111 kkt_residual);
112 }
113}
114
115
116template <typename ConstraintComponentBaseTypePtr, typename ContactStatusType,
117 typename SplitDirectionType>
119 const std::vector<ConstraintComponentBaseTypePtr>& constraints,
120 const ContactStatusType& contact_status,
121 std::vector<ConstraintComponentData>& data, const SplitDirectionType& d) {
122 assert(constraints.size() == data.size());
123 for (int i=0; i<constraints.size(); ++i) {
124 assert(data[i].dimc() == constraints[i]->dimc());
125 assert(data[i].checkDimensionalConsistency());
126 constraints[i]->expandSlackAndDual(contact_status, data[i], d);
127 }
128}
129
130
131template <typename ConstraintComponentBaseTypePtr>
132inline double maxSlackStepSize(
133 const std::vector<ConstraintComponentBaseTypePtr>& constraints,
134 const std::vector<ConstraintComponentData>& data) {
135 assert(constraints.size() == data.size());
136 double min_step_size = 1;
137 for (int i=0; i<constraints.size(); ++i) {
138 assert(data[i].dimc() == constraints[i]->dimc());
139 assert(data[i].checkDimensionalConsistency());
140 const double step_size = constraints[i]->maxSlackStepSize(data[i]);
141 if (step_size < min_step_size) {
142 min_step_size = step_size;
143 }
144 }
145 return min_step_size;
146}
147
148
149template <typename ConstraintComponentBaseTypePtr>
150inline double maxDualStepSize(
151 const std::vector<ConstraintComponentBaseTypePtr>& constraints,
152 const std::vector<ConstraintComponentData>& data) {
153 assert(constraints.size() == data.size());
154 double min_step_size = 1;
155 for (int i=0; i<constraints.size(); ++i) {
156 assert(data[i].dimc() == constraints[i]->dimc());
157 assert(data[i].checkDimensionalConsistency());
158 const double step_size = constraints[i]->maxDualStepSize(data[i]);
159 if (step_size < min_step_size) {
160 min_step_size = step_size;
161 }
162 }
163 return min_step_size;
164}
165
166
167inline void updateSlack(std::vector<ConstraintComponentData>& data,
168 const double step_size) {
169 assert(step_size > 0);
170 for (auto& e : data) {
171 e.slack.noalias() += step_size * e.dslack;
172 }
173}
174
175
176inline void updateDual(std::vector<ConstraintComponentData>& data,
177 const double step_size) {
178 assert(step_size > 0);
179 for (auto& e : data) {
180 e.dual.noalias() += step_size * e.ddual;
181 }
182}
183
184
185template <typename ConstraintComponentBaseTypePtr>
186inline void setBarrierParam(std::vector<ConstraintComponentBaseTypePtr>& constraints,
187 const double barrier_param) {
188 for (auto& constraint : constraints) {
189 constraint->setBarrierParam(barrier_param);
190 }
191}
192
193
194template <typename ConstraintComponentBaseTypePtr>
196 std::vector<ConstraintComponentBaseTypePtr>& constraints,
197 const double fraction_to_boundary_rule) {
198 for (auto& constraint : constraints) {
199 constraint->setFractionToBoundaryRule(fraction_to_boundary_rule);
200 }
201}
202
203} // namespace constraintsimpl
204} // namespace robotoc
205
206#endif // ROBOTOC_CONSTRAINTS_IMPL_HXX_
Data used in constraint components. Composed by slack, dual (Lagrange multiplier),...
Definition: constraint_component_data.hpp:17
Dynamics and kinematics model of robots. Wraps pinocchio::Model and pinocchio::Data....
Definition: robot.hpp:32
The KKT matrix split into a time stage.
Definition: split_kkt_matrix.hpp:18
KKT residual split into each time stage.
Definition: split_kkt_residual.hpp:18
Solution to the optimal control problem split into a time stage.
Definition: split_solution.hpp:20
void expandSlackAndDual(const std::vector< ConstraintComponentBaseTypePtr > &constraints, const ContactStatusType &contact_status, std::vector< ConstraintComponentData > &data, const SplitDirectionType &d)
Expands the slack and dual, i.e., computes the directions of the slack and dual variables from the di...
Definition: constraints_impl.hxx:118
void setBarrierParam(std::vector< ConstraintComponentBaseTypePtr > &constraints, const double barrier_param)
Sets the barrier parameter.
Definition: constraints_impl.hxx:186
void condenseSlackAndDual(const std::vector< ConstraintComponentBaseTypePtr > &constraints, const ContactStatusType &contact_status, std::vector< ConstraintComponentData > &data, SplitKKTMatrix &kkt_matrix, SplitKKTResidual &kkt_residual)
Condenses the slack and dual variables. linearizeConstraints() must be called before this function.
Definition: constraints_impl.hxx:101
double maxSlackStepSize(const std::vector< ConstraintComponentBaseTypePtr > &constraints, const std::vector< ConstraintComponentData > &data)
Computes and returns the maximum step size by applying fraction-to-boundary-rule to the direction of ...
Definition: constraints_impl.hxx:132
void setFractionToBoundaryRule(std::vector< ConstraintComponentBaseTypePtr > &constraints, const double fraction_to_boundary_rule)
Sets the parameter of the fraction-to-boundary-rule.
Definition: constraints_impl.hxx:195
void createConstraintsData(const std::vector< ConstraintComponentBaseTypePtr > &constraints, std::vector< ConstraintComponentData > &data)
Creates constraints data.
Definition: constraints_impl.hxx:19
void evalConstraint(const std::vector< ConstraintComponentBaseTypePtr > &constraints, Robot &robot, const ContactStatusType &contact_status, std::vector< ConstraintComponentData > &data, const SplitSolution &s)
Computes the primal residual, residual in the complementary slackness, and the log-barrier_param func...
Definition: constraints_impl.hxx:66
void updateSlack(std::vector< ConstraintComponentData > &data, const double step_size)
Updates the slack variables according to the step size.
Definition: constraints_impl.hxx:167
double maxDualStepSize(const std::vector< ConstraintComponentBaseTypePtr > &constraints, const std::vector< ConstraintComponentData > &data)
Computes and returns the maximum step size by applying fraction-to-boundary-rule to the direction of ...
Definition: constraints_impl.hxx:150
bool isFeasible(const std::vector< ConstraintComponentBaseTypePtr > &constraints, Robot &robot, const ContactStatusType &contact_status, std::vector< ConstraintComponentData > &data, const SplitSolution &s)
Checks whether the current solution s is feasible or not.
Definition: constraints_impl.hxx:33
void clear(std::vector< ConstraintComponentBaseTypePtr > &constraints)
Clears the vector of the constraints.
Definition: constraints_impl.hxx:13
void linearizeConstraints(const std::vector< ConstraintComponentBaseTypePtr > &constraints, Robot &robot, const ContactStatusType &contact_status, std::vector< ConstraintComponentData > &data, const SplitSolution &s, SplitKKTResidual &kkt_residual)
Evaluates the constraints (i.e., calls evalConstraint()) and adds the products of the Jacobian of the...
Definition: constraints_impl.hxx:82
void setSlackAndDual(const std::vector< ConstraintComponentBaseTypePtr > &constraints, Robot &robot, const ContactStatusType &contact_status, std::vector< ConstraintComponentData > &data, const SplitSolution &s)
Sets the slack and dual variables of each constraint components.
Definition: constraints_impl.hxx:51
void updateDual(std::vector< ConstraintComponentData > &data, const double step_size)
Updates the dual variables according to the step size.
Definition: constraints_impl.hxx:176
Definition: constraint_component_base.hpp:17