- 3.0.2 optimal control module.
DARE-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, typename SCALAR>
13 {
14 }
15 
16 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
19  const control_matrix_t& R,
20  const state_matrix_t& A,
21  const control_gain_matrix_t& B,
23  bool verbose,
24  const SCALAR eps,
25  size_t maxIter)
26 {
27  // If initialized to zero, in the next iteration P becomes Q. Directly initializing to Q avoids the extra iteration.
28  return computeSteadyStateRiccatiMatrix(Q, R, A, B, Q, K, verbose, eps, maxIter);
29 }
30 
31 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
34  const control_matrix_t& R,
35  const state_matrix_t& A,
36  const control_gain_matrix_t& B,
39  bool verbose,
40  const SCALAR eps,
41  size_t maxIter)
42 {
43  state_matrix_t P_prev;
44  size_t numIter = 0;
45 
46  SCALAR diff = 1;
47 
48  while (diff >= eps && numIter < maxIter)
49  {
50  P_prev = P;
51  dynamicRDE_.iterateRobust(Q, R, A, B, P, K);
52  diff = (P - P_prev).cwiseAbs().maxCoeff();
53  if (!K.allFinite())
54  throw std::runtime_error("DARE : Failed to converge - K is unstable.");
55  numIter++;
56  }
57 
58  if (diff >= eps)
59  throw std::runtime_error("DARE : Failed to converge - maximum number of iterations reached.");
60 
61  if (verbose)
62  {
63  std::cout << "DARE : converged after " << numIter << " iterations out of a maximum of " << maxIter << std::endl;
64  std::cout << "Resulting K: " << K << std::endl;
65  }
66 
67  return P;
68 }
69 
70 } // namespace optcon
71 } // namespace ct
Eigen::Matrix< SCALAR, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: DARE.hpp:30
DARE()
Definition: DARE-impl.hpp:12
Eigen::Matrix< SCALAR, STATE_DIM, CONTROL_DIM > control_gain_matrix_t
Definition: DARE.hpp:32
CppAD::AD< CppAD::cg::CG< double > > SCALAR
Eigen::Matrix< SCALAR, CONTROL_DIM, STATE_DIM > control_feedback_t
Definition: DARE.hpp:33
Discrete-Time Algebraic Riccati Equation.
Definition: DARE.hpp:24
Eigen::Matrix< double, nStates, nStates > state_matrix_t
const bool verbose
Definition: ConstraintComparison.h:18
state_matrix_t computeSteadyStateRiccatiMatrix(const state_matrix_t &Q, const control_matrix_t &R, const state_matrix_t &A, const control_gain_matrix_t &B, control_feedback_t &K, bool verbose=false, const SCALAR eps=1e-6, size_t maxIter=1000)
Definition: DARE-impl.hpp:18