13

I've got a piece of undocumented code, which I have to understand to fix an error. The following method is called optimization and it is supposed to find the maximum of a very complex function f. Unfortunately, it fails under some circumstances (i.e. it reaches the "Max iteration reached" line).

I already tried to write some unit tests, but this didn't help much.

So I want to understand how this method really works and if it implements a specific, and well known optimization algorithm. Maybe I can then understand, if it is suitable to solve the required equations.

public static double optimization(double x1, double x2, double x3, Function<Double, Double> f, double epsilon) {
    double y1 = f.apply(x1);
    double y2 = f.apply(x2);
    double y3 = f.apply(x3);

    double a = (   x1*(y2-y3)+   x2*(y3-y1)+   x3*(y1-y2)) / ((x1-x2)*(x1-x3)*(x3-x2));
    double b = (x1*x1*(y2-y3)+x2*x2*(y3-y1)+x3*x3*(y1-y2)) / ((x1-x2)*(x1-x3)*(x2-x3));
    int i=0;
    do {
        i=i+1;

        x3=x2;
        x2=x1;
        x1=-1.*b/(2*a);

        y1=f.apply(x1);
        y2=f.apply(x2);
        y3=f.apply(x3);

        a = (   x1*(y2-y3)+   x2*(y3-y1)+   x3*(y1-y2))/((x1-x2)*(x1-x3)*(x3-x2));
        b = (x1*x1*(y2-y3)+x2*x2*(y3-y1)+x3*x3*(y1-y2))/((x1-x2)*(x1-x3)*(x2-x3));
    } while((Math.abs(x1 - x2) > epsilon) && (i<1000));
    if (i==1000){
        Log.debug("Max iteration reached");
    }
    return x1;
}
Stanley F.
  • 1,846
  • 16
  • 29
  • judging from the name `epsilon`, it seems that it does some operation within some acceptable epsilon error, it's like doing some computation narrowing the result, until it becomes `Math.abs(x1 - x2) < epsilon)`, but only up to 1000 interations – Eugene Sep 13 '18 at 09:49
  • 1
    If you don't understand what the function does, look at where/how it's used, this might give you some idea what it's supposed to do – tkausl Sep 13 '18 at 09:50
  • The code shows what the function does, what are you exactly asking? – m0skit0 Sep 13 '18 at 09:51
  • What values / functions does the surrounding code pass as parameter `Function f`? – deHaar Sep 13 '18 at 09:52
  • @tkausl I know, that it is supposed to search for a maximum in `f`. I just don't get *how* it works and if `f` needs to fit any specific requirements. – Stanley F. Sep 13 '18 at 09:52
  • @deHaar The callers pass mathematical functions from the kinetics including nested polynomials of 36th and 45th degree with coefficients read from a file. – Stanley F. Sep 13 '18 at 10:02
  • @m0skit0 Assume this [mcve]: `double d(double x) { return x*x; }` I see that it multiplies a number with itself. But I ask for the name "square function" (or a more detailed description (i.e. *how does it work*) in case of my more complex code). – Stanley F. Sep 13 '18 at 10:12

1 Answers1

11

This seems to be a Successive parabolic interpolation.

One of the clues is the replacement of the oldest of three estimates by the position of the extremum,

    x3= x2;
    x2= x1;
    x1= -1. * b / (2 * a);

The method may fail if the estimates do not achieve an extremum configuration (in particular at an inflection point).

  • That's it. This method also fails, if the function is multi-modal, which it is in my case. Besides a maximum, it also has a pole, which the optimization tries to find in some circumstances (and thus runs to infinity). – Stanley F. Sep 17 '18 at 04:20
  • @StanleyF.: fortunately, with floating-point arithmetic, reaching infinity doesn't take forever ;-) –  Sep 17 '18 at 07:00