aligator  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
solver-util.hpp
Go to the documentation of this file.
1
4#pragma once
5
7
8namespace aligator {
9
12template <typename Scalar>
14 std::vector<typename math_types<Scalar>::VectorXs> &xs) {
15 const std::size_t nsteps = problem.numSteps();
16 xs.resize(nsteps + 1);
17 if (problem.initCondIsStateError()) {
18 xs[0] = problem.getInitState();
19 } else {
20 if (problem.stages_.size() > 0) {
21 xs[0] = problem.stages_[0]->xspace().neutral();
22 } else {
24 "The problem should have either a StateErrorResidual as an initial "
25 "condition or at least one stage.");
26 }
27 }
28 for (std::size_t i = 0; i < nsteps; i++) {
29 const StageModelTpl<Scalar> &sm = *problem.stages_[i];
30 xs[i + 1] = sm.xspace_next().neutral();
31 }
32}
33
36template <typename Scalar>
38 std::vector<typename math_types<Scalar>::VectorXs> &us) {
39 const std::size_t nsteps = problem.numSteps();
40 us.resize(nsteps);
41 for (std::size_t i = 0; i < nsteps; i++) {
42 const StageModelTpl<Scalar> &sm = *problem.stages_[i];
43 us[i] = sm.uspace().neutral();
44 }
45}
46
47template <typename Scalar>
49 using VectorXs = typename math_types<Scalar>::VectorXs;
50 std::vector<VectorXs> xs, us, vs, lbdas;
51 const size_t nsteps = problem.numSteps();
52 xs_default_init(problem, xs);
53 us_default_init(problem, us);
54 // initialize multipliers...
55 vs.resize(nsteps + 1);
56 lbdas.resize(nsteps + 1);
57 lbdas[0].setZero(problem.init_constraint_->nr);
58 for (size_t i = 0; i < nsteps; i++) {
59 const StageModelTpl<Scalar> &sm = *problem.stages_[i];
60 lbdas[i + 1].setZero(sm.ndx2());
61 vs[i].setZero(sm.nc());
62 }
63
64 if (!problem.term_cstrs_.empty()) {
65 vs[nsteps].setZero(problem.term_cstrs_.totalDim());
66 }
67
68 return std::make_tuple(std::move(xs), std::move(us), std::move(vs),
69 std::move(lbdas));
70}
71
74template <typename T1, typename T2>
75[[nodiscard]] bool assign_no_resize(const std::vector<T1> &lhs,
76 std::vector<T2> &rhs) {
77 static_assert(std::is_base_of_v<Eigen::EigenBase<T1>, T1>,
78 "T1 should be an Eigen object!");
79 static_assert(std::is_base_of_v<Eigen::EigenBase<T2>, T2>,
80 "T2 should be an Eigen object!");
81 if (lhs.size() != rhs.size())
82 return false;
83
84 const auto same_dims = [](auto &x, auto &y) {
85 return (x.cols() == y.cols()) && (x.rows() == y.rows());
86 };
87
88 for (std::size_t i = 0; i < lhs.size(); i++) {
89 if (!same_dims(lhs[i], rhs[i]))
90 return false;
91 rhs[i] = lhs[i];
92 }
93 return true;
94}
95
98template <typename Scalar>
100 const TrajOptProblemTpl<Scalar> &problem,
101 const typename math_types<Scalar>::VectorOfVectors &xs_init,
102 const typename math_types<Scalar>::VectorOfVectors &us_init,
103 typename math_types<Scalar>::VectorOfVectors &xs_out,
104 typename math_types<Scalar>::VectorOfVectors &us_out) {
105 const std::size_t nsteps = problem.numSteps();
106 xs_out.reserve(nsteps + 1);
107 us_out.reserve(nsteps);
108 if (xs_init.empty()) {
109 xs_default_init(problem, xs_out);
110 } else if (!assign_no_resize(xs_init, xs_out)) {
111 ALIGATOR_RUNTIME_ERROR("warm-start for xs has wrong size!");
112 }
113 if (us_init.empty()) {
114 us_default_init(problem, us_out);
115 } else if (!assign_no_resize(us_init, us_out)) {
116 ALIGATOR_RUNTIME_ERROR("warm-start for us has wrong size!");
117 }
118}
119
120} // namespace aligator
#define ALIGATOR_RUNTIME_ERROR(...)
Definition exceptions.hpp:7
Main package namespace.
void check_trajectory_and_assign(const TrajOptProblemTpl< Scalar > &problem, const typename math_types< Scalar >::VectorOfVectors &xs_init, const typename math_types< Scalar >::VectorOfVectors &us_init, typename math_types< Scalar >::VectorOfVectors &xs_out, typename math_types< Scalar >::VectorOfVectors &us_out)
Check the input state-control trajectory is a consistent warm-start for the output.
bool assign_no_resize(const std::vector< T1 > &lhs, std::vector< T2 > &rhs)
Assign a vector of Eigen types into another, ensure there is no resize.
void us_default_init(const TrajOptProblemTpl< Scalar > &problem, std::vector< typename math_types< Scalar >::VectorXs > &us)
Default-initialize a controls trajectory from the neutral element of each control space.
void xs_default_init(const TrajOptProblemTpl< Scalar > &problem, std::vector< typename math_types< Scalar >::VectorXs > &xs)
Default-intialize a trajectory to the neutral states for each state space at each stage.
auto problemInitializeSolution(const TrajOptProblemTpl< Scalar > &problem)
A stage in the control problem.
Definition fwd.hpp:90
const Manifold & xspace_next() const
const Manifold & uspace() const
int nc() const
Total number of constraints.
Trajectory optimization problem.
Definition fwd.hpp:104
xyz::polymorphic< UnaryFunction > init_constraint_
Initial condition.
ConstraintStackTpl< Scalar > term_cstrs_
Terminal constraints.
std::size_t numSteps() const
std::vector< xyz::polymorphic< StageModel > > stages_
Stages of the control problem.
ConstVectorRef getInitState() const
Get initial state constraint.