- 3.0.2 optimal control module.
NLOptConSolver.hpp
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 
9 
12 
15 
16 namespace ct {
17 namespace optcon {
18 
19 
23 template <size_t STATE_DIM,
24  size_t CONTROL_DIM,
25  size_t P_DIM = STATE_DIM / 2,
26  size_t V_DIM = STATE_DIM / 2,
27  typename SCALAR = double,
28  bool CONTINUOUS = true>
29 class NLOptConSolver final
30  : public OptConSolver<NLOptConSolver<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>,
31  typename NLOCAlgorithm<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>::Policy_t,
32  NLOptConSettings,
33  STATE_DIM,
34  CONTROL_DIM,
35  SCALAR,
36  CONTINUOUS>
37 {
38 public:
39  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
40 
41  static const size_t STATE_D = STATE_DIM;
42  static const size_t CONTROL_D = CONTROL_DIM;
43  static const size_t POS_DIM = P_DIM;
44  static const size_t VEL_DIM = V_DIM;
45 
49  STATE_DIM,
50  CONTROL_DIM,
51  SCALAR,
52  CONTINUOUS>
54 
55 
56  typedef NLOptConSettings Settings_t;
57  typedef SCALAR Scalar_t;
58 
60 
64 
65 
67  NLOptConSolver(const OptConProblem_t& optConProblem, const Settings_t& settings);
68 
70  NLOptConSolver(const OptConProblem_t& optConProblem,
71  const std::string& settingsFile,
72  bool verbose = true,
73  const std::string& ns = "alg");
74 
76  virtual ~NLOptConSolver() = default;
77 
81  void initialize(const OptConProblem_t& optConProblem, const Settings_t& settings);
82 
86  void configure(const Settings_t& settings) override;
87 
88 
92  virtual void prepareIteration();
93 
98  virtual bool finishIteration();
99 
100 
104  virtual void prepareMPCIteration();
105 
110  virtual bool finishMPCIteration();
111 
116  virtual bool runIteration() override;
117 
121  void setInitialGuess(const Policy_t& initialGuess) override;
122 
126  virtual bool solve() override;
127 
132  virtual const Policy_t& getSolution() override;
133 
138  virtual const core::StateTrajectory<STATE_DIM, SCALAR> getStateTrajectory() const override;
139 
145 
150  virtual const core::tpl::TimeArray<SCALAR>& getTimeArray() const override;
151 
156  virtual SCALAR getTimeHorizon() const override;
157 
164  virtual void changeTimeHorizon(const SCALAR& tf) override;
165 
172  virtual void changeInitialState(const core::StateVector<STATE_DIM, SCALAR>& x0) override;
173 
180  virtual void changeCostFunction(const typename OptConProblem_t::CostFunctionPtr_t& cf) override;
181 
188  virtual void changeNonlinearSystem(const typename OptConProblem_t::DynamicsPtr_t& dyn) override;
189 
196  virtual void changeLinearSystem(const typename OptConProblem_t::LinearPtr_t& lin) override;
197 
198  virtual SCALAR getCost() const override;
199 
201  const Settings_t& getSettings();
202 
204  const std::shared_ptr<Backend_t>& getBackend();
205 
207  std::vector<typename OptConProblem_t::DynamicsPtr_t>& getNonlinearSystemsInstances() override;
208 
210  const std::vector<typename OptConProblem_t::DynamicsPtr_t>& getNonlinearSystemsInstances() const override;
211 
213  std::vector<typename OptConProblem_t::LinearPtr_t>& getLinearSystemsInstances() override;
214 
216  const std::vector<typename OptConProblem_t::LinearPtr_t>& getLinearSystemsInstances() const override;
217 
219  std::vector<typename OptConProblem_t::CostFunctionPtr_t>& getCostFunctionInstances() override;
220 
222  const std::vector<typename OptConProblem_t::CostFunctionPtr_t>& getCostFunctionInstances() const override;
223 
225  std::vector<typename OptConProblem_t::ConstraintPtr_t>& getInputBoxConstraintsInstances() override;
226 
228  const std::vector<typename OptConProblem_t::ConstraintPtr_t>& getInputBoxConstraintsInstances() const override;
229 
231  std::vector<typename OptConProblem_t::ConstraintPtr_t>& getStateBoxConstraintsInstances() override;
232 
234  const std::vector<typename OptConProblem_t::ConstraintPtr_t>& getStateBoxConstraintsInstances() const override;
235 
237  std::vector<typename OptConProblem_t::ConstraintPtr_t>& getGeneralConstraintsInstances() override;
238 
240  const std::vector<typename OptConProblem_t::ConstraintPtr_t>& getGeneralConstraintsInstances() const override;
241 
243  void logSummaryToMatlab(const std::string& fileName);
244 
245 protected:
247  std::shared_ptr<Backend_t> nlocBackend_;
248 
250  std::shared_ptr<NLOCAlgorithm_t> nlocAlgorithm_;
251 
252 private:
254  void setAlgorithm(const Settings_t& settings);
255 };
256 
257 
258 } // namespace optcon
259 } // namespace ct
virtual const core::StateTrajectory< STATE_DIM, SCALAR > getStateTrajectory() const override
Definition: NLOptConSolver-impl.hpp:152
Definition: NLOCAlgorithm.hpp:19
void initialize(const OptConProblem_t &optConProblem, const Settings_t &settings)
Definition: NLOptConSolver-impl.hpp:46
std::vector< typename OptConProblem_t::LinearPtr_t > & getLinearSystemsInstances() override
get reference to the linearized system
Definition: NLOptConSolver-impl.hpp:264
Definition: OptConSolver.h:39
virtual SCALAR getTimeHorizon() const override
Get the time horizon the solver currently operates on.
Definition: NLOptConSolver-impl.hpp:175
static const size_t CONTROL_D
Definition: NLOptConSolver.hpp:42
void logSummaryToMatlab(const std::string &fileName)
logging a short summary to matlab
Definition: NLOptConSolver-impl.hpp:351
virtual const core::ControlTrajectory< CONTROL_DIM, SCALAR > getControlTrajectory() const override
Definition: NLOptConSolver-impl.hpp:160
const std::shared_ptr< Backend_t > & getBackend()
get a reference to the backend (
Definition: NLOptConSolver-impl.hpp:237
static EIGEN_MAKE_ALIGNED_OPERATOR_NEW const size_t STATE_D
Definition: NLOptConSolver.hpp:41
virtual void changeLinearSystem(const typename OptConProblem_t::LinearPtr_t &lin) override
Change the linear system.
Definition: NLOptConSolver-impl.hpp:213
std::vector< typename OptConProblem_t::ConstraintPtr_t > & getGeneralConstraintsInstances() override
get reference to the general constraints
Definition: NLOptConSolver-impl.hpp:335
NLOptConSolver(const OptConProblem_t &optConProblem, const Settings_t &settings)
constructor
Definition: NLOptConSolver-impl.hpp:12
NLOCAlgorithm_t::Backend_t Backend_t
Definition: NLOptConSolver.hpp:63
std::vector< typename OptConProblem_t::DynamicsPtr_t > & getNonlinearSystemsInstances() override
get reference to the nonlinear system
Definition: NLOptConSolver-impl.hpp:246
NLOptConSettings Settings_t
Definition: NLOptConSolver.hpp:56
NLOCAlgorithm_t::Policy_t Policy_t
Definition: NLOptConSolver.hpp:62
std::vector< typename OptConProblem_t::CostFunctionPtr_t > & getCostFunctionInstances() override
get reference to the cost function
Definition: NLOptConSolver-impl.hpp:282
virtual void changeNonlinearSystem(const typename OptConProblem_t::DynamicsPtr_t &dyn) override
Change the nonlinear system.
Definition: NLOptConSolver-impl.hpp:205
void configure(const Settings_t &settings) override
Definition: NLOptConSolver-impl.hpp:62
NLOCAlgorithm< STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS > NLOCAlgorithm_t
Definition: NLOptConSolver.hpp:61
virtual void prepareIteration()
Definition: NLOptConSolver-impl.hpp:74
virtual ~NLOptConSolver()=default
destructor
virtual void changeInitialState(const core::StateVector< STATE_DIM, SCALAR > &x0) override
Change the initial state for the optimal control problem.
Definition: NLOptConSolver-impl.hpp:189
void setInitialGuess(const Policy_t &initialGuess) override
Definition: NLOptConSolver-impl.hpp:118
OptConSolver< NLOptConSolver< STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS >, typename NLOCAlgorithm< STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS >::Policy_t, NLOptConSettings, STATE_DIM, CONTROL_DIM, SCALAR, CONTINUOUS > Base
Definition: NLOptConSolver.hpp:53
Settings for the NLOptCon algorithm.
Definition: NLOptConSettings.hpp:198
CppAD::AD< CppAD::cg::CG< double > > SCALAR
virtual bool solve() override
Definition: NLOptConSolver-impl.hpp:126
virtual const core::tpl::TimeArray< SCALAR > & getTimeArray() const override
Definition: NLOptConSolver-impl.hpp:168
static const size_t VEL_DIM
Definition: NLOptConSolver.hpp:44
std::vector< typename OptConProblem_t::ConstraintPtr_t > & getStateBoxConstraintsInstances() override
get reference to the box constraints
Definition: NLOptConSolver-impl.hpp:317
std::vector< typename OptConProblem_t::ConstraintPtr_t > & getInputBoxConstraintsInstances() override
get reference to the box constraints
Definition: NLOptConSolver-impl.hpp:300
std::shared_ptr< Backend_t > nlocBackend_
the backend holding all the math operations
Definition: NLOptConSolver.hpp:247
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef std::conditional< CONTINUOUS, ContinuousOptConProblem< STATE_DIM, CONTROL_DIM, SCALAR >, DiscreteOptConProblem< STATE_DIM, CONTROL_DIM, SCALAR > >::type OptConProblem_t
Definition: OptConSolver.h:46
virtual bool finishMPCIteration()
Definition: NLOptConSolver-impl.hpp:95
virtual bool runIteration() override
Definition: NLOptConSolver-impl.hpp:102
virtual SCALAR getCost() const override
Definition: NLOptConSolver-impl.hpp:221
virtual const Policy_t & getSolution() override
Definition: NLOptConSolver-impl.hpp:144
const Settings_t & getSettings()
get a reference to the current settings
Definition: NLOptConSolver-impl.hpp:229
static const size_t POS_DIM
Definition: NLOptConSolver.hpp:43
virtual void changeCostFunction(const typename OptConProblem_t::CostFunctionPtr_t &cf) override
Change the cost function.
Definition: NLOptConSolver-impl.hpp:197
virtual bool finishIteration()
Definition: NLOptConSolver-impl.hpp:81
Base::OptConProblem_t OptConProblem_t
Definition: NLOptConSolver.hpp:59
virtual void changeTimeHorizon(const SCALAR &tf) override
Change the time horizon the solver operates on.
Definition: NLOptConSolver-impl.hpp:182
C++ implementation of GNMS.
Definition: NLOCBackendBase.hpp:48
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef ct::core::StateFeedbackController< STATE_DIM, CONTROL_DIM, SCALAR > Policy_t
Definition: NLOCAlgorithm.hpp:24
StateVector< state_dim > x0
Definition: ConstrainedNLOCTest.cpp:14
std::shared_ptr< NLOCAlgorithm_t > nlocAlgorithm_
the algorithm for sequencing the math operations in correct manner
Definition: NLOptConSolver.hpp:250
const bool verbose
Definition: ConstraintComparison.h:18
Definition: NLOptConSolver.hpp:29
virtual void prepareMPCIteration()
Definition: NLOptConSolver-impl.hpp:88
SCALAR Scalar_t
Definition: NLOptConSolver.hpp:57