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/make fishegg by csv #366

Merged
merged 5 commits into from
Dec 19, 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ Please see [here](https://github.com/hrntsm/Tunny/releases) for the data release
- Variables can now be set to log scale from the UI.
- Checkbox which minimize Rhino Window when start optimization
- Support some optunahub sampler.
- Auto sampler, MOEA/D sampler, MO-CMA-ES sampler,
- Auto sampler, MOEA/D sampler, MO-CMA-ES sampler
- ConstructFishEggByCsv component
- Make it easy to set FishEgg to use csv.

### Changed

Expand Down
9 changes: 3 additions & 6 deletions Optuna/Sampler/CmaEsSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@ public class CmaEsSampler : SamplerBase
public bool ConsiderPrunedTrials { get; set; }
public string RestartStrategy { get; set; } = string.Empty;
public int IncPopsize { get; set; } = 2;
public int? PopulationSize { get; set; }
public int? PopulationSize { get; set; } = 2;
public bool UseSeparableCma { get; set; }
public bool UseWarmStart { get; set; }
public string WarmStartStudyName { get; set; } = string.Empty;
public bool WithMargin { get; set; } = true;
public bool WithMargin { get; set; }
public bool LrAdapt { get; set; }

public dynamic ToPython(string storagePath, Dictionary<string, double> firstVariables)
public dynamic ToPython(string storagePath, PyDict x0)
{
dynamic optuna = Py.Import("optuna");
Dictionary<string, double> x0 = UseFirstEggToX0 && firstVariables != null && firstVariables.Count > 0
? firstVariables
: null;

return UseWarmStart
? optuna.samplers.CmaEsSampler(
Expand Down
4 changes: 2 additions & 2 deletions Tunny.Core/Settings/Sampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Sampler
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)
public dynamic ToPython(SamplerType type, string storagePath, bool hasConstraints, PyDict cmaEsX0)
{
TLog.MethodStart();
dynamic optunaSampler;
Expand All @@ -51,7 +51,7 @@ public dynamic ToPython(SamplerType type, string storagePath, bool hasConstraint
optunaSampler = NsgaIII.ToPython(hasConstraints);
break;
case SamplerType.CmaEs:
optunaSampler = CmaEs.ToPython(storagePath, firstVariables);
optunaSampler = CmaEs.ToPython(storagePath, cmaEsX0);
break;
case SamplerType.QMC:
optunaSampler = QMC.ToPython();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

namespace Tunny.Core.Util
{
public class SelectionCsvReader
public class CsvReader
{
private readonly string _filePath;

public SelectionCsvReader(string filePath)
public CsvReader(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
Expand All @@ -24,7 +25,39 @@ public SelectionCsvReader(string filePath)
_filePath = filePath;
}

public int[] ReadSelection(CsvType csvType)
public List<Dictionary<string, string>> ReadFishEggCsv()
{
var lines = File.ReadLines(_filePath).ToList();

string[] headers = lines[0].Split(',');
var result = new List<Dictionary<string, string>>();

for (int lineIndex = 1; lineIndex < lines.Count; lineIndex++)
{
string[] values = lines[lineIndex].Split(',');

if (headers.Length != values.Length)
{
throw new InvalidDataException("The number of columns in the CSV file is not consistent.");
}

var rowDictionary = new Dictionary<string, string>();
for (int i = 0; i < headers.Length; i++)
{
if (string.IsNullOrWhiteSpace(values[i]))
{
continue;
}
rowDictionary[headers[i]] = values[i];
}

result.Add(rowDictionary);
}

return result;
}

public int[] ReadSelectionCsv(CsvType csvType)
{
string key = csvType == CsvType.Dashboard
? key = "Number"
Expand Down Expand Up @@ -60,6 +93,6 @@ public int[] ReadSelection(CsvType csvType)
public enum CsvType
{
Dashboard,
DesignExplorer
DesignExplorer,
}
}
5 changes: 5 additions & 0 deletions Tunny.CoreTests/TestFile/EggTest1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x0,x1
0.5,0.5
12,0.1
0.1,15
,0.5
2 changes: 2 additions & 0 deletions Tunny.CoreTests/TestFile/EggTest2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x0,x1
0.5
6 changes: 6 additions & 0 deletions Tunny.CoreTests/Tunny.CoreTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
<None Update="TestFile\DesignExplorer.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestFile\EggTest1.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestFile\EggTest2.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestFile\journal.log">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
87 changes: 87 additions & 0 deletions Tunny.CoreTests/Util/CsvReaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.IO;

using Xunit;

namespace Tunny.Core.Util.Tests
{
public class CsvReaderTests
{
[Fact]
public void DashboardCsvTest()
{
var reader = new CsvReader("TestFile/Dashboard.csv");
int[] result = reader.ReadSelectionCsv(CsvType.Dashboard);

Assert.Equal(4, result.Length);
Assert.Equal(1, result[0]);
Assert.Equal(8, result[1]);
Assert.Equal(10, result[2]);
Assert.Equal(11, result[3]);
}

[Fact]
public void DesignExplorerCsvTest()
{
var reader = new CsvReader("TestFile/DesignExplorer.csv");
int[] result = reader.ReadSelectionCsv(CsvType.DesignExplorer);

Assert.Equal(4, result.Length);
Assert.Equal(4, result[0]);
Assert.Equal(5, result[1]);
Assert.Equal(9, result[2]);
Assert.Equal(16, result[3]);
}

[Fact]
public void NullFilePathTest()
{
Assert.Throws<ArgumentException>(() => new CsvReader(null));
}

[Fact]
public void FileNotFoundTest()
{
Assert.Throws<FileNotFoundException>(() => new CsvReader("TestFile/NotFound.csv"));
}

[Fact]
public void NoLineTest()
{
var reader = new CsvReader("TestFile/empty.csv");
int[] result = reader.ReadSelectionCsv(CsvType.Dashboard);

Assert.Empty(result);
}

[Fact]
public void NoTargetColumnTest()
{
var reader = new CsvReader("TestFile/ColumnsError.csv");
int[] result = reader.ReadSelectionCsv(CsvType.Dashboard);

Assert.Empty(result);
}

[Fact]
public void ReadFishEggCsvTest()
{
var reader = new CsvReader("TestFile/EggTest1.csv");
List<Dictionary<string, string>> result = reader.ReadFishEggCsv();

Assert.Equal(4, result.Count);
Assert.Equal("0.5", result[0]["x0"]);
Assert.Equal("0.5", result[0]["x1"]);
Assert.False(result[3].ContainsKey("x0"));
Assert.Equal("0.5", result[0]["x1"]);
}

[Fact]
public void ReadFishEggCsvErrorTest()
{
var reader = new CsvReader("TestFile/EggTest2.csv");
Assert.Throws<InvalidDataException>(() => reader.ReadFishEggCsv());
}
}
}
66 changes: 0 additions & 66 deletions Tunny.CoreTests/Util/SelectionCsvReaderTests.cs

This file was deleted.

Loading
Loading