aligator  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
solver-fddp.hpp
Go to the documentation of this file.
1
6#pragma once
7
11
12#include "workspace.hpp"
13#include "results.hpp"
14
16#include "aligator/threads.hpp"
17
18#include <boost/unordered_map.hpp>
19
20namespace aligator {
21
27template <typename Scalar> struct SolverFDDPTpl {
35 using Manifold = ManifoldAbstractTpl<Scalar>;
41 using CallbackPtr = shared_ptr<CallbackBaseTpl<Scalar>>;
42 using CallbackMap = boost::unordered_map<std::string, CallbackPtr>;
43
45
48 Scalar reg_init;
49 Scalar preg_ = reg_init;
50 Scalar reg_min_ = 1e-9;
51 Scalar reg_max_ = 1e9;
53 Scalar reg_dec_factor_ = 0.1;
55 Scalar reg_inc_factor_ = 10.;
57
58 Scalar th_grad_ = 1e-12;
59 Scalar th_step_dec_ = 0.5;
60 Scalar th_step_inc_ = 0.01;
61
62 typename Linesearch<Scalar>::Options ls_params;
63
64 VerboseLevel verbose_;
66 std::size_t max_iters;
71
73
74 void setNumThreads(const std::size_t num_threads) {
75 num_threads_ = num_threads;
76 omp::set_default_options(num_threads);
77 }
78 std::size_t getNumThreads() const { return num_threads_; }
79
80protected:
82 std::size_t num_threads_;
85
86public:
89
90 SolverFDDPTpl(const Scalar tol = 1e-6,
91 VerboseLevel verbose = VerboseLevel::QUIET,
92 const Scalar reg_init = 1e-9,
93 const std::size_t max_iters = 200);
94
96 void setup(const Problem &problem);
97
108 static Scalar forwardPass(const Problem &problem, const Results &results,
109 Workspace &workspace, const Scalar alpha);
110
117 void updateExpectedImprovement(Workspace &workspace, Results &results) const;
118
125 void expectedImprovement(Workspace &workspace, Scalar &d1, Scalar &d2) const;
126
133 inline Scalar computeInfeasibility(const Problem &problem);
134
136 void backwardPass(const Problem &problem, Workspace &workspace) const;
137
141 ALIGATOR_INLINE void acceptGains(const Workspace &workspace,
142 Results &results) const {
143 assert(workspace.kktRhs.size() == results.gains_.size());
145 results.gains_ = workspace.kktRhs;
146 }
147
148 inline void increaseRegularization() {
150 preg_ = std::min(preg_, reg_max_);
151 }
152
153 inline void decreaseRegularization() {
155 preg_ = std::max(preg_, reg_min_);
156 }
157
159 inline Scalar computeCriterion(Workspace &workspace);
160
162 void registerCallback(const std::string &name, CallbackPtr cb) {
163 callbacks_[name] = cb;
164 }
165
166 const CallbackMap &getCallbacks() const { return callbacks_; }
167 void removeCallback(const std::string &name) { callbacks_.erase(name); }
168 auto getCallbackNames() const {
169 std::vector<std::string> keys;
170 for (const auto &item : callbacks_) {
171 keys.push_back(item.first);
172 }
173 return keys;
174 }
175 CallbackPtr getCallback(const std::string &name) const {
176 auto cb = callbacks_.find(name);
177 if (cb != end(callbacks_)) {
178 return cb->second;
179 }
180 return nullptr;
181 }
182
184 void clearCallbacks() { callbacks_.clear(); }
185
186 void invokeCallbacks(Workspace &workspace, Results &results) {
187 for (const auto &cb : callbacks_) {
188 cb.second->call(workspace, results);
189 }
190 }
191
192 bool run(const Problem &problem, const std::vector<VectorXs> &xs_init = {},
193 const std::vector<VectorXs> &us_init = {});
194
195 static const ExplicitDynamicsData &
196 stage_get_dynamics_data(const StageDataTpl<Scalar> &data);
197};
198
199} // namespace aligator
200
201#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
202#include "./solver-fddp.txx"
203#endif
#define ALIGATOR_NOMALLOC_SCOPED
#define ALIGATOR_INLINE
Definition macros.hpp:13
void set_default_options(std::size_t, bool=true)
Definition threads.hpp:38
Main package namespace.
Data struct for CostAbstractTpl.
Definition fwd.hpp:65
Specific data struct for explicit dynamics ExplicitDynamicsModelTpl.
Definition fwd.hpp:80
Explicit forward dynamics model .
Definition fwd.hpp:77
A table logging utility to log the trace of the numerical solvers.
Definition logger.hpp:31
Q-function model parameters.
std::vector< MatrixXs > gains_
Riccati gains.
The feasible DDP (FDDP) algorithm, from Mastalli et al. (2020).
void updateExpectedImprovement(Workspace &workspace, Results &results) const
Pre-compute parts of the directional derivatives – this is done before linesearch.
WorkspaceFDDPTpl< Scalar > Workspace
void setup(const Problem &problem)
Allocate workspace and results structs.
TrajOptProblemTpl< Scalar > Problem
Scalar reg_inc_factor_
Regularization increase factor.
static Scalar forwardPass(const Problem &problem, const Results &results, Workspace &workspace, const Scalar alpha)
Perform a nonlinear rollout, keeping an infeasibility gap.
void expectedImprovement(Workspace &workspace, Scalar &d1, Scalar &d2) const
Finish computing the directional derivatives – this is done within linesearch.
Linesearch< Scalar >::Options ls_params
Scalar reg_dec_factor_
Regularization decrease factor.
boost::unordered_map< std::string, CallbackPtr > CallbackMap
void setNumThreads(const std::size_t num_threads)
std::size_t max_iters
Maximum number of iterations for the solver.
shared_ptr< CallbackBaseTpl< Scalar > > CallbackPtr
std::size_t getNumThreads() const
ManifoldAbstractTpl< Scalar > Manifold
std::size_t num_threads_
Number of threads to use when evaluating the problem or its derivatives.
CallbackMap callbacks_
Callbacks.
SolverFDDPTpl(const Scalar tol=1e-6, VerboseLevel verbose=VerboseLevel::QUIET, const Scalar reg_init=1e-9, const std::size_t max_iters=200)
Data struct for stage models StageModelTpl.
Definition fwd.hpp:93
A stage in the control problem.
Definition fwd.hpp:90
Problem data struct.
Definition fwd.hpp:107
Trajectory optimization problem.
Definition fwd.hpp:104
Storage for the value function model parameters.
Workspace for solver SolverFDDP.
Definition workspace.hpp:10
std::vector< MatrixXs > kktRhs
Buffer for KKT system right-hand sides.
Definition workspace.hpp:27