ocra-recipes
Doxygen documentation for the ocra-recipes repository
IFunction.h
Go to the documentation of this file.
1 
38 #ifndef _OCRABASE_IFUNCTION_H_
39 #define _OCRABASE_IFUNCTION_H_
40 
41 #ifdef WIN32
42 # pragma once
43 #endif
44 
45 #include <ocra/util/MathTypes.h>
46 #include "ocra/optim/ocra_assert.h"
47 #include <iostream>
48 #include <vector>
49 #include <stdexcept>
50 
56 namespace ocra
57 {
65  {
66  FUN_VALUE = 0, //< \f$ f(x,t) \f$
67  PARTIAL_X, //< \f$ \frac{\partial f}{\partial x}(x,t) \f$
68  PARTIAL_T, //< \f$ \frac{\partial f}{\partial t}(x,t) \f$, i.e. time derivative with x considered as constant
69  FUN_DOT, //< \f$ \dot{f}(x,t) \f$, i.e. time derivative where x is a function of t. \f$ \dot{f} = \frac{\partial f}{\partial x} \dot{x} + \frac{\partial f}{\partial t} \f$
70  PARTIAL_X_DOT, //< \f$ \dot{\left(\frac{\partial f}{\partial x}\right)}(x,t) \f$
71  PARTIAL_XX, //< \f$ \frac{\partial^2 f}{\partial x^2}(x,t) \f$
72  FUN_DDOT, //< \f$ \ddot{f}(x,t) \f$
73  PARTIAL_TT, //< \f$ \frac{\partial^2 f}{\partial t^2}(x,t) \f$
74  PARTIAL_TX, //< \f$ \frac{\partial^2 f}{\partial t \partial x}(x,t) \f$
75  PARTIAL_XT, //< \f$ \frac{\partial^2 f}{\partial x \partial t}(x,t) \f$. necessary only for non C^2 functions...
76  PARTIAL_T_DOT, //< \f$ \dot{\left(\frac{\partial f}{\partial t}\right)}(x,t) \f$
77  PARTIAL_X_DOT_X_DOT, //< \f$ \dot{\left(\frac{\partial f}{\partial x}\right)}(x,t) \dot{x} \f$
78  PROP_NUMBER //< the number of abilities
79  };
80 
86  template<class T>
88  {
89  typedef int sub_type_t;
90  };
91 
92 
111  #define DECLARE_SUBTYPE_TRAITS(type, subTypeReturn, preAccess, access) \
112  template<> struct return_type_traits<type > \
113  { \
114  typedef subTypeReturn sub_type_t; \
115  static subTypeReturn getSub(const type& v, int index) {return preAccess(v.access(index));} \
116  static void resize(type& v, int m, int n=0);\
117  };
118 
123  DECLARE_SUBTYPE_TRAITS(VectorXd, double, /*nothing*/, operator[]);
124  DECLARE_SUBTYPE_TRAITS(MatrixXd, MatrixXdRow, /*nothing*/, row);
125  DECLARE_SUBTYPE_TRAITS(std::vector<MatrixXd*>, const MatrixXd&, *, operator[]);
126 
127  #undef DECLARE_SUBTYPE_TRAITS
128 
130  inline void return_type_traits<VectorXd>::resize(VectorXd& v, int m, int n) {v.resize(m);}
131 
133  inline void return_type_traits<MatrixXd>::resize(MatrixXd& v, int m, int n) {v.resize(m,n);}
134 
136  inline void return_type_traits<std::vector<MatrixXd*> >::resize(std::vector<MatrixXd*>& v, int m, int n)
137  {
138  ocra_assert(0<=m && "invalide size m");
139  ocra_assert(0<=n && "invalide size n");
140  //if the dimension decrease, we remove the supernumerary matrices.
141  //entered only if m < v.size()
142  for (int i=(int)v.size()-1; i>=m; --i)
143  {
144  delete v[i];
145  v.pop_back();
146  }
147 
148  //we resize the matrices
149  for (size_t i=0; i<v.size(); ++i)
150  v[i]->resize(n, n);
151 
152  //if the dimension increases, we add the necessary matrices.
153  //entered only if m > v.size()
154  for (size_t i=v.size(); i<(size_t)m; ++i)
155  v.push_back(new MatrixXd(n, n));
156  }
157 
178  #define DECLARE_FUNCTION_TRAITS(templateArg, propName, returnType, virtualFunctionName, abilityId) \
179  template<templateArg> struct ocra_function_traits propName \
180  { \
181  typedef returnType type_t; \
182  typedef return_type_traits<returnType>::sub_type_t sub_type_t; \
183  virtual void virtualFunctionName() const \
184  { \
185  std::stringstream s; \
186  s << "[Function::" << #virtualFunctionName << "] is not implemented. Did you forget to declare its overload as a protected const method ?"; \
187  std::cout << s.str()<<std::endl; \
188  throw std::runtime_error(s.str()); \
189  } \
190  \
193  template<class FunctionType> static void update(const FunctionType& function) { function.virtualFunctionName(); } \
194  static bool extractUsage(const std::vector<bool>& usageSet) {return usageSet[abilityId];}\
195  };
196 
198  DECLARE_FUNCTION_TRAITS( eFunctionAbility Property, /* general */, int /*arbitrary type */, updateProperty /* arbitraryName */, -1 /* arbitrary id*/);
199 
205  DECLARE_FUNCTION_TRAITS(/* special */, <FUN_VALUE>, VectorXd, updateValue, FUN_VALUE);
206  DECLARE_FUNCTION_TRAITS(/* special */, <PARTIAL_X>, MatrixXd, updateJacobian, PARTIAL_X);
207  DECLARE_FUNCTION_TRAITS(/* special */, <PARTIAL_T>, VectorXd, updatePartialT, PARTIAL_T);
208  DECLARE_FUNCTION_TRAITS(/* special */, <FUN_DOT>, VectorXd, updateFdot, FUN_DOT);
209  DECLARE_FUNCTION_TRAITS(/* special */, <PARTIAL_X_DOT>, MatrixXd, updateJdot, PARTIAL_X_DOT);
210  DECLARE_FUNCTION_TRAITS(/* special */, <PARTIAL_XX>, std::vector<MatrixXd*>, updateHessian, PARTIAL_XX);
211  DECLARE_FUNCTION_TRAITS(/* special */, <FUN_DDOT>, VectorXd, updateFddot, FUN_DDOT);
212  DECLARE_FUNCTION_TRAITS(/* special */, <PARTIAL_TT>, VectorXd, updatePartialTT, PARTIAL_TT);
213  DECLARE_FUNCTION_TRAITS(/* special */, <PARTIAL_TX>, MatrixXd, updatePartialTX, PARTIAL_TX);
214  DECLARE_FUNCTION_TRAITS(/* special */, <PARTIAL_XT>, MatrixXd, updatePartialXT, PARTIAL_XT);
215  DECLARE_FUNCTION_TRAITS(/* special */, <PARTIAL_T_DOT>, VectorXd, updatePartialTdot, PARTIAL_T_DOT);
216  DECLARE_FUNCTION_TRAITS(/* special */, <PARTIAL_X_DOT_X_DOT>, VectorXd, updateJdotXdot, PARTIAL_X_DOT_X_DOT);
217 
218  #undef DECLARE_FUNCTION_TRAITS
219 
220 
221 
242  template<eFunctionAbility Property>
243  class IFunction
244  : public ocra_function_traits<Property>
245  {
246  // ------------------------------------------------------------
247  // -------------- Constructor / Destructor --------------------
248  // ------------------------------------------------------------
249  protected:
256  IFunction(const std::vector<bool>& props)
257  :ocra_function_traits<Property>()
258  ,_used(ocra_function_traits<Property>::extractUsage(props))
259  ,_validated(false)
260  {
261  };
262 
264  {
265  resizeData(0, 0);
266  }
267 
268  private:
271  IFunction(const IFunction&);
272  IFunction& operator= (const IFunction&);
274 
275  public:
281  typedef typename ocra_function_traits<Property>::type_t return_type;
282  typedef typename ocra_function_traits<Property>::sub_type_t return_sub_type;
284 
285  protected:
287  void invalidate() {_validated = false;}
288 
290  bool isValid() const {return _validated;}
291 
293  bool canBeComputed() const {return _used;}
294 
295 
321  template<class FunctionType>
322  const return_type& get(FunctionType& data) const
323  {
324  ocra_assert(_used && "this function property is not used bordel");
325  if (!_validated)
326  {
327  ocra_function_traits<Property>::update(data);
328  _validated = true;
329  }
330  return _val;
331  }
332 
342  template<class FunctionType>
343  return_sub_type get(FunctionType& data, int index) const
344  {
345  get(data);
346  return return_type_traits<return_type>::getSub(_val, index);
347  }
348 
354  void resizeData(int m, int n)
355  {
356  if (_used)
357  {
358  _validated = false;
360  }
361  }
362  protected:
363  mutable return_type _val; //< the memory buffer to keep the result of the computation.
364 
365  private:
366  mutable bool _validated; //< the flag to know wether _val is up-to-date or not
367  bool _used; //< the flag indicating if this ability is used
368  };
369 
370 
380 #define OCRA_FUNCTION_INTERFACE_LIST(begin, pre, end) \
381  begin IFunction<FUN_VALUE> end \
382  pre IFunction<PARTIAL_X> end \
383  pre IFunction<PARTIAL_T> end \
384  pre IFunction<FUN_DOT> end \
385  pre IFunction<PARTIAL_X_DOT> end \
386  pre IFunction<PARTIAL_XX> end \
387  pre IFunction<FUN_DDOT> end \
388  pre IFunction<PARTIAL_TT> end \
389  pre IFunction<PARTIAL_TX> end \
390  pre IFunction<PARTIAL_XT> end \
391  pre IFunction<PARTIAL_T_DOT> end \
392  pre IFunction<PARTIAL_X_DOT_X_DOT> end \
393  /*pre IFunction<[NEW_PROP]> end*/
394 
396 #define OCRA_FUNCTION_INTERFACE_COMA ,
397 
403 #define OCRA_FUNCTION_INTERFACE_INHERITANCE(inheritanceAccessRight)\
404  OCRA_FUNCTION_INTERFACE_LIST(inheritanceAccessRight, OCRA_FUNCTION_INTERFACE_COMA inheritanceAccessRight, )
405 
412 #define OCRA_APPLY_FUNCTION_ON_ALL_INTERFACE(methodAndArgs) \
413  OCRA_FUNCTION_INTERFACE_LIST(, , ::methodAndArgs;)
414 
418 #define OCRA_FUNCTION_INTERFACE_INITIALIZE(usageSet)\
419  OCRA_FUNCTION_INTERFACE_LIST(, OCRA_FUNCTION_INTERFACE_COMA, (usageSet))
420 
421 }
422 
423 #endif //_OCRABASE_IFUNCTION_H_
424 
425 // cmake:sourcegroup=Function
IFunction(const std::vector< bool > &props)
IFunction Constructor.
Definition: IFunction.h:256
Computation ability of a ocra function.
Definition: IFunction.h:243
DECLARE_FUNCTION_TRAITS(eFunctionAbility Property,, int, updateProperty,-1)
return_type _val
Definition: IFunction.h:363
ocra_function_traits< Property >::sub_type_t return_sub_type
Definition: IFunction.h:282
bool isValid() const
Definition: IFunction.h:290
eFunctionAbility
Enumeration of the computation abilities of a ocra function.
Definition: IFunction.h:64
type and function definitions for the return types.
Definition: IFunction.h:87
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
ocra_function_traits< Property >::type_t return_type
Definition: IFunction.h:281
void resizeData(int m, int n)
Definition: IFunction.h:354
bool canBeComputed() const
Definition: IFunction.h:293
Eigen::DenseBase< MatrixXd >::ConstRowXpr MatrixXdRow
Definition: MathTypes.h:29
DECLARE_SUBTYPE_TRAITS(VectorXd, double,, operator[])
void invalidate()
Definition: IFunction.h:287
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
Declaration file of the OptimizationVariable class.