ocra-recipes
Doxygen documentation for the ocra-recipes repository
DoubleDiagonalLinearFunction.cpp
Go to the documentation of this file.
2 
3 namespace ocra
4 {
5  DoubleDiagonalLinearFunction::DoubleDiagonalLinearFunction(Variable& x,
6  const double diagonalElementValue1, const double diagonalElementValue2, const double vectorElementValue,
7  const bool useDefaultValue)
8  :NamedInstance("double diagonal linear function")
11  , LinearFunction(x, x.getSize()/2)
12  , _useDefaultValue(useDefaultValue)
13  , _defaultDiagonalValue1(diagonalElementValue1)
14  , _defaultDiagonalValue2(diagonalElementValue2)
15  , _defaultbValue(vectorElementValue)
16  {
17  _d1.resize(_dim);
18  _d1.setConstant(diagonalElementValue1);
19  _d2.resize(_dim);
20  _d2.setConstant(diagonalElementValue2);
21  buildA();
22  _b.resize(_dim);
23  _b.setConstant(vectorElementValue);
24  }
25 
26 
28  {
29  if (d.size() != _dim)
30  throw std::runtime_error("[ocra::DoubleDiagonalLinearFunction::changeDiagonal1] Size of diagonal d doesn't match the function dimension");
31  _d1 = d;
32  buildA();
33  }
34 
36  {
37  if (d.size() != _dim)
38  throw std::runtime_error("[ocra::DoubleDiagonalLinearFunction::changeDiagonal2] Size of diagonal d doesn't match the function dimension");
39  _d2 = d;
40  buildA();
41  }
42 
43  void DoubleDiagonalLinearFunction::changeDiagonal1(const double diagonalElementValue, const bool changeDefault)
44  {
45  _d1.setConstant(diagonalElementValue);
46  if (changeDefault)
47  _defaultDiagonalValue1 = diagonalElementValue;
48  buildA();
49  }
50 
51  void DoubleDiagonalLinearFunction::changeDiagonal2(const double diagonalElementValue, const bool changeDefault)
52  {
53  _d2.setConstant(diagonalElementValue);
54  if (changeDefault)
55  _defaultDiagonalValue2 = diagonalElementValue;
56  buildA();
57  }
58 
60  {
62  }
63 
65  {
67  }
68 
70  {
71  _defaultbValue = v;
72  }
73 
75  {
76  //test if A is double diagonal
77  ocra_assert(A.block(0,0,_dim,_dim).isDiagonal());
78  ocra_assert(A.block(0,_dim,_dim,_dim).isDiagonal());
79 
81  }
82 
83 
85  {
86  const VectorXd& x_value = x.getValue();
87  _value = _d1.asDiagonal()*x_value.head(_dim) + _d2.asDiagonal()*x_value.tail(_dim) + _b;
88  }
89 
91  {
92  bool increase = _dim < x.getSize()/2;
93  if (!_useDefaultValue && increase)
94  throw std::runtime_error("[ocra::DoubleDiagonalLinearFunction::doUpdateSize] No default value to increase the size of A");
95 
96  int oldDim = _dim;
97  int dim = x.getSize();
98 
99  if (increase)
100  {
101  VectorXd tmpb(_b); //we need to save _b before resizing
102  _b.resize(dim);
103  _b.head(oldDim) = tmpb;
104  _b.tail(dim-oldDim).setConstant(_defaultbValue);
105 
106  _d1.resize(dim); //value of _d1 are in _jacobian, no need to save them
107  _d2.resize(dim); //value of _d2 are in _jacobian
108  _d1.head(oldDim) = _jacobian.block(0,0,oldDim,oldDim).diagonal();
109  _d2.head(oldDim) = _jacobian.block(0,oldDim,oldDim,oldDim).diagonal();
110  _d1.tail(dim-oldDim).setConstant(_defaultDiagonalValue1);
111  _d2.tail(dim-oldDim).setConstant(_defaultDiagonalValue2);
112  }
113  else
114  {
115  VectorXd tmpb(_b); //we need to save _b before resizing
116  _b.resize(dim);
117  _b = tmpb.head(dim);
118 
119  _d1.resize(dim);
120  _d2.resize(dim);
121  _d1 = _jacobian.block(0,0,oldDim,oldDim).diagonal().head(dim);
122  _d2 = _jacobian.block(0,oldDim,oldDim,oldDim).diagonal().head(dim);
123  }
124  }
125 
127  {
128  buildA();
129  }
130 
132  {
133  ocra_assert(2*(x.getSize()/2) == x.getSize() && "double diagonal linear function can only work with variables whose size is even.");
134  return x.getSize()/2;
135  }
136 
138  {
139  // do nothing: this overloaded method avoid a default exception to be thrown
140  }
141 
142  void DoubleDiagonalLinearFunction::buildA()
143  {
144  _jacobian.setZero();
145  _jacobian.block(0,0,_dim,_dim).diagonal() = _d1;
146  _jacobian.block(0,_dim,_dim,_dim).diagonal() = _d2;
147  propagate<EVT_CHANGE_VALUE>();
148  }
149 
150 }
151 
152 // cmake:sourcegroup=Function
VectorXd & _value
Definition: Function.h:312
const VectorXd & getValue() const
Definition: Variable.cpp:94
void changeA(const MatrixXd &A)
Variable & x
Definition: Function.h:309
LinearFunction class.
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
MatrixXd & _jacobian
Definition: Function.h:313
Declaration file of the DoubleDiagonalLinearFunction class.
virtual void changeDefaultDiagonalValue1(const double v)
int getSize() const
Definition: Variable.cpp:81
virtual void changeDiagonal1(const VectorXd &d)
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
virtual void changeDefaultDiagonalValue2(const double v)
virtual void changeDiagonal2(const VectorXd &d)
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
const int & _dim
Definition: Function.h:320
virtual void changeDefaultbValue(const double v)