aligator  0.6.1
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>
13void xs_default_init(const TrajOptProblemTpl<Scalar> &problem,
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>
37void us_default_init(const TrajOptProblemTpl<Scalar> &problem,
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>
48auto problemInitializeSolution(const TrajOptProblemTpl<Scalar> &problem) {
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_condition_->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 Scalar>
76 const TrajOptProblemTpl<Scalar> &problem,
77 const typename math_types<Scalar>::VectorOfVectors &xs_init,
78 const typename math_types<Scalar>::VectorOfVectors &us_init,
79 typename math_types<Scalar>::VectorOfVectors &xs_out,
80 typename math_types<Scalar>::VectorOfVectors &us_out) {
81 const std::size_t nsteps = problem.numSteps();
82 xs_out.reserve(nsteps + 1);
83 us_out.reserve(nsteps);
84 if (xs_init.size() == 0) {
85 xs_default_init(problem, xs_out);
86 } else {
87 if (xs_init.size() != (nsteps + 1)) {
88 ALIGATOR_RUNTIME_ERROR("warm-start for xs has wrong size!");
89 }
90 xs_out = xs_init;
91 }
92 if (us_init.size() == 0) {
93 us_default_init(problem, us_out);
94 } else {
95 if (us_init.size() != nsteps) {
96 ALIGATOR_RUNTIME_ERROR("warm-start for us has wrong size!");
97 }
98 us_out = us_init;
99 }
100}
101
102} // namespace aligator
#define ALIGATOR_RUNTIME_ERROR(msg)
Definition exceptions.hpp:6
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.
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)