cureos / csnumerics

Portable numerical algorithms in C#
GNU Lesser General Public License v3.0
32 stars 11 forks source link

Cobyla turns into infinite loop and never finishes #8

Open jcb-entrnce opened 3 years ago

jcb-entrnce commented 3 years ago

When tot at Cobyla.cs:962 is very small, this line evaluates to temp = 0.0. This causes line 963 to divide by zero, evaluating to alpha = NaN. This eventually causes both step and stepful to both be NaN, which causes Cobyla.cs:1292 to always evaluate to false (because (NaN == NaN) == false), looping forever.

This is annoying since it makes the algorithm unusable for production purposes.

dsanalytics commented 3 years ago

@jcb-entrnce could you please provide the objective function and the constraints used, in the unit test code form hopefully, so that we could try to reproduce the problem? Also, any extra info on top of that would be appreciated - thanks in advance.

anders9ustafsson commented 1 year ago

@jcb-entrnce Sorry for this extremely late response. Looking at the code, my initial estimate would be that the following code in lines 963-964 would resolve your issue:

var alpha = temp != 0 ? sp / temp : Math.Sign(sp);
var beta = temp != 0 ? tot / temp : Math.Sign(tot);

(potentially checking a small epsilon rather than 0). Do you agree?