diff --git a/.gitignore b/.gitignore index dda8400a..3c47d795 100644 --- a/.gitignore +++ b/.gitignore @@ -460,6 +460,5 @@ $RECYCLE.BIN/ ## add by me Tunny/Lib/python.zip Tunny/Lib/whl.zip -!OptunaTests/TestFile/*.log -!OptunaTests/TestFile/*.db +!*/TestFile/** coveragereport/* \ No newline at end of file diff --git a/Tunny.Core/PostProcess/ModelResult.cs b/Tunny.Core/PostProcess/ModelResult.cs deleted file mode 100644 index 3042eb65..00000000 --- a/Tunny.Core/PostProcess/ModelResult.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -using Optuna.Trial; - -using Tunny.Core.Util; - -namespace Tunny.Core.PostProcess -{ - public class ModelResult - { - public int Number { get; set; } - public double[] Objectives { get; set; } - public Dictionary Variables { get; set; } - public Dictionary> Attributes { get; set; } - - public ModelResult(Trial trial) - { - TLog.MethodStart(); - Number = trial.Number; - Variables = trial.Params; - Objectives = trial.Values; - Attributes = ParseAttrs(trial.UserAttrs); - } - - private static Dictionary> ParseAttrs(Dictionary userAttrs) - { - TLog.MethodStart(); - var attributes = new Dictionary>(); - foreach (string key in userAttrs.Keys) - { - string[] values = userAttrs[key] as string[]; - attributes.Add(key, values.ToList()); - } - return attributes; - } - } -} diff --git a/Tunny.Core/Solver/Output.cs b/Tunny.Core/Solver/Output.cs new file mode 100644 index 00000000..ac0a8593 --- /dev/null +++ b/Tunny.Core/Solver/Output.cs @@ -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() : 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 targetNumbers, Study study) + { + TLog.MethodStart(); + var trials = new List(); + 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(); + } + } +} diff --git a/Tunny.Core/Storage/StorageHandler.cs b/Tunny.Core/Storage/StorageHandler.cs index 71e98c9c..9a8098f6 100644 --- a/Tunny.Core/Storage/StorageHandler.cs +++ b/Tunny.Core/Storage/StorageHandler.cs @@ -1,6 +1,7 @@ using System; using System.IO; +using Optuna.Storage; using Optuna.Study; using Tunny.Core.Util; @@ -75,5 +76,25 @@ 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; + } } } diff --git a/Tunny.CoreTests/Solver/OutputTests.cs b/Tunny.CoreTests/Solver/OutputTests.cs new file mode 100644 index 00000000..cbabbf5f --- /dev/null +++ b/Tunny.CoreTests/Solver/OutputTests.cs @@ -0,0 +1,54 @@ +using Optuna.Trial; + +using Xunit; + +namespace Tunny.Core.Solver.Tests +{ + public class OutputTests + { + private readonly Output _output; + + public OutputTests() + { + _output = new Output("TestFile/journal.log"); + } + + [Fact] + public void GetParetoFrontTrialTest() + { + Trial[] trials = _output.GetTargetTrial(new [] { -1 }, "output_test"); + Assert.Equal(2, trials.Length); + } + + [Fact] + public void GetAllTargetTrialTest() + { + Trial[] trials = _output.GetTargetTrial(new [] { -10 }, "output_test"); + Assert.Equal(10, trials.Length); + } + + [Fact] + public void GetSpecificTargetTrialTest() + { + Trial[] trials = _output.GetTargetTrial(new [] { 3, 2, 9 }, "output_test"); + Assert.Equal(3, trials.Length); + Assert.Equal(3, trials[0].Number); + Assert.Equal(2, trials[1].Number); + Assert.Equal(9, trials[2].Number); + } + + [Fact] + public void GetSpecificTargetTrialWithNullTest() + { + Trial[] trials = _output.GetTargetTrial(new [] { 100 }, "output_test"); + Assert.Empty(trials); + } + + [Fact] + public void NonExistentStudyTest() + { + Trial[] trials = _output.GetTargetTrial(new [] { -1 }, "nonexistent_study"); + Assert.Empty(trials); + } + } +} diff --git a/Tunny.CoreTests/TestFile/journal.log b/Tunny.CoreTests/TestFile/journal.log new file mode 100644 index 00000000..a01ddfe0 --- /dev/null +++ b/Tunny.CoreTests/TestFile/journal.log @@ -0,0 +1,64 @@ +{"op_code": 0, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_name": "output_test", "directions": [1, 1]} +{"op_code": 2, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_id": 0, "user_attr": {"variable_names": ["x", "y"]}} +{"op_code": 2, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_id": 0, "user_attr": {"tunny_version": "0.11.0"}} +{"op_code": 3, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_id": 0, "system_attr": {"study:metric_names": ["v0", "v1"]}} +{"op_code": 4, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_id": 0, "datetime_start": "2024-03-11T18:40:55.851324"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 0, "param_name": "x", "param_value_internal": 1.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 0, "param_name": "y", "param_value_internal": -1.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 8, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 0, "user_attr": {"Constraint": [992.0, 448.0]}} +{"op_code": 9, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 0, "system_attr": {"constraints": [992.0, 448.0]}} +{"op_code": 6, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 0, "state": 1, "values": [8.0, 52.0], "datetime_complete": "2024-03-11T18:40:55.898645"} +{"op_code": 4, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_id": 0, "datetime_start": "2024-03-11T18:40:55.904660"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 1, "param_name": "x", "param_value_internal": -11.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 1, "param_name": "y", "param_value_internal": -13.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 8, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 1, "user_attr": {"Constraint": [-160.0, -80.0]}} +{"op_code": 9, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 1, "system_attr": {"constraints": [-160.0, -80.0]}} +{"op_code": 6, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 1, "state": 1, "values": [1160.0, 580.0], "datetime_complete": "2024-03-11T18:40:55.931506"} +{"op_code": 4, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_id": 0, "datetime_start": "2024-03-11T18:40:55.935015"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 2, "param_name": "x", "param_value_internal": 13.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 2, "param_name": "y", "param_value_internal": 10.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 8, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 2, "user_attr": {"Constraint": [-76.0, 411.0]}} +{"op_code": 9, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 2, "system_attr": {"constraints": [-76.0, 411.0]}} +{"op_code": 6, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 2, "state": 1, "values": [1076.0, 89.0], "datetime_complete": "2024-03-11T18:40:55.960902"} +{"op_code": 4, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_id": 0, "datetime_start": "2024-03-11T18:40:55.964406"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 3, "param_name": "x", "param_value_internal": 25.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 3, "param_name": "y", "param_value_internal": 17.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 8, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 3, "user_attr": {"Constraint": [-2656.0, -44.0]}} +{"op_code": 9, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 3, "system_attr": {"constraints": [-2656.0, -44.0]}} +{"op_code": 6, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 3, "state": 1, "values": [3656.0, 544.0], "datetime_complete": "2024-03-11T18:40:55.990868"} +{"op_code": 4, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_id": 0, "datetime_start": "2024-03-11T18:40:55.993869"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 4, "param_name": "x", "param_value_internal": -13.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 4, "param_name": "y", "param_value_internal": -9.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 8, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 4, "user_attr": {"Constraint": [0.0, -20.0]}} +{"op_code": 9, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 4, "system_attr": {"constraints": [0.0, -20.0]}} +{"op_code": 6, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 4, "state": 1, "values": [1000.0, 520.0], "datetime_complete": "2024-03-11T18:40:56.020016"} +{"op_code": 4, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_id": 0, "datetime_start": "2024-03-11T18:40:56.023016"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 5, "param_name": "x", "param_value_internal": -10.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 5, "param_name": "y", "param_value_internal": 12.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 8, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 5, "user_attr": {"Constraint": [24.0, 226.0]}} +{"op_code": 9, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 5, "system_attr": {"constraints": [24.0, 226.0]}} +{"op_code": 6, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 5, "state": 1, "values": [976.0, 274.0], "datetime_complete": "2024-03-11T18:40:56.049157"} +{"op_code": 4, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_id": 0, "datetime_start": "2024-03-11T18:40:56.052157"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 6, "param_name": "x", "param_value_internal": 26.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 6, "param_name": "y", "param_value_internal": -6.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 8, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 6, "user_attr": {"Constraint": [-1848.0, -62.0]}} +{"op_code": 9, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 6, "system_attr": {"constraints": [-1848.0, -62.0]}} +{"op_code": 6, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 6, "state": 1, "values": [2848.0, 562.0], "datetime_complete": "2024-03-11T18:40:56.081181"} +{"op_code": 4, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_id": 0, "datetime_start": "2024-03-11T18:40:56.085181"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 7, "param_name": "x", "param_value_internal": 18.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 7, "param_name": "y", "param_value_internal": 22.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 8, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 7, "user_attr": {"Constraint": [-2232.0, 42.0]}} +{"op_code": 9, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 7, "system_attr": {"constraints": [-2232.0, 42.0]}} +{"op_code": 6, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 7, "state": 1, "values": [3232.0, 458.0], "datetime_complete": "2024-03-11T18:40:56.116238"} +{"op_code": 4, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_id": 0, "datetime_start": "2024-03-11T18:40:56.121744"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 8, "param_name": "x", "param_value_internal": -13.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 8, "param_name": "y", "param_value_internal": 3.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 8, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 8, "user_attr": {"Constraint": [288.0, 172.0]}} +{"op_code": 9, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 8, "system_attr": {"constraints": [288.0, 172.0]}} +{"op_code": 6, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 8, "state": 1, "values": [712.0, 328.0], "datetime_complete": "2024-03-11T18:40:56.151750"} +{"op_code": 4, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "study_id": 0, "datetime_start": "2024-03-11T18:40:56.155766"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 9, "param_name": "x", "param_value_internal": 8.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 5, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 9, "param_name": "y", "param_value_internal": 1.0, "distribution": "{\"name\": \"IntDistribution\", \"attributes\": {\"log\": false, \"step\": 1, \"low\": -15, \"high\": 30}}"} +{"op_code": 8, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 9, "user_attr": {"Constraint": [740.0, 475.0]}} +{"op_code": 9, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 9, "system_attr": {"constraints": [740.0, 475.0]}} +{"op_code": 6, "worker_id": "a4e31300-6239-4801-9d2b-ff51d0d0c585-21232", "trial_id": 9, "state": 1, "values": [260.0, 25.0], "datetime_complete": "2024-03-11T18:40:56.181772"} diff --git a/Tunny.CoreTests/Tunny.CoreTests.csproj b/Tunny.CoreTests/Tunny.CoreTests.csproj index 61e5d2d3..8b929d7c 100644 --- a/Tunny.CoreTests/Tunny.CoreTests.csproj +++ b/Tunny.CoreTests/Tunny.CoreTests.csproj @@ -17,4 +17,10 @@ + + + PreserveNewest + + + diff --git a/Tunny/Handler/OutputLoop.cs b/Tunny/Handler/OutputLoop.cs index 996bfa17..0ffb3121 100644 --- a/Tunny/Handler/OutputLoop.cs +++ b/Tunny/Handler/OutputLoop.cs @@ -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; @@ -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; @@ -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(); } @@ -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 fishes, ModelResult model, IEnumerable nickname) + public static void SetResultToFish(List fishes, Trial trial, IEnumerable 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 SetVariables(ModelResult model, IEnumerable nickNames) + private static Dictionary SetVariables(Dictionary variables, IEnumerable 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 SetObjectives(ModelResult model) + private static Dictionary SetObjectives(double[] values) { TLog.MethodStart(); string[] nickNames = Component.GhInOut.Objectives.GetNickNames(); var objectives = new Dictionary(); - 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 SetAttributes(ModelResult model) + private static Dictionary SetAttributes(Dictionary> trialAttr) { TLog.MethodStart(); var attribute = new Dictionary(); - foreach (KeyValuePair> attr in model.Attributes) + foreach (KeyValuePair> attr in trialAttr) { if (attr.Key == "Geometry") { @@ -124,5 +125,17 @@ private static Dictionary SetAttributes(ModelResult model) } return attribute; } + + private static Dictionary> ParseAttrs(Dictionary userAttrs) + { + TLog.MethodStart(); + var attributes = new Dictionary>(); + foreach (string key in userAttrs.Keys) + { + string[] values = userAttrs[key] as string[]; + attributes.Add(key, values.ToList()); + } + return attributes; + } } } diff --git a/Tunny/Solver/Output.cs b/Tunny/Solver/Output.cs deleted file mode 100644 index b2093d2a..00000000 --- a/Tunny/Solver/Output.cs +++ /dev/null @@ -1,127 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.IO; -using System.Linq; -using System.Windows.Forms; - -using Optuna.Storage; -using Optuna.Study; -using Optuna.Trial; - -using Tunny.Core.PostProcess; -using Tunny.Core.Util; -using Tunny.Handler; -using Tunny.UI; - -namespace Tunny.Core.Solver -{ - public class Output - { - private readonly string _storagePath; - - public Output(string storagePath) - { - _storagePath = storagePath; - } - - public ModelResult[] GetModelResult(int[] resultNum, string studyName, BackgroundWorker worker) - { - TLog.MethodStart(); - var modelResult = new List(); - IOptunaStorage storage = GetStorage(); - Study targetStudy = storage.GetAllStudies().FirstOrDefault(s => s.StudyName == studyName); - if (targetStudy == null) - { - TunnyMessageBox.Show("There are no output models. Please check study name.", "Tunny", MessageBoxButtons.OK, MessageBoxIcon.Error); - return modelResult.ToArray(); - } - SetTrialsToModelResult(resultNum, modelResult, targetStudy, worker); - return modelResult.ToArray(); - } - - private IOptunaStorage GetStorage() - { - string ext = Path.GetExtension(_storagePath); - IOptunaStorage storage; - if (ext == ".db" || ext == ".sqlite") - { - storage = new Optuna.Storage.RDB.SqliteStorage(_storagePath, true); - } - else if (ext == ".log") - { - storage = new Optuna.Storage.Journal.JournalStorage(_storagePath, true); - } - else - { - throw new ArgumentException("Storage type not supported"); - } - - return storage; - } - - private static void SetTrialsToModelResult(int[] resultNum, List modelResult, Study study, BackgroundWorker worker) - { - TLog.MethodStart(); - if (resultNum[0] == -1) - { - ParatoSolutions(modelResult, study, worker); - } - else if (resultNum[0] == -10) - { - AllTrials(modelResult, study, worker); - } - else - { - UseModelNumber(resultNum, modelResult, study, worker); - } - } - - private static void UseModelNumber(IReadOnlyList resultNum, List modelResult, Study study, BackgroundWorker worker) - { - TLog.MethodStart(); - var trials = new List(); - for (int i = 0; i < resultNum.Count; i++) - { - int res = resultNum[i]; - try - { - trials.Add(study.Trials.FirstOrDefault(t => t.Number == res)); - } - catch (Exception e) - { - TunnyMessageBox.Show("Error\n\n" + e.Message, "Tunny", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - SetAndReportModelResult(modelResult, worker, trials); - } - - private static void AllTrials(List modelResult, Study study, BackgroundWorker worker) - { - TLog.MethodStart(); - List trials = study.Trials; - SetAndReportModelResult(modelResult, worker, trials); - } - - private static void ParatoSolutions(List modelResult, Study study, BackgroundWorker worker) - { - TLog.MethodStart(); - Trial[] bestTrials = study.BestTrials; - SetAndReportModelResult(modelResult, worker, bestTrials.ToList()); - } - - private static void SetAndReportModelResult(List modelResult, BackgroundWorker worker, List trials) - { - for (int i = 0; i < trials.Count; i++) - { - Trial trial = trials[i]; - if (OutputLoop.IsForcedStopOutput) - { - break; - } - modelResult.Add(new ModelResult(trial)); - worker?.ReportProgress(i * 100 / trials.Count); - } - } - } -}