ocra-recipes
Doxygen documentation for the ocra-recipes repository
RowFunction.h
Go to the documentation of this file.
1 
10 #ifndef _OCRABASE_ROW_H_
11 #define _OCRABASE_ROW_H_
12 
13 // includes
14 #include "ocra/optim/CompileTimeChecks.h"
15 #include "ocra/optim/Function.h"
16 
22 namespace ocra
23 {
34  template<class T>
35  class RowFunction : public T::functionType_t, private IsDerivedFrom<T, Function>
36  {
37  // ------------------------ structures --------------------------------------
38  public:
39  typedef T functionType_t; //< alias on the type of the mother class. Needed to duplicate the function tree.
40  protected:
41  private:
42 
43  // ------------------------ public static members ---------------------------
44  public:
45 
46  // ------------------------ constructors ------------------------------------
47  private:
48  RowFunction();
50  protected:
51  public:
52  RowFunction(T* function, const cfl_size_t index);
53  RowFunction(T* function, const std::vector<int>& indices);
54 
55  // ------------------------ public interface --------------------------------
56  public:
57 
58  // ------------------------ public methods ----------------------------------
59  public:
60 
61  // ------------------------ public static methods ---------------------------
62  public:
63 
64  // ------------------------ protected methods -------------------------------
65  protected:
66  virtual void computeValue(void) const;
67  virtual void computeGradient(void) const;
68  virtual void computeHessian(void) const;
69  virtual void computeJdot(void) const;
70  virtual void computeJdotXdot(void) const;
71 
72  virtual void doUpdateSize(void);
73 
74  // ------------------------ protected static methods ------------------------
75  protected:
76 
77  // ------------------------ private methods ---------------------------------
78  private:
79 
80  // ------------------------ private static methods --------------------------
81  private:
82 
83  // ------------------------ protected members -------------------------------
84  protected:
85 
86  // ------------------------ protected static members ------------------------
87  protected:
89  std::vector<int> _indices;
90 
91  // ------------------------ private members ---------------------------------
92  private:
93 
94  // ------------------------ private static members --------------------------
95  private:
96 
97  // ------------------------ friendship declarations -------------------------
98  };
99 
100 
101  template <class T>
102  inline RowFunction<T>::RowFunction(T* function, const cfl_size_t index)
103  : Function(function->getVariable(), 1, function->getType(), function->getConvexityProperty(),
104  function->getContinuityProperty(),function->canComputeHessian(), function->canComputeGradient())
105  , _indices(1), _f(function)
106  {
107  _indices[0] = index;
108  function->attach(*this);
109  this->_name = "rowFunction";
110  }
111 
112  template <class T>
113  inline RowFunction<T>::RowFunction(T* function, const std::vector<int>& indices)
114  : Function(function->getVariable(), (cfl_size_t)indices.size(), function->getType(), function->getConvexityProperty(),
115  function->getContinuityProperty(),function->canComputeHessian(), function->canComputeGradient())
116  , _indices(indices), _f(function)
117  {
118  assert(indices.size() <= function->getDimension());
119  function->attach(*this);
120  this->_name = "rowFunction";
121  }
122 
123  template <class T>
124  inline void RowFunction<T>::computeValue(void) const
125  {
126  const Vector& v = _f->getValues();
127  for (cfl_size_t i=0; i<this->_dimension; ++i)
128  this->_value[i] = v[_indices[i]];
129  }
130 
131  template <class T>
132  inline void RowFunction<T>::computeGradient(void) const
133  {
134  const Matrix& g = _f->getGradients();
135  for (cfl_size_t i=0; i<this->_dimension; ++i)
136  {
137  for (cfl_size_t j=0; j<this->_x->getSize(); ++j)
138  {
139  this->_gradient(i,j) = g(_indices[i], j);
140  }
141  }
142  }
143 
144  template <class T>
145  inline void RowFunction<T>::computeHessian(void) const
146  {
147  throw std::runtime_error("RowFunction<T>::computeHessian: method non implemented.");
148  for (cfl_size_t i=0; i<this->_dimension; ++i)
149  {
150  //TODO [todo] : write properly
151 // _hessians[i]->copyValuesFrom(_f->getHessian(_indices[i]));
152  }
153  }
154 
155  template <class T>
156  inline void RowFunction<T>::computeJdot(void) const
157  {
158  const Matrix& g = _f->getJdot();
159  for (cfl_size_t i=0; i<this->_dimension; ++i)
160  {
161  for (cfl_size_t j=0; j<this->_x->getSize(); ++j)
162  {
163  this->_Jdot(i,j) = g(_indices[i], j);
164  }
165  }
166  }
167 
168  template <class T>
169  inline void RowFunction<T>::computeJdotXdot(void) const
170  {
171  const Vector& v = _f->getJdotXdot();
172  for (cfl_size_t i=0; i<this->_dimension; ++i)
173  {
174  this->_JdotXdot[i] = v[_indices[i]];
175  }
176  }
177 
178  template <class T>
180  {
181  }
182 
183 }
184 
185 #endif //_OCRABASE_ROW_H_
186 
187 // cmake:sourcegroup=toBeUpdated
188 
virtual void computeJdot(void) const
Definition: RowFunction.h:156
virtual void doUpdateSize(void)
Definition: RowFunction.h:179
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
Function class.
Definition: Function.h:77
std::vector< int > _indices
Definition: RowFunction.h:89
virtual void computeValue(void) const
Definition: RowFunction.h:124
Function * _f
Definition: RowFunction.h:88
virtual void computeGradient(void) const
Definition: RowFunction.h:132
Declaration file of the Function class.
virtual void computeJdotXdot(void) const
Definition: RowFunction.h:169
RowFunction class.
Definition: RowFunction.h:35
virtual void computeHessian(void) const
Definition: RowFunction.h:145