ocra-recipes
Doxygen documentation for the ocra-recipes repository
LinearTask.cpp
Go to the documentation of this file.
3 
4 #include <stdexcept>
5 
6 //using namespace xde; //TODO: we comment that, we should not!!!!
7 
8 namespace ocra
9 {
10  LinearTask::LinearTask(Function& f, Function& L)
11  :NamedInstance("linear task")
14  ,LinearFunction(f.getVariable().getTimeDerivative(), f.getDimension())
15  ,_f(&f), _L(&L)
16  {
17  //check
18  if (_dim != L.getVariable().getSize())
19  throw std::runtime_error("[ocra::LinearTask::LinearTask] Input size of L doesn't match with dimension of f");
20  if (_dim != L.getDimension())
21  throw std::runtime_error("[ocra::LinearTask::LinearTask] Dimension of L doesn't match with dimension of f");
22 
23  //dependencies
26  f.connect<EVT_RESIZE>(*this, &LinearTask::updateDimension);
28  //f.connect<EVT_CHANGE_VALUE, Function>(L, &Function::invalidateAll); //emulate the fact that L takes f as an input
29  }
30 
32  {
35  _f->disconnect<EVT_RESIZE>(*this, &LinearTask::updateDimension);
37  }
38 
39  void LinearTask::updateb() const
40  {
44  _b = _L->getValue();
45  }
46 
47 
48 
50  {
51  VectorXd w(f->getDimension());
52  w.setConstant(weight);
54  }
55 
56 
58  {
60  int n = v->getSize();
61  LinearFunction* L = new DiagonalLinearFunction(*v, weight, 0., true, weight[n-1]);
62  return new LinearTask(*f, *L);
63  }
64 
65 
67  {
68  int n = f->getDimension();
69  double wi = weight / fi;
70  if (n == 0)
71  {
73  LinearFunction* L = new DiagonalLinearFunction(*v, wi, 0., true);
74  return new LinearTask(*f, *L);
75  }
76  else
77  {
78  VectorXd w(n);
79  w.fill(wi);
81  }
82  }
83 
84 
85  LinearTask* LinearTask::createInequalityTaskWithLinearLaw(Function* f, double weight, const VectorXd& fi)
86  {
87  int n = f->getDimension();
88  ocra_assert(fi.size() == n);
89  VectorXd w(n);
90  for (int i=0; i<n; ++i)
91  w[i] = weight / fi[i];
93  }
94 
95 
96  LinearTask* LinearTask::createInequalityTaskWithLinearLaw(Function* f, const VectorXd& weight, double fi)
97  {
98  int n = f->getDimension();
99  ocra_assert(weight.size() == n);
100  VectorXd w(n);
101  double di = 1./fi;
102  for (int i=0; i<n; ++i)
103  w[i] = weight[i] * di;
105  }
106 
107 
108  LinearTask* LinearTask::createInequalityTaskWithLinearLaw(Function* f, const VectorXd& weight, const VectorXd& fi)
109  {
110  int n = f->getDimension();
111  ocra_assert(fi.size() == n);
112  ocra_assert(weight.size() == n);
113  VectorXd w(n);
114  for (int i=0; i<n; ++i)
115  w[i] = weight[i] / fi[i];
117  }
118 
119 
121  {
122  ocra_assert(weightDividedFi.size() == f->getDimension());
124  int n = v->getSize();
125  VectorXd w(weightDividedFi);
126  w*=-1;
127  LinearFunction* L = new DiagonalLinearFunction(*v, w, 0., true, weightDividedFi[n-1]);
128  return new LinearTask(*f, *L);
129  }
130 
132  {
133  _jacobian = _f->getJacobian();
134  }
135 
136 
138  {
139  //do nothing : this overload allows to resize
140  }
141 
142  void LinearTask::doUpdateDimensionBegin(int newDimension)
143  {
144  //do nothing : this overload allows to resize
145  }
146 
147  void LinearTask::doUpdateDimensionEnd(int oldDimension)
148  {
149  assert(_L->getVariable().isBaseVariable() && "only base variable can be resized");
150  static_cast<BaseVariable*>(&_L->getVariable())->resize(_f->getDimension());
151  LinearFunction::doUpdateDimensionEnd(oldDimension); //to resize _b
152  }
153 
154  void LinearTask::updateDimension(int timestamp)
155  {
157  }
158 
159 }
160 
161 namespace ocra
162 {
164  {
165  int n=1;
166  BaseVariable x("x", n);
167 
168  BaseVariable fx("fx", 2*n);
169 
170  MatrixXd id(2*n,n);
171  MatrixXd gain(2*n,2*n);
172  VectorXd limit(2*n);
173  VectorXd zero(2*n);
174  id.setZero();
175  gain.setZero();
176  zero.setZero();
177  for (int i=0; i<n; ++i)
178  {
179  id(i,i) = 1;
180  id(n+i,i) = 1;
181  limit[i] = -i-1; // -upper bound
182  limit[n+i] = -i-1; //lower bound
183  gain(i,i) = 1./(i+1);
184  gain(n+i,n+i) = 1./(n+i+1);
185  }
186  LinearFunction JointLimit(x, id, limit);
187  LinearFunction TaskGain(fx, gain, zero);
188 
189  LinearTask JointTask(JointLimit, TaskGain);
190 
191  VectorXd v(n);
192  for (int i=0; i<n; ++i)
193  std::cin >> v[i];
194  x.setValue(v);
195  VectorXd dv(n);
196  for (int i=0; i<n; ++i)
197  std::cin >> dv[i];
198  x.getTimeDerivative().setValue(dv);
199 
200  std::cout << JointTask.getValue() << std::endl;
201  }
202 }
203 
204 // cmake:sourcegroup=Function
205 
const Variable & getVariable() const
Definition: Function.cpp:46
void desinhibitPropagationFromb() const
int getDimension() const
Definition: Function.cpp:41
Variable * createOutputVariable(Function &f)
void doUpdateDimensionBegin(int newDimension)
Definition: LinearTask.cpp:142
void updateJacobian() const
Definition: LinearTask.cpp:131
void setValue(const VectorXd &value)
Definition: Variable.cpp:99
Variable & x
Definition: Function.h:309
Declaration file of the LinearTask class.
LinearTask class.
Definition: LinearTask.h:37
LinearFunction class.
Function * _f
Definition: LinearTask.h:89
static LinearTask * createInequalityTaskWithLinearLaw(Function *f, double weight, double fi)
Definition: LinearTask.cpp:66
Declaration file of the FunctionProperties class.
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
virtual void doUpdateDimensionEnd(int oldDimension)
Definition: Function.cpp:114
Function class.
Definition: Function.h:77
MatrixXd & _jacobian
Definition: Function.h:313
const MatrixXd & getJacobian() const
Definition: Function.h:375
int getSize() const
Definition: Variable.cpp:81
void resize()
Definition: Function.cpp:86
void invalidateb(int timestamp)
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
void inhibitPropagationFromb() const
const VectorXd & getValue() const
Definition: Function.h:365
void doUpdateDimensionEnd(int oldDimension)
Definition: LinearTask.cpp:147
void doUpdateInputSizeBegin()
Definition: LinearTask.cpp:137
void updateb() const
Definition: LinearTask.cpp:39
void testLinearTask()
Definition: LinearTask.cpp:163
void changeFunctionDimension(int newDimension)
Definition: Function.cpp:56
void invalidateAll()
Definition: Function.h:330
BaseVariable & getTimeDerivative()
Get the time derivative/primitive of the variable.
Definition: Variable.cpp:370
static LinearTask * createEqualityTaskWithLinearLaw(Function *f, double weight)
Definition: LinearTask.cpp:49
Function * _L
Definition: LinearTask.h:90
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
virtual bool isBaseVariable() const
Definition: Variable.cpp:169
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.
Implements a basic variable.
Definition: Variable.h:304
DiagonalLinearFunction class.