6 NewtonSolver::NewtonSolver(
bool fullNewton)
8 , _adaptativeAlpha(true)
9 , _completeMethod(fullNewton)
24 _adaptativeAlpha = adapt;
29 return _adaptativeAlpha;
35 _epsilonSqr = eps*eps;
40 return std::sqrt(_epsilonSqr);
68 _objectives.push_back(&obj);
73 for(
size_t i = 0; i < _objectives.size(); ++i)
75 if(&_objectives[i]->getFunction() == &obj)
86 _objectives.erase(std::find(_objectives.begin(), _objectives.end(), &obj));
91 std::cout <<
"objective(s):" <<std::endl;
92 for (
unsigned int i=0; i<_objectives.size(); ++i)
93 std::cout << _objectives[i]->getValue() << std::endl;
98 return "non implemented yet";
106 _buffer.
resize(s*(2*s+3));
112 new (&_ldlt) LDLT<MatrixXd>(s);
121 if (_x0.size() != _x.size())
123 _x0 = VectorXd::Zero(_x.size());
135 void NewtonSolver::initInfo()
138 _info.
residual = std::numeric_limits<double>::max();
145 void NewtonSolver::newtonSolve()
148 for (; _info.
iter<=_maxIter; ++_info.
iter)
153 if (!_ldlt.isPositive())
160 _p = _ldlt.solve(_g);
161 _x -= compute_alpha()*_p;
172 double NewtonSolver::compute_alpha()
174 if (_adaptativeAlpha)
176 double ri = _alpha*_alpha*_p.squaredNorm();
185 double newAlpha = _alpha;
187 _alpha = std::min<double> (_alpha * 1.1, 1.0);
193 _info.
residual = _alpha*_alpha*_p.squaredNorm();
198 void NewtonSolver::compute_H()
201 for (
size_t i=0; i<_objectives.size(); ++i)
203 Function& obj = _objectives[i]->getFunction();
213 _tmpH = J.transpose()*J;
219 void NewtonSolver::compute_g()
222 for (
size_t i=0; i<_objectives.size(); ++i)
224 Function& obj = _objectives[i]->getFunction();
232 void NewtonSolver::translateReturnInfo(
eReturnInfo& ocraInfo)
236 else if (_info.
iter>_maxIter)
265 MatrixXd A1 = MatrixXd::Random(n1,n1);
266 VectorXd b1 = VectorXd::Random(n1);
267 MatrixXd A2 = 100*MatrixXd::Random(n2,n2);
268 VectorXd b2 = 100*VectorXd::Random(n2);
277 std::cout << solver.
solve().
solution.transpose() << std::endl;
284 std::cout << solverQP.
solve().
solution.transpose() << std::endl;
287 std::cout <<
"It works !" << std::endl;
289 std::cout <<
"It doesn't work !" << std::endl;
306 MatrixXd A1 = MatrixXd::Random(n1,n1);
307 VectorXd b1 = VectorXd::Random(n1);
308 MatrixXd A2 = 100*MatrixXd::Random(n2,n2);
309 VectorXd b2 = 100*VectorXd::Random(n2);
319 for (
int i=0; i<100; ++i)
321 std::cout << solver.
solve().
solution.transpose() << std::endl;
322 A1 += a*MatrixXd::Random(n1,n1);
323 b1 += a*VectorXd::Random(n1);
324 A2 += a*MatrixXd::Random(n2,n2);
325 b2 += a*VectorXd::Random(n2);
const Variable & getVariable() const
const VectorXd & get_x0() const
void setVariableValue(const VectorXd &value)
void changeA(const MatrixXd &A)
void printValuesAtSolution()
void testNewtonSolver01()
void internalAddObjective(const GenericObjective &objective)
void addCompressed2d(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
Eigen::Map< MatrixXd > MatrixMap
Declaration file of the SquaredLinearFunction class.
void set_x0(const VectorXd &x0)
OptimizationResult _result
const OptimizationResult & solve()
const std::vector< int > & findMapping(Variable &var)
void setAdaptativeAlpha(bool adapt)
void changeb(const VectorXd &b)
void internalRemoveObjective(const GenericObjective &objective)
Declaration file of the NewtonSolver class.
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
Declaration file of the QLDSolver class.
void setEpsilon(double eps)
const MatrixXd & getJacobian() const
Eigen::Map< VectorXd > VectorMap
bool getAdaptativeAlpha() const
void addCompressedByRow(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
SquaredLinearFunction class.
const VectorXd & getValue() const
const OptimizationResult & getLastResult() const
void addObjective(QuadraticObjective &obj)
virtual Function & getFunction()
void addObjective(GenericObjective &obj)
A concatenation of base variables and other composite variables.
void testNewtonSolver02()
void setMaxIter(int maxIter)
#define ocra_assert(ocra_expression)
void removeObjective(Function &obj)
double getEpsilon() const
Implements a basic variable.
ptr_type allocate(size_t n)