ocra-recipes
Doxygen documentation for the ocra-recipes repository
DiagonalLinearFunction.cpp
Go to the documentation of this file.
2 
3 #include <stdexcept>
4 
5 namespace ocra
6 {
7  DiagonalLinearFunction::DiagonalLinearFunction(Variable& x, const double diagonalElementValue,
8  const double vectorElementValue,
9  const bool useDefaultValue)
10  : NamedInstance("diagonal linear function")
13  , LinearFunction(x, x.getSize())
14  , _useDefaultValue(useDefaultValue), _defaultDiagonalValue(diagonalElementValue)
15  , _defaultbValue(vectorElementValue)
16  {
17  _d.resize(_dim);
18  _d.setConstant(diagonalElementValue);
19  buildA();
20  _b.resize(_dim);
21  _b.setConstant(vectorElementValue);
22  }
23 
24 
25  DiagonalLinearFunction::DiagonalLinearFunction(Variable& x)
26  : NamedInstance("diagonal linear function")
29  , LinearFunction(x, x.getSize())
30  , _useDefaultValue(false)
32  , _defaultbValue(0.)
33  {
34  }
35 
36 
38  {
40  ocra_assert(_d.size() == _dim);
41  buildA();
42  }
43 
44 
45  void DiagonalLinearFunction::changeDiagonal(const double diagonalElementValue, const bool changeDefault)
46  {
47  doChangeDiagonal(diagonalElementValue, changeDefault);
48  buildA();
49  }
50 
51 
53  {
55  }
56 
57 
59  {
61  }
62 
63 
65  {
66  _d = d;
67  }
68 
69 
70  void DiagonalLinearFunction::doChangeDiagonal(const double diagonalElementValue, const bool changeDefault)
71  {
72  _d.setConstant(diagonalElementValue);
73  if (changeDefault)
74  _defaultDiagonalValue = diagonalElementValue;
75  }
76 
77 
79  {
81  }
82 
83 
85  {
86  _defaultbValue = v;
87  }
88 
89 
90 
91  void DiagonalLinearFunction::doChangeA(const MatrixXd& A)
92  {
93  ocra_assert(A.isDiagonal());
94 
96  }
97 
98 
100  {
101  _value = _d.asDiagonal()*x.getValue() + _b;
102  }
103 
104 
106  {
107  bool increase = _dim < x.getSize();
108  if (!_useDefaultValue && increase)
109  throw std::runtime_error("[ocra::DiagonalLinearFunction::doUpdateSize] No default value to increase the size of A");
110 
111  int oldDim = _dim;
112  int dim = x.getSize();
113 
114  if (increase)
115  {
116  VectorXd tmpb(_b); //we need to save _b before resizing
117  _b.resize(dim);
118  _b.head(oldDim) = tmpb;
119  _b.tail(dim-oldDim).setConstant(_defaultbValue);
120 
121  _d.resize(dim); //value of _d are in _jacobian, no need to save them
122  _d.head(oldDim) = _jacobian.diagonal();
123  _d.tail(dim-oldDim).setConstant(_defaultDiagonalValue);
124  }
125  else
126  {
127  VectorXd tmpb(_b); //we need to save _b before resizing
128  _b.resize(dim);
129  _b = tmpb.head(dim);
130 
131  _d.resize(dim); //value of _d are in _jacobian, no need to save them
132  _d = _jacobian.diagonal().head(dim);
133  }
134  }
135 
137  {
138  buildA();
139  }
140 
141 
142  void DiagonalLinearFunction::buildA()
143  {
144  _jacobian.setZero();
145  _jacobian.diagonal() = _d;
146  propagate<EVT_CHANGE_VALUE>();
147  }
148 
149 
151  {
152  return x.getSize();
153  }
154 
156  {
157  // do nothing: this overloaded method avoid a default exception to be thrown
158  }
159 
160 
161 
163  {
164  BaseVariable x("x", 5);
165  DiagonalLinearFunction f(x, 1., 0., true);
166 
167  x.setValue(VectorXd::Random(5));
168  std::cout << x.getValue() << std::endl;
169  std::cout << f.getValue() << std::endl << std::endl;
170 
171  x.resize(6);
172  x.setValue(VectorXd::Random(6));
173  std::cout << x.getValue() << std::endl;
174  std::cout << f.getValue() << std::endl << std::endl;
175  std::cout << f.getJacobian() << std::endl << std::endl;
176  }
177 }
178 
179 // cmake:sourcegroup=Function
180 
VectorXd & _value
Definition: Function.h:312
const VectorXd & getValue() const
Definition: Variable.cpp:94
virtual void doChangeA(const MatrixXd &A)
virtual void doChangeDefaultbValue(const double v)
void doUpdateDimensionBegin(int newDimension)
void setValue(const VectorXd &value)
Definition: Variable.cpp:99
Variable & x
Definition: Function.h:309
void changeDiagonal(const VectorXd &d)
void testDiagonalLinearFunction()
LinearFunction class.
void changeDefaultDiagonalValue(const double v)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
MatrixXd & _jacobian
Definition: Function.h:313
virtual void doChangeDefaultDiagonalValue(const double v)
const MatrixXd & getJacobian() const
Definition: Function.h:375
int getSize() const
Definition: Variable.cpp:81
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
const VectorXd & getValue() const
Definition: Function.h:365
Declaration file of the DiagonalLinearFunction class.
void resize(size_t newSize)
Definition: Variable.cpp:344
virtual void doChangeA(const MatrixXd &A)
virtual void doChangeDiagonal(const VectorXd &d)
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
void changeDefaultbValue(const double v)
const int & _dim
Definition: Function.h:320
Implements a basic variable.
Definition: Variable.h:304
DiagonalLinearFunction class.