robotoc
robotoc - efficient ROBOT Optimal Control solvers
Loading...
Searching...
No Matches
pdipm.hxx
Go to the documentation of this file.
1#ifndef ROBOTOC_CONSTRAINTS_PDIPM_HXX_
2#define ROBOTOC_CONSTRAINTS_PDIPM_HXX_
3
5
6#include <cmath>
7#include <cassert>
8
9
10namespace robotoc {
11namespace pdipm {
12
13inline void setSlackAndDualPositive(const double barrier_param,
15 assert(barrier_param > 0);
16 assert(data.checkDimensionalConsistency());
17 const double sqrt_barrier = std::sqrt(barrier_param);
18 for (int i=0; i<data.slack.size(); ++i) {
19 if (data.slack.coeff(i) < sqrt_barrier) {
20 data.slack.coeffRef(i) = sqrt_barrier;
21 }
22 }
23 data.dual.array() = barrier_param / data.slack.array();
24}
25
26
27inline void computeComplementarySlackness(const double barrier_param,
29 assert(barrier_param > 0);
30 assert(data.checkDimensionalConsistency());
31 data.cmpl.array() = data.slack.array() * data.dual.array() - barrier_param;
32}
33
34
35inline void computeComplementarySlackness(const double barrier_param,
37 const int start, const int size) {
38 assert(barrier_param > 0);
39 assert(data.checkDimensionalConsistency());
40 data.cmpl.segment(start, size).array()
41 = data.slack.segment(start, size).array()
42 * data.dual.segment(start, size).array() - barrier_param;
43}
44
45
46template <int Size>
47inline void computeComplementarySlackness(const double barrier_param,
49 const int start) {
50 assert(barrier_param > 0);
51 assert(data.checkDimensionalConsistency());
52 data.cmpl.template segment<Size>(start).array()
53 = data.slack.template segment<Size>(start).array()
54 * data.dual.template segment<Size>(start).array() - barrier_param;
55}
56
57
58inline double computeComplementarySlackness(const double barrier_param,
59 const double slack,
60 const double dual) {
61 assert(barrier_param > 0);
62 return (slack * dual - barrier_param);
63}
64
65
67 assert(data.checkDimensionalConsistency());
68 data.cond.array() = (data.dual.array()*data.residual.array()-data.cmpl.array())
69 / data.slack.array();
70}
71
72
74 const int start, const int size) {
75 assert(data.checkDimensionalConsistency());
76 data.cond.segment(start, size).array()
77 = (data.dual.segment(start, size).array()
78 *data.residual.segment(start, size).array()
79 -data.cmpl.segment(start, size).array())
80 / data.slack.segment(start, size).array();
81}
82
83
84template <int Size>
86 const int start) {
87 assert(data.checkDimensionalConsistency());
88 data.cond.template segment<Size>(start).array()
89 = (data.dual.template segment<Size>(start).array()
90 *data.residual.template segment<Size>(start).array()
91 -data.cmpl.template segment<Size>(start).array())
92 / data.slack.template segment<Size>(start).array();
93}
94
95
96inline double computeCondensingCoeffcient(const double slack, const double dual,
97 const double residual,
98 const double cmpl) {
99 return ((dual*residual-cmpl)/slack);
100}
101
102
103inline double fractionToBoundarySlack(const double fraction_rate,
104 const ConstraintComponentData& data) {
105 assert(fraction_rate > 0);
106 assert(fraction_rate <= 1);
107 assert(data.checkDimensionalConsistency());
108 return fractionToBoundary(data.dimc(), fraction_rate, data.slack, data.dslack);
109}
110
111
112inline double fractionToBoundaryDual(const double fraction_rate,
113 const ConstraintComponentData& data) {
114 assert(fraction_rate > 0);
115 assert(fraction_rate <= 1);
116 assert(data.checkDimensionalConsistency());
117 return fractionToBoundary(data.dimc(), fraction_rate, data.dual, data.ddual);
118}
119
120
121inline double fractionToBoundary(const int dim, const double fraction_rate,
122 const Eigen::VectorXd& vec,
123 const Eigen::VectorXd& dvec) {
124 assert(dim > 0);
125 assert(fraction_rate > 0);
126 assert(fraction_rate <= 1);
127 assert(vec.size() == dim);
128 assert(dvec.size() == dim);
129 double min_fraction_to_boundary = 1;
130 for (int i=0; i<dim; ++i) {
131 const double fraction_to_boundary
132 = - fraction_rate * (vec.coeff(i)/dvec.coeff(i));
133 if (fraction_to_boundary > 0 && fraction_to_boundary < 1) {
134 if (fraction_to_boundary < min_fraction_to_boundary) {
135 min_fraction_to_boundary = fraction_to_boundary;
136 }
137 }
138 }
139 assert(min_fraction_to_boundary > 0);
140 assert(min_fraction_to_boundary <= 1);
141 return min_fraction_to_boundary;
142}
143
144
145inline double fractionToBoundary(const double fraction_rate,
146 const double var, const double dvar) {
147 assert(fraction_rate > 0);
148 assert(fraction_rate <= 1);
149 const double fraction_to_boundary = - fraction_rate * (var/dvar);
150 if (fraction_to_boundary > 0 && fraction_to_boundary < 1) {
151 return fraction_to_boundary;
152 }
153 else {
154 return 1.0;
155 }
156}
157
158
160 assert(data.checkDimensionalConsistency());
161 data.ddual.array()
162 = - (data.dual.array()*data.dslack.array()+data.cmpl.array())
163 / data.slack.array();
164}
165
166
168 const int start, const int size) {
169 data.ddual.segment(start, size).array()
170 = - (data.dual.segment(start, size).array()
171 *data.dslack.segment(start, size).array()
172 +data.cmpl.segment(start, size).array())
173 / data.slack.segment(start, size).array();
174}
175
176
177template <int Size>
179 const int start) {
180 data.ddual.template segment<Size>(start).array()
181 = - (data.dual.template segment<Size>(start).array()
182 *data.dslack.template segment<Size>(start).array()
183 +data.cmpl.template segment<Size>(start).array())
184 / data.slack.template segment<Size>(start).array();
185}
186
187
188inline double computeDualDirection(const double slack, const double dual,
189 const double dslack, const double cmpl) {
190 return (- (dual * dslack + cmpl) / slack);
191}
192
193
194template <typename VectorType>
195inline double logBarrier(const double barrier_param,
196 const Eigen::MatrixBase<VectorType>& vec) {
197 assert(barrier_param > 0);
198 assert(vec.array().minCoeff() > 0);
199 return (- barrier_param * vec.array().log().sum());
200}
201
202} // namespace pdipm
203} // namespace robotoc
204
205#endif // ROBOTOC_CONSTRAINTS_PDIPM__HXX_
Data used in constraint components. Composed by slack, dual (Lagrange multiplier),...
Definition: constraint_component_data.hpp:17
Eigen::VectorXd ddual
Newton direction of the dual. Size is ConstraintComponentData::dimc().
Definition: constraint_component_data.hpp:91
Eigen::VectorXd residual
Primal residual of the constraint. Size is ConstraintComponentData::dimc().
Definition: constraint_component_data.hpp:73
bool checkDimensionalConsistency() const
Check whether dimensions of slack, dual, residual, cmpl, dslack, ddual are ConstraintComponentData::d...
int dimc() const
Dimension of the constraint.
Definition: constraint_component_data.hpp:160
Eigen::VectorXd dslack
Newton direction of the slack. Size is ConstraintComponentData::dimc().
Definition: constraint_component_data.hpp:85
Eigen::VectorXd cond
Used in condensing of slack and dual. Size is ConstraintComponentData::dimc().
Definition: constraint_component_data.hpp:97
Eigen::VectorXd cmpl
Residual in the complementary slackness between slack and dual. Size is ConstraintComponentData::dimc...
Definition: constraint_component_data.hpp:79
Eigen::VectorXd slack
Slack variable of the constraint. Size is ConstraintComponentData::dimc(). All elements must be posit...
Definition: constraint_component_data.hpp:61
Eigen::VectorXd dual
Dual variable (Lagrange multiplier) of the constraint. Size is ConstraintComponentData::dimc()....
Definition: constraint_component_data.hpp:67
double fractionToBoundary(const int dim, const double fraction_rate, const Eigen::VectorXd &vec, const Eigen::VectorXd &dvec)
Applies the fraction-to-boundary-rule.
Definition: pdipm.hxx:121
void computeCondensingCoeffcient(ConstraintComponentData &data)
Computes the coefficient of the condensing.
Definition: pdipm.hxx:66
void computeComplementarySlackness(const double barrier_param, ConstraintComponentData &data)
Computes the residual in the complementarity slackness between the slack and dual variables.
Definition: pdipm.hxx:27
void computeDualDirection(ConstraintComponentData &data)
Computes the direction of the dual variable from slack, primal residual, complementary slackness,...
Definition: pdipm.hxx:159
double fractionToBoundaryDual(const double fraction_rate, const ConstraintComponentData &data)
Applies the fraction-to-boundary-rule to the directions of the dual variables.
Definition: pdipm.hxx:112
void setSlackAndDualPositive(const double barrier_param, ConstraintComponentData &data)
Sets the slack and dual variables positive.
Definition: pdipm.hxx:13
double fractionToBoundarySlack(const double fraction_rate, const ConstraintComponentData &data)
Applies the fraction-to-boundary-rule to the directions of the slack variables.
Definition: pdipm.hxx:103
double logBarrier(const double barrier_param, const Eigen::MatrixBase< VectorType > &vec)
Computes the log barrier function.
Definition: pdipm.hxx:195
Definition: constraint_component_base.hpp:17