@@ -35,6 +35,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
35
36
36
#include < ocs2_oc/multiple_shooting/Helpers.h>
37
37
#include < ocs2_oc/multiple_shooting/Initialization.h>
38
+ #include < ocs2_oc/multiple_shooting/MetricsComputation.h>
38
39
#include < ocs2_oc/multiple_shooting/PerformanceIndexComputation.h>
39
40
#include < ocs2_oc/multiple_shooting/Transcription.h>
40
41
#include < ocs2_oc/oc_problem/OcpSize.h>
@@ -176,6 +177,7 @@ void SqpSolver::runImpl(scalar_t initTime, const vector_t& initState, scalar_t f
176
177
177
178
// Bookkeeping
178
179
performanceIndeces_.clear ();
180
+ std::vector<Metrics> metrics (timeDiscretization.size ());
179
181
180
182
int iter = 0 ;
181
183
sqp::Convergence convergence = sqp::Convergence::FALSE ;
@@ -197,7 +199,7 @@ void SqpSolver::runImpl(scalar_t initTime, const vector_t& initState, scalar_t f
197
199
198
200
// Apply step
199
201
linesearchTimer_.startTimer ();
200
- const auto stepInfo = takeStep (baselinePerformance, timeDiscretization, initState, deltaSolution, x, u);
202
+ const auto stepInfo = takeStep (baselinePerformance, timeDiscretization, initState, deltaSolution, x, u, metrics );
201
203
performanceIndeces_.push_back (stepInfo.performanceAfterStep );
202
204
linesearchTimer_.endTimer ();
203
205
@@ -211,6 +213,7 @@ void SqpSolver::runImpl(scalar_t initTime, const vector_t& initState, scalar_t f
211
213
212
214
computeControllerTimer_.startTimer ();
213
215
primalSolution_ = toPrimalSolution (timeDiscretization, std::move (x), std::move (u));
216
+ problemMetrics_ = multiple_shooting::toProblemMetrics (timeDiscretization, metrics);
214
217
computeControllerTimer_.endTimer ();
215
218
216
219
++numProblems_;
@@ -362,21 +365,11 @@ PerformanceIndex SqpSolver::setupQuadraticSubproblem(const std::vector<Annotated
362
365
}
363
366
364
367
PerformanceIndex SqpSolver::computePerformance (const std::vector<AnnotatedTime>& time, const vector_t & initState, const vector_array_t & x,
365
- const vector_array_t & u, ProblemMetrics& problemMetrics ) {
366
- // Problem horizon
368
+ const vector_array_t & u, std::vector<Metrics>& metrics ) {
369
+ // Problem size
367
370
const int N = static_cast <int >(time .size ()) - 1 ;
368
-
369
- // extract indices for setting up problemMetrics
370
- int numEvents = 0 , numIntermediates = 0 ;
371
- std::vector<int > indices (N);
372
- for (int i = 0 ; i < N; ++i) {
373
- indices[i] = (time [i].event == AnnotatedTime::Event::PreEvent) ? numEvents++ : numIntermediates++;
374
- }
375
-
376
- // resize
377
- problemMetrics.clear ();
378
- problemMetrics.preJumps .resize (numEvents);
379
- problemMetrics.intermediates .resize (numIntermediates);
371
+ metrics.clear ();
372
+ metrics.resize (N + 1 );
380
373
381
374
std::vector<PerformanceIndex> performance (settings_.nThreads , PerformanceIndex ());
382
375
std::atomic_int timeIndex{0 };
@@ -386,34 +379,32 @@ PerformanceIndex SqpSolver::computePerformance(const std::vector<AnnotatedTime>&
386
379
387
380
int i = timeIndex++;
388
381
while (i < N) {
389
- const auto problemMetricsIndex = indices[i];
390
382
if (time [i].event == AnnotatedTime::Event::PreEvent) {
391
383
// Event node
392
- problemMetrics. preJumps [problemMetricsIndex ] = multiple_shooting::computeEventMetrics (ocpDefinition, time [i].time , x[i], x[i + 1 ]);
393
- performance[workerId] += toPerformanceIndex (problemMetrics. preJumps [problemMetricsIndex ]);
384
+ metrics[i ] = multiple_shooting::computeEventMetrics (ocpDefinition, time [i].time , x[i], x[i + 1 ]);
385
+ performance[workerId] += toPerformanceIndex (metrics[i ]);
394
386
} else {
395
387
// Normal, intermediate node
396
388
const scalar_t ti = getIntervalStart (time [i]);
397
389
const scalar_t dt = getIntervalDuration (time [i], time [i + 1 ]);
398
- problemMetrics.intermediates [problemMetricsIndex] =
399
- multiple_shooting::computeIntermediateMetrics (ocpDefinition, discretizer_, ti, dt, x[i], x[i + 1 ], u[i]);
400
- performance[workerId] += toPerformanceIndex (problemMetrics.intermediates [problemMetricsIndex], dt);
390
+ metrics[i] = multiple_shooting::computeIntermediateMetrics (ocpDefinition, discretizer_, ti, dt, x[i], x[i + 1 ], u[i]);
391
+ performance[workerId] += toPerformanceIndex (metrics[i], dt);
401
392
}
402
393
403
394
i = timeIndex++;
404
395
}
405
396
406
397
if (i == N) { // Only one worker will execute this
407
398
const scalar_t tN = getIntervalStart (time [N]);
408
- problemMetrics. final = multiple_shooting::computeTerminalMetrics (ocpDefinition, tN, x[N]);
409
- performance[workerId] += toPerformanceIndex (problemMetrics. final );
399
+ metrics[N] = multiple_shooting::computeTerminalMetrics (ocpDefinition, tN, x[N]);
400
+ performance[workerId] += toPerformanceIndex (metrics[N] );
410
401
}
411
402
};
412
403
runParallel (std::move (parallelTask));
413
404
414
405
// Account for initial state in performance
415
406
const vector_t initDynamicsViolation = initState - x.front ();
416
- problemMetrics. intermediates .front ().dynamicsViolation += initDynamicsViolation;
407
+ metrics .front ().dynamicsViolation += initDynamicsViolation;
417
408
performance.front ().dynamicsViolationSSE += initDynamicsViolation.squaredNorm ();
418
409
419
410
// Sum performance of the threads
@@ -424,7 +415,7 @@ PerformanceIndex SqpSolver::computePerformance(const std::vector<AnnotatedTime>&
424
415
425
416
sqp::StepInfo SqpSolver::takeStep (const PerformanceIndex& baseline, const std::vector<AnnotatedTime>& timeDiscretization,
426
417
const vector_t & initState, const OcpSubproblemSolution& subproblemSolution, vector_array_t & x,
427
- vector_array_t & u) {
418
+ vector_array_t & u, std::vector<Metrics>& metrics ) {
428
419
using StepType = FilterLinesearch::StepType;
429
420
430
421
/*
@@ -450,13 +441,14 @@ sqp::StepInfo SqpSolver::takeStep(const PerformanceIndex& baseline, const std::v
450
441
scalar_t alpha = 1.0 ;
451
442
vector_array_t xNew (x.size ());
452
443
vector_array_t uNew (u.size ());
444
+ std::vector<Metrics> metricsNew (x.size ());
453
445
do {
454
446
// Compute step
455
447
multiple_shooting::incrementTrajectory (u, du, alpha, uNew);
456
448
multiple_shooting::incrementTrajectory (x, dx, alpha, xNew);
457
449
458
450
// Compute cost and constraints
459
- const PerformanceIndex performanceNew = computePerformance (timeDiscretization, initState, xNew, uNew);
451
+ const PerformanceIndex performanceNew = computePerformance (timeDiscretization, initState, xNew, uNew, metricsNew );
460
452
461
453
// Step acceptance and record step type
462
454
bool stepAccepted;
@@ -474,7 +466,7 @@ sqp::StepInfo SqpSolver::takeStep(const PerformanceIndex& baseline, const std::v
474
466
if (stepAccepted) { // Return if step accepted
475
467
x = std::move (xNew);
476
468
u = std::move (uNew);
477
- problemMetrics. swap (problemMetricsNew_ );
469
+ metrics = std::move (metricsNew );
478
470
479
471
// Prepare step info
480
472
sqp::StepInfo stepInfo;
@@ -508,8 +500,8 @@ sqp::StepInfo SqpSolver::takeStep(const PerformanceIndex& baseline, const std::v
508
500
stepInfo.du_norm = 0.0 ;
509
501
stepInfo.performanceAfterStep = baseline;
510
502
stepInfo.totalConstraintViolationAfterStep = FilterLinesearch::totalConstraintViolation (baseline);
511
-
512
- std::ignore = computePerformance (timeDiscretization, initState, xNew, uNew, problemMetrics );
503
+ // compute metrics for the baseline rollout
504
+ std::ignore = computePerformance (timeDiscretization, initState, x, u, metrics );
513
505
514
506
if (settings_.printLinesearch ) {
515
507
std::cerr << " [Linesearch terminated] Step size: " << stepInfo.stepSize << " , Step Type: " << toString (stepInfo.stepType ) << " \n " ;
0 commit comments