ocra-recipes
Doxygen documentation for the ocra-recipes repository
SquaredLinearFunction.cpp
Go to the documentation of this file.
2 
3 namespace ocra
4 {
5  SquaredLinearFunction::SquaredLinearFunction(LinearFunction* f)
6  :NamedInstance("squared linear function")
9  ,QuadraticFunction(f->getVariable())
10  , _f(f)
11  , _weight(VectorXd::Ones(f->getDimension()))
12  {
19  }
20 
22  {
27  }
28 
29 
30  void SquaredLinearFunction::changeWeight(const VectorXd& weight)
31  {
32  if(weight.size() != _f->getDimension()){
33  std::stringstream errorStream;
34  errorStream << "[SquaredLinearFunction::changeWeight] weight has dimension: " << weight.size() << " and should be: " << _f->getDimension()<<".\n";
35  std::string errorString = errorStream.str();
36  throw std::runtime_error(errorString);
37  }
38  _weight = weight;
39  }
40 
41  // void SquaredLinearFunction::setWeight(const VectorXd& weight)
42  // {
43  // if(weight.size() != _f->getDimension())
44  // throw std::runtime_error("[SquaredLinearFunction::changeWeight] weight has not the appropriate size");
45  // _weight = weight;
46  // }
47 
48 
50  {
51  return *_f;
52  }
53 
54 
56  {
57  return *_f;
58  }
59 
60 
62  {
63  *IFunction<PARTIAL_XX>::_val[0] = _f->getA().transpose()* _weight.asDiagonal() * _f->getA();
64  }
65 
66 
68  {
69  // std::cout << "updateQ in SquaredLinearFunction\n"<< _weight.transpose() << std::endl;
70  *_q[0] = _f->getA().transpose()*_weight.asDiagonal()*_f->getb();
71  }
72 
73 
75  {
76  _r[0] = 0.5 * _f->getb().transpose() * _weight.asDiagonal() *_f->getb();
77  }
78 
79 
81  {
82  // Do nothing. This overload is just here to enable input resizing (default implementation throw an exception).
83  }
84 
85 
87  {
88  if(_f->getDimension() < _weight.size())
89  {
90  VectorXd oldWeight = _weight;
91  _weight = oldWeight.head(_f->getDimension());
92  }
93  else if(_f->getDimension() < _weight.size())
94  {
95  VectorXd oldWeight = _weight;
96  _weight = VectorXd::Ones(_f->getDimension());
97  _weight.head(oldWeight.size()) = oldWeight;
98  }
99  }
100 
101 }
102 
104 
105 namespace ocra
106 {
108  {
109  BaseVariable x("x", 3);
110  MatrixXd A(2,3); A << 1,0,0, 0,1,1;
111  VectorXd b(2); b << 1,0;
112  LinearFunction lf(x, A, b);
113  SquaredLinearFunction sqf(&lf);
114 
115  VectorXd v(3); v << 1,2,1;
116  x.setValue(v);
117 
118  std::cout << sqf.getValue() << std::endl;
119 
120  BaseVariable y("y", 3);
121  DiagonalLinearFunction dl(y,1.,2.,true);
122  SquaredLinearFunction sqf2(&dl);
123 
124  VectorXd vy(3); vy << 1,2,1;
125  y.setValue(vy);
126  std::cout << sqf2.getJacobian() << std::endl;
127 
128  y.resize(4);
129  vy.resize(4); vy << 1,2,1,0;
130  y.setValue(vy);
131  std::cout << sqf2.getJacobian() << std::endl;
132  }
133 }
134 
135 // cmake:sourcegroup=Function
Computation ability of a ocra function.
Definition: IFunction.h:243
void invalidateq(int timestamp)
int getDimension() const
Definition: Function.cpp:41
void inhibitPropagationFrom_q_or_r() const
void setValue(const VectorXd &value)
Definition: Variable.cpp:99
Variable & x
Definition: Function.h:309
Declaration file of the SquaredLinearFunction class.
LinearFunction class.
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
void testSquaredLinearFunction()
const MatrixXd & getJacobian() const
Definition: Function.h:375
std::vector< VectorXd * > _q
const VectorXd & getb() const
SquaredLinearFunction class.
const VectorXd & getValue() const
Definition: Function.h:365
Declaration file of the DiagonalLinearFunction class.
void resize(size_t newSize)
Definition: Variable.cpp:344
const MatrixXd & getA() const
void changeWeight(const VectorXd &weight)
void changeConvexityProperty(eFunctionConvexity newProperty)
void invalidateAll()
Definition: Function.h:330
void invalidater(int timestamp)
QuadraticFunction class.
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
DiagonalLinearFunction class.