- 3.0.2 optimal control module.
SingleShooting-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 
10 
11 namespace ct {
12 namespace optcon {
13 
14 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
16  std::shared_ptr<Backend_t>& backend_,
17  const Settings_t& settings)
18  : Base(backend_)
19 {
20 }
21 
22 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
24 {
25  this->backend_->configure(settings);
26 }
27 
28 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
30  const Policy_t& initialGuess)
31 {
32  this->backend_->setInitialGuess(initialGuess);
33 }
34 
35 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
37 {
39 
40  return finishIteration();
41 }
42 
43 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
45 {
46  if (!this->backend_->isInitialized())
47  throw std::runtime_error("SingleShooting is not initialized!");
48 
49  if (!this->backend_->isConfigured())
50  throw std::runtime_error("SingleShooting is not configured!");
51 
52  this->backend_->checkProblem();
53 }
54 
55 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
57 {
58  int K = this->backend_->getNumSteps();
59 
60  bool debugPrint = this->backend_->getSettings().debugPrint;
61 
62  // if first iteration, compute shots and rollout and cost!
63  if (this->backend_->iteration() == 0)
64  {
65  if (!this->backend_->nominalRollout())
66  throw std::runtime_error("Rollout failed. System became unstable");
67 
68  this->backend_->updateCosts();
69  }
70 
71 #ifdef MATLAB_FULL_LOG
72  if (this->backend_->iteration() == 0)
73  this->backend_->logInitToMatlab();
74 #endif
75 
76  auto start = std::chrono::steady_clock::now();
77  auto startEntire = start;
78 
79  // set box constraints and do LQ approximation
80  this->backend_->setInputBoxConstraintsForLQOCProblem();
81  this->backend_->setStateBoxConstraintsForLQOCProblem();
82  this->backend_->computeLQApproximation(0, K - 1);
83  auto end = std::chrono::steady_clock::now();
84  auto diff = end - start;
85  if (debugPrint)
86  std::cout << "[SingleShooting]: Computing LQ approximation took "
87  << std::chrono::duration<double, std::milli>(diff).count() << " ms" << std::endl;
88 
89  end = std::chrono::steady_clock::now();
90  diff = end - startEntire;
91  if (debugPrint)
92  std::cout << "[SingleShooting]: Forward pass took " << std::chrono::duration<double, std::milli>(diff).count()
93  << " ms" << std::endl;
94 
95  if (debugPrint)
96  std::cout << "[SingleShooting]: #2 Solve LQOC Problem" << std::endl;
97 
98  start = std::chrono::steady_clock::now();
99  this->backend_->solveFullLQProblem();
100  this->backend_->extractSolution();
101  end = std::chrono::steady_clock::now();
102  diff = end - start;
103  if (debugPrint)
104  std::cout << "[SingleShooting]: Solving LQOC problem took "
105  << std::chrono::duration<double, std::milli>(diff).count() << " ms" << std::endl;
106 
107  // update solutions and line-search
108  if (debugPrint)
109  std::cout << "[SingleShooting]: #3 LineSearch" << std::endl;
110 
111  start = std::chrono::steady_clock::now();
112  bool foundBetter = this->backend_->lineSearch();
113  end = std::chrono::steady_clock::now();
114  diff = end - start;
115  if (debugPrint)
116  std::cout << "[SingleShooting]: Line search took " << std::chrono::duration<double, std::milli>(diff).count()
117  << " ms" << std::endl;
118 
119  diff = end - startEntire;
120  if (debugPrint)
121  std::cout << "[SingleShooting]: finishIteration took "
122  << std::chrono::duration<double, std::milli>(diff).count() << " ms" << std::endl;
123 
124  this->backend_->printSummary();
125 
126 #ifdef MATLAB_FULL_LOG
127  this->backend_->logToMatlab(this->backend_->iteration());
128 #endif
129 
130  this->backend_->iteration()++;
131 
132  return foundBetter;
133 }
134 
135 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
137 {
139 }
140 
141 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
143 {
144  finishIteration();
145  return true;
146 }
147 
148 } // namespace optcon
149 } // namespace ct
Definition: NLOCAlgorithm.hpp:19
virtual void configure(const Settings_t &settings) override
configure the solver
Definition: SingleShooting-impl.hpp:23
virtual bool runIteration() override
runIteration combines prepareIteration and finishIteration
Definition: SingleShooting-impl.hpp:36
SingleShooting(std::shared_ptr< Backend_t > &backend_, const Settings_t &settings)
constructor
Definition: SingleShooting-impl.hpp:15
std::shared_ptr< Backend_t > backend_
Definition: NLOCAlgorithm.hpp:47
Settings for the NLOptCon algorithm.
Definition: NLOptConSettings.hpp:198
virtual bool finishMPCIteration() override
Definition: SingleShooting-impl.hpp:142
virtual void prepareIteration() override
Definition: SingleShooting-impl.hpp:44
virtual void prepareMPCIteration() override
Definition: SingleShooting-impl.hpp:136
virtual bool finishIteration() override
Definition: SingleShooting-impl.hpp:56
Base::Policy_t Policy_t
Definition: SingleShooting.hpp:31
virtual void setInitialGuess(const Policy_t &initialGuess) override
set an initial guess
Definition: SingleShooting-impl.hpp:29