1#ifndef ROBOTOC_CONSTRAINTS_IMPL_HXX_
2#define ROBOTOC_CONSTRAINTS_IMPL_HXX_
10namespace constraintsimpl {
12template <
typename Constra
intComponentBaseTypePtr>
13inline void clear(std::vector<ConstraintComponentBaseTypePtr>& constraints) {
18template <
typename Constra
intComponentBaseTypePtr>
20 const std::vector<ConstraintComponentBaseTypePtr>& constraints,
21 std::vector<ConstraintComponentData>& data) {
23 for (
const auto& constraint : constraints) {
25 constraint->getBarrierParam());
26 constraint->allocateExtraData(component_data);
27 data.push_back(component_data);
32template <
typename Constra
intComponentBaseTypePtr,
typename ContactStatusType>
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);
50template <
typename Constra
intComponentBaseTypePtr,
typename ContactStatusType>
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]);
65template <
typename Constra
intComponentBaseTypePtr,
typename ContactStatusType>
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);
81template <
typename Constra
intComponentBaseTypePtr,
typename ContactStatusType>
83 const std::vector<ConstraintComponentBaseTypePtr>& constraints,
84 Robot& robot,
const ContactStatusType& contact_status,
85 std::vector<ConstraintComponentData>& data,
const SplitSolution& s,
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,
100template <
typename Constra
intComponentBaseTypePtr,
typename ContactStatusType>
102 const std::vector<ConstraintComponentBaseTypePtr>& constraints,
103 const ContactStatusType& contact_status,
104 std::vector<ConstraintComponentData>& data,
SplitKKTMatrix& kkt_matrix,
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,
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);
131template <
typename Constra
intComponentBaseTypePtr>
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;
145 return min_step_size;
149template <
typename Constra
intComponentBaseTypePtr>
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;
163 return min_step_size;
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;
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;
185template <
typename Constra
intComponentBaseTypePtr>
187 const double barrier_param) {
188 for (
auto& constraint : constraints) {
189 constraint->setBarrierParam(barrier_param);
194template <
typename Constra
intComponentBaseTypePtr>
196 std::vector<ConstraintComponentBaseTypePtr>& constraints,
197 const double fraction_to_boundary_rule) {
198 for (
auto& constraint : constraints) {
199 constraint->setFractionToBoundaryRule(fraction_to_boundary_rule);
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