7#include <Eigen/SparseCore>
13template <
typename InType,
typename OutScalar>
15 const Eigen::DenseBase<InType> &input,
16 Eigen::SparseMatrix<OutScalar> &out,
bool update) {
17 assert(i0 + input.rows() <= out.rows() &&
"Inconsistent rows");
18 assert(j0 + input.cols() <= out.cols() &&
"Inconsistent cols");
20 for (Index i = 0; i < input.rows(); i++) {
21 for (Index j = 0; j < input.cols(); j++) {
23 out.coeffRef(i0 + i, j0 + j) = input(i, j);
25 out.insert(i0 + i, j0 + j) = input(i, j);
30template <
typename Scalar>
32 Eigen::SparseMatrix<Scalar> &out,
bool update) {
34 assert(i0 <= i1 &&
"i0 should be lesser than i1. Can't assign empty range.");
35 assert(i1 <= out.rows() &&
"Inconsistent rows");
36 assert(i1 <= out.cols() &&
"Inconsistent cols");
37 for (Index kk = i0; kk < i1; kk++) {
39 out.coeffRef(kk, kk) = value;
41 out.insert(kk, kk) = value;
46template <
typename Scalar>
48 const Scalar mueq, Eigen::SparseMatrix<Scalar> &mat,
49 Eigen::Matrix<Scalar, -1, 1> &rhs,
bool update);
51template <
typename Scalar>
61 bool verbose =
false);
64template <
typename Scalar>
66 const auto &knots = problem.
stages;
68 const size_t N = knots.size() - 1UL;
70 for (
size_t t = 0; t <= N; t++) {
71 const auto &model = knots[t];
72 nrows += model.nx + model.nu + model.nc;
80template <
typename Scalar>
95 lbdas[0] = solution.head(nc0);
98 for (
size_t t = 0; t <= N; t++) {
101 auto seg = solution.segment(idx, n);
102 xs[t] = seg.head(knot.
nx);
103 us[t] = seg.segment(knot.
nx, knot.
nu);
104 vs[t] = seg.segment(knot.
nx + knot.
nu, knot.
nc);
107 lbdas[t + 1] = solution.segment(idx, knot.
nx2);
113template <
typename Scalar>
116 std::vector<VectorXs> xs;
117 std::vector<VectorXs> us;
118 std::vector<VectorXs> vs;
119 std::vector<VectorXs> lbdas;
127 lbdas[0].setZero(problem.
nc0());
128 for (
uint i = 0; i <= N; i++) {
130 xs[i].setZero(kn.
nx);
131 us[i].setZero(kn.
nu);
132 vs[i].setZero(kn.
nc);
135 lbdas[i + 1].setZero(kn.
nx2);
137 if (problem.
stages.back().nu == 0) {
140 return std::make_tuple(std::move(xs), std::move(us), std::move(vs),
144#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
146 const LqrProblemTpl<context::Scalar> &problem,
const context::Scalar mueq,
147 Eigen::SparseMatrix<context::Scalar> &mat, context::VectorXs &rhs,
149extern template std::array<context::Scalar, 3>
151 const LqrProblemTpl<context::Scalar> &,
152 boost::span<const context::VectorXs>, boost::span<const context::VectorXs>,
153 boost::span<const context::VectorXs>, boost::span<const context::VectorXs>,
void sparseAssignDiagonal(Eigen::Index i0, Eigen::Index i1, Scalar value, Eigen::SparseMatrix< Scalar > &out, bool update)
void sparseAssignDenseBlock(Eigen::Index i0, Eigen::Index j0, const Eigen::DenseBase< InType > &input, Eigen::SparseMatrix< OutScalar > &out, bool update)
Helper to assign a dense matrix into a range of coefficients of a sparse matrix.
void lqrCreateSparseMatrix(const LqrProblemTpl< Scalar > &problem, const Scalar mueq, Eigen::SparseMatrix< Scalar > &mat, Eigen::Matrix< Scalar, -1, 1 > &rhs, bool update)
auto lqrInitializeSolution(const LqrProblemTpl< Scalar > &problem)
void lqrDenseSolutionToTraj(const LqrProblemTpl< Scalar > &problem, const typename math_types< Scalar >::ConstVectorRef solution, std::vector< typename math_types< Scalar >::VectorXs > &xs, std::vector< typename math_types< Scalar >::VectorXs > &us, std::vector< typename math_types< Scalar >::VectorXs > &vs, std::vector< typename math_types< Scalar >::VectorXs > &lbdas)
Convert dense RHS solution to its trajectory [x,u,v,lambda] solution.
std::array< Scalar, 3 > lqrComputeKktError(const LqrProblemTpl< Scalar > &problem, boost::span< const typename math_types< Scalar >::VectorXs > xs, boost::span< const typename math_types< Scalar >::VectorXs > us, boost::span< const typename math_types< Scalar >::VectorXs > vs, boost::span< const typename math_types< Scalar >::VectorXs > lbdas, const Scalar mueq, const std::optional< typename math_types< Scalar >::ConstVectorRef > &theta=std::nullopt, bool verbose=false)
uint lqrNumRows(const LqrProblemTpl< Scalar > &problem)
Compute the number of rows in the problem matrix.
Struct describing a stage of a constrained LQ problem.
int horizon() const noexcept
uint nc0() const noexcept
Dimension of the initial condition constraint.
Typedefs for math (Eigen vectors, matrices) depending on scalar type.