Skip to content

Commit

Permalink
Remove ModelResult
Browse files Browse the repository at this point in the history
  • Loading branch information
NATSUME Hiroaki committed Mar 11, 2024
1 parent 5597467 commit 2a8846d
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 184 deletions.
38 changes: 0 additions & 38 deletions Tunny.Core/PostProcess/ModelResult.cs

This file was deleted.

64 changes: 64 additions & 0 deletions Tunny.Core/Solver/Output.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;

using Optuna.Storage;
using Optuna.Study;
using Optuna.Trial;

using Tunny.Core.Storage;
using Tunny.Core.Util;

namespace Tunny.Core.Solver
{
public class Output
{
private readonly string _storagePath;

public Output(string storagePath)
{
_storagePath = storagePath;
}

public Trial[] GetTargetTrial(int[] targetNumbers, string studyName)
{
TLog.MethodStart();
IOptunaStorage storage = StorageHandler.GetStorage(_storagePath);
Study targetStudy = storage.GetAllStudies().FirstOrDefault(s => s.StudyName == studyName);
return targetStudy == null ? Array.Empty<Trial>() : GetTargetTrials(targetNumbers, targetStudy);
}

private static Trial[] GetTargetTrials(int[] targetNumbers, Study study)
{
TLog.MethodStart();
if (targetNumbers[0] == -1)
{
return study.BestTrials;
}
else if (targetNumbers[0] == -10)
{
return study.Trials.ToArray();
}
else
{
return UseTrialNumber(targetNumbers, study);
}
}

private static Trial[] UseTrialNumber(IReadOnlyList<int> targetNumbers, Study study)
{
TLog.MethodStart();
var trials = new List<Trial>();
for (int i = 0; i < targetNumbers.Count; i++)
{
int target = targetNumbers[i];
Trial trial = study.Trials.FirstOrDefault(t => t.Number == target);
if (trial != null)
{
trials.Add(trial);
}
}
return trials.ToArray();
}
}
}
22 changes: 22 additions & 0 deletions Tunny.Core/Storage/StorageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;

using Optuna.Storage;
using Optuna.Study;

using Tunny.Core.Util;
Expand Down Expand Up @@ -75,5 +76,26 @@ public StudySummary[] GetStudySummaries(string storagePath)

return storage.GetStudySummaries(storagePath);
}

public static IOptunaStorage GetStorage(string path)
{
string ext = Path.GetExtension(path);
IOptunaStorage storage;
if (ext == ".db" || ext == ".sqlite")
{
storage = new Optuna.Storage.RDB.SqliteStorage(path, true);
}
else if (ext == ".log")
{
storage = new Optuna.Storage.Journal.JournalStorage(path, true);
}
else
{
throw new ArgumentException("Storage type not supported");
}

return storage;
}

}
}
51 changes: 32 additions & 19 deletions Tunny/Handler/OutputLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
using System.Linq;
using System.Windows.Forms;

using Optuna.Trial;

using Rhino.Geometry;
using Rhino.Runtime;

using Tunny.Component.Optimizer;
using Tunny.Core.PostProcess;
using Tunny.Core.Settings;
using Tunny.Core.Solver;
using Tunny.Core.TEnum;
Expand Down Expand Up @@ -39,8 +40,8 @@ internal static void Run(object sender, DoWorkEventArgs e)
if (Component != null)
{
var output = new Output(Settings.Storage.Path);
ModelResult[] modelResult = output.GetModelResult(Indices, StudyName, s_worker);
if (modelResult.Length == 0)
Trial[] targetTrials = output.GetTargetTrial(Indices, StudyName);
if (targetTrials.Length == 0)
{
TunnyMessageBox.Show("There are no output models. Please check study name.", "Tunny", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
Expand All @@ -50,9 +51,9 @@ internal static void Run(object sender, DoWorkEventArgs e)
TunnyMessageBox.Show("Pareto solution is output with no constraints taken into account.", "Tunny", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

foreach (ModelResult result in modelResult)
foreach (Trial trial in targetTrials)
{
SetResultToFish(fishes, result, NickNames);
SetResultToFish(fishes, trial, NickNames);
}
Component.Fishes = fishes.ToArray();
}
Expand All @@ -62,53 +63,53 @@ internal static void Run(object sender, DoWorkEventArgs e)
TunnyMessageBox.Show("Output result to fish completed successfully.", "Tunny");
}

public static void SetResultToFish(List<Fish> fishes, ModelResult model, IEnumerable<string> nickname)
public static void SetResultToFish(List<Fish> fishes, Trial trial, IEnumerable<string> nickname)
{
TLog.MethodStart();
fishes.Add(new Fish
{
ModelNumber = model.Number,
Variables = SetVariables(model, nickname),
Objectives = SetObjectives(model),
Attributes = SetAttributes(model),
ModelNumber = trial.Number,
Variables = SetVariables(trial.Params, nickname),
Objectives = SetObjectives(trial.Values),
Attributes = SetAttributes(ParseAttrs(trial.UserAttrs)),
});
}

private static Dictionary<string, object> SetVariables(ModelResult model, IEnumerable<string> nickNames)
private static Dictionary<string, object> SetVariables(Dictionary<string, object> variables, IEnumerable<string> nickNames)
{
TLog.MethodStart();
return nickNames.SelectMany(name => model.Variables.Where(obj => obj.Key == name))
return nickNames.SelectMany(name => variables.Where(obj => obj.Key == name))
.ToDictionary(variable => variable.Key, variable => variable.Value);
}

private static Dictionary<string, double> SetObjectives(ModelResult model)
private static Dictionary<string, double> SetObjectives(double[] values)
{
TLog.MethodStart();
string[] nickNames = Component.GhInOut.Objectives.GetNickNames();
var objectives = new Dictionary<string, double>();
if (model.Objectives == null)
if (values == null)
{
return null;
}
for (int i = 0; i < model.Objectives.Length; i++)
for (int i = 0; i < values.Length; i++)
{
if (objectives.ContainsKey(nickNames[i]))
{
objectives.Add(nickNames[i] + i, model.Objectives[i]);
objectives.Add(nickNames[i] + i, values[i]);
}
else
{
objectives.Add(nickNames[i], model.Objectives[i]);
objectives.Add(nickNames[i], values[i]);
}
}
return objectives;
}

private static Dictionary<string, object> SetAttributes(ModelResult model)
private static Dictionary<string, object> SetAttributes(Dictionary<string, List<string>> trialAttr)
{
TLog.MethodStart();
var attribute = new Dictionary<string, object>();
foreach (KeyValuePair<string, List<string>> attr in model.Attributes)
foreach (KeyValuePair<string, List<string>> attr in trialAttr)
{
if (attr.Key == "Geometry")
{
Expand All @@ -124,5 +125,17 @@ private static Dictionary<string, object> SetAttributes(ModelResult model)
}
return attribute;
}

private static Dictionary<string, List<string>> ParseAttrs(Dictionary<string, object> userAttrs)
{
TLog.MethodStart();
var attributes = new Dictionary<string, List<string>>();
foreach (string key in userAttrs.Keys)
{
string[] values = userAttrs[key] as string[];
attributes.Add(key, values.ToList());
}
return attributes;
}
}
}
127 changes: 0 additions & 127 deletions Tunny/Solver/Output.cs

This file was deleted.

0 comments on commit 2a8846d

Please sign in to comment.