mathnet / mathnet-numerics

Math.NET Numerics
http://numerics.mathdotnet.com
MIT License
3.47k stars 893 forks source link

GoldenSectionMinimizer.FindMinimum() does not respect the lowerBound argument #733

Open bt-88 opened 3 years ago

bt-88 commented 3 years ago

The function ScalarMinimizationResult FindMinimum(IScalarObjectiveFunction objective, double lowerBound, double upperBound) (in Numerics.Optimization) does not respect the lowerBound argument.

In my example of FindMinimum(somefunction, 0.05, 0.4) the GS-algorithm expands below 0.05 and tries to check values that are outside of the boundary [0.05, 0.4].

In the source code, I have identified the 'problem': https://github.com/mathnet/mathnet-numerics/blob/ad05896d3b4e317c3e5418e5fadf3968604fdbcd/src/Numerics/Optimization/GoldenSectionMinimizer.cs#L75-L78

So, in my case, the new lower bound is set to:

lowerBound = 0.5*(0.4+0.05) - 2*0.5*(0.4-0.05); // This is -0.125

Is this indeed a bug?

RichardMelito commented 3 years ago

I encountered the same thing with FindMinimum.OfScalarFunctionConstrained since it is implemented with GoldenSectionMinimizer.FindMinimum. A workaround is to edit the function being minimized so that it returns double.MaxValue if the given value is outside of the bounds.

diluculo commented 3 years ago

The bounds seems to be intended just as a starting interval to search. It is why there are LowerExpansionFactor UpperExpansionFactor arguments. Please see here. It might be better to change the names to avoid confusion.

arielhammon commented 2 years ago

I also encountered this problem. @diluculo is correct; it is hard baked into the algorithm. @RichardMelito's work-around works great.