- 3.0.2 optimal control module.
SymplecticTest.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 #include <chrono>
7 
8 #include <gtest/gtest.h>
9 
10 
22 namespace ct {
23 namespace optcon {
24 namespace example {
25 
26 using namespace ct::core;
27 using namespace ct::optcon;
28 
29 using std::shared_ptr;
30 
31 const size_t state_dim = 2; // position, velocity
32 const size_t control_dim = 1; // force
33 
34 const double kStiffness = 1;
35 
37 class Dynamics : public SymplecticSystem<1, 1, control_dim>
38 {
39 public:
40  Dynamics() : SymplecticSystem<1, 1, control_dim>(SYSTEM_TYPE::SECOND_ORDER) {}
42  const StateVector<1>& v,
43  const ControlVector<1>& control,
44  StateVector<1>& pDot) override
45  {
46  pDot = v;
47  }
48 
49  virtual void computeVdot(const StateVector<2>& x,
50  const StateVector<1>& p,
51  const ControlVector<1>& control,
52  StateVector<1>& vDot) override
53  {
54  vDot(0) = control(0) - kStiffness * p(0) * p(0); // mass is 1 k
55  }
56 
57  Dynamics* clone() const override { return new Dynamics(); };
58 };
59 
60 
62 class LinearizedSystem : public LinearSystem<state_dim, control_dim>
63 {
64 public:
65  state_matrix_t A_;
66  state_control_matrix_t B_;
67 
68 
71  const double t = 0.0) override
72  {
73  A_ << 0, 1, -2 * x(0) * kStiffness, 0;
74  return A_;
75  }
76 
79  const double t = 0.0) override
80  {
81  B_ << 0, 1;
82  return B_;
83  }
84 
85  LinearizedSystem* clone() const override { return new LinearizedSystem(); };
86 };
87 
89 std::shared_ptr<CostFunctionQuadratic<state_dim, control_dim>> createCostFunction(Eigen::Vector2d& x_final)
90 {
91  Eigen::Matrix2d Q;
92  Q << 0, 0, 0, 1;
93 
94  Eigen::Matrix<double, 1, 1> R;
95  R << 100.0;
96 
97  Eigen::Vector2d x_nominal = Eigen::Vector2d::Zero();
98  Eigen::Matrix<double, 1, 1> u_nominal = Eigen::Matrix<double, 1, 1>::Zero();
99 
100  Eigen::Matrix2d Q_final;
101  Q_final << 10, 0, 0, 10;
102 
103  std::shared_ptr<CostFunctionQuadratic<state_dim, control_dim>> quadraticCostFunction(
104  new CostFunctionQuadraticSimple<state_dim, control_dim>(Q, R, x_nominal, u_nominal, x_final, Q_final));
105 
106  return quadraticCostFunction;
107 }
108 
109 
111 {
112  std::cout << "setting up problem " << std::endl;
113 
114  typedef NLOptConSolver<state_dim, control_dim, state_dim / 2, state_dim / 2> NLOptConSolver;
115 
116  Eigen::Vector2d x_final;
117  x_final << 2, 0;
118 
119  NLOptConSettings gnms_settings;
120  gnms_settings.nThreads = 1;
121  gnms_settings.epsilon = 0.0;
122  gnms_settings.max_iterations = 1;
123  gnms_settings.recordSmallestEigenvalue = false;
124  gnms_settings.min_cost_improvement = 1e-6;
125  gnms_settings.fixedHessianCorrection = false;
126  gnms_settings.dt = 0.05;
127  gnms_settings.K_sim = 50;
128  gnms_settings.K_shot = 1;
129  gnms_settings.integrator = ct::core::IntegrationType::EULER_SYM;
130  // gnms_settings.discretization = NLOptConSettings::APPROXIMATION::FORWARD_EULER;
131  gnms_settings.useSensitivityIntegrator = true;
132  gnms_settings.nlocp_algorithm = NLOptConSettings::NLOCP_ALGORITHM::GNMS;
133  gnms_settings.lqocp_solver = NLOptConSettings::LQOCP_SOLVER::GNRICCATI_SOLVER;
134  gnms_settings.lineSearchSettings.type = LineSearchSettings::TYPE::NONE;
135  gnms_settings.loggingPrefix = "GNMS";
136  gnms_settings.printSummary = false;
137 
138 
139  NLOptConSettings ilqr_settings = gnms_settings;
140  ilqr_settings.nlocp_algorithm = NLOptConSettings::NLOCP_ALGORITHM::ILQR;
141  ilqr_settings.loggingPrefix = "ILQR";
142 
143 
144  shared_ptr<ControlledSystem<state_dim, control_dim>> nonlinearSystem(new Dynamics);
145  shared_ptr<LinearSystem<state_dim, control_dim>> analyticLinearSystem(new LinearizedSystem);
146  shared_ptr<CostFunctionQuadratic<state_dim, control_dim>> costFunction = createCostFunction(x_final);
147 
148  // times
149  ct::core::Time tf = 3.0;
150  size_t nSteps = std::round(tf / gnms_settings.dt);
151 
152  // provide initial guess
153  StateVector<state_dim> initState;
154  initState.setZero(); //initState(1) = 1.0;
155  StateVectorArray<state_dim> x0(nSteps + 1, initState);
157  uff << kStiffness * initState(0) * initState(0);
158  ControlVectorArray<control_dim> u0(nSteps, uff);
159  for (size_t i = 0; i < nSteps + 1; i++)
160  {
161  // x0 [i] = x_final*double(i)/double(nSteps);
162  }
163 
166 
167  NLOptConSolver::Policy_t initController(x0, u0, u0_fb, gnms_settings.dt);
168 
169  // construct single-core single subsystem OptCon Problem
171  tf, x0[0], nonlinearSystem, costFunction, analyticLinearSystem);
172 
173 
174  // std::cout << "initializing gnms solver" << std::endl;
175  NLOptConSolver gnms(optConProblem, gnms_settings);
176  NLOptConSolver ilqr(optConProblem, ilqr_settings);
177 
178 
179  gnms.configure(gnms_settings);
180  gnms.setInitialGuess(initController);
181 
182  ilqr.configure(ilqr_settings);
183  ilqr.setInitialGuess(initController);
184 
185  // std::cout << "running gnms solver" << std::endl;
186 
187  size_t numIterations = 0;
188 
189  while (numIterations < 50)
190  {
191  gnms.runIteration();
192  ilqr.runIteration();
193 
194  // test trajectories
197 
198  // // test trajectories
199  // StateTrajectory<state_dim> xRollout = ilqr.getStateTrajectory();
200  // ControlTrajectory<control_dim> uRollout = ilqr.getControlTrajectory();
201 
202  numIterations++;
203 
204  // std::cout<<"x final iLQR: " << xRollout.back().transpose() << std::endl;
205  // std::cout<<"u final iLQR: " << uRollout.back().transpose() << std::endl;
206  }
207 }
208 
209 
210 } // namespace example
211 } // namespace optcon
212 } // 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
virtual const core::StateTrajectory< STATE_DIM, SCALAR > getStateTrajectory() const override
Definition: NLOptConSolver-impl.hpp:152
void symplecticTest()
Definition: SymplecticTest.h:110
LQOCP_SOLVER lqocp_solver
which nonlinear optimal control algorithm is to be used
Definition: NLOptConSettings.hpp:259
Dynamics class for the GNMS unit test, slightly nonlinear dynamics.
Definition: DiehlSystem.h:20
bool printSummary
Definition: NLOptConSettings.hpp:278
std::string loggingPrefix
the solver for the linear-quadratic optimal control problem
Definition: NLOptConSettings.hpp:260
virtual void computeVdot(const StateVector< 2 > &x, const StateVector< 1 > &p, const ControlVector< 1 > &control, StateVector< 1 > &vDot) override
Definition: SymplecticTest.h:49
double dt
Eigenvalue correction factor for Hessian regularization.
Definition: NLOptConSettings.hpp:262
TYPE type
Definition: NLOptConSettings.hpp:58
virtual const core::ControlTrajectory< CONTROL_DIM, SCALAR > getControlTrajectory() const override
Definition: NLOptConSolver-impl.hpp:160
std::shared_ptr< CostFunctionQuadratic< state_dim, control_dim > > createCostFunction(Eigen::Vector2d &x_final)
Create a cost function for the GNMS unit test.
Definition: SymplecticTest.h:89
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
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
A simple quadratic cost function.
Definition: CostFunctionQuadraticSimple.hpp:23
NLOCAlgorithm_t::Policy_t Policy_t
Definition: NLOptConSolver.hpp:62
void configure(const Settings_t &settings) override
Definition: NLOptConSolver-impl.hpp:62
double min_cost_improvement
duration of a shot as an integer multiple of dt
Definition: NLOptConSettings.hpp:265
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
void setInitialGuess(const Policy_t &initialGuess) override
Definition: NLOptConSolver-impl.hpp:118
NLOCP_ALGORITHM nlocp_algorithm
Definition: NLOptConSettings.hpp:258
Dynamics * clone() const override
Definition: SymplecticTest.h:57
SECOND_ORDER
bool useSensitivityIntegrator
Definition: NLOptConSettings.hpp:279
Settings for the NLOptCon algorithm.
Definition: NLOptConSettings.hpp:198
LinearizedSystem * clone() const override
Definition: SymplecticTest.h:85
const double kStiffness
Definition: SymplecticTest.h:34
for i
Definition: mpc_unittest_plotting.m:14
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
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
virtual bool runIteration() override
Definition: NLOptConSolver-impl.hpp:102
Dynamics()
Definition: SymplecticTest.h:40
int max_iterations
trade off between external (general and path) constraint violation and cost
Definition: NLOptConSettings.hpp:269
void computePdot(const StateVector< 2 > &x, const StateVector< 1 > &v, const ControlVector< 1 > &control, StateVector< 1 > &pDot) override
Definition: SymplecticTest.h:41
Linear system class for the GNMS unit test.
Definition: DiehlSystem.h:37
bool recordSmallestEigenvalue
perform Hessian regularization by incrementing the eigenvalues by epsilon.
Definition: NLOptConSettings.hpp:271
const state_matrix_t & getDerivativeState(const StateVector< state_dim > &x, const ControlVector< control_dim > &u, const double t=0.0) override
Definition: SymplecticTest.h:69
Eigen::Matrix< double, nStates, nStates > state_matrix_t
StateVector< state_dim > x0
Definition: ConstrainedNLOCTest.cpp:14
ct::core::IntegrationType integrator
Definition: NLOptConSettings.hpp:255
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
const state_control_matrix_t & getDerivativeControl(const StateVector< state_dim > &x, const ControlVector< control_dim > &u, const double t=0.0) override
Definition: SymplecticTest.h:77
Definition: OptConProblemBase.h:40
const size_t control_dim
Definition: ConstraintComparison.h:21
Definition: exampleDir.h:9
double Time