ultimaille / stlbfgs

C++ L-BFGS implementation using plain STL
GNU Affero General Public License v3.0
32 stars 9 forks source link

Divide by zero error in line search #8

Open dspoel opened 2 months ago

dspoel commented 2 months ago

At line 40 in the linesearch.cpp code there is a divide by zero occurring.

    double find_quadratic_minimizer(double a, double ga, double b, double gb) {
        return b + ((b - a)*gb)/(ga - gb);
    }

I tried to code a work around with an if (ga == gb) statement, but don't know what the return value should be in that case. Suggestions?

    double find_quadratic_minimizer(double a, double ga, double b, double gb) {
        if (ga == gb) {
          return 0;
        }
        else {
          return b + ((b - a)*gb)/(ga - gb);
        }
    }
ssloy commented 2 months ago

This functions looks for the minimizer of a quadratic function f(x) defined over the interval [a,b] with given derivatives f'(a) and f'(b). In order to have the problem well-posed the derivatives must obey the condition f'(a)<0 and f'(b)>0.

find_quadratic_minimizer is called from lines 51 and 46 ; 51 should not pose any problem since the condition is verified explicitly, and a call from 46 guarantees two different values f(a) and f(b), and therefore the derivatives must differ.

It does seem that you are working beyond the limits of machine precision.

It is a bad idea to have this workaround. Linesearch must fail in this case. Division by zero is supposed to be checked in lines 90-98 of linesearch.cpp, it would be a good idea to investigate. Do you get an exception?

dspoel commented 2 months ago

Yes, the code crashes with some specific input with a 1/0 error.

I do think precision may be the issue and will try to move to an integer x vector.