- 3.0.2 optimal control module.
LinearSystemTest.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4  **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include <chrono>
9 
10 #include <gtest/gtest.h>
11 
12 #include "../testSystems/LinearOscillator.h"
13 
14 namespace ct {
15 namespace optcon {
16 namespace example {
17 
18 using namespace ct::core;
19 using namespace ct::optcon;
20 
21 using std::shared_ptr;
22 
23 
34 TEST(LinearSystemsTest, NLOCSolverTest)
35 {
36  typedef NLOptConSolver<state_dim, control_dim, state_dim / 2, state_dim / 2> NLOptConSolver;
37 
38  // count executed tests
39  size_t testCounter = 0;
40 
41  // desired final state
42  Eigen::Vector2d x_final;
43  x_final << 20, 0;
44 
45  // given initial state
46  StateVector<state_dim> initState;
47  initState.setZero();
48  initState(1) = 1.0;
49 
50  // provide algorithm settings
51  NLOptConSettings nloc_settings;
52  nloc_settings.epsilon = 0.0;
53  nloc_settings.recordSmallestEigenvalue = false;
54  nloc_settings.fixedHessianCorrection = false;
55  nloc_settings.dt = 0.01;
56  nloc_settings.discretization = NLOptConSettings::APPROXIMATION::FORWARD_EULER; // default approximation
57  nloc_settings.lqocp_solver = NLOptConSettings::LQOCP_SOLVER::GNRICCATI_SOLVER;
58  nloc_settings.printSummary = false;
59 
60  // loop through all solver classes
61  for (int algClass = 0; algClass < NLOptConSettings::NLOCP_ALGORITHM::NUM_TYPES; algClass++)
62  {
63  nloc_settings.nlocp_algorithm = static_cast<NLOptConSettings::NLOCP_ALGORITHM>(algClass);
64 
65  // switch line search on or off
66  for (int toggleLS = 0; toggleLS <= 1; toggleLS++)
67  {
68  nloc_settings.lineSearchSettings.type = static_cast<LineSearchSettings::TYPE>(toggleLS);
69 
70  // toggle between single and multi-threading
71  for (size_t nThreads = 1; nThreads < 5; nThreads = nThreads + 3)
72  {
73  nloc_settings.nThreads = nThreads;
74 
75  // toggle between iLQR/GNMS and hybrid methods with K_shot !=1
76  for (size_t kshot = 1; kshot < 11; kshot = kshot + 9)
77  {
78  nloc_settings.K_shot = kshot;
79 
80  if (kshot > 1 && nloc_settings.nlocp_algorithm == NLOptConSettings::NLOCP_ALGORITHM::ILQR)
81  continue; // proceed to next test case
82 
83  // toggle sensitivity integrator
84  for (size_t sensInt = 0; sensInt <= 1; sensInt++)
85  {
86  nloc_settings.useSensitivityIntegrator = bool(sensInt);
87 
88  // toggle over simulation time-steps
89  for (size_t ksim = 1; ksim <= 5; ksim = ksim + 4)
90  {
91  nloc_settings.K_sim = ksim;
92 
93  // catch special case, simulation sub-time steps only make sense when sensitivity integrator active
94  if ((nloc_settings.useSensitivityIntegrator == false) && (ksim > 1))
95  continue; // proceed to next test case
96 
97  // toggle integrator type
98  for (size_t integratortype = 0; integratortype <= 1; integratortype++)
99  {
100  if (integratortype == 0)
101  nloc_settings.integrator = ct::core::IntegrationType::EULERCT;
102  else if (integratortype == 1 && nloc_settings.useSensitivityIntegrator == true)
103  {
104  // use RK4 with exactly integrated sensitivities
105  nloc_settings.integrator = ct::core::IntegrationType::RK4CT;
106  }
107  else
108  continue; // proceed to next test case
109 
110  // nloc_settings.print();
111 
112  shared_ptr<ControlledSystem<state_dim, control_dim>> nonlinearSystem(
113  new LinearOscillator());
114  shared_ptr<LinearSystem<state_dim, control_dim>> analyticLinearSystem(
115  new LinearOscillatorLinear());
116  shared_ptr<CostFunctionQuadratic<state_dim, control_dim>> costFunction =
117  tpl::createCostFunctionLinearOscillator<double>(x_final);
118 
119  // times
120  ct::core::Time tf = 1.0;
121  size_t nSteps = nloc_settings.computeK(tf);
122 
123  // initial controller
124  StateVectorArray<state_dim> x0(nSteps + 1, initState);
126  uff << kStiffness * initState(0);
127  ControlVectorArray<control_dim> u0(nSteps, uff);
128 
132 
133  NLOptConSolver::Policy_t initController(x0, u0, u0_fb, nloc_settings.dt);
134 
135  // construct single-core single subsystem OptCon Problem
137  tf, x0[0], nonlinearSystem, costFunction, analyticLinearSystem);
138 
139 
140  NLOptConSolver solver(optConProblem, nloc_settings);
141  solver.configure(nloc_settings);
142  solver.setInitialGuess(initController);
143 
145  solver.runIteration(); // only this one should be required to solve LQ problem
146  solver.runIteration();
148  const SummaryAllIterations<double>& summary = solver.getBackend()->getSummary();
150  ASSERT_GT(summary.lx_norms.front(), 1e-9);
151  ASSERT_GT(summary.lu_norms.front(), 1e-9);
152 
154  ASSERT_LT(summary.lx_norms.back(), 1e-10);
155  ASSERT_LT(summary.lu_norms.back(), 1e-10);
156  ASSERT_LT(summary.defect_l1_norms.back(), 1e-10);
157  ASSERT_LT(summary.defect_l2_norms.back(), 1e-10);
158 
159  testCounter++;
160 
161  } // toggle integrator type
162  } // toggle simulation time steps
163  } // toggle sensitivity integrator
164  } // toggle k_shot
165  } // toggle multi-threading / single-threading
166  } // toggle line-search
167  } // toggle solver class
168 
169  std::cout << "Performed " << testCounter << " successful NLOC tests with linear systems" << std::endl;
170 
171 } // end TEST
172 
173 
174 } // namespace example
175 } // namespace optcon
176 } // namespace ct
LineSearchSettings lineSearchSettings
number of threads for eigen parallelization (applies both to MP and ST) Note. in order to activate Ei...
Definition: NLOptConSettings.hpp:275
LQOCP_SOLVER lqocp_solver
which nonlinear optimal control algorithm is to be used
Definition: NLOptConSettings.hpp:259
bool printSummary
Definition: NLOptConSettings.hpp:278
double dt
Eigenvalue correction factor for Hessian regularization.
Definition: NLOptConSettings.hpp:262
TYPE type
Definition: NLOptConSettings.hpp:58
const std::shared_ptr< Backend_t > & getBackend()
get a reference to the backend (
Definition: NLOptConSolver-impl.hpp:237
const size_t state_dim
Definition: ConstraintComparison.h:20
double epsilon
the prefix to be stored before the matfile name for logging
Definition: NLOptConSettings.hpp:261
NLOCAlgorithm_t::Policy_t Policy_t
Definition: NLOptConSolver.hpp:62
void configure(const Settings_t &settings) override
Definition: NLOptConSolver-impl.hpp:62
NLOCP_ALGORITHM
algorithm types for solving the NLOC problem
Definition: NLOptConSettings.hpp:202
std::vector< SCALAR > defect_l2_norms
Definition: NLOCResults.hpp:25
void setInitialGuess(const Policy_t &initialGuess) override
Definition: NLOptConSolver-impl.hpp:118
Definition: NLOCResults.hpp:18
NLOCP_ALGORITHM nlocp_algorithm
Definition: NLOptConSettings.hpp:258
bool useSensitivityIntegrator
Definition: NLOptConSettings.hpp:279
Settings for the NLOptCon algorithm.
Definition: NLOptConSettings.hpp:198
TEST(ConstraintComparison, comparisonAnalyticAD)
Definition: ConstraintComparison.h:158
const double kStiffness
Definition: SymplecticTest.h:34
APPROXIMATION discretization
which integrator to use during the NLOptCon forward rollout
Definition: NLOptConSettings.hpp:256
bool fixedHessianCorrection
the maximum admissible number of NLOptCon main iterations
Definition: NLOptConSettings.hpp:270
int K_sim
sampling time for the control input (seconds)
Definition: NLOptConSettings.hpp:263
std::vector< SCALAR > lx_norms
step update norms
Definition: NLOCResults.hpp:30
virtual bool runIteration() override
Definition: NLOptConSolver-impl.hpp:102
int computeK(double timeHorizon) const
log to matlab (true/false)
Definition: NLOptConSettings.hpp:290
tpl::LinearOscillator< double > LinearOscillator
Definition: LinearOscillator.h:105
std::vector< SCALAR > defect_l1_norms
different overall defect norms
Definition: NLOCResults.hpp:24
bool recordSmallestEigenvalue
perform Hessian regularization by incrementing the eigenvalues by epsilon.
Definition: NLOptConSettings.hpp:271
StateVector< state_dim > x0
Definition: ConstrainedNLOCTest.cpp:14
tpl::LinearOscillatorLinear< double > LinearOscillatorLinear
Definition: LinearOscillator.h:106
ct::core::IntegrationType integrator
Definition: NLOptConSettings.hpp:255
std::vector< SCALAR > lu_norms
Definition: NLOCResults.hpp:31
int nThreads
save the smallest eigenvalue of the Hessian
Definition: NLOptConSettings.hpp:272
int K_shot
number of sub-integration-steps
Definition: NLOptConSettings.hpp:264
Definition: NLOptConSolver.hpp:29
TYPE
types of backtracking line-search
Definition: NLOptConSettings.hpp:25
Definition: OptConProblemBase.h:40
const size_t control_dim
Definition: ConstraintComparison.h:21
Definition: exampleDir.h:9
double Time