COPP C++ API
C++ interface for COPP trajectory optimization
Loading...
Searching...
No Matches
objective.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <initializer_list>
4#include <utility>
5#include <vector>
6
7#include "copp/core.hpp"
8
9namespace copp
10{
11
17 enum class ObjectiveKind
18 {
19 Time = 0,
20 Linear = 1,
23 };
24
30 class Objective
31 {
32 public:
36 static Objective Time(double weight = 1.0)
37 {
38 return Objective(ObjectiveKind::Time, weight, {}, {}, {});
39 }
40
46 static Objective Linear(
47 double weight,
50 {
51 return Objective(
53 weight,
54 copy_span(alpha),
55 copy_span(beta),
56 {});
57 }
58
59 static Objective Linear(
60 double weight,
61 std::initializer_list<double> alpha,
62 std::initializer_list<double> beta)
63 {
64 return Linear(weight, Span<const double>(alpha.begin(), alpha.size()),
65 Span<const double>(beta.begin(), beta.size()));
66 }
67
74 {
75 return Objective(
77 weight,
78 {},
79 {},
80 copy_span(normalize));
81 }
82
83 static Objective ThermalEnergy(double weight, std::initializer_list<double> normalize)
84 {
85 return ThermalEnergy(
86 weight,
87 Span<const double>(normalize.begin(), normalize.size()));
88 }
89
94 {
95 return Objective(
97 weight,
98 {},
99 {},
100 copy_span(normalize));
101 }
102
103 static Objective TotalVariationTorque(double weight, std::initializer_list<double> normalize)
104 {
106 weight,
107 Span<const double>(normalize.begin(), normalize.size()));
108 }
109
110 ObjectiveKind kind() const noexcept { return kind_; }
111 double weight() const noexcept { return weight_; }
112
113 Span<const double> alpha() const noexcept
114 {
115 return Span<const double>(alpha_.data(), alpha_.size());
116 }
117
118 Span<const double> beta() const noexcept
119 {
120 return Span<const double>(beta_.data(), beta_.size());
121 }
122
124 {
125 return Span<const double>(normalize_.data(), normalize_.size());
126 }
127
128 private:
129 Objective(
131 double weight,
132 std::vector<double> alpha,
133 std::vector<double> beta,
134 std::vector<double> normalize)
135 : kind_(kind),
136 weight_(weight),
137 alpha_(std::move(alpha)),
138 beta_(std::move(beta)),
139 normalize_(std::move(normalize)) {}
140
141 static std::vector<double> copy_span(Span<const double> values)
142 {
143 if (values.empty())
144 {
145 return {};
146 }
147 return std::vector<double>(values.data(), values.data() + values.size());
148 }
149
151 double weight_ = 1.0;
152 std::vector<double> alpha_;
153 std::vector<double> beta_;
154 std::vector<double> normalize_;
155 };
156
166 namespace objective
167 {
168 inline Objective Time(double weight = 1.0)
169 {
170 return Objective::Time(weight);
171 }
172
174 double weight,
175 Span<const double> alpha,
177 {
178 return Objective::Linear(weight, alpha, beta);
179 }
180
182 double weight,
183 std::initializer_list<double> alpha,
184 std::initializer_list<double> beta)
185 {
186 return Objective::Linear(weight, alpha, beta);
187 }
188
189 inline Objective ThermalEnergy(double weight, Span<const double> normalize)
190 {
191 return Objective::ThermalEnergy(weight, normalize);
192 }
193
194 inline Objective ThermalEnergy(double weight, std::initializer_list<double> normalize)
195 {
196 return Objective::ThermalEnergy(weight, normalize);
197 }
198
199 inline Objective TotalVariationTorque(double weight, Span<const double> normalize)
200 {
201 return Objective::TotalVariationTorque(weight, normalize);
202 }
203
205 double weight,
206 std::initializer_list<double> normalize)
207 {
208 return Objective::TotalVariationTorque(weight, normalize);
209 }
210 } // namespace objective
211
212} // namespace copp
Owning objective descriptor used by C++ COPP solver problem objects.
Definition objective.hpp:31
ObjectiveKind kind() const noexcept
Span< const double > beta() const noexcept
Span< const double > alpha() const noexcept
static Objective Linear(double weight, Span< const double > alpha, Span< const double > beta)
Add a linear torque objective.
Definition objective.hpp:46
static Objective ThermalEnergy(double weight, std::initializer_list< double > normalize)
Definition objective.hpp:83
Span< const double > normalize() const noexcept
static Objective TotalVariationTorque(double weight, std::initializer_list< double > normalize)
static Objective Time(double weight=1.0)
Minimize traversal time.
Definition objective.hpp:36
static Objective TotalVariationTorque(double weight, Span< const double > normalize)
Penalize total variation of torque along the path.
Definition objective.hpp:93
static Objective Linear(double weight, std::initializer_list< double > alpha, std::initializer_list< double > beta)
Definition objective.hpp:59
static Objective ThermalEnergy(double weight, Span< const double > normalize)
Penalize normalized squared torque / thermal energy.
Definition objective.hpp:73
double weight() const noexcept
Non-owning view of a contiguous one-dimensional array.
Definition core.hpp:139
Python-like factory namespace for constructing COPP objective descriptors such as time,...
Objective Linear(double weight, Span< const double > alpha, Span< const double > beta)
Objective ThermalEnergy(double weight, Span< const double > normalize)
Objective TotalVariationTorque(double weight, Span< const double > normalize)
Objective Time(double weight=1.0)
Root namespace for the C++ facade: paths, robots, constraints, matrices, profiles,...
ObjectiveKind
Objective kinds accepted by COPP optimization solvers.
Definition objective.hpp:18