20struct ActivationBoundsTpl {
21 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
23 typedef _Scalar Scalar;
25 typedef typename MathBase::VectorXs VectorXs;
26 typedef typename MathBase::MatrixXs MatrixXs;
28 ActivationBoundsTpl(
const VectorXs& lower,
const VectorXs& upper,
29 const Scalar b = Scalar(1.))
30 : lb(lower), ub(upper), beta(b) {
31 if (lb.size() != ub.size()) {
32 throw_pretty(
"Invalid argument: " 33 <<
"The lower and upper bounds don't have the same " 34 "dimension (lb,ub dimensions equal to " +
35 std::to_string(lb.size()) +
"," +
36 std::to_string(ub.size()) +
", respectively)");
38 if (beta < Scalar(0) || beta > Scalar(1.)) {
40 "Invalid argument: " <<
"The range of beta is between 0 and 1");
42 for (std::size_t i = 0; i < static_cast<std::size_t>(lb.size()); ++i) {
43 if (isfinite(lb(i)) && isfinite(ub(i))) {
44 if (lb(i) - ub(i) > Scalar(0)) {
45 throw_pretty(
"Invalid argument: " 46 <<
"The lower and upper bounds are badly defined; ub " 47 "has to be bigger / equals to lb");
51 if (!isfinite(lb(i))) {
52 lb(i) = -std::numeric_limits<Scalar>::max();
54 if (!isfinite(ub(i))) {
55 ub(i) = std::numeric_limits<Scalar>::max();
59 if (beta >= Scalar(0) && beta <= Scalar(1.)) {
60 for (std::size_t i = 0; i < static_cast<std::size_t>(lb.size()); ++i) {
62 if (lb(i) != (-std::numeric_limits<Scalar>::max()) &&
63 ub(i) != (std::numeric_limits<Scalar>::max())) {
64 Scalar m = Scalar(0.5) * (lb(i) + ub(i));
65 Scalar d = Scalar(0.5) * (ub(i) - lb(i));
74 ActivationBoundsTpl(
const ActivationBoundsTpl& other)
75 : lb(other.lb), ub(other.ub), beta(other.beta) {}
76 ActivationBoundsTpl() : beta(Scalar(1.)) {}
78 template <
typename NewScalar>
79 ActivationBoundsTpl<NewScalar> cast()
const {
80 typedef ActivationBoundsTpl<NewScalar> ReturnType;
81 ReturnType res(lb.template cast<NewScalar>(), ub.template cast<NewScalar>(),
82 scalar_cast<NewScalar>(beta));
86 ActivationBoundsTpl& operator=(
const ActivationBoundsTpl& other) {
99 const ActivationBoundsTpl& bounds) {
104 void print(std::ostream& os)
const {
105 typedef typename ScalarSelector<Scalar>::type PrintableScalar;
106 os <<
"ActivationBounds {lb=" 107 << lb.template cast<PrintableScalar>().transpose()
108 <<
", ub=" << ub.template cast<PrintableScalar>().transpose()
109 <<
", beta=" << scalar_cast<PrintableScalar>(beta) <<
"}";
118class ActivationModelQuadraticBarrierTpl
119 :
public ActivationModelAbstractTpl<_Scalar> {
121 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
123 ActivationModelQuadraticBarrierTpl)
125 typedef _Scalar Scalar;
127 typedef ActivationModelAbstractTpl<Scalar> Base;
131 typedef typename MathBase::VectorXs VectorXs;
132 typedef typename MathBase::MatrixXs MatrixXs;
134 explicit ActivationModelQuadraticBarrierTpl(
const ActivationBounds& bounds)
135 : Base(bounds.lb.size()), bounds_(bounds) {};
136 virtual ~ActivationModelQuadraticBarrierTpl() =
default;
138 virtual void calc(
const std::shared_ptr<ActivationDataAbstract>& data,
139 const Eigen::Ref<const VectorXs>& r)
override {
140 if (
static_cast<std::size_t
>(r.size()) != nr_) {
142 "Invalid argument: " <<
"r has wrong dimension (it should be " +
143 std::to_string(nr_) +
")");
146 std::shared_ptr<Data> d = std::static_pointer_cast<Data>(data);
148 d->rlb_min = (r - bounds_.lb).array().min(Scalar(0.));
149 d->rub_max = (r - bounds_.ub).array().max(Scalar(0.));
150 data->a_value = Scalar(0.5) * d->rlb_min.matrix().squaredNorm() +
151 Scalar(0.5) * d->rub_max.matrix().squaredNorm();
154 virtual void calcDiff(
const std::shared_ptr<ActivationDataAbstract>& data,
155 const Eigen::Ref<const VectorXs>& r)
override {
156 if (
static_cast<std::size_t
>(r.size()) != nr_) {
158 "Invalid argument: " <<
"r has wrong dimension (it should be " +
159 std::to_string(nr_) +
")");
162 std::shared_ptr<Data> d = std::static_pointer_cast<Data>(data);
163 data->Ar = (d->rlb_min + d->rub_max).matrix();
165 using pinocchio::internal::if_then_else;
166 for (Eigen::Index i = 0; i < data->Arr.cols(); i++) {
167 data->Arr.diagonal()[i] = if_then_else(
168 pinocchio::internal::LE, r[i] - bounds_.lb[i], Scalar(0.), Scalar(1.),
169 if_then_else(pinocchio::internal::GE, r[i] - bounds_.ub[i],
170 Scalar(0.), Scalar(1.), Scalar(0.)));
174 virtual std::shared_ptr<ActivationDataAbstract> createData()
override {
175 return std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(),
this);
178 template <
typename NewScalar>
179 ActivationModelQuadraticBarrierTpl<NewScalar> cast()
const {
180 typedef ActivationModelQuadraticBarrierTpl<NewScalar> ReturnType;
181 ReturnType res(bounds_.template cast<NewScalar>());
185 const ActivationBounds& get_bounds()
const {
return bounds_; };
186 void set_bounds(
const ActivationBounds& bounds) { bounds_ = bounds; };
193 virtual void print(std::ostream& os)
const override {
194 os <<
"ActivationModelQuadraticBarrier {nr=" << nr_ <<
"}";
201 ActivationBounds bounds_;
205struct ActivationDataQuadraticBarrierTpl
206 :
public ActivationDataAbstractTpl<_Scalar> {
207 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
209 typedef _Scalar Scalar;
211 typedef typename MathBase::ArrayXs ArrayXs;
212 typedef typename MathBase::VectorXs VectorXs;
213 typedef typename MathBase::DiagonalMatrixXs DiagonalMatrixXs;
214 typedef ActivationDataAbstractTpl<Scalar> Base;
216 template <
typename Activation>
217 explicit ActivationDataQuadraticBarrierTpl(Activation*
const activation)
219 rlb_min(activation->get_nr()),
220 rub_max(activation->get_nr()) {