proxsuite-nlp  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear programming on manifolds.
Loading...
Searching...
No Matches
cost-sum.hpp
Go to the documentation of this file.
1
3#pragma once
4
6
7namespace proxsuite {
8namespace nlp {
9
12template <typename _Scalar> struct CostSumTpl : CostFunctionBaseTpl<_Scalar> {
13public:
14 using Scalar = _Scalar;
17 using BasePtr = shared_ptr<Base>;
18
19 std::vector<BasePtr> components_;
20 std::vector<Scalar> weights_;
21
22 CostSumTpl(int nx, int ndx) : Base(nx, ndx) {}
23
25 CostSumTpl(int nx, int ndx, const std::vector<BasePtr> &comps,
26 const std::vector<Scalar> &weights)
27 : Base(nx, ndx), components_(comps), weights_(weights) {
28 assert(components_.size() == weights_.size());
29 }
30
31 std::size_t numComponents() const { return components_.size(); }
32
33 auto clone() const { return std::make_shared<CostSumTpl>(*this); }
34
35 Scalar call(const ConstVectorRef &x) const {
36 Scalar result_ = 0.;
37 for (std::size_t i = 0; i < numComponents(); i++) {
38 result_ += weights_[i] * components_[i]->call(x);
39 }
40 return result_;
41 }
42
43 void computeGradient(const ConstVectorRef &x, VectorRef out) const {
44 out.setZero();
45 for (std::size_t i = 0; i < numComponents(); i++) {
46 out.noalias() = out + weights_[i] * components_[i]->computeGradient(x);
47 }
48 }
49
50 void computeHessian(const ConstVectorRef &x, MatrixRef out) const {
51 out.setZero();
52 for (std::size_t i = 0; i < numComponents(); i++) {
53 out.noalias() = out + weights_[i] * components_[i]->computeHessian(x);
54 }
55 }
56
57 /* CostSum API definition */
58
59 void addComponent(shared_ptr<Base> comp, const Scalar w = 1.) {
60 components_.push_back(comp);
61 weights_.push_back(w);
62 }
63
64 CostSumTpl<Scalar> &operator+=(const shared_ptr<Base> &other) {
65 addComponent(other);
66 return *this;
67 }
68
70 components_.insert(components_.end(), other.components_.begin(),
71 other.components_.end());
72 weights_.insert(weights_.end(), other.weights_.begin(),
73 other.weights_.end());
74 return *this;
75 }
76
78 for (auto &weight : weights_) {
79 weight = rhs * weight;
80 }
81 return *this;
82 }
83
84 // printing
85 friend std::ostream &operator<<(std::ostream &ostr,
86 const CostSumTpl<Scalar> &cost) {
87 const std::size_t nc = cost.numComponents();
88 ostr << "CostSum(num_components=" << nc;
89 ostr << ", weights=(";
90 for (std::size_t i = 0; i < nc; i++) {
91 ostr << cost.weights_[i];
92 if (i < nc - 1)
93 ostr << ", ";
94 }
95 ostr << ")";
96 ostr << ")";
97 return ostr;
98 }
99
100 friend auto operator*(CostSumTpl const &self,
101 Scalar a) -> shared_ptr<CostSumTpl> {
102 auto out = self.clone();
103 (*out) *= a;
104 return out;
105 }
106
107 friend auto operator*(Scalar a, CostSumTpl const &self) { return self * a; }
108
109 friend auto operator-(CostSumTpl const &self) {
110 return self * static_cast<Scalar>(-1.);
111 }
112};
113
114} // namespace nlp
115} // namespace proxsuite
116
117#include "proxsuite-nlp/cost-sum.hxx"
118
119#ifdef PROXSUITE_NLP_ENABLE_TEMPLATE_INSTANTIATION
120#include "proxsuite-nlp/cost-sum.txx"
121#endif
#define PROXSUITE_NLP_DYNAMIC_TYPEDEFS(Scalar)
Definition math.hpp:26
Main package namespace.
Definition bcl-params.hpp:5
Base class for differentiable cost functions.
Definition fwd.hpp:89
Defines the sum of one or more cost functions .
Definition cost-sum.hpp:12
friend auto operator-(CostSumTpl const &self)
Definition cost-sum.hpp:109
CostSumTpl< Scalar > & operator+=(const shared_ptr< Base > &other)
Definition cost-sum.hpp:64
friend auto operator*(CostSumTpl const &self, Scalar a) -> shared_ptr< CostSumTpl >
Definition cost-sum.hpp:100
CostSumTpl< Scalar > & operator+=(const CostSumTpl< Scalar > &other)
Definition cost-sum.hpp:69
void computeHessian(const ConstVectorRef &x, MatrixRef out) const
Definition cost-sum.hpp:50
friend auto operator*(Scalar a, CostSumTpl const &self)
Definition cost-sum.hpp:107
std::vector< BasePtr > components_
Definition cost-sum.hpp:19
Scalar call(const ConstVectorRef &x) const
Evaluate the cost function.
Definition cost-sum.hpp:35
CostSumTpl< Scalar > & operator*=(Scalar rhs)
Definition cost-sum.hpp:77
void computeGradient(const ConstVectorRef &x, VectorRef out) const
Definition cost-sum.hpp:43
shared_ptr< Base > BasePtr
Definition cost-sum.hpp:17
CostSumTpl(int nx, int ndx)
cost component weights
Definition cost-sum.hpp:22
void addComponent(shared_ptr< Base > comp, const Scalar w=1.)
Definition cost-sum.hpp:59
std::vector< Scalar > weights_
component sub-costs
Definition cost-sum.hpp:20
CostSumTpl(int nx, int ndx, const std::vector< BasePtr > &comps, const std::vector< Scalar > &weights)
Constructor with a predefined vector of components.
Definition cost-sum.hpp:25
friend std::ostream & operator<<(std::ostream &ostr, const CostSumTpl< Scalar > &cost)
Definition cost-sum.hpp:85
std::size_t numComponents() const
Definition cost-sum.hpp:31