- 3.0.2 optimal control module.
OptconDiscreteSystemInterface-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, 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, typename SCALAR>
21  const control_vector_t& u,
22  const state_vector_t& x_next,
23  const int n,
24  size_t subSteps,
25  state_matrix_t& A,
27  const size_t threadId)
28 {
29  this->linearSystems_[threadId]->getAandB(x, u, x_next, n, subSteps, A, B);
30 }
31 
32 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
34  const state_vector_t& state,
35  const time_t n,
36  const control_vector_t& control,
37  state_vector_t& stateNext,
38  const size_t threadId)
39 {
40  this->controller_[threadId]->setControl(control);
41  this->systems_[threadId]->propagateControlledDynamics(state, n, control, stateNext);
42 }
43 
44 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
46  const typename optConProblem_t::DynamicsPtr_t& dyn)
47 {
48  if (dyn == nullptr)
49  throw std::runtime_error("system dynamics are nullptr");
50 
51  for (int i = 0; i < this->settings_.nThreads + 1; i++)
52  {
53  this->systems_.at(i) = typename optConProblem_t::DynamicsPtr_t(dyn->clone());
54  this->systems_.at(i)->setController(this->controller_.at(i));
55  }
56 }
57 
58 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
60  const typename optConProblem_t::LinearPtr_t& lin)
61 {
62  if (lin == nullptr)
63  throw std::runtime_error("linear system dynamics are nullptr");
64 
65  for (int i = 0; i < this->settings_.nThreads + 1; i++)
66  {
67  this->linearSystems_.at(i) = typename optConProblem_t::LinearPtr_t(lin->clone());
68  }
69 }
70 
71 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
73 {
74  this->systems_.resize(this->settings_.nThreads + 1);
75  this->linearSystems_.resize(this->settings_.nThreads + 1);
76 
77  for (int i = 0; i < this->settings_.nThreads + 1; i++)
78  {
79  // make a deep copy of the system for each thread
80  this->systems_.at(i) =
81  typename optConProblem_t::DynamicsPtr_t(this->optConProblem_.getNonlinearSystem()->clone());
82  this->systems_.at(i)->setController(this->controller_.at(i));
83 
84  this->linearSystems_.at(i) =
85  typename optConProblem_t::LinearPtr_t(this->optConProblem_.getLinearSystem()->clone());
86  }
87 }
88 
89 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
91 {
92  if (settings.nThreads != this->settings_.nThreads)
93  {
94  throw std::runtime_error("Number of threads cannot be changed after instance has been created.");
95  }
96 
97  this->settings_ = settings;
98 }
99 
100 } // namespace optcon
101 } // namespace ct
std::vector< ConstantControllerPtr, Eigen::aligned_allocator< ConstantControllerPtr > > controller_
Definition: OptconSystemInterface.h:123
std::vector< typename optConProblem_t::DynamicsPtr_t > systems_
Definition: OptconSystemInterface.h:111
std::vector< typename optConProblem_t::LinearPtr_t > linearSystems_
Definition: OptconSystemInterface.h:119
const LinearPtr_t getLinearSystem() const
Definition: OptConProblemBase-impl.h:151
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
const DynamicsPtr_t getNonlinearSystem() const
Definition: OptConProblemBase-impl.h:138
virtual void changeLinearSystem(const typename optConProblem_t::LinearPtr_t &lin) override
Definition: OptconDiscreteSystemInterface-impl.h:59
virtual void changeNonlinearSystem(const typename optConProblem_t::DynamicsPtr_t &dyn) override
Definition: OptconDiscreteSystemInterface-impl.h:45
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: OptconDiscreteSystemInterface-impl.h:33
Base::settings_t settings_t
Definition: OptconDiscreteSystemInterface.h:43
Base::state_matrix_t state_matrix_t
Definition: OptconDiscreteSystemInterface.h:34
Base::control_vector_t control_vector_t
Definition: OptconDiscreteSystemInterface.h:32
for i
Definition: mpc_unittest_plotting.m:14
Base::state_control_matrix_t state_control_matrix_t
Definition: OptconDiscreteSystemInterface.h:35
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: OptconDiscreteSystemInterface-impl.h:20
optConProblem_t optConProblem_
the constant controller for forward-integration during one time-step
Definition: OptconSystemInterface.h:125
OptconDiscreteSystemInterface(const optConProblem_t &problem, const settings_t &settings)
constructor
Definition: OptconDiscreteSystemInterface-impl.h:12
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
Base::state_vector_t state_vector_t
Definition: OptconDiscreteSystemInterface.h:33
virtual void initialize() override
perform necessary setup work
Definition: OptconDiscreteSystemInterface-impl.h:72
virtual void configure(const settings_t &settings) override
Definition: OptconDiscreteSystemInterface-impl.h:90
int nThreads
save the smallest eigenvalue of the Hessian
Definition: NLOptConSettings.hpp:272
Base::optConProblem_t optConProblem_t
Definition: OptconDiscreteSystemInterface.h:42