- 3.0.2 optimal control module.
NLOptConSolver-impl.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 
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, bool CONTINUOUS>
13  const OptConProblem_t& optConProblem,
14  const Settings_t& settings)
15 {
16  initialize(optConProblem, settings);
17 }
18 
19 
20 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
22  const OptConProblem_t& optConProblem,
23  const std::string& settingsFile,
24  bool verbose,
25  const std::string& ns)
26  : NLOptConSolver(optConProblem, Settings_t::fromConfigFile(settingsFile, verbose, ns))
27 {
28 }
29 
30 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
32 {
33  if (settings.isSingleShooting())
34  {
35  nlocAlgorithm_ = std::shared_ptr<NLOCAlgorithm<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>>(
37  }
38  else
39  {
40  nlocAlgorithm_ = std::shared_ptr<NLOCAlgorithm<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>>(
42  }
43 }
44 
45 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
47  const OptConProblem_t& optConProblem,
48  const Settings_t& settings)
49 {
50  if (settings.nThreads > 1)
51  nlocBackend_ = std::shared_ptr<Backend_t>(
53  else
54  nlocBackend_ = std::shared_ptr<Backend_t>(
56 
57  setAlgorithm(settings);
58 }
59 
60 
61 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
63 {
64  if (nlocBackend_->getSettings().nThreads != settings.nThreads)
65  throw std::runtime_error("cannot switch from ST to MT or vice versa. Please call initialize.");
66 
67  nlocBackend_->configure(settings);
68 
69  setAlgorithm(settings);
70 }
71 
72 
73 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
75 {
76  nlocAlgorithm_->prepareIteration();
77 }
78 
79 
80 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
82 {
83  return nlocAlgorithm_->finishIteration();
84 }
85 
86 
87 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
89 {
90  nlocAlgorithm_->prepareMPCIteration();
91 }
92 
93 
94 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
96 {
97  return nlocAlgorithm_->finishMPCIteration();
98 }
99 
100 
101 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
103 {
104 #ifdef DEBUG_PRINT
105  auto startSolve = std::chrono::steady_clock::now();
106 #endif
107  bool success = nlocAlgorithm_->runIteration();
108 #ifdef DEBUG_PRINT
109  auto endSolve = std::chrono::steady_clock::now();
110  std::cout << "[NLOC]: runIteration() took "
111  << std::chrono::duration<double, std::milli>(endSolve - startSolve).count() << " ms" << std::endl;
112 #endif
113  return success;
114 }
115 
116 
117 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
119  const Policy_t& initialGuess)
120 {
121  nlocBackend_->setInitialGuess(initialGuess);
122 }
123 
124 
125 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
127 {
128  bool foundBetter = true;
129  int numIterations = 0;
130 
131  while (foundBetter && (numIterations < nlocBackend_->getSettings().max_iterations))
132  {
133  foundBetter = runIteration();
134 
135  numIterations++;
136  }
137 
138  return (numIterations > 1 || foundBetter || (numIterations == 1 && !foundBetter));
139 }
140 
141 
142 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
145 {
146  return nlocBackend_->getSolution();
147 }
148 
149 
150 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
153 {
154  return nlocBackend_->getStateTrajectory();
155 }
156 
157 
158 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
161 {
162  return nlocBackend_->getControlTrajectory();
163 }
164 
165 
166 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
169 {
170  return nlocBackend_->getTimeArray();
171 }
172 
173 
174 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
176 {
177  return nlocBackend_->getTimeHorizon();
178 }
179 
180 
181 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
183 {
184  nlocBackend_->changeTimeHorizon(tf);
185 }
186 
187 
188 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
191 {
192  nlocBackend_->changeInitialState(x0);
193 }
194 
195 
196 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
198  const typename OptConProblem_t::CostFunctionPtr_t& cf)
199 {
200  nlocBackend_->changeCostFunction(cf);
201 }
202 
203 
204 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
206  const typename OptConProblem_t::DynamicsPtr_t& dyn)
207 {
208  nlocBackend_->changeNonlinearSystem(dyn);
209 }
210 
211 
212 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
214  const typename OptConProblem_t::LinearPtr_t& lin)
215 {
216  nlocBackend_->changeLinearSystem(lin);
217 }
218 
219 
220 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
222 {
223  return nlocBackend_->getCost();
224 }
225 
226 
227 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
230 {
231  return nlocBackend_->getSettings();
232 }
233 
234 
235 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
236 const std::shared_ptr<typename NLOptConSolver<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>::Backend_t>&
238 {
239  return nlocBackend_;
240 }
241 
242 
243 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
244 std::vector<
247 {
248  return nlocBackend_->getNonlinearSystemsInstances();
249 }
250 
251 
252 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
253 const std::vector<
256 {
257  return nlocBackend_->getNonlinearSystemsInstances();
258 }
259 
260 
261 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
262 std::vector<
265 {
266  return nlocBackend_->getLinearSystemsInstances();
267 }
268 
269 
270 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
271 const std::vector<
274 {
275  return nlocBackend_->getLinearSystemsInstances();
276 }
277 
278 
279 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
280 std::vector<typename NLOptConSolver<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>::OptConProblem_t::
281  CostFunctionPtr_t>&
283 {
284  return nlocBackend_->getCostFunctionInstances();
285 }
286 
287 
288 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
289 const std::vector<typename NLOptConSolver<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>::OptConProblem_t::
290  CostFunctionPtr_t>&
292 {
293  return nlocBackend_->getCostFunctionInstances();
294 }
295 
296 
297 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
298 std::vector<typename NLOptConSolver<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>::OptConProblem_t::
299  ConstraintPtr_t>&
301 {
302  return nlocBackend_->getInputBoxConstraintsInstances();
303 }
304 
305 
306 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
307 const std::vector<typename NLOptConSolver<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>::OptConProblem_t::
308  ConstraintPtr_t>&
310 {
311  return nlocBackend_->getInputBoxConstraintsInstances();
312 }
313 
314 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
315 std::vector<typename NLOptConSolver<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>::OptConProblem_t::
316  ConstraintPtr_t>&
318 {
319  return nlocBackend_->getStateBoxConstraintsInstances();
320 }
321 
322 
323 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
324 const std::vector<typename NLOptConSolver<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>::OptConProblem_t::
325  ConstraintPtr_t>&
327 {
328  return nlocBackend_->getStateBoxConstraintsInstances();
329 }
330 
331 
332 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
333 std::vector<typename NLOptConSolver<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>::OptConProblem_t::
334  ConstraintPtr_t>&
336 {
337  return nlocBackend_->getGeneralConstraintsInstances();
338 }
339 
340 
341 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
342 const std::vector<typename NLOptConSolver<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>::OptConProblem_t::
343  ConstraintPtr_t>&
345 {
346  return nlocBackend_->getGeneralConstraintsInstances();
347 }
348 
349 
350 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
352  const std::string& fileName)
353 {
354  nlocBackend_->logSummaryToMatlab(fileName);
355 }
356 
357 } // namespace optcon
358 } // namespace ct
virtual const core::StateTrajectory< STATE_DIM, SCALAR > getStateTrajectory() const override
Definition: NLOptConSolver-impl.hpp:152
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
virtual SCALAR getTimeHorizon() const override
Get the time horizon the solver currently operates on.
Definition: NLOptConSolver-impl.hpp:175
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
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
std::vector< typename OptConProblem_t::DynamicsPtr_t > & getNonlinearSystemsInstances() override
get reference to the nonlinear system
Definition: NLOptConSolver-impl.hpp:246
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
bool isSingleShooting() const
return if this is a single-shooting algorithm (or not)
Definition: NLOptConSettings.hpp:306
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
virtual void prepareIteration()
Definition: NLOptConSolver-impl.hpp:74
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
Definition: SingleShooting.hpp:21
Definition: NLOCBackendST.hpp:25
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
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
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
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
Definition: NLOCBackendMP.hpp:31
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
Definition: MultipleShooting.hpp:21
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
int nThreads
save the smallest eigenvalue of the Hessian
Definition: NLOptConSettings.hpp:272
Definition: NLOptConSolver.hpp:29
virtual void prepareMPCIteration()
Definition: NLOptConSolver-impl.hpp:88