diff --git a/CHANGELOG.md b/CHANGELOG.md index d6e229ba..d446bd2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ for new features. - Stopped putting built files together in gha. - Because some people did not work in some environments +- If the objective function contains null for 10 consecutive times, optimization is stopped. ### Deprecated diff --git a/Tunny/Solver/Algorithm.cs b/Tunny/Solver/Algorithm.cs index a5e49e13..2ed62a73 100644 --- a/Tunny/Solver/Algorithm.cs +++ b/Tunny/Solver/Algorithm.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Reflection; using System.Text; +using System.Windows.Forms; using Python.Runtime; @@ -202,7 +203,7 @@ private void RunOptimize(RunOptimizeSettings optSet, out double[] xTest, out Eva while (true) { - if (CheckOptimizeComplete(optSet.NTrials, optSet.Timeout, trialNum, startTime)) + if (result == null || CheckOptimizeComplete(optSet.NTrials, optSet.Timeout, trialNum, startTime)) { break; } @@ -223,7 +224,7 @@ private void RunHumanInTheLoopOptimize(RunOptimizeSettings optSet, int nBatch, o while (true) { - if (CheckOptimizeComplete(optSet.NTrials, optSet.Timeout, trialNum, startTime)) + if (result == null || CheckOptimizeComplete(optSet.NTrials, optSet.Timeout, trialNum, startTime)) { break; } @@ -244,9 +245,8 @@ private EvaluatedGHResult RunSingleOptimizeStep(RunOptimizeSettings optSet, doub dynamic trial = optSet.Study.ask(); var result = new EvaluatedGHResult(); - //TODO: Is this the correct way to handle the case of null? int nullCount = 0; - while (nullCount < 10) + while (true) { for (int j = 0; j < Variables.Count; j++) { @@ -259,7 +259,17 @@ private EvaluatedGHResult RunSingleOptimizeStep(RunOptimizeSettings optSet, doub result = EvalFunc(pState, progress); optSet.HumanInTheLoop?.SaveNote(optSet.Study, trial, result.ObjectiveImages); - if (result.ObjectiveValues.Contains(double.NaN)) + if (nullCount >= 10) + { + TunnyMessageBox.Show( + "The objective function returned NaN 10 times in a row. Tunny terminates the optimization. Please check the objective function.", + "Tunny", + MessageBoxButtons.OK, + MessageBoxIcon.Error + ); + return null; + } + else if (result.ObjectiveValues.Contains(double.NaN)) { trial = optSet.Study.ask(); nullCount++;