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