Commit 26f071b2 authored by VAN TOLL Wouter's avatar VAN TOLL Wouter

Improved the LocalSearch class (although it remains unused for now).

parent cfc7d7b7
......@@ -19,11 +19,12 @@
** See the file AUTHORS.md for a list of all contributors.
*/
#include "tools/localsearch.h"
#include "iostream"
#include <tools/localsearch.h>
#include <iostream>
using namespace std;
double LocalSearch::backtr(Vector2D &x0, Vector2D grad, double (*F)(Vector2D), double alpha_0, double gamma, double delta, double rhok)
double LocalSearch::backtr(const Vector2D &x0, const Vector2D& grad, const std::function<float(const Vector2D&)>& F, double alpha_0, double gamma, double delta, double rhok)
{
/*
https://www.mathworks.com/matlabcentral/fileexchange/45572-backtracking-armijo-type?s_tid=FX_rc2_behav
......@@ -57,16 +58,14 @@ double LocalSearch::backtr(Vector2D &x0, Vector2D grad, double (*F)(Vector2D), d
%
*/
const Vector2D& Xk = x0;
const Vector2D& dk = grad;
F(Xk); // test
float Fx0 = F(x0);
float dkSqrMag = dk.sqrMagnitude();
float dkSqrMag = grad.sqrMagnitude();
float dkMag = sqrtf(dkSqrMag);
// positive direction (+)alpha
double alpha = alpha_0;
while (F(Xk+(float)alpha*dk)>F(Xk)-gamma*alpha*alpha*dkSqrMag)
while (F(x0+(float)alpha*grad)>Fx0-gamma*alpha*alpha*dkSqrMag)
{
if (alpha*dkMag < rhok)
alpha = 0; // % <-- failure to search for a value of alpha nonzero
......@@ -75,11 +74,11 @@ double LocalSearch::backtr(Vector2D &x0, Vector2D grad, double (*F)(Vector2D), d
}
double alpha1 = alpha;
double F1 = F(Xk+(float)alpha1*dk)-(F(Xk)-gamma*alpha1*alpha1*dkSqrMag);
double F1 = F(x0+(float)alpha1*grad)-(Fx0-gamma*alpha1*alpha1*dkSqrMag);
// negative direction (-)alpha
alpha = alpha_0;
while (F(Xk-(float)alpha*dk)>F(Xk)-gamma*alpha*alpha*dkSqrMag)
while (F(x0-(float)alpha*grad)>Fx0-gamma*alpha*alpha*dkSqrMag)
{
if (alpha*dkMag < rhok)
alpha = 0; // % <-- failure to search for a value of alpha nonzero
......@@ -88,12 +87,8 @@ double LocalSearch::backtr(Vector2D &x0, Vector2D grad, double (*F)(Vector2D), d
}
double alpha2 = -alpha;
double F2 = F(Xk+(float)alpha2*dk)-(F(Xk)-gamma*alpha2*alpha2*dkSqrMag);
// choice of the value of alpha for which it is provided with sufficient reduction
if (F1<F2)
alpha = alpha1;
else
alpha = alpha2;
double F2 = F(x0+(float)alpha2*grad)-(Fx0-gamma*alpha2*alpha2*dkSqrMag);
return alpha;
// choice of the value of alpha for which it is provided with sufficient reduction
return (F1 < F2 ? alpha1 : alpha2);
}
......@@ -22,13 +22,14 @@
#ifndef LOCALSEARCH_H
#define LOCALSEARCH_H
#include "vector2D.h"
#include <tools/vector2D.h>
#include <functional>
class LocalSearch
{
public:
LocalSearch() {}
static double backtr(Vector2D& x0, Vector2D grad, double (*F)(Vector2D), double alpha_0=1, double gamma=1e-4, double delta=0.5, double rhok=1e-8);
static double backtr(const Vector2D& x0, const Vector2D& grad, const std::function<float(const Vector2D&)>& F, double alpha_0=1, double gamma=1e-4, double delta=0.5, double rhok=1e-8);
};
#endif // LOCALSEARCH_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment