11 template <
size_t STATE_DIM,
size_t CONTROL_DIM>
18 int SELECT[2 * STATE_DIM];
19 int N = 2 * STATE_DIM;
20 double WR[T.ColsAtCompileTime];
21 double WI[T.ColsAtCompileTime];
30 int TCols = schur_matrix_t::ColsAtCompileTime;
33 dtrsen_(
"N",
"V", &SELECT[0], &TCols, T.data(), &N, U.data(), &N, &WR[0], &WI[0], &MS, &S, &SEP, WORKDUMMY, &LWORK,
34 &IWORKQUERY[0], &LIWORK, &INFO);
36 LWORK_ = WORKDUMMY[0] + 32;
37 LIWORK_ = IWORKQUERY[0] + 32;
40 IWORK_.resize(LIWORK_);
44 std::cout <<
"Lapack invocation of dtrsen failed!" << std::endl;
50 template <
size_t STATE_DIM,
size_t CONTROL_DIM>
58 bool useIterativeSolver)
63 R_inverse.diagonal().noalias() = R.diagonal().cwiseInverse();
67 R_inverse.noalias() = R.inverse();
71 M << A, -B * R_inverse * B.transpose(), -Q, -A.transpose();
73 if (useIterativeSolver)
74 return solveSchurIterative(M, P);
76 return solveSchurDirect(M, P);
79 template <
size_t STATE_DIM,
size_t CONTROL_DIM>
85 const bool RisDiagonal,
86 const bool useIterativeSolver)
91 solve(Q, R, A, B, P, RisDiagonal, Rinv, useIterativeSolver);
96 template <
size_t STATE_DIM,
size_t CONTROL_DIM>
102 bool converged =
false;
109 if (iterations > maxIterations)
116 converged = Mnew.isApprox(Mlocal, epsilon);
124 state_matrix_t M11(Mlocal.template block<STATE_DIM, STATE_DIM>(0, 0));
125 state_matrix_t M12(Mlocal.template block<STATE_DIM, STATE_DIM>(0, STATE_DIM));
126 state_matrix_t M21(Mlocal.template block<STATE_DIM, STATE_DIM>(STATE_DIM, 0));
127 state_matrix_t M22(Mlocal.template block<STATE_DIM, STATE_DIM>(STATE_DIM, STATE_DIM));
133 U.template block<STATE_DIM, STATE_DIM>(0, 0) = M12;
134 U.template block<STATE_DIM, STATE_DIM>(STATE_DIM, 0) = M22 + state_matrix_t::Identity();
136 V.template block<STATE_DIM, STATE_DIM>(0, 0) = M11 + state_matrix_t::Identity();
137 V.template block<STATE_DIM, STATE_DIM>(STATE_DIM, 0) = M21;
141 FullPivLU_.compute(U);
143 P = FullPivLU_.solve(-V);
148 template <
size_t STATE_DIM,
size_t CONTROL_DIM>
152 const bool computeU =
true;
153 schur_.compute(M, computeU);
155 if (schur_.info() != Eigen::Success)
157 throw std::runtime_error(
158 "LQR Schur computation failed. Most likely problem is set up wrongly or not solvable.");
164 int SELECT[2 * STATE_DIM];
165 double WR[2 * STATE_DIM];
166 double WI[2 * STATE_DIM];
171 int N = 2 * STATE_DIM;
173 for (
size_t i = 0;
i < 2 * STATE_DIM;
i++)
176 if (
i == (2 * STATE_DIM - 1) || std::abs(T(
i + 1,
i)) < 1e-12)
178 SELECT[
i] =
static_cast<int>(T(
i,
i) < 0);
183 SELECT[
i] =
static_cast<int>((T(
i,
i) + T(
i + 1,
i + 1)) / 2.0 < 0);
184 SELECT[
i + 1] = SELECT[
i];
189 dtrsen_(
"N",
"V", &SELECT[0], &N, T.data(), &N, U.data(), &N, &WR[0], &WI[0], &MS, &S, &SEP, WORK_.data(), &LWORK_,
190 IWORK_.data(), &LIWORK_, &INFO);
192 const state_matrix_t& U11 = U.template block<STATE_DIM, STATE_DIM>(0, 0);
193 const state_matrix_t& U21 = U.template block<STATE_DIM, STATE_DIM>(STATE_DIM, 0);
196 P.noalias() = U21 * U11.inverse();
205 throw std::runtime_error(
206 "solveSchurDirect() in CARE can only be used if the lapack library is installed on your system.");
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, const bool RisDiagonal=false, const bool useIterativeSolver=false)
Definition: CARE-impl.hpp:80
CARE()
Definition: CARE-impl.hpp:12
Continuous-Time Algebraic Riccati Equation.
Definition: CARE.hpp:46
Eigen::Matrix< double, 2 *STATE_DIM, 2 *STATE_DIM > schur_matrix_t
Definition: CARE.hpp:57
Eigen::Matrix< double, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: CARE.hpp:52
for i
Definition: mpc_unittest_plotting.m:14
bool solve(const state_matrix_t &Q, const control_matrix_t &R, const state_matrix_t &A, const control_gain_matrix_t &B, state_matrix_t &P, bool RisDiagonal, control_matrix_t &R_inverse, bool useIterativeSolver=false)
Definition: CARE-impl.hpp:51
Eigen::Matrix< double, STATE_DIM, CONTROL_DIM > control_gain_matrix_t
Definition: CARE.hpp:54
Eigen::Matrix< double, nStates, nStates > state_matrix_t
Eigen::Matrix< double, 2 *STATE_DIM, STATE_DIM > factor_matrix_t
Definition: CARE.hpp:58