ocra-recipes
Doxygen documentation for the ocra-recipes repository
SumOfLinearFunctions.cpp
Go to the documentation of this file.
4 
5 namespace ocra
6 {
7  SumOfLinearFunctions::SumOfLinearFunctions(int dimension)
8  : NamedInstance("sum of linear functions")
10  , CoupledInputOutputSize(false)
11  , LinearFunction(*new CompositeVariable("SumOfLinearFunctionsVariable"), dimension)
12  , _variable("SumOfLinearFunctionsVariable")
13  , _offset(VectorXd::Zero(dimension))
14  {
15  static_cast<CompositeVariable&>(x).add( const_cast<CompositeVariable&>(_variable.getVariable()) );
16  _b.setZero();
17  }
18 
20  {
21  while(!_functions.empty())
22  removeFunction(*_functions.back().first);
23 
24  disconnectVariable(); // since... (see two line below)
25 
26  delete &x; // we allocated it ourselves
27  }
28 
30  {
31  if(f.getDimension() != _dim)
32  throw std::runtime_error("[ocra::SumOfLinearFunctions::addFunction] f must have the dimension set at construction");
33 
34  for (size_t i=0; i<_functions.size(); ++i)
35  {
36  if (_functions[i].first == &f)
37  {
38  _functions[i].second += scale;
39  return *this;
40  }
41  }
42 
43  _functions.push_back(std::make_pair(&f,scale));
44  _variable.insert(&f.getVariable());
45  _variable.recomputeVariable();
48  f.connect<EVT_RESIZE>(*this, &SumOfLinearFunctions::checkThatFunctionsDimensionDoNotChange);
49 
50  invalidateAll();
51  invalidateb(-1);
52 
53  return *this;
54  }
55 
57  {
58  for (size_t i=0; i<_functions.size(); ++i)
59  {
60  if (_functions[i].first == &f)
61  {
62  _functions[i].second -= scale;
63  if (std::abs(_functions[i].second) < 1.e-8)
64  {
65  f.disconnect<EVT_RESIZE>(*this, &SumOfLinearFunctions::checkThatFunctionsDimensionDoNotChange);
68  _variable.remove(&f.getVariable());
69  _variable.recomputeVariable();
70  _functions.erase(_functions.begin() + i);
71  }
72  invalidateAll();
73  invalidateb(-1);
74  return *this;
75  }
76  }
77  ocra_assert(false && "the function to remove was not found");
78 
79  return *this;
80  }
81 
83  {
84  for (size_t i=0;i<_functions.size(); ++i)
85  {
86  if (_functions[i].first == &f)
87  {
88  f.disconnect<EVT_RESIZE>(*this, &SumOfLinearFunctions::checkThatFunctionsDimensionDoNotChange);
91  _variable.remove(&f.getVariable());
92  _variable.recomputeVariable();
93  _functions.erase(_functions.begin() + i);
94  invalidateAll();
95  invalidateb(-1);
96  return *this;
97  }
98  }
99  ocra_assert(false && "the function to remove was not found");
100 
101  return *this;
102  }
103 
105  {
106  if(!_variable.isVariableUpToDate())
107  _variable.recomputeVariable();
108 
109  _jacobian.setZero();
110  for (size_t i=0; i<_functions.size(); ++i)
111  utils::addCompressedByCol(_functions[i].first->getJacobian(), _jacobian, _variable.find(&_functions[i].first->getVariable())->getMapping(), _functions[i].second);
112  }
113 
115  {
116  _b = _offset;
117  for (size_t i=0; i<_functions.size(); ++i)
118  _b += _functions[i].second * _functions[i].first->getb();
119  }
120 
122  {
123  // does nothing : this overload allows to resize
124  }
125 
126  void SumOfLinearFunctions::doChangeA(const MatrixXd& A)
127  {
128  throw std::runtime_error("[ocra::SumOfLinearFunctions::changeA] invalid operation on SumOfLinearFunctions");
129  }
130 
131  void SumOfLinearFunctions::doChangeb(const VectorXd& b)
132  {
133  ocra_assert(b.size()==getDimension() && "Wrong size for b in sum of linear function");
134  _offset = b;
135  invalidateb(-1);
136  }
137 
138  void SumOfLinearFunctions::checkThatFunctionsDimensionDoNotChange(int timestamp)
139  {
140  for (size_t i=0; i<_functions.size(); ++i)
141  if(_functions[i].first->getDimension() != _dim)
142  throw std::runtime_error("[ocra::SumOfLinearFunctions::checkThatFunctionsDimensionDoNotChange] Dimension of function " + _functions[i].first->getName() + " changed!");
143  }
144 }
145 
146 // cmake:sourcegroup=Function
SumOfLinearFunctions & removeFunction(LinearFunction &f, double scale)
const Variable & getVariable() const
Definition: Function.cpp:46
void addCompressedByCol(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
int getDimension() const
Definition: Function.cpp:41
void insert(Variable *var)
Variable & x
Definition: Function.h:309
bool isVariableUpToDate() const
void disconnectVariable()
Definition: Function.cpp:31
LinearFunction class.
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
MatrixXd & _jacobian
Definition: Function.h:313
AbilitySet & add(eFunctionAbility prop)
Definition: AbilitySet.h:96
void invalidateb(int timestamp)
void remove(Variable *var)
void recomputeVariable() const
void doChangeA(const MatrixXd &A)
VariableMapping * find(Variable *var) const
void invalidateAll()
Definition: Function.h:330
void doChangeb(const VectorXd &b)
SumOfLinearFunctions & addFunction(LinearFunction &f, double scale=1.)
A concatenation of base variables and other composite variables.
Definition: Variable.h:357
CompositeVariable & getVariable()
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
const int & _dim
Definition: Function.h:320
void disconnect(Derived &object, void(Base::*callbackToErase)(int)) const
Disconnect non-static method.
void connect(Derived &object, void(Base::*newCallback)(int)) const
Call this method to register a non-static method as a callback.
Declaration file of the VariableMapping class.