- 3.0.2 optimal control module.
OptconContinuousSystemInterface-impl.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 namespace ct {
9 namespace optcon {
10 
11 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
13  const optConProblem_t& problem,
14  const settings_t& settings)
15  : Base(problem, settings)
16 {
17 }
18 
19 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
21 {
22 }
23 
24 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
26  const typename optConProblem_t::DynamicsPtr_t& dyn)
27 {
28  if (dyn == nullptr)
29  throw std::runtime_error("system dynamics are nullptr");
30 
31  for (int i = 0; i < this->settings_.nThreads + 1; i++)
32  {
33  this->systems_.at(i) = typename optConProblem_t::DynamicsPtr_t(dyn->clone());
34  this->systems_.at(i)->setController(this->controller_.at(i));
35 
36  discretizers_.at(i) = system_discretizer_ptr_t(new discretizer_t(
37  this->systems_.at(i), this->settings_.dt, this->settings_.integrator, this->settings_.K_sim));
38  discretizers_.at(i)->initialize();
39  }
40 }
41 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
43  const typename optConProblem_t::LinearPtr_t& lin)
44 {
45  if (lin == nullptr)
46  throw std::runtime_error("linear system dynamics are nullptr");
47 
48  for (int i = 0; i < this->settings_.nThreads + 1; i++)
49  {
50  this->linearSystems_.at(i) = typename optConProblem_t::LinearPtr_t(lin->clone());
51  sensitivity_.at(i)->setLinearSystem(this->linearSystems_.at(i));
52  }
53 }
54 
55 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
57 {
58  this->systems_.resize(this->settings_.nThreads + 1);
59  this->linearSystems_.resize(this->settings_.nThreads + 1);
60  discretizers_.resize(this->settings_.nThreads + 1);
61  sensitivity_.resize(this->settings_.nThreads + 1);
62 
63  for (int i = 0; i < this->settings_.nThreads + 1; i++)
64  {
65  // make a deep copy of the system for each thread
66  this->systems_.at(i) =
67  typename optConProblem_t::DynamicsPtr_t(this->optConProblem_.getNonlinearSystem()->clone());
68  this->systems_.at(i)->setController(this->controller_.at(i));
69 
70  this->linearSystems_.at(i) =
71  typename optConProblem_t::LinearPtr_t(this->optConProblem_.getLinearSystem()->clone());
72 
73  discretizers_.at(i) = system_discretizer_ptr_t(new discretizer_t(
74  this->systems_.at(i), this->settings_.dt, this->settings_.integrator, this->settings_.K_sim));
75  discretizers_.at(i)->initialize();
76 
78  {
79  if (this->settings_.integrator != ct::core::IntegrationType::EULER &&
80  this->settings_.integrator != ct::core::IntegrationType::EULERCT &&
81  this->settings_.integrator != ct::core::IntegrationType::RK4 &&
82  this->settings_.integrator != ct::core::IntegrationType::RK4CT &&
83  this->settings_.integrator != ct::core::IntegrationType::EULER_SYM)
84  throw std::runtime_error("sensitivity integrator only available for Euler and RK4 integrators");
85 
86  sensitivity_.at(i) = SensitivityPtr(
88  this->settings_.getSimulationTimestep(), this->linearSystems_.at(i), this->controller_.at(i),
90  }
91  else
92  {
93  sensitivity_.at(i) = SensitivityPtr(
95  this->settings_.dt, this->linearSystems_.at(i), this->settings_.discretization));
96  }
97  }
98 }
99 
100 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
102  const settings_t& settings)
103 {
104  if (settings.nThreads != this->settings_.nThreads)
105  {
106  throw std::runtime_error("Number of threads cannot be changed after instance has been created.");
107  }
108  if (settings.integrator != this->settings_.integrator)
109  {
110  throw std::runtime_error("Cannot change integration type.");
111  }
112 
113  // update system discretizer with new settings
114  for (int i = 0; i < settings.nThreads + 1; i++)
115  {
116  if (!sensitivity_[i])
117  break;
118 
119  sensitivity_[i]->setApproximation(settings.discretization);
120 
121  if (settings.useSensitivityIntegrator)
122  sensitivity_[i]->setTimeDiscretization(settings.getSimulationTimestep());
123  else
124  sensitivity_[i]->setTimeDiscretization(settings.dt);
125 
126  discretizers_[i]->setParameters(settings.dt, settings.K_sim);
127  }
128 
129  this->settings_ = settings;
130 }
131 
132 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
134  const state_vector_t& state,
135  const time_t n,
136  const control_vector_t& control,
137  state_vector_t& stateNext,
138  const size_t threadId)
139 {
140  this->controller_[threadId]->setControl(control);
141  discretizers_[threadId]->propagateControlledDynamics(state, n, control, stateNext);
142 }
143 
144 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
146  StateVectorArrayPtr& subStepsX,
147  const size_t threadId)
148 {
149  subStepsX = discretizers_[threadId]->getSubstates();
150 }
151 
152 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
154  ControlVectorArrayPtr& subStepsU,
155  const size_t threadId)
156 {
157  subStepsU = discretizers_[threadId]->getSubcontrols();
158 }
159 
160 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
162  const StateSubstepsPtr& xSubsteps,
163  const ControlSubstepsPtr& uSubsteps,
164  const size_t threadId)
165 {
166  sensitivity_[threadId]->setSubstepTrajectoryReference(xSubsteps.get(), uSubsteps.get());
167 }
168 
169 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR>
171  const control_vector_t& u,
172  const state_vector_t& x_next,
173  const int n,
174  size_t subSteps,
175  state_matrix_t& A,
177  const size_t threadId)
178 {
179  sensitivity_[threadId]->getAandB(x, u, x_next, n, subSteps, A, B);
180 }
181 
182 } // namespace optcon
183 } // namespace ct
Base::optConProblem_t optConProblem_t
Definition: OptconContinuousSystemInterface.h:58
virtual void changeNumStages(const int numStages) override
set the number of stages/time steps
Definition: OptconContinuousSystemInterface-impl.h:20
std::shared_ptr< Sensitivity_t > SensitivityPtr
Definition: OptconContinuousSystemInterface.h:51
std::vector< ConstantControllerPtr, Eigen::aligned_allocator< ConstantControllerPtr > > controller_
Definition: OptconSystemInterface.h:123
Base::StateVectorArrayPtr StateVectorArrayPtr
Definition: OptconContinuousSystemInterface.h:53
Base::ControlVectorArrayPtr ControlVectorArrayPtr
Definition: OptconContinuousSystemInterface.h:55
std::vector< typename optConProblem_t::DynamicsPtr_t > systems_
Definition: OptconSystemInterface.h:111
double dt
Eigenvalue correction factor for Hessian regularization.
Definition: NLOptConSettings.hpp:262
std::vector< typename optConProblem_t::LinearPtr_t > linearSystems_
Definition: OptconSystemInterface.h:119
virtual void setSubstepTrajectoryReference(const StateSubstepsPtr &xSubsteps, const ControlSubstepsPtr &uSubsteps, const size_t threadId) override
Definition: OptconContinuousSystemInterface-impl.h:161
const LinearPtr_t getLinearSystem() const
Definition: OptConProblemBase-impl.h:151
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
virtual void changeLinearSystem(const typename optConProblem_t::LinearPtr_t &lin) override
Definition: OptconContinuousSystemInterface-impl.h:42
virtual void getSubcontrols(ControlVectorArrayPtr &subStepsU, const size_t threadId) override
Definition: OptconContinuousSystemInterface-impl.h:153
const DynamicsPtr_t getNonlinearSystem() const
Definition: OptConProblemBase-impl.h:138
virtual void getAandB(const state_vector_t &x, const control_vector_t &u, const state_vector_t &x_next, const int n, size_t subSteps, state_matrix_t &A, state_control_matrix_t &B, const size_t threadId) override
retrieve discrete-time linear system matrices A and B.
Definition: OptconContinuousSystemInterface-impl.h:170
double getSimulationTimestep() const
compute the simulation timestep
Definition: NLOptConSettings.hpp:300
virtual void initialize() override
perform necessary setup work
Definition: OptconContinuousSystemInterface-impl.h:56
std::shared_ptr< discretizer_t > system_discretizer_ptr_t
Definition: OptconContinuousSystemInterface.h:48
Base::settings_t settings_t
Definition: OptconContinuousSystemInterface.h:59
bool useSensitivityIntegrator
Definition: NLOptConSettings.hpp:279
ct::core::SystemDiscretizer< STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR > discretizer_t
Definition: OptconContinuousSystemInterface.h:47
for i
Definition: mpc_unittest_plotting.m:14
APPROXIMATION discretization
which integrator to use during the NLOptCon forward rollout
Definition: NLOptConSettings.hpp:256
int K_sim
sampling time for the control input (seconds)
Definition: NLOptConSettings.hpp:263
optConProblem_t optConProblem_
the constant controller for forward-integration during one time-step
Definition: OptconSystemInterface.h:125
bool timeVaryingDiscretization
Definition: NLOptConSettings.hpp:257
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
Base::control_vector_t control_vector_t
Definition: OptconContinuousSystemInterface.h:42
virtual void changeNonlinearSystem(const typename optConProblem_t::DynamicsPtr_t &dyn) override
Definition: OptconContinuousSystemInterface-impl.h:25
Base::state_vector_t state_vector_t
Definition: OptconContinuousSystemInterface.h:43
ct::core::IntegrationType integrator
Definition: NLOptConSettings.hpp:255
virtual void getSubstates(StateVectorArrayPtr &subStepsX, const size_t threadId) override
Definition: OptconContinuousSystemInterface-impl.h:145
int nThreads
save the smallest eigenvalue of the Hessian
Definition: NLOptConSettings.hpp:272
Base::state_matrix_t state_matrix_t
Definition: OptconContinuousSystemInterface.h:44
Base::ControlSubstepsPtr ControlSubstepsPtr
Definition: OptconContinuousSystemInterface.h:56
Base::state_control_matrix_t state_control_matrix_t
Definition: OptconContinuousSystemInterface.h:45
virtual void configure(const settings_t &settings) override
Definition: OptconContinuousSystemInterface-impl.h:101
Base::StateSubstepsPtr StateSubstepsPtr
Definition: OptconContinuousSystemInterface.h:54
OptconContinuousSystemInterface(const optConProblem_t &problem, const settings_t &settings)
constructor
Definition: OptconContinuousSystemInterface-impl.h:12
virtual void propagateControlledDynamics(const state_vector_t &state, const time_t n, const control_vector_t &control, state_vector_t &stateNext, const size_t threadId) override
propagate discrete-time dynamics
Definition: OptconContinuousSystemInterface-impl.h:133