ocra-recipes
Doxygen documentation for the ocra-recipes repository
LinearFunction.cpp
Go to the documentation of this file.
2 
3 //std includes
4 #include <sstream>
5 #include <stdexcept>
6 
7 //using namespace xde; //TODO: we comment that, we should not!!!!
8 
9 namespace ocra
10 {
11  LinearFunction::LinearFunction(Variable& x, int dimension)
12  :NamedInstance("linear function")
16  ,_bIsUpToDate(false)
17  ,_inhibitPropagationFromb(false)
18  {
19  _b.resize(_dim);
20  }
21 
23  {
24  }
25 
26  const MatrixXd& LinearFunction::getA() const
27  {
28  return getJacobian();
29  }
30 
31  const VectorXd& LinearFunction::getb() const
32  {
33  if (!_bIsUpToDate)
34  {
35  updateb();
36  _bIsUpToDate = true;
37  }
38  return _b;
39  }
40 
41  void LinearFunction::changeA(const MatrixXd& A)
42  {
43  doChangeA(A);
44  ocra_assert((_jacobian.rows() == _dim && _jacobian.cols() == x.getSize()) &&
45  "The size of the new matrix A does not match with the size of previous A");
46  propagate<EVT_CHANGE_VALUE>();
47  }
48 
49  void LinearFunction::changeb(const VectorXd& b)
50  {
51  doChangeb(b);
52  _bIsUpToDate = true;
53  ocra_assert(_b.size() == _dim && "Size of b does not match with function dimension.");
54  propagate<EVT_CHANGE_VALUE>();
55  }
56 
57  void LinearFunction::doChangeA(const MatrixXd& A)
58  {
59  _jacobian = A;
60  }
61 
62  void LinearFunction::doChangeb(const VectorXd& b)
63  {
64  _b = b;
65  }
66 
67  void LinearFunction::invalidateb(int timestamp)
68  {
69  _bIsUpToDate = false;
70  if (!_inhibitPropagationFromb)
71  propagate<EVT_CHANGE_VALUE>();
72  }
73 
75  {
76  //force the evaluation of the jacobian and b
77  _value.noalias() = getJacobian() * x.getValue() + getb();
78  }
79 
81  {
82  //do nothing, gradient is constant and initialized in the ctor
83  }
84 
86  {
87  //do nothing
88  }
89 
91  {
92  _b.resize(_dim);
93  _bIsUpToDate = false;
94  }
95 
97  {
98  _inhibitPropagationFromb = true;
99  }
100 
102  {
103  _inhibitPropagationFromb = false;
104  }
105 
106 }
107 
108 
109 #include "ocra/optim/Variable.h"
110 #include <math.h>
111 
112 namespace ocra
113 {
115  {
116  BaseVariable x1("x1", 2);
117  BaseVariable x3("x2", 2);
118  BaseVariable x2("x3", 2);
119  CompositeVariable y("y", x1);
120  y.add(x2).add(x3);
121  int n = 3;
122  MatrixXd A(n,y.getSize());
123  VectorXd b(n);
124  VectorXd v(y.getSize());
125 
126  for (int i=0; i<n; ++i)
127  {
128  b[i] = (4*rand())/RAND_MAX - 2;
129  for (int j=0; j<y.getSize(); ++j)
130  A(i,j) = (5*rand())/(RAND_MAX) - 2;
131  }
132  std::cout << A << std::endl;
133  std::cout << b << std::endl;
134 
135  LinearFunction f(y,A,b);
136 
137  for (int i=0; i<y.getSize(); ++i)
138  v[i] = (4*rand())/RAND_MAX - 2;
139 
140  y.setValue(v);
141  std::cout << (VectorXd)y << std::endl;
142  std::cout << f.getValue() << std::endl;
143  }
144 }
145 
146 // cmake:sourcegroup=Function
virtual void doUpdateInputSizeEnd()
virtual void updateJacobian() const
void desinhibitPropagationFromb() const
virtual void doChangeb(const VectorXd &b)
VectorXd & _value
Definition: Function.h:312
const VectorXd & getValue() const
Definition: Variable.cpp:94
virtual void doChangeA(const MatrixXd &A)
void changeA(const MatrixXd &A)
virtual void updateb() const
virtual void updateValue() const
void setValue(const VectorXd &value)
Definition: Variable.cpp:99
Variable & x
Definition: Function.h:309
void changeb(const VectorXd &b)
LinearFunction class.
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
Declaration file of the LinearFunction class.
const MatrixXd & getJacobian() const
Definition: Function.h:375
int getSize() const
Definition: Variable.cpp:81
const VectorXd & getb() const
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
const MatrixXd & getA() const
CompositeVariable & add(Variable &child)
Attach/detach the child to/from this node.
Definition: Variable.cpp:580
void testLinearFunction()
A concatenation of base variables and other composite variables.
Definition: Variable.h:357
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
Declaration file of the Variable class.
const int & _dim
Definition: Function.h:320
Implements a basic variable.
Definition: Variable.h:304