ocra-recipes
Doxygen documentation for the ocra-recipes repository
WeightedSquareDistanceFunction.h
Go to the documentation of this file.
1 
13 #ifndef _OCRA_WEIGHTED_SQUARE_DISTANCE_FUNCTION_H_
14 #define _OCRA_WEIGHTED_SQUARE_DISTANCE_FUNCTION_H_
15 
16 // includes
18 
24 namespace ocra
25 {
33  {
34  // ------------------------ structures --------------------------------------
35  public:
36  typedef QuadraticFunction functionType_t; //< alias on the type of the mother class. Needed to duplicate the function tree.
37 
38  // ------------------------ constructors ------------------------------------
39  private:
42 
43  public:
44  template <class VectorBase>
45  WeightedSquareDistanceFunction(Variable& x, double weight, const VectorBase& reference);
46 
47  template <class VectorBase1, class VectorBase2>
48  WeightedSquareDistanceFunction(Variable& x, const VectorBase1& weight, const VectorBase2& reference);
49 
50  // ------------------------ public interface --------------------------------
51  public:
52  void changeWeight(double weight);
53 
54  template <class VectorBase>
55  void changeWeight(const VectorBase& weight);
56 
57  const VectorXd& getWeight() const { return _weight; }
58 
59  template <class VectorBase>
60  void changeReference(const VectorBase& reference);
61 
62  // ------------------------ protected methods -------------------------------
63  protected:
64  void updateValue() const;
65  void updateJacobian() const;
66 
68  void doUpdateInputSizeEnd();
69 
70  void doChangePi(const MatrixXd& Pi, int index = 0);
71  void doChangeqi(const VectorXd& qi, int index = 0);
72  void doChangeri(double ri, int index = 0);
73 
74  // ------------------------ private methods ---------------------------------
75  private:
76  void computeP();
77  void computeq();
78  void computer();
79 
80  // ------------------------ protected members -------------------------------
81  protected:
82  VectorXd _weight; //< diagonal of the weight matrix
83  VectorXd _reference; //< \f&x_{ref} \f&
84  double _defaultWeight; //< default value to be used when the variable size increase. Default for _reference is 0
85  };
86 
87 
88  template<class VectorBase>
89  inline WeightedSquareDistanceFunction::WeightedSquareDistanceFunction(Variable& x, double weight, const VectorBase& reference)
90  :NamedInstance("weighted square distance function")
92  ,CoupledInputOutputSize(false)
93  ,QuadraticFunction(x), _defaultWeight(weight), _reference(reference)
94  {
96  ocra_assert(reference.rows()==1 || reference.cols()==1);
97 
98  if (reference.size() != x.getSize())
99  throw std::runtime_error("[WeightedSquareDistanceFunction::WeightedSquareDistanceFunction] size of reference does not match the size of the variable");
100 
101  _weight.resize(x.getSize());
102  _weight.fill(_defaultWeight);
103  computeP();
104  computeq();
105  computer();
106  if (weight>0)
108  else if (weight<0)
110  else
112  }
113 
114 
115  template <class VectorBase1, class VectorBase2>
116  inline WeightedSquareDistanceFunction::WeightedSquareDistanceFunction(Variable& x, const VectorBase1& weight, const VectorBase2& reference)
117  :NamedInstance("weighted square distance function")
119  ,CoupledInputOutputSize(false)
120  ,QuadraticFunction(x), _weight(weight), _reference(reference)
121  {
123  ocra_assert(weight.rows()==1 || weight.cols()==1);
125  ocra_assert(reference.rows()==1 || reference.cols()==1);
126 
127  if (reference.size() != x.getSize())
128  throw std::runtime_error("[WeightedSquareDistanceFunction::WeightedSquareDistanceFunction] size of reference does not match the size of the variable");
129  if (weight.size() != x.getSize())
130  throw std::runtime_error("[WeightedSquareDistanceFunction::WeightedSquareDistanceFunction] size of weight does not match the size of the variable");
131 
132  _defaultWeight = weight[weight.size()-1];
133  computeP();
134  computeq();
135  computer();
136 
137  if ((weight.array()>0).all())
139  else if ((weight.array()<0).all())
141  else
143  }
144 
145 
146  template <class VectorBase>
147  inline void WeightedSquareDistanceFunction::changeWeight(const VectorBase& weight)
148  {
149  if (weight.size() != x.getSize())
150  throw std::runtime_error("[WeightedSquareDistanceFunction::changeWeight] size of weight does not match the size of the variable");
151 
152  _weight = weight;
153  _defaultWeight = weight[weight.size()-1];
154  computeP();
155  computeq();
156  computer();
157  invalidateAll();
158  propagate<EVT_CHANGE_VALUE>();
159  }
160 
161 
162  template <class VectorBase>
163  inline void WeightedSquareDistanceFunction::changeReference(const VectorBase& reference)
164  {
165  if (reference.size() != x.getSize())
166  throw std::runtime_error("[WeightedSquareDistanceFunction::changeReference] size of reference does not match the size of the variable");
167 
168  _reference = reference;
169  computeq();
170  computer();
171  invalidateAll();
172  propagate<EVT_CHANGE_VALUE>();
173  }
174 
175 
176 
177 
179 }
180 
181 #endif //_OCRA_WEIGHTED_SQUARE_DISTANCE_FUNCTION_H_
182 
183 // cmake:sourcegroup=Function
184 
void changeReference(const VectorBase &reference)
Variable & x
Definition: Function.h:309
void doChangeqi(const VectorXd &qi, int index=0)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
void testWeightedSquareDistanceFunction()
int getSize() const
Definition: Variable.cpp:81
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
void changeConvexityProperty(eFunctionConvexity newProperty)
void invalidateAll()
Definition: Function.h:330
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
QuadraticFunction class.
#define OCRA_STATIC_ASSERT_VECTOR_OR_DYNAMIC_MATRIX(TYPE)
Definition: MathTypes.h:31
WeightedSquareDistanceFunction class.
Declaration file of the QuadraticFunction class.
void doChangePi(const MatrixXd &Pi, int index=0)