aligator 0.18.0
A versatile and efficient C++ library for real-time constrained 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
17#include "aligator/threads.hpp"
18
19#include <boost/unordered_map.hpp>
20#include <boost/version.hpp>
21
22namespace aligator {
23
29template <typename Scalar> struct SolverFDDPTpl {
43 using CallbackPtr = shared_ptr<CallbackBaseTpl<Scalar>>;
44 using CallbackMap = boost::unordered_map<std::string, CallbackPtr,
45 ExtendedStringHash, std::equal_to<>>;
46
48
51 Scalar reg_init;
52 Scalar preg_ = reg_init;
53 Scalar reg_min_ = 1e-9;
54 Scalar reg_max_ = 1e9;
56 Scalar reg_dec_factor_ = 0.1;
58 Scalar reg_inc_factor_ = 10.;
60
61 Scalar th_grad_ = 1e-12;
62 Scalar th_step_dec_ = 0.5;
63 Scalar th_step_inc_ = 0.01;
64
66
69 std::size_t max_iters;
74
76
77 void setNumThreads(const std::size_t num_threads) {
78 num_threads_ = num_threads;
79 omp::set_default_options(num_threads);
80 }
81 std::size_t getNumThreads() const { return num_threads_; }
82
83protected:
85 std::size_t num_threads_;
88
89public:
92
93 SolverFDDPTpl(const Scalar tol = 1e-6,
95 const Scalar reg_init = 1e-9,
96 const std::size_t max_iters = 200);
97
99 void setup(const Problem &problem);
100
111 static Scalar forwardPass(const Problem &problem, const Results &results,
112 Workspace &workspace, const Scalar alpha);
113
120 void updateExpectedImprovement(Workspace &workspace, Results &results) const;
121
128 void expectedImprovement(Workspace &workspace, Scalar &d1, Scalar &d2) const;
129
136 inline Scalar computeInfeasibility(const Problem &problem);
137
139 void backwardPass(const Problem &problem, Workspace &workspace) const;
140
144 ALIGATOR_INLINE void acceptGains(const Workspace &workspace,
145 Results &results) const {
146 assert(workspace.kktRhs.size() == results.gains_.size());
148 results.gains_ = workspace.kktRhs;
149 }
150
151 inline void increaseRegularization() {
153 preg_ = std::min(preg_, reg_max_);
154 }
155
156 inline void decreaseRegularization() {
158 preg_ = std::max(preg_, reg_min_);
159 }
160
162 inline Scalar computeCriterion(Workspace &workspace);
163
165 void registerCallback(std::string_view name, CallbackPtr cb) {
166#if defined(BOOST_VERSION) && BOOST_VERSION >= 107500
167 callbacks_.insert_or_assign(name, cb);
168#else
169 callbacks_.insert_or_assign(std::string(name), cb);
170#endif
171 }
172
173 const CallbackMap &getCallbacks() const { return callbacks_; }
174
175 void removeCallback(std::string_view name) {
176#if defined(BOOST_VERSION) && BOOST_VERSION >= 107500
177 callbacks_.erase(name);
178#else
179 callbacks_.erase(std::string(name));
180#endif
181 }
182
183 auto getCallbackNames() const {
184 std::vector<std::string> keys;
185 for (const auto &item : callbacks_) {
186 keys.push_back(item.first);
187 }
188 return keys;
189 }
190 CallbackPtr getCallback(std::string_view name) const {
191#if defined(BOOST_VERSION) && BOOST_VERSION >= 107500
192 auto cb = callbacks_.find(name);
193#else
194 auto cb = callbacks_.find(std::string(name));
195#endif
196 if (cb != end(callbacks_)) {
197 return cb->second;
198 }
199 return nullptr;
200 }
201
203 void clearCallbacks() { callbacks_.clear(); }
204
205 void invokeCallbacks() {
206 for (const auto &cb : callbacks_) {
207 cb.second->call(workspace_, results_);
208 }
209 }
210
211 bool run(const Problem &problem, const std::vector<VectorXs> &xs_init = {},
212 const std::vector<VectorXs> &us_init = {});
213};
214
215} // namespace aligator
216
217#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
218#include "./solver-fddp.txx"
219#endif
LinesearchOptions< T > Options
#define ALIGATOR_INLINE
Definition fwd.hpp:27
Base structs for linesearch algorithms.
#define ALIGATOR_NOMALLOC_SCOPED
Definition math.hpp:44
::aligator::context::Scalar Scalar
Definition context.hpp:14
void set_default_options(std::size_t num_threads, bool dynamic=true)
Definition threads.hpp:25
Main package namespace.
VerboseLevel
Verbosity level.
Definition fwd.hpp:82
@ QUIET
Definition fwd.hpp:82
Data struct for CostAbstractTpl.
Specific data struct for explicit dynamics ExplicitDynamicsModelTpl.
Explicit forward dynamics model .
Extended hashing function for strings which supports const char* and std::string_view.
A table logging utility to log the trace of the numerical solvers.
Definition logger.hpp:32
Base class for manifolds, to use in cost funcs, solvers...
Q-function model parameters.
std::vector< MatrixXs > gains_
Riccati gains.
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.
boost::unordered_map< std::string, CallbackPtr, ExtendedStringHash, std::equal_to<> > CallbackMap
TrajOptProblemTpl< Scalar > Problem
ExplicitDynamicsModelTpl< Scalar > DynamicsModel
TrajOptDataTpl< Scalar > ProblemData
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.
void setNumThreads(const std::size_t num_threads)
ResultsFDDPTpl< Scalar > Results
std::size_t max_iters
Maximum number of iterations for the solver.
CostDataAbstractTpl< Scalar > CostData
shared_ptr< CallbackBaseTpl< Scalar > > CallbackPtr
StageModelTpl< Scalar > StageModel
std::size_t getNumThreads() const
StageDataTpl< Scalar > StageData
ManifoldAbstractTpl< Scalar > Manifold
QFunctionTpl< Scalar > QParams
ExplicitDynamicsDataTpl< Scalar > ExplicitDynamicsData
ValueFunctionTpl< Scalar > VParams
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.
A stage in the control problem.
Problem data struct.
Trajectory optimization problem.
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