Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/refactor output #264

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
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();
}
}
}
21 changes: 21 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,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;
}
}
}
54 changes: 54 additions & 0 deletions Tunny.CoreTests/Solver/OutputTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
64 changes: 64 additions & 0 deletions Tunny.CoreTests/TestFile/journal.log
Original file line number Diff line number Diff line change
@@ -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"}
6 changes: 6 additions & 0 deletions Tunny.CoreTests/Tunny.CoreTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@
<ProjectReference Include="..\Tunny.Core\Tunny.Core.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="TestFile\journal.log">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading
Loading