Skip to content

Commit 4f24fda

Browse files
committed
refactor Ipm
1 parent ced25bc commit 4f24fda

File tree

4 files changed

+14
-56
lines changed

4 files changed

+14
-56
lines changed

ocs2_ipm/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ include_directories(
4949

5050
# Multiple shooting solver library
5151
add_library(${PROJECT_NAME}
52-
src/IpmInitialization.cpp
5352
src/IpmHelpers.cpp
5453
src/IpmPerformanceIndexComputation.cpp
5554
src/IpmSettings.cpp

ocs2_ipm/include/ocs2_ipm/IpmInitialization.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ namespace ipm {
4343
* @param initialSlackMarginRate : Additional margin rate of the initial slack variables. Corresponds to `slack_bound_frac` option of IPOPT.
4444
* @return Initialized slack variable.
4545
*/
46-
vector_t initializeSlackVariable(const vector_t& ineqConstraint, scalar_t initialSlackLowerBound, scalar_t initialSlackMarginRate);
46+
inline vector_t initializeSlackVariable(const vector_t& ineqConstraint, scalar_t initialSlackLowerBound, scalar_t initialSlackMarginRate) {
47+
return (1.0 + initialSlackMarginRate) * ineqConstraint.cwiseMax(initialSlackLowerBound);
48+
}
4749

4850
/**
4951
* Initializes the dual variable. The initialization of dual variables follows IPOPT option `bound_mult_init_method`: `mu-based`
@@ -55,8 +57,10 @@ vector_t initializeSlackVariable(const vector_t& ineqConstraint, scalar_t initia
5557
* @param initialDualMarginRate : Additional margin rate of the initial dual variables.
5658
* @return Initialized dual variable.
5759
*/
58-
vector_t initializeDualVariable(const vector_t& slack, scalar_t barrierParam, scalar_t initialSlackLowerBound,
59-
scalar_t initialSlackMarginRate);
60+
inline vector_t initializeDualVariable(const vector_t& slack, scalar_t barrierParam, scalar_t initialDualLowerBound,
61+
scalar_t initialDualMarginRate) {
62+
return (1.0 + initialDualMarginRate) * (barrierParam * slack.cwiseInverse()).cwiseMax(initialDualLowerBound);
63+
}
6064

6165
} // namespace ipm
6266
} // namespace ocs2

ocs2_ipm/src/IpmInitialization.cpp

Lines changed: 0 additions & 45 deletions
This file was deleted.

ocs2_ipm/src/IpmPerformanceIndexComputation.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ PerformanceIndex computePerformanceIndex(const multiple_shooting::Transcription&
4343
auto performance = multiple_shooting::computePerformanceIndex(transcription, dt);
4444

4545
if (slackStateIneq.size() > 0) {
46-
performance.cost += (-dt * barrierParam * slackStateIneq.array().log().sum());
46+
performance.cost -= dt * barrierParam * slackStateIneq.array().log().sum();
4747
}
4848
if (slackStateInputIneq.size() > 0) {
49-
performance.cost += (-dt * barrierParam * slackStateInputIneq.array().log().sum());
49+
performance.cost -= dt * barrierParam * slackStateInputIneq.array().log().sum();
5050
}
5151

5252
if (transcription.stateIneqConstraints.f.size() > 0) {
@@ -64,7 +64,7 @@ PerformanceIndex computePerformanceIndex(const multiple_shooting::TerminalTransc
6464
auto performance = multiple_shooting::computePerformanceIndex(transcription);
6565

6666
if (slackIneq.size() > 0) {
67-
performance.cost += (-barrierParam * slackIneq.array().log().sum());
67+
performance.cost -= barrierParam * slackIneq.array().log().sum();
6868
}
6969

7070
if (transcription.ineqConstraints.f.size() > 0) {
@@ -79,7 +79,7 @@ PerformanceIndex computePerformanceIndex(const multiple_shooting::EventTranscrip
7979
auto performance = multiple_shooting::computePerformanceIndex(transcription);
8080

8181
if (slackIneq.size() > 0) {
82-
performance.cost += (-barrierParam * slackIneq.array().log().sum());
82+
performance.cost -= barrierParam * slackIneq.array().log().sum();
8383
}
8484

8585
if (transcription.ineqConstraints.f.size() > 0) {
@@ -114,12 +114,12 @@ PerformanceIndex toPerformanceIndex(const Metrics& metrics, scalar_t dt, scalar_
114114
PerformanceIndex performance = toPerformanceIndex(metrics, dt);
115115

116116
if (slackStateIneq.size() > 0) {
117-
performance.cost += (-dt * barrierParam * slackStateIneq.array().log().sum());
117+
performance.cost -= dt * barrierParam * slackStateIneq.array().log().sum();
118118
performance.equalityConstraintsSSE += dt * (toVector(metrics.stateIneqConstraint) - slackStateIneq).squaredNorm();
119119
}
120120

121121
if (slackStateInputIneq.size() > 0 && enableStateInequalityConstraints) {
122-
performance.cost += (-dt * barrierParam * slackStateInputIneq.array().log().sum());
122+
performance.cost -= dt * barrierParam * slackStateInputIneq.array().log().sum();
123123
performance.equalityConstraintsSSE += dt * (toVector(metrics.stateInputIneqConstraint) - slackStateInputIneq).squaredNorm();
124124
}
125125

@@ -130,7 +130,7 @@ PerformanceIndex toPerformanceIndex(const Metrics& metrics, scalar_t barrierPara
130130
PerformanceIndex performance = toPerformanceIndex(metrics);
131131

132132
if (slackIneq.size() > 0) {
133-
performance.cost += (-barrierParam * slackIneq.array().log().sum());
133+
performance.cost -= barrierParam * slackIneq.array().log().sum();
134134
performance.equalityConstraintsSSE += (toVector(metrics.stateIneqConstraint) - slackIneq).squaredNorm();
135135
}
136136

0 commit comments

Comments
 (0)