- 3.0.2 core module.
BarrierActivation.hpp
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include "ActivationBase.hpp"
10 
11 namespace ct {
12 namespace core {
13 
14 namespace tpl {
15 
28 template <typename SCALAR, typename TRAIT = typename ct::core::tpl::TraitSelector<SCALAR>::Trait>
29 class BarrierActivation : public ActivationBase<SCALAR>
30 {
31 public:
33  BarrierActivation() : ub_active_(false), lb_active_(false), ub_((SCALAR)0.0), lb_((SCALAR)0.0), alpha_((SCALAR)0.0)
34  {
35  }
42  BarrierActivation(const SCALAR& ub, const SCALAR& lb, const SCALAR& alpha = SCALAR(1.0))
43  : ub_active_(true), lb_active_(true), ub_(ub), lb_(lb), alpha_(alpha)
44  {
45  }
47  virtual ~BarrierActivation() {}
49  virtual void loadConfigFile(const std::string& filename, const std::string& termName, bool verbose = false)
50  {
51  boost::property_tree::ptree pt;
52  boost::property_tree::read_info(filename, pt);
53 
54  // load the steepness multiplier
55  alpha_ = pt.get<SCALAR>(termName + ".alpha");
56 
57  try
58  {
59  // if there is no or a wrongly specified upper bound
60  ub_ = pt.get<SCALAR>(termName + ".upper_bound");
61  } catch (std::exception& e)
62  {
63  // we set the upper bound inactive
64  std::cout << e.what() << std::endl;
65  ub_active_ = false;
66  }
67 
68  try
69  {
70  // if there is no or a wrongly specified lower bound
71  lb_ = pt.get<SCALAR>(termName + ".lower_bound");
72  } catch (std::exception& e)
73  {
74  // we set the lower bound inactive
75  std::cout << e.what() << std::endl;
76  lb_active_ = false;
77  }
78 
79  // do some checks
80  if ((lb_active_ && ub_active_) && (lb_ > ub_))
81  {
82  throw std::runtime_error("BarrierActivation: lower bound cannot be greater than upper bound.");
83  }
84  if (alpha_ < 0)
85  {
86  throw std::runtime_error("BarrierActivation: alpha must be >= 0");
87  }
88  }
89 
90 
96  void setLowerBoundOnly(const SCALAR& lb, const SCALAR& alpha = (SCALAR)1.0)
97  {
98  lb_active_ = true;
99  ub_active_ = false;
100  lb_ = lb;
101  alpha_ = alpha;
102  }
103 
109  void setUpperBoundOnly(const SCALAR& ub, const SCALAR& alpha = (SCALAR)1.0)
110  {
111  lb_active_ = false;
112  ub_active_ = true;
113  ub_ = ub;
114  alpha_ = alpha;
115  }
116 
118  virtual SCALAR computeActivation(const SCALAR x) override
119  {
120  return (SCALAR)ub_active_ * TRAIT::exp(alpha_ * (x - ub_)) +
121  (SCALAR)lb_active_ * TRAIT::exp(alpha_ * (lb_ - x));
122  }
124  virtual SCALAR firstOrderDerivative(const SCALAR x) override
125  {
126  return (SCALAR)ub_active_ * alpha_ * TRAIT::exp(alpha_ * (x - ub_)) -
127  (SCALAR)lb_active_ * alpha_ * TRAIT::exp(alpha_ * (lb_ - x));
128  }
131  {
132  return (SCALAR)ub_active_ * alpha_ * alpha_ * TRAIT::exp(alpha_ * (x - ub_)) +
133  (SCALAR)lb_active_ * alpha_ * alpha_ * TRAIT::exp(alpha_ * (lb_ - x));
134  }
136  virtual void printInfo()
137  {
138  if (ub_active_)
139  {
140  std::cout << "Barrier Activation with upper bound " << ub_ << ", steepness-multiplier " << alpha_
141  << std::endl;
142  }
143  if (lb_active_)
144  {
145  std::cout << "Barrier Activation with lower bound " << lb_ << ", steepness-multiplier " << alpha_
146  << std::endl;
147  }
148  }
149 
150 protected:
161 };
162 } // namespace tpl
163 
165 } // namespace core
166 } // namespace ct
virtual ~BarrierActivation()
destructor
Definition: BarrierActivation.hpp:47
void setUpperBoundOnly(const SCALAR &ub, const SCALAR &alpha=(SCALAR) 1.0)
activate the upper bound while deactivating the lower bound
Definition: BarrierActivation.hpp:109
virtual void loadConfigFile(const std::string &filename, const std::string &termName, bool verbose=false)
load activations from file
Definition: BarrierActivation.hpp:49
Definition: BarrierActivation.hpp:29
CppAD::AD< CppAD::cg::CG< double > > SCALAR
void setLowerBoundOnly(const SCALAR &lb, const SCALAR &alpha=(SCALAR) 1.0)
activate the lower bound while deactivating the upper bound.
Definition: BarrierActivation.hpp:96
SCALAR lb_
the upper bound
Definition: BarrierActivation.hpp:158
BarrierActivation(const SCALAR &ub, const SCALAR &lb, const SCALAR &alpha=SCALAR(1.0))
constructor with input arguments for upper and lower bound
Definition: BarrierActivation.hpp:42
BarrierActivation()
trivial constructor, which deactivates upper and lower bounds.
Definition: BarrierActivation.hpp:33
Definition: ActivationBase.hpp:18
const bool verbose
Definition: JacobianCGTest.h:19
bool lb_active_
boolean indicating of lower bound is activated
Definition: BarrierActivation.hpp:154
virtual SCALAR computeActivation(const SCALAR x) override
compute activation multiplier based on scalar input
Definition: BarrierActivation.hpp:118
SCALAR alpha_
steepness-multiplier (controls how &#39;aggressive&#39; the boundary is)
Definition: BarrierActivation.hpp:160
SCALAR ub_
the upper bound
Definition: BarrierActivation.hpp:156
virtual void printInfo()
print to console
Definition: BarrierActivation.hpp:136
virtual SCALAR secondOrderDerivative(const SCALAR x)
second order derivative of this activation
Definition: BarrierActivation.hpp:130
virtual SCALAR firstOrderDerivative(const SCALAR x) override
first order derivative of this activation
Definition: BarrierActivation.hpp:124
bool ub_active_
boolean indicating if upper bound is activated
Definition: BarrierActivation.hpp:152