aligator  0.9.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
sum-of-costs.hpp
Go to the documentation of this file.
1#pragma once
2
4// Faster than std::unordered_map with Bost 1.80
5// https://martin.ankerl.com/2022/08/27/hashmap-bench-01/#boost__unordered_map
6#include <boost/unordered_map.hpp>
7
8namespace aligator {
9
10template <typename Scalar> struct CostStackDataTpl;
11
19template <typename _Scalar> struct CostStackTpl : CostAbstractTpl<_Scalar> {
20 using Scalar = _Scalar;
24 using PolyCost = xyz::polymorphic<CostBase>;
26 using Manifold = ManifoldAbstractTpl<Scalar>;
27 using CostItem = std::pair<PolyCost, Scalar>;
28 using CostKey = std::variant<std::size_t, std::string>;
29 using CostMap = boost::unordered::unordered_map<CostKey, CostItem>;
30 using CostIterator = typename CostMap::iterator;
31
33
37 bool checkDimension(const CostBase &comp) const;
38
41 CostStackTpl(xyz::polymorphic<Manifold> space, const int nu,
42 const std::vector<PolyCost> &comps = {},
43 const std::vector<Scalar> &weights = {});
44
45 CostStackTpl(xyz::polymorphic<Manifold> space, const int nu,
46 const CostMap &comps)
47 : CostBase(space, nu), components_(comps) {
48 for (const auto &[key, item] : comps) {
49 auto &cost = *item.first;
50 if (!this->checkDimension(cost)) {
51 ALIGATOR_DOMAIN_ERROR(fmt::format(
52 "Cannot add new component due to inconsistent input dimensions "
53 "(got ({:d}, {:d}), expected ({:d}, {:d}))",
54 cost.ndx(), cost.nu, this->ndx(), this->nu));
55 }
56 }
57 }
58
60 CostStackTpl(const PolyCost &cost);
61
62 inline CostItem &addCost(const PolyCost &cost, const Scalar weight = 1.) {
63 const std::size_t size = components_.size();
64 return this->addCost(size, cost, weight);
65 }
66
67 CostItem &addCost(const CostKey &key, const PolyCost &cost,
68 const Scalar weight = 1.);
69
70 inline std::size_t size() const { return components_.size(); }
71
73 template <typename Derived> Derived *getComponent(const CostKey &key) {
74 CostItem &item = components_.at(key);
75 return dynamic_cast<Derived *>(&*item.first);
76 }
77
78 template <typename Derived>
79 const Derived *getComponent(const CostKey &key) const {
80 CostItem &item = components_.at(key);
81 return dynamic_cast<const Derived *>(&*item.first);
82 }
83
84 void evaluate(const ConstVectorRef &x, const ConstVectorRef &u,
85 CostData &data) const;
86
87 void computeGradients(const ConstVectorRef &x, const ConstVectorRef &u,
88 CostData &data) const;
89
90 void computeHessians(const ConstVectorRef &x, const ConstVectorRef &u,
91 CostData &data) const;
92
93 shared_ptr<CostData> createData() const;
94};
95
96namespace {
97template <typename T>
98using CostPtr =
99 xyz::polymorphic<CostAbstractTpl<T>>; //< convenience typedef for rest
100 // of this file
101}
102
103template <typename T>
104xyz::polymorphic<CostStackTpl<T>> operator+(const CostPtr<T> &c1,
105 const CostPtr<T> &c2) {
106 return xyz::polymorphic<CostStackTpl<T>>({c1, c2}, {1., 1.});
107}
108
109template <typename T>
110xyz::polymorphic<CostStackTpl<T>>
111operator+(xyz::polymorphic<CostStackTpl<T>> &&c1, const CostPtr<T> &c2) {
112 c1->addCost(c2, 1.);
113 return c1;
114}
115
116template <typename T>
117xyz::polymorphic<CostStackTpl<T>>
118operator+(xyz::polymorphic<CostStackTpl<T>> &&c1, CostPtr<T> &&c2) {
119 c1->addCost(std::move(c2), 1.);
120 return c1;
121}
122
123template <typename T>
124xyz::polymorphic<CostStackTpl<T>>
125operator+(const xyz::polymorphic<CostStackTpl<T>> &c1, CostPtr<T> &&c2) {
126 c1->addCost(std::move(c2), 1.);
127 return c1;
128}
129
130template <typename T>
131xyz::polymorphic<CostStackTpl<T>>
132operator*(T u, xyz::polymorphic<CostStackTpl<T>> &&c1) {
133 for (auto &[key, item] : c1->components_) {
134 item.second *= u;
135 }
136 return c1;
137}
138
139template <typename _Scalar>
141 using Scalar = _Scalar;
144 using CostKey = typename CostStack::CostKey;
145 using DataMap =
146 boost::unordered::unordered_map<CostKey, shared_ptr<CostData>>;
149};
150} // namespace aligator
151
152#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
153#include "aligator/modelling/costs/sum-of-costs.txx"
154#endif
#define ALIGATOR_DOMAIN_ERROR(msg)
Main package namespace.
xyz::polymorphic< CostStackTpl< T > > operator*(T u, xyz::polymorphic< CostStackTpl< T > > &&c1)
xyz::polymorphic< CostStackTpl< T > > operator+(const CostPtr< T > &c1, const CostPtr< T > &c2)
Stage costs for control problems.
Definition fwd.hpp:65
xyz::polymorphic< Manifold > space
Data struct for CostAbstractTpl.
Definition fwd.hpp:68
CostStackDataTpl(const CostStackTpl< Scalar > &obj)
typename CostStack::CostKey CostKey
boost::unordered::unordered_map< CostKey, shared_ptr< CostData > > DataMap
Weighted sum of multiple cost components.
CostItem & addCost(const PolyCost &cost, const Scalar weight=1.)
CostItem & addCost(const CostKey &key, const PolyCost &cost, const Scalar weight=1.)
void computeGradients(const ConstVectorRef &x, const ConstVectorRef &u, CostData &data) const
std::variant< std::size_t, std::string > CostKey
ManifoldAbstractTpl< Scalar > Manifold
Derived * getComponent(const CostKey &key)
Get component, cast down to the specified type.
xyz::polymorphic< CostBase > PolyCost
CostStackTpl(const PolyCost &cost)
Constructor from a single CostBase instance.
CostStackTpl(xyz::polymorphic< Manifold > space, const int nu, const std::vector< PolyCost > &comps={}, const std::vector< Scalar > &weights={})
Constructor with a specified dimension, and optional vector of components and weights.
boost::unordered::unordered_map< CostKey, CostItem > CostMap
CostStackTpl(xyz::polymorphic< Manifold > space, const int nu, const CostMap &comps)
ALIGATOR_DYNAMIC_TYPEDEFS(Scalar)
void evaluate(const ConstVectorRef &x, const ConstVectorRef &u, CostData &data) const
bool checkDimension(const CostBase &comp) const
Check the dimension of a component.
typename CostMap::iterator CostIterator
const Derived * getComponent(const CostKey &key) const
void computeHessians(const ConstVectorRef &x, const ConstVectorRef &u, CostData &data) const
shared_ptr< CostData > createData() const
std::pair< PolyCost, Scalar > CostItem
std::size_t size() const