ocra-recipes
Doxygen documentation for the ocra-recipes repository
Function.cpp
Go to the documentation of this file.
1 #include "ocra/optim/Function.h"
2 #include <algorithm>
3 
4 namespace ocra
5 {
6  Function::Function(Variable& x, int dimension, eFunctionLinearity linearity, eFunctionConvexity convexity,
7  int continuity, bool explicitlyTimeDependant, bool separableTimeDependancy)
9  , NamedInstance("function")
11  , CoupledInputOutputSize(false)
12  , IFunctionProperties(linearity, convexity, continuity, explicitlyTimeDependant, separableTimeDependancy)
13  , OCRA_FUNCTION_INTERFACE_INITIALIZE(getUsageSet())
14  , x(x)
15  , _value(IFunction<FUN_VALUE>::_val), _jacobian(IFunction<PARTIAL_X>::_val)
16  , _dimension(dimension)
17  , _dim(_dimension)
18  , _hasDisconnected(false)
19  {
22 
23  resize();
24  }
25 
26 
28  {
29  }
30 
32  {
33  if(! _hasDisconnected)
34  {
35  _hasDisconnected = true;
38  }
39  }
40 
42  {
43  return _dimension;
44  }
45 
47  {
48  return x;
49  }
50 
52  {
53  return x;
54  }
55 
56  void Function::changeFunctionDimension(int newDimension)
57  {
58  changeFunctionDimensionImpl(newDimension);
60  }
61 
62  void Function::changeFunctionDimensionImpl(int newDimension)
63  {
64  doUpdateDimensionBegin(newDimension);
65  int oldDimension = _dimension;
66  _dimension = newDimension;
67  resize();
68  doUpdateDimensionEnd(oldDimension);
69  }
70 
71  void Function::updateInputSize(int timestamp)
72  {
74 
76  stopPropagation<EVT_RESIZE>();
77  else
78  changeFunctionDimensionImpl(computeDimensionFromInputSize());
79 
80  resize();
82 
83  ocra_assert(!(OCRA_FUNCTION_INTERFACE_LIST( , ,::isValid() ||) false)); //we test that all values have been invalidated
84  }
85 
87  {
88  const int m = _dimension;
89  const int n = x.getSize();
90  OCRA_APPLY_FUNCTION_ON_ALL_INTERFACE(resizeData(m,n));
91  }
92 
94  {
95  throw std::runtime_error("[Function::doUpdateInputSizeBegin]: by default, the function variable is not resizable. doUpdateInputSizeBegin must be overloaded if the function accept variable resizing.");
96  }
97 
99  {
100  //do nothing
101  }
102 
104  {
105  ocra_assert(false && "This should never happen, this function has to be overloaded in contexts where it might be called");
106  return 0;
107  }
108 
109  void Function::doUpdateDimensionBegin(int newDimension)
110  {
111  throw std::runtime_error("[Function::doUpdateDimensionBegin]: by default, a function is not resizable. doUpdateDimensionBegin must be overloaded if the function dimension can be changed.");
112  }
113 
114  void Function::doUpdateDimensionEnd(int oldDimension)
115  {
116  //do nothing
117  }
118 
119  void Function::updateFdot() const
120  {
124 
125  IFunction<FUN_DOT>::_val = getJacobian()*(static_cast<VectorXd>(x.getTimeDerivative()));
127  IFunction<FUN_DOT>::_val += get<PARTIAL_T>();
128  }
129 
131  {
138  Variable& x_dot = x.getTimeDerivative();
139  IFunction<FUN_DDOT>::_val = getJacobian()*static_cast<VectorXd>(x_dot.getTimeDerivative())
140  + get<PARTIAL_X_DOT>()*static_cast<VectorXd>(x_dot);
142  IFunction<FUN_DDOT>::_val += get<PARTIAL_XT>()*static_cast<VectorXd>(x_dot) + get<PARTIAL_TT>();
143  }
144 
146  {
149  IFunction<PARTIAL_X_DOT>::_val = get<PARTIAL_X_DOT>()*x.getTimeDerivative().getValue();
150  }
151 }
152 
153 
154 
155 //test
157 {
158  public:
159  MyFunction(ocra::Variable& x, int dimension)
160  : NamedInstance("myFunction")
162  , CoupledInputOutputSize(false)
163  , Function(x, dimension)
164  {
165  }
166 
167  virtual void updateValue() const
168  {
169  std::cout << "Welcome in updateValue" << std::endl;
170  }
171 
172  void updateJacobian() const
173  {
174  std::cout << "Welcome in updateJacobian" << std::endl;
175  }
176 
177  void updateFdot() const
178  {
179  std::cout << "Welcome in updateFdot" << std::endl;
180  }
181 };
182 
183 #include "ocra/optim/Variable.h"
185 {
186  ocra::BaseVariable x("x", 10);
187  MyFunction f(x, 10);
188  f.get<ocra::FUN_VALUE>();
189  f.get<ocra::PARTIAL_X>(3);
190 // f.get<ocra::PARTIAL_T>();
191  f.get<ocra::FUN_DOT>();
192 }
193 
194 // cmake:sourcegroup=Function
195 
const Variable & getVariable() const
Definition: Function.cpp:46
Computation ability of a ocra function.
Definition: IFunction.h:243
virtual Variable & getTimeDerivative()
Get the time derivative/primitive of the variable.
Definition: Variable.cpp:106
virtual int computeDimensionFromInputSize() const
Definition: Function.cpp:103
int getDimension() const
Definition: Function.cpp:41
const VectorXd & getValue() const
Definition: Variable.cpp:94
virtual void updateFdot() const
Definition: Function.cpp:119
Variable & x
Definition: Function.h:309
virtual void doUpdateDimensionBegin(int newDimension)
Definition: Function.cpp:109
#define OCRA_FUNCTION_INTERFACE_LIST(begin, pre, end)
Definition: IFunction.h:380
NamedInstance(const std::string &name)
void testFunction()
Definition: Function.cpp:184
virtual ~Function()
Definition: Function.cpp:27
void disconnectVariable()
Definition: Function.cpp:31
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
virtual void updateFddot() const
Definition: Function.cpp:130
virtual void doUpdateDimensionEnd(int oldDimension)
Definition: Function.cpp:114
Function class.
Definition: Function.h:77
bool isValid() const
Definition: Function.h:341
bool hasTimeDerivative() const
Definition: Variable.cpp:120
const IFunction< Ability >::return_type & get() const
Definition: Function.h:353
MyFunction(ocra::Variable &x, int dimension)
Definition: Function.cpp:159
virtual void doUpdateInputSizeEnd()
Definition: Function.cpp:98
const MatrixXd & getJacobian() const
Definition: Function.h:375
bool isExplicitlyTimeDependant(void) const
void updateJacobian() const
Definition: Function.cpp:172
int getSize() const
Definition: Variable.cpp:81
void resize()
Definition: Function.cpp:86
Function(Variable &x, int dimension, eFunctionLinearity linearity=LINEARITY_UNDEFINED, eFunctionConvexity convexity=CONVEXITY_UNDEFINED, int continuity=CONTINUITY_UNKNOWN, bool explicitlyTimeDependant=false, bool separableTimeDependancy=true)
Function Constructor.
Definition: Function.cpp:6
virtual void updateValue() const
Definition: Function.cpp:167
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
#define OCRA_APPLY_FUNCTION_ON_ALL_INTERFACE(methodAndArgs)
Definition: IFunction.h:412
CoupledInputOutputSize(bool coupledInputOutputSize)
void updateFdot() const
Definition: Function.cpp:177
void changeFunctionDimension(int newDimension)
Definition: Function.cpp:56
Declaration file of the Function class.
void invalidateAll()
Definition: Function.h:330
virtual void doUpdateInputSizeBegin()
Definition: Function.cpp:93
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
virtual void updateJdotXdot() const
Definition: Function.cpp:145
Declaration file of the Variable class.
#define OCRA_FUNCTION_INTERFACE_INITIALIZE(usageSet)
Definition: IFunction.h:418
void disconnect(Derived &object, void(Base::*callbackToErase)(int)) const
Disconnect non-static method.
void connect(Derived &object, void(Base::*newCallback)(int)) const
Call this method to register a non-static method as a callback.
Implements a basic variable.
Definition: Variable.h:304
AbilitySet(const std::vector< bool > &usageSet)
Definition: AbilitySet.h:23
void updateInputSize(int timestamp)
Definition: Function.cpp:71