- 3.0.2 optimal control module.
OptConSolver.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 
16 #pragma once
17 
19 
20 
21 namespace ct {
22 namespace optcon {
23 
24 
32 template <typename DERIVED,
33  typename POLICY,
34  typename SETTINGS,
35  size_t STATE_DIM,
36  size_t CONTROL_DIM,
37  typename SCALAR = double,
38  bool CONTINUOUS = true>
40 {
41 public:
42  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
43 
44  typedef typename std::conditional<CONTINUOUS,
47 
48  static const size_t STATE_D = STATE_DIM;
49  static const size_t CONTROL_D = CONTROL_DIM;
50 
51  typedef POLICY Policy_t;
52  typedef SETTINGS Settings_t;
53  typedef DERIVED Derived;
54  typedef SCALAR Scalar_t;
55 
57  virtual ~OptConSolver() {}
73  virtual void setProblem(const OptConProblem_t& optConProblem)
74  {
75  optConProblem.verify();
76 
77  changeTimeHorizon(optConProblem.getTimeHorizon());
78  changeInitialState(optConProblem.getInitialState());
79  changeCostFunction(optConProblem.getCostFunction());
80  changeNonlinearSystem(optConProblem.getNonlinearSystem());
81  changeLinearSystem(optConProblem.getLinearSystem());
82 
83  if (optConProblem.getInputBoxConstraints())
84  changeInputBoxConstraints(optConProblem.getInputBoxConstraints());
85  if (optConProblem.getStateBoxConstraints())
86  changeStateBoxConstraints(optConProblem.getStateBoxConstraints());
87  if (optConProblem.getGeneralConstraints())
88  changeGeneralConstraints(optConProblem.getGeneralConstraints());
89  }
90 
94  virtual void configure(const Settings_t& settings) = 0;
95 
99  void configureFromFile(const std::string& filename,
100  bool verbose = true,
101  const std::string& ns = DERIVED::SolverName)
102  {
103  Settings_t settings;
104  settings.load(filename, verbose, ns);
105  configure(settings);
106  }
107 
112  virtual bool solve() = 0;
113 
118  virtual bool runIteration() { throw std::runtime_error("runIteration not supported by solver"); }
123  virtual const Policy_t& getSolution() = 0;
124 
130 
136 
141  virtual const core::tpl::TimeArray<SCALAR>& getTimeArray() const = 0;
142 
143 
147  virtual void setInitialGuess(const Policy_t& initialGuess) = 0;
148 
149 
154  virtual SCALAR getTimeHorizon() const = 0;
155 
156 
163  virtual void changeTimeHorizon(const SCALAR& tf) = 0;
164 
172 
179  virtual void changeCostFunction(const typename OptConProblem_t::CostFunctionPtr_t& cf) = 0;
180 
187  virtual void changeNonlinearSystem(const typename OptConProblem_t::DynamicsPtr_t& dyn) = 0;
188 
195  virtual void changeLinearSystem(const typename OptConProblem_t::LinearPtr_t& lin) = 0;
196 
206  virtual void changeInputBoxConstraints(const typename OptConProblem_t::ConstraintPtr_t con)
207  {
208  throw std::runtime_error("The current solver does not support input box constraints!");
209  }
210 
211  virtual void changeStateBoxConstraints(const typename OptConProblem_t::ConstraintPtr_t con)
212  {
213  throw std::runtime_error("The current solver does not support state box constraints!");
214  }
215 
225  virtual void changeGeneralConstraints(const typename OptConProblem_t::ConstraintPtr_t con)
226  {
227  throw std::runtime_error("The current solver does not support general constraints!");
228  }
229 
230  virtual SCALAR getCost() const { throw std::runtime_error("Get cost not implemented"); }
240  virtual std::vector<typename OptConProblem_t::DynamicsPtr_t>& getNonlinearSystemsInstances() = 0;
241 
242  virtual const std::vector<typename OptConProblem_t::DynamicsPtr_t>& getNonlinearSystemsInstances() const = 0;
243 
253  virtual std::vector<typename OptConProblem_t::LinearPtr_t>& getLinearSystemsInstances() = 0;
254 
255  virtual const std::vector<typename OptConProblem_t::LinearPtr_t>& getLinearSystemsInstances() const = 0;
256 
266  virtual std::vector<typename OptConProblem_t::CostFunctionPtr_t>& getCostFunctionInstances() = 0;
267 
268  virtual const std::vector<typename OptConProblem_t::CostFunctionPtr_t>& getCostFunctionInstances() const = 0;
269 
280  virtual std::vector<typename OptConProblem_t::ConstraintPtr_t>& getInputBoxConstraintsInstances()
281  {
282  throw std::runtime_error("getInputBoxConstraintsInstances not supported.");
283  }
284 
285  virtual const std::vector<typename OptConProblem_t::ConstraintPtr_t>& getInputBoxConstraintsInstances() const
286  {
287  throw std::runtime_error("getInputBoxConstraintsInstances not supported.");
288  }
289 
290  virtual std::vector<typename OptConProblem_t::ConstraintPtr_t>& getStateBoxConstraintsInstances()
291  {
292  throw std::runtime_error("getStateBoxConstraintsInstances not supported.");
293  }
294 
295  virtual const std::vector<typename OptConProblem_t::ConstraintPtr_t>& getStateBoxConstraintsInstances() const
296  {
297  throw std::runtime_error("getStateBoxConstraintsInstances not supported.");
298  }
299 
300 
311  virtual std::vector<typename OptConProblem_t::ConstraintPtr_t>& getGeneralConstraintsInstances()
312  {
313  throw std::runtime_error("getGeneralConstraintsInstances not supported.");
314  }
315 
316  virtual const std::vector<typename OptConProblem_t::ConstraintPtr_t>& getGeneralConstraintsInstances() const
317  {
318  throw std::runtime_error("getGeneralConstraintsInstances not supported.");
319  }
320 
321 #ifdef CPPADCG
322 
331  virtual void generateAndCompileCode(
333  const ct::core::DerivativesCppadSettings& settings)
334  {
335  throw std::runtime_error("Generate and compile code not implemented for this solver");
336  }
337 #endif
338 
346  virtual void generateCode(const ct::core::DerivativesCppadSettings& settings)
347  {
348  throw std::runtime_error("Generate Code not implemented for this solver");
349  }
350 };
351 } // namespace optcon
352 } // namespace ct
virtual SCALAR getCost() const
Definition: OptConSolver.h:230
Definition: OptConSolver.h:39
virtual void setProblem(const OptConProblem_t &optConProblem)
Assigns the optimal control problem to the solver.
Definition: OptConSolver.h:73
virtual void configure(const Settings_t &settings)=0
virtual const std::vector< typename OptConProblem_t::ConstraintPtr_t > & getStateBoxConstraintsInstances() const
Definition: OptConSolver.h:295
virtual void setInitialGuess(const Policy_t &initialGuess)=0
virtual const Policy_t & getSolution()=0
virtual bool runIteration()
Definition: OptConSolver.h:118
virtual bool solve()=0
virtual void changeTimeHorizon(const SCALAR &tf)=0
Change the time horizon the solver operates on.
virtual const core::ControlTrajectory< CONTROL_DIM, SCALAR > getControlTrajectory() const =0
virtual void changeInitialState(const core::StateVector< STATE_DIM, SCALAR > &x0)=0
Change the initial state for the optimal control problem.
virtual std::vector< typename OptConProblem_t::LinearPtr_t > & getLinearSystemsInstances()=0
Direct accessor to the linear system instances.
virtual void changeCostFunction(const typename OptConProblem_t::CostFunctionPtr_t &cf)=0
Change the cost function.
virtual SCALAR getTimeHorizon() const =0
Get the time horizon the solver currently operates on.
virtual const std::vector< typename OptConProblem_t::ConstraintPtr_t > & getGeneralConstraintsInstances() const
Definition: OptConSolver.h:316
POLICY Policy_t
Definition: OptConSolver.h:51
virtual std::vector< typename OptConProblem_t::ConstraintPtr_t > & getGeneralConstraintsInstances()
Direct accessor to the general constraints.
Definition: OptConSolver.h:311
CppAD::AD< CppAD::cg::CG< double > > SCALAR
DERIVED Derived
Definition: OptConSolver.h:53
virtual std::vector< typename OptConProblem_t::ConstraintPtr_t > & getStateBoxConstraintsInstances()
Definition: OptConSolver.h:290
virtual void changeStateBoxConstraints(const typename OptConProblem_t::ConstraintPtr_t con)
Definition: OptConSolver.h:211
SETTINGS Settings_t
Definition: OptConSolver.h:52
virtual ~OptConSolver()
Definition: OptConSolver.h:57
virtual void changeGeneralConstraints(const typename OptConProblem_t::ConstraintPtr_t con)
Change the general constraints.
Definition: OptConSolver.h:225
virtual std::vector< typename OptConProblem_t::ConstraintPtr_t > & getInputBoxConstraintsInstances()
Direct accessor to the box constraint instances.
Definition: OptConSolver.h:280
void configureFromFile(const std::string &filename, bool verbose=true, const std::string &ns=DERIVED::SolverName)
Definition: OptConSolver.h:99
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 void generateCode(const ct::core::DerivativesCppadSettings &settings)
Generates source AD source code which can be used in the solver. This method needs to be called ahead...
Definition: OptConSolver.h:346
virtual const core::StateTrajectory< STATE_DIM, SCALAR > getStateTrajectory() const =0
void load(const std::string &filename, bool verbose=true, const std::string &ns="dms")
Definition: DmsSettings.h:124
virtual void changeInputBoxConstraints(const typename OptConProblem_t::ConstraintPtr_t con)
Change the box constraints.
Definition: OptConSolver.h:206
OptConSolver()
Definition: OptConSolver.h:56
StateVector< state_dim > x0
Definition: ConstrainedNLOCTest.cpp:14
virtual void changeNonlinearSystem(const typename OptConProblem_t::DynamicsPtr_t &dyn)=0
Change the nonlinear system.
const bool verbose
Definition: ConstraintComparison.h:18
static const size_t CONTROL_D
Definition: OptConSolver.h:49
virtual std::vector< typename OptConProblem_t::DynamicsPtr_t > & getNonlinearSystemsInstances()=0
Direct accessor to the system instances.
virtual const std::vector< typename OptConProblem_t::ConstraintPtr_t > & getInputBoxConstraintsInstances() const
Definition: OptConSolver.h:285
virtual void changeLinearSystem(const typename OptConProblem_t::LinearPtr_t &lin)=0
Change the linear system.
SCALAR Scalar_t
Definition: OptConSolver.h:54
Definition: OptConProblemBase.h:40
static const size_t STATE_D
Definition: OptConSolver.h:48
virtual const core::tpl::TimeArray< SCALAR > & getTimeArray() const =0
virtual std::vector< typename OptConProblem_t::CostFunctionPtr_t > & getCostFunctionInstances()=0
Direct accessor to the cost function instances.