ocra-recipes
Doxygen documentation for the ocra-recipes repository
QuadraticFunction.cpp
Go to the documentation of this file.
2 
3 namespace ocra
4 {
5  QuadraticFunction::QuadraticFunction(Variable& x, int dimension)
6  :NamedInstance("QuadraticFunction")
10  , false //not time dependant
11  , true) //thus time separable
12  , _qIsUpToDate(false)
13  , _rIsUpToDate(false)
14  ,_inhibitPropagationFrom_q_or_r(false)
15  {
16  for (int i=0; i<dimension; ++i)
17  _q.push_back(x.getSize() ? new VectorXd(x.getSize()) : new VectorXd()); //TODO: Fix this (Shoot Eigen until it handles VectorXd(0))
18  _r.resize(dimension);
19  }
20 
22  {
23  for (size_t i=0; i<_q.size(); ++i)
24  delete _q[i];
25  }
26 
27  void QuadraticFunction::changePi(const MatrixXd& Pi, int index)
28  {
29  ocra_assert(index < _dim);
30 
31  doChangePi(Pi, index);
32 
34  && IFunction<PARTIAL_XX>::_val[index]->rows() == x.getSize()
35  && "Pi does not have the good dimensions");
36 
37  propagate<EVT_CHANGE_VALUE>();
38  }
39 
40  void QuadraticFunction::changeqi(const VectorXd& qi, int index)
41  {
42  ocra_assert(index <_dim);
43 
44  doChangeqi(qi, index);
45 
46  ocra_assert(_q[index]->size() == x.getSize());
47 
48  propagate<EVT_CHANGE_VALUE>();
49  }
50 
51  void QuadraticFunction::changeri(real ri, int index)
52  {
53  ocra_assert(index <_dim);
54 
55  doChangeri(ri, index);
56 
57  propagate<EVT_CHANGE_VALUE>();
58  }
59 
60  void QuadraticFunction::doChangePi(const MatrixXd& Pi, int index)
61  {
62  ocra_assert((Pi-Pi.transpose()).isZero() && "Pi is not symmetric");
63  *IFunction<PARTIAL_XX>::_val[index] = Pi;
64  }
65 
66  void QuadraticFunction::doChangeqi(const VectorXd& qi, int index)
67  {
68  *_q[index] = qi;
69  }
70 
71  void QuadraticFunction::doChangeri(real ri, int index)
72  {
73  _r[index] = ri;
74  }
75 
76 
78  {
79  for (int i=0; i<_dim; ++i)
80  {
81  const MatrixXd& Pi = getPi(i);
82 
83  _value[i] = 0.5*x.getValue().dot(Pi*x.getValue());
84  _value[i] += x.getValue().dot(getqi(i));
85  }
86  _value += getr();
87  }
88 
89 
91  {
92  for (int i=0; i<_dim; ++i)
93  {
94  const MatrixXd& Pi = getPi(i);
95 
96  _jacobian.row(i) = Pi*x.getValue() + getqi(i);
97  }
98  }
99 
101  {
102  //do nothing
103  }
104 
106  {
107  //do nothing
108  }
109 
111  {
112  //do nothing
113  }
114 
116  {
117  for (int i=0; i<_dim; ++i)
118  _q[i]->resize(x.getSize());
119  //invalidateq();
120  }
121 
123  {
124  if (oldDimension < _dim)
125  {
126  for (int i=oldDimension; i<_dim; ++i)
127  _q.push_back(new VectorXd(x.getSize()));
128  }
129  else
130  {
131  for (int i=oldDimension-1; i>=_dim; --i)
132  {
133  delete _q[i];
134  _q.pop_back();
135  }
136  }
137  _r.resize(_dim);
138  //invalidateq();
139  //invalidater();
140  }
141 
142 
143  void QuadraticFunction::invalidateq(int timestamp)
144  {
145  _qIsUpToDate = false;
146  propagate<EVT_CHANGE_VALUE>(timestamp);
147  }
148 
149  void QuadraticFunction::invalidater(int timestamp)
150  {
151  _rIsUpToDate = false;
152  propagate<EVT_CHANGE_VALUE>(timestamp);
153  }
154 
156  {
157  _inhibitPropagationFrom_q_or_r = true;
158  }
159 
161  {
162  _inhibitPropagationFrom_q_or_r = false;
163  }
164 
165 
166 
167 
168 
170  {
171  BaseVariable x("x",5);
172  MatrixXd Q = MatrixXd::Random(5,5);
173  MatrixXd P = 0.5*(Q + Q.transpose());
174  VectorXd q = VectorXd::Random(5);
175  double r = VectorXd::Random(1)[0];
176 
177  QuadraticFunction qf(x, P, q, r);
178 
179  x.setValue(VectorXd::Random(5));
180  std::cout << qf.getValue() << std::endl << std::endl;
181  std::cout << qf.getJacobian() << std::endl << std::endl;
182  std::cout << qf.get<PARTIAL_XX>(0) << std::endl << std::endl;
183 
184  P.setZero();
185  q << 1,2,3,4,5;
186  P.diagonal() = q;
187  x.setValue(VectorXd::Constant(5, 1));
188 
189  qf.changePi(P);
190  qf.changeqi(q);
191  qf.changeri(0);
192 
193  std::cout << qf.getValue() << std::endl << std::endl;
194  std::cout << qf.getJacobian() << std::endl << std::endl;
195  std::cout << qf.get<PARTIAL_XX>(0) << std::endl << std::endl;
196  }
197 
198 }
199 
200 // cmake:sourcegroup=Function
virtual void updater() const
Computation ability of a ocra function.
Definition: IFunction.h:243
void invalidateq(int timestamp)
virtual void doChangeqi(const VectorXd &qi, int index)
void inhibitPropagationFrom_q_or_r() const
VectorXd & _value
Definition: Function.h:312
const VectorXd & getValue() const
Definition: Variable.cpp:94
void changePi(const MatrixXd &Pi, int index=0)
void setValue(const VectorXd &value)
Definition: Variable.cpp:99
Variable & x
Definition: Function.h:309
virtual void doChangePi(const MatrixXd &Pi, int index)
void testQuadraticFunction()
virtual void updateq() const
virtual void updateValue() const
virtual void doChangeri(double ri, int index)
virtual void updateJacobian() const
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
Function class.
Definition: Function.h:77
MatrixXd & _jacobian
Definition: Function.h:313
const IFunction< Ability >::return_type & get() const
Definition: Function.h:353
virtual void doUpdateInputSizeEnd()
const MatrixXd & getJacobian() const
Definition: Function.h:375
const VectorXd & getqi(int index=0) const
int getSize() const
Definition: Variable.cpp:81
void changeqi(const VectorXd &qi, int index=0)
std::vector< VectorXd * > _q
void resize()
Definition: Function.cpp:86
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
void desinhibitPropagationFrom_q_or_r() const
const MatrixXd & getPi(int index=0) const
virtual void updateHessian() const
const VectorXd & getValue() const
Definition: Function.h:365
void changeri(double ri, int index=0)
double real
Definition: MathTypes.h:27
void invalidater(int timestamp)
const VectorXd & getr() const
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
QuadraticFunction class.
virtual void doUpdateDimensionEnd(int oldDimension)
const int & _dim
Definition: Function.h:320
Implements a basic variable.
Definition: Variable.h:304
Declaration file of the QuadraticFunction class.