Skip to content

Commit

Permalink
Merge pull request #192 from hrntsm/Fix/ten-times-null-exception
Browse files Browse the repository at this point in the history
Fix/ten times null exception
  • Loading branch information
hrntsm authored Sep 4, 2023
2 parents 3165858 + 0962c60 commit 7fe59f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 15 additions & 5 deletions Tunny/Solver/Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

using Python.Runtime;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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++)
{
Expand All @@ -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++;
Expand Down

0 comments on commit 7fe59f3

Please sign in to comment.