ocra-recipes
Doxygen documentation for the ocra-recipes repository
WeightedSquareDistanceFunction.cpp
Go to the documentation of this file.
2 #include <stdexcept>
3 
4 namespace ocra
5 {
6  void WeightedSquareDistanceFunction::doChangePi(const MatrixXd& Pi, int index)
7  {
8  ocra_assert(Pi.cols() == x.getSize());
9  ocra_assert(Pi.rows() == x.getSize());
10 
11  //check if Pi is a diagonal matrix
12  if (!Pi.isDiagonal())
13  throw std::runtime_error("[WeightedSquareDistanceFunction::changePi] Given Pi is not a diagonal matrix.");
14 
15  _weight = Pi.diagonal();
16 
17  computeP();
18  computeq();
19  computer();
20  }
21 
22 
23  void WeightedSquareDistanceFunction::doChangeqi(const VectorXd& qi, int index)
24  {
25  throw std::runtime_error("[WeightedSquareDistanceFunction::changeqi] changeqi is not available directly. Please use changeWeight and changeReference instead.");
26  }
27 
28 
29  void WeightedSquareDistanceFunction::doChangeri(double ri, int index)
30  {
31  throw std::runtime_error("[WeightedSquareDistanceFunction::changeqi] changeri is not available directly. Please use changeWeight and changeReference instead.");
32  }
33 
35  {
36  _defaultWeight = weight;
37  _weight.fill(_defaultWeight);
38  computeP();
39  computeq();
40  computer();
41  if (weight>0)
43  else if (weight<0)
45  else
47 
48  invalidateAll();
49  propagate<EVT_CHANGE_VALUE>();
50  }
51 
52 
54  {
55  _value[0] = (_weight.array() * (x.getValue().array()-_reference.array()).square()).sum();
56  }
57 
58 
60  {
61  _jacobian.row(0).array() = 2*_weight.array() * x.getValue().array() + getqi(0).array();
62  }
63 
64 
66  {
67  // Do nothing. This overload is just here to enable input resizing (default implementation throw an exception).
68  }
69 
70 
72  {
73  int s = static_cast<int>(_weight.size());
74 
75  if (x.getSize()>s)
76  {
77  //we need to fill the end of _weight and _reference with default values
78  _weight.conservativeResize(x.getSize());
79  _weight.tail(x.getSize()-s).setConstant(_defaultWeight);
80 
81  _reference.conservativeResize(x.getSize());
82  _reference.tail(x.getSize()-s).setZero();
83  }
84  else
85  {
86  _weight.conservativeResize(x.getSize());
87  _reference.conservativeResize(x.getSize());
88  }
89 
91  computeP();
92  computeq();
93  computer();
94  }
95 
96 
97  void WeightedSquareDistanceFunction::computeP()
98  {
99  IFunction<PARTIAL_XX>::_val[0]->setZero();
100  IFunction<PARTIAL_XX>::_val[0]->diagonal() = _weight;
101  }
102 
103  void WeightedSquareDistanceFunction::computeq()
104  {
105  _q[0]->array() = - _weight.array()*_reference.array();
106  }
107 
108  void WeightedSquareDistanceFunction::computer()
109  {
110  _r[0] = 0.5*(_reference.array()*_weight.array()*_reference.array()).sum();
111  }
112 
113 }
114 
115 
116 #include "ocra/optim/Variable.h"
117 
118 namespace ocra
119 {
121  {
122  BaseVariable x("x", 3);
123  BaseVariable y("y", 4);
124  BaseVariable z("z", 5);
125 
126  CompositeVariable X("X");
127  X.add(x).add(y);
128 
129  VectorXd ref(X.getSize());
130  ref.setZero();
131 
132 
133  WeightedSquareDistanceFunction f(X, 1., ref);
134 
135  VectorXd v(X.getSize());
136  v.setConstant(1);
137 
138  X.setValue(v);
139  std::cout << f.getValue() << std::endl;
140  std::cout << std::endl;
141 
142  X.add(z);
143  v.resize(X.getSize());
144  v.setConstant(1);
145 
146  X.setValue(v);
147  std::cout << f.getValue() << std::endl;
148  std::cout << std::endl;
149 
150  X.remove(y);
151  v.resize(X.getSize());
152  v.setConstant(1);
153 
154  X.setValue(v);
155  std::cout << f.getValue() << std::endl;
156  std::cout << std::endl;
157 
158  VectorXd w(X.getSize());
159  for (int i=0; i<X.getSize(); ++i)
160  w[i] = i+1;
161  f.changeWeight(w);
162 
163  std::cout << f.getValue() << std::endl;
164  std::cout << std::endl;
165  }
166 }
167 
168 // cmake:sourcegroup=Function
169 
Computation ability of a ocra function.
Definition: IFunction.h:243
VectorXd & _value
Definition: Function.h:312
const VectorXd & getValue() const
Definition: Variable.cpp:94
void setValue(const VectorXd &value)
Definition: Variable.cpp:99
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()
MatrixXd & _jacobian
Definition: Function.h:313
virtual void doUpdateInputSizeEnd()
const VectorXd & getqi(int index=0) const
int getSize() const
Definition: Variable.cpp:81
std::vector< VectorXd * > _q
Declaration file of the WeightedSquareDistanceFunction class.
const VectorXd & getValue() const
Definition: Function.h:365
void changeConvexityProperty(eFunctionConvexity newProperty)
void invalidateAll()
Definition: Function.h:330
CompositeVariable & add(Variable &child)
Attach/detach the child to/from this node.
Definition: Variable.cpp:580
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.
CompositeVariable & remove(Variable &child)
Definition: Variable.cpp:620
WeightedSquareDistanceFunction class.
Implements a basic variable.
Definition: Variable.h:304
void doChangePi(const MatrixXd &Pi, int index=0)