Skip to content

Commit

Permalink
Add bruteforce sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
NATSUME Hiroaki committed Jun 5, 2024
1 parent 9be1a59 commit dc97019
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 6 deletions.
15 changes: 15 additions & 0 deletions Optuna/Sampler/BruteForceSampler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Optuna.Sampler
{
/// <summary>
/// https://optuna.readthedocs.io/en/stable/reference/samplers/generated/optuna.samplers.BruteForceSampler.html
/// </summary>
public class BruteForceSampler : SamplerBase
{
public dynamic ToPython(dynamic optuna)
{

Check warning on line 9 in Optuna/Sampler/BruteForceSampler.cs

View check run for this annotation

Codecov / codecov/patch

Optuna/Sampler/BruteForceSampler.cs#L9

Added line #L9 was not covered by tests
return optuna.samplers.BruteForceSampler(
seed: Seed
);
}

Check warning on line 13 in Optuna/Sampler/BruteForceSampler.cs

View check run for this annotation

Codecov / codecov/patch

Optuna/Sampler/BruteForceSampler.cs#L11-L13

Added lines #L11 - L13 were not covered by tests
}
}
4 changes: 4 additions & 0 deletions Tunny.Core/Settings/Sampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Sampler
public QMCSampler QMC { get; set; } = new QMCSampler();
public BoTorchSampler BoTorch { get; set; } = new BoTorchSampler();
public GPSampler GP { get; set; } = new GPSampler();
public BruteForceSampler BruteForce { get; set; } = new BruteForceSampler();

public dynamic ToPython(SamplerType type, string storagePath, bool hasConstraints, Dictionary<string, double> firstVariables)
{
Expand Down Expand Up @@ -52,6 +53,9 @@ public dynamic ToPython(SamplerType type, string storagePath, bool hasConstraint
case SamplerType.Random:
optunaSampler = Random.ToPython(optuna);
break;
case SamplerType.BruteForce:
optunaSampler = BruteForce.ToPython(optuna);
break;

Check warning on line 58 in Tunny.Core/Settings/Sampler.cs

View check run for this annotation

Codecov / codecov/patch

Tunny.Core/Settings/Sampler.cs#L58

Added line #L58 was not covered by tests
default:
throw new ArgumentException("Invalid sampler type.");
}
Expand Down
3 changes: 2 additions & 1 deletion Tunny.Core/TEnum/EndState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum EndState
StoppedByUser,
DirectionNumNotMatch,
UseExitStudyWithoutContinue,
Error
Error,
StoppedByOptuna
}
}
1 change: 1 addition & 0 deletions Tunny.Core/TEnum/SamplerType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public enum SamplerType
CmaEs = 5,
QMC = 6,
Random = 7,
BruteForce = 8,
}
}
19 changes: 15 additions & 4 deletions Tunny/Solver/Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private void RunOptimize(OptimizationHandlingInfo optInfo, out Parameter[] param

while (true)
{
if (result == null || CheckOptimizeComplete(optInfo.NTrials, optInfo.Timeout, trialNum, startTime))
if (result == null || CheckOptimizeComplete(optInfo, trialNum, startTime))
{
break;
}
Expand All @@ -235,7 +235,7 @@ private void RunPreferentialOptimize(OptimizationHandlingInfo optInfo, int nBatc

while (true)
{
if (result == null || CheckOptimizeComplete(optInfo.NTrials, optInfo.Timeout, trialNum, startTime))
if (result == null || CheckOptimizeComplete(optInfo, trialNum, startTime))
{
break;
}
Expand All @@ -261,7 +261,7 @@ private void RunHumanSidlerInputOptimize(OptimizationHandlingInfo optInfo, int n

while (true)
{
if (result == null || CheckOptimizeComplete(optInfo.NTrials, optInfo.Timeout, trialNum, startTime))
if (result == null || CheckOptimizeComplete(optInfo, trialNum, startTime))
{
break;
}
Expand Down Expand Up @@ -347,6 +347,7 @@ private TrialGrasshopperItems RunSingleOptimizeStep(OptimizationHandlingInfo opt
}
else if (optInfo.HumanSliderInput == null && optInfo.Preferential == null)
{
dynamic aa = optInfo.Study._stop_flag;

Check notice on line 350 in Tunny/Solver/Algorithm.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Tunny/Solver/Algorithm.cs#L350

Remove the unused local variable 'aa'.
optInfo.Study.tell(trial, result.Objectives.Numbers);
SetTrialResultLog(trialNum, result, optInfo, parameter);
}
Expand Down Expand Up @@ -447,9 +448,14 @@ private static TrialGrasshopperItems TenTimesNullResultErrorMessage()
return null;
}

private bool CheckOptimizeComplete(int nTrials, double timeout, int trialNum, DateTime startTime)
private bool CheckOptimizeComplete(OptimizationHandlingInfo optInfo, int trialNum, DateTime startTime)
{
TLog.MethodStart();

int nTrials = optInfo.NTrials;
double timeout = optInfo.Timeout;
dynamic study = optInfo.Study;

bool isOptimizeCompleted = false;
if (trialNum >= nTrials)
{
Expand All @@ -467,6 +473,11 @@ private bool CheckOptimizeComplete(int nTrials, double timeout, int trialNum, Da
OptimizeLoop.IsForcedStopOptimize = false;
isOptimizeCompleted = true;
}
else if (study._stop_flag)
{
EndState = EndState.StoppedByOptuna;
isOptimizeCompleted = true;
}

return isOptimizeCompleted;
}
Expand Down
6 changes: 6 additions & 0 deletions Tunny/Solver/Solver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ private static void ToComponentEndMessage(Algorithm optimize, BoneFishComponent
case EndState.StoppedByUser:
message = "Solver completed successfully. The user stopped the solver.";
break;
case EndState.StoppedByOptuna:
message = "Solver completed successfully. The Optuna stopped the solver.";
break;
case EndState.DirectionNumNotMatch:
message = "Solver error. The number of Objective in the existing Study does not match the one that you tried to run; Match the number of objective, or change the \"Study Name\".";
break;
Expand Down Expand Up @@ -143,6 +146,9 @@ private static void ShowUIEndMessages(EndState endState)
case EndState.StoppedByUser:
TunnyMessageBox.Show("Solver completed successfully.\n\nThe user stopped the solver.", "Tunny");
break;
case EndState.StoppedByOptuna:
TunnyMessageBox.Show("Solver completed successfully.\n\nThe Optuna stopped the solver.", "Tunny");
break;
case EndState.DirectionNumNotMatch:
TunnyMessageBox.Show("Solver error.\n\nThe number of Objective in the existing Study does not match the one that you tried to run; Match the number of objective, or change the \"Study Name\".", "Tunny", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
Expand Down
3 changes: 2 additions & 1 deletion Tunny/UI/OptimizationWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dc97019

Please sign in to comment.