aligator  0.14.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
expose-solver-prox.cpp
Go to the documentation of this file.
1
6
8
9#include <eigenpy/std-unique-ptr.hpp>
10#include <eigenpy/variant.hpp>
11
12namespace aligator {
13namespace python {
14
15using context::Scalar;
17using LinesearchOptions = Linesearch::Options;
18
19static void exposeLinesearch() {
20
21 bp::enum_<LSInterpolation>("LSInterpolation",
22 "Linesearch interpolation scheme.")
23 .value("BISECTION", LSInterpolation::BISECTION)
24 .value("QUADRATIC", LSInterpolation::QUADRATIC)
25 .value("CUBIC", LSInterpolation::CUBIC);
26 bp::class_<Linesearch>("Linesearch", bp::no_init)
27 .def(bp::init<const LinesearchOptions &>(("self"_a, "options")))
28 .def_readwrite("options", &Linesearch::options_);
29 bp::class_<ArmijoLinesearch<Scalar>, bp::bases<Linesearch>>(
30 "ArmijoLinesearch", bp::no_init)
31 .def(bp::init<const LinesearchOptions &>(("self"_a, "options")));
32 bp::class_<LinesearchOptions>("LinesearchOptions", "Linesearch options.",
33 bp::init<>(("self"_a), "Default constructor."))
34 .def_readwrite("armijo_c1", &LinesearchOptions::armijo_c1)
35 .def_readwrite("wolfe_c2", &LinesearchOptions::wolfe_c2)
36 .def_readwrite(
37 "dphi_thresh", &LinesearchOptions::dphi_thresh,
38 "Threshold on the derivative at the initial point; the linesearch "
39 "will be early-terminated if the derivative is below this threshold.")
40 .def_readwrite("alpha_min", &LinesearchOptions::alpha_min,
41 "Minimum step size.")
42 .def_readwrite("max_num_steps", &LinesearchOptions::max_num_steps)
43 .def_readwrite("interp_type", &LinesearchOptions::interp_type,
44 "Interpolation type: bisection, quadratic or cubic.")
45 .def_readwrite("contraction_min", &LinesearchOptions::contraction_min,
46 "Minimum step contraction.")
47 .def_readwrite("contraction_max", &LinesearchOptions::contraction_max,
48 "Maximum step contraction.")
49 .def(bp::self_ns::str(bp::self));
50
51 bp::class_<NonmonotoneLinesearch<Scalar>, bp::bases<Linesearch>>(
52 "NonmonotoneLinesearch", bp::no_init)
53 .def(bp::init<LinesearchOptions>(("self"_a, "options")))
54 .def_readwrite("avg_eta", &NonmonotoneLinesearch<Scalar>::avg_eta)
55 .def_readwrite("beta_dec", &NonmonotoneLinesearch<Scalar>::beta_dec);
56}
57
59 using context::ConstVectorRef;
60 using context::Results;
62 using context::VectorRef;
64
66
67 bp::enum_<LQSolverChoice>("LQSolverChoice")
68 .value("LQ_SOLVER_SERIAL", LQSolverChoice::SERIAL)
69 .value("LQ_SOLVER_PARALLEL", LQSolverChoice::PARALLEL)
70 .value("LQ_SOLVER_STAGEDENSE", LQSolverChoice::STAGEDENSE)
71 .export_values();
72
73 bp::class_<Workspace, bp::bases<WorkspaceBaseTpl<Scalar>>,
74 boost::noncopyable>(
75 "Workspace", "Workspace for ProxDDP.",
76 bp::init<const TrajOptProblem &>(("self"_a, "problem")))
77 .def_readonly("lqr_problem", &Workspace::lqr_problem,
78 "Buffers for the LQ subproblem.")
79 .def_readonly("Lxs", &Workspace::Lxs)
80 .def_readonly("Lus", &Workspace::Lus)
81 .def_readonly("Lds", &Workspace::Lds)
82 .def_readonly("Lvs", &Workspace::Lvs)
83 .def_readonly("dxs", &Workspace::dxs)
84 .def_readonly("dus", &Workspace::dus)
85 .def_readonly("dvs", &Workspace::dvs)
86 .def_readonly("dlams", &Workspace::dlams)
87 .def_readonly("trial_vs", &Workspace::trial_vs)
88 .def_readonly("trial_lams", &Workspace::trial_lams)
89 .def_readonly("lams_plus", &Workspace::lams_plus)
90 .def_readonly("lams_pdal", &Workspace::lams_pdal)
91 .def_readonly("vs_plus", &Workspace::vs_plus)
92 .def_readonly("vs_pdal", &Workspace::vs_pdal)
93 .def_readonly("shifted_constraints", &Workspace::shifted_constraints)
94 .def_readonly("cstr_proj_jacs", &Workspace::cstr_proj_jacs)
95 .def_readonly("inner_crit", &Workspace::inner_criterion)
96 .def_readonly("active_constraints", &Workspace::active_constraints)
97 .def_readonly("prev_xs", &Workspace::prev_xs)
98 .def_readonly("prev_us", &Workspace::prev_us)
99 .def_readonly("prev_lams", &Workspace::prev_lams)
100 .def_readonly("prev_vs", &Workspace::prev_vs)
101 .def_readonly("stage_infeasibilities", &Workspace::stage_infeasibilities)
102 .def_readonly("state_dual_infeas", &Workspace::state_dual_infeas)
103 .def_readonly("control_dual_infeas", &Workspace::control_dual_infeas)
105
106 bp::class_<Results, bp::bases<ResultsBaseTpl<Scalar>>, boost::noncopyable>(
107 "Results", "Results struct for proxDDP.",
108 bp::init<const TrajOptProblem &>(("self"_a, "problem")))
109 .def("cycleAppend", &Results::cycleAppend, ("self"_a, "problem", "x0"),
110 "Cycle the results.")
111 .def_readonly("al_iter", &Results::al_iter)
112 .def_readonly("lams", &Results::lams)
114
115 using SolverType = SolverProxDDPTpl<Scalar>;
116 using ls_variant_t = SolverType::LinesearchVariant::variant_t;
117
118 auto cls =
119 bp::class_<SolverType, boost::noncopyable>(
120 "SolverProxDDP",
121 "A proximal, augmented Lagrangian solver, using a DDP-type scheme to "
122 "compute "
123 "search directions and feedforward, feedback gains."
124 " The solver instance initializes both a Workspace and a Results "
125 "struct.",
126 bp::init<const Scalar, const Scalar, std::size_t, VerboseLevel,
128 ("self"_a, "tol", "mu_init"_a = 1e-2, "max_iters"_a = 1000,
129 "verbose"_a = VerboseLevel::QUIET,
131 "hess_approx"_a = HessianApprox::GAUSS_NEWTON)))
132 .def("cycleProblem", &SolverType::cycleProblem,
133 ("self"_a, "problem", "data"),
134 "Cycle the problem data (for MPC applications).")
135 .def_readwrite("bcl_params", &SolverType::bcl_params,
136 "BCL parameters.")
137 .def_readwrite("max_refinement_steps",
138 &SolverType::max_refinement_steps_)
139 .def_readwrite("refinement_threshold",
140 &SolverType::refinement_threshold_)
141 .def_readwrite("linear_solver_choice",
142 &SolverType::linear_solver_choice)
143 .def_readwrite("multiplier_update_mode",
144 &SolverType::multiplier_update_mode)
145 .def_readwrite("mu_init", &SolverType::mu_init,
146 "Initial AL penalty parameter.")
147 .add_property("mu", &SolverType::mu)
148 .def_readwrite(
149 "rollout_max_iters", &SolverType::rollout_max_iters,
150 "Maximum number of iterations when solving the forward dynamics.")
151 .def_readwrite("max_al_iters", &SolverType::max_al_iters,
152 "Maximum number of AL iterations.")
153 .def_readwrite("ls_mode", &SolverType::ls_mode, "Linesearch mode.")
154 .def_readwrite("sa_strategy", &SolverType::sa_strategy_,
155 "StepAcceptance strategy.")
156 .def_readwrite("rollout_type", &SolverType::rollout_type_,
157 "Rollout type.")
158 .def_readwrite("dual_weight", &SolverType::dual_weight,
159 "Dual penalty weight.")
160 .def_readwrite("reg_min", &SolverType::reg_min,
161 "Minimum regularization value.")
162 .def_readwrite("reg_max", &SolverType::reg_max,
163 "Maximum regularization value.")
164 .def("updateLQSubproblem", &SolverType::updateLQSubproblem, "self"_a)
165 .def("computeCriterion", &SolverType::computeCriterion, "self"_a,
166 "Compute problem stationarity.")
167 .add_property("linearSolver",
168 bp::make_getter(&SolverType::linearSolver_,
169 eigenpy::ReturnInternalStdUniquePtr{}),
170 "Linear solver for the semismooth Newton method.")
171 .def_readwrite("filter", &SolverType::filter_,
172 "Pair filter used to accept a step.")
173 .def("computeInfeasibilities", &SolverType::computeInfeasibilities,
174 ("self"_a, "problem"), "Compute problem infeasibilities.")
175 .add_property("num_threads", &SolverType::getNumThreads)
176 .def("setNumThreads", &SolverType::setNumThreads,
177 ("self"_a, "num_threads"))
178 .add_property("target_dual_tol", &SolverType::getDualTolerance)
179 .def("setDualTolerance", &SolverType::setDualTolerance,
180 ("self"_a, "tol"),
181 "Manually set the solver's dual infeasibility tolerance. Once "
182 "this method is called, the dual tolerance and primal tolerance "
183 "(target_tol) will not be synced when the latter changes and "
184 "`solver.run()` is called.")
186 .add_property("linesearch",
187 bp::make_function(
188 +[](const SolverType &s) -> const ls_variant_t & {
189 return s.linesearch_;
190 },
191 eigenpy::ReturnInternalVariant<ls_variant_t>{}))
192 .def("run", &SolverType::run,
193 ("self"_a, "problem", "xs_init"_a = bp::list(),
194 "us_init"_a = bp::list(), "vs_init"_a = bp::list(),
195 "lams_init"_a = bp::list()),
196 "Run the algorithm. Can receive initial guess for "
197 "multiplier trajectory.");
198
199 {
200 using AlmParams = SolverType::AlmParams;
201 bp::scope scope{cls};
202#define _c(name) def_readwrite(#name, &AlmParams::name)
203 bp::class_<AlmParams>("AlmParams", "Parameters for the ALM algorithm",
204 bp::init<>("self"_a))
205 ._c(prim_alpha)
206 ._c(prim_beta)
207 ._c(dual_alpha)
208 ._c(dual_beta)
209 ._c(mu_update_factor)
210 ._c(dyn_al_scale)
211 ._c(mu_lower_bound);
212#undef _c
213 }
214}
215
216} // namespace python
217} // namespace aligator
WorkspaceTpl< Scalar > Workspace
Definition context.hpp:45
ResultsTpl< Scalar > Results
Definition context.hpp:46
TrajOptProblemTpl< Scalar > TrajOptProblem
Definition context.hpp:35
The Python bindings.
Definition blk-matrix.hpp:5
Linesearch< Scalar > Linesearch
Linesearch::Options LinesearchOptions
static void exposeLinesearch()
Main package namespace.
VerboseLevel
Verbosity level.
Definition fwd.hpp:82
@ QUIET
Definition fwd.hpp:82
StepAcceptanceStrategy
Whether to use linesearch or filter during step acceptance phase.
Definition enums.hpp:29
HessianApprox
Definition enums.hpp:14
@ GAUSS_NEWTON
Use the Gauss-Newton approximation.
Definition enums.hpp:18
Definitions for the proximal trajectory optimization algorithm.
Scalar avg_eta
Weight for moving average.
A proximal, augmented Lagrangian-type solver for trajectory optimization.