Skip to content

Commit

Permalink
Merge pull request #180 from hrntsm/feature/support-multi-constraints
Browse files Browse the repository at this point in the history
Feature/support multi constraints
  • Loading branch information
hrntsm authored Jul 24, 2023
2 parents ce70bab + 3f1ffa6 commit c417dd2
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Please see [here](https://github.com/hrntsm/Tunny/releases) for the data release
### Changed

- When optimizing with CMA-ES, the with Margin option is enabled by default.
- Support multi constraints.
- Bump up optuna v3.2.0
- Bump up optuna-dashboard v0.10.2

Expand Down
Binary file not shown.
Binary file modified Samples/Grasshopper/optimization_with_constraint.gh
Binary file not shown.
Binary file modified Samples/Grasshopper/sample.gh
Binary file not shown.
6 changes: 3 additions & 3 deletions Tunny/Component/Util/ConstructFishAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ConstructFishAttribute()
protected override void RegisterInputParams(GH_InputParamManager pManager)
{
pManager.AddGeometryParameter("Geometry", "Geometry", GeomDescription, GH_ParamAccess.list);
pManager.AddNumberParameter("Constraint", "Constraint", ConstraintDescription, GH_ParamAccess.item);
pManager.AddNumberParameter("Constraint", "Constraint", ConstraintDescription, GH_ParamAccess.list);
pManager.AddGenericParameter("Attr1", "Attr1", AttrDescription, GH_ParamAccess.list);
pManager.AddGenericParameter("Attr2", "Attr2", AttrDescription, GH_ParamAccess.list);
Params.Input[0].Optional = true;
Expand Down Expand Up @@ -66,8 +66,8 @@ private void GetInputData(IGH_DataAccess DA, int paramCount, IDictionary<string,
string key = Params.Input[i].NickName;
if (i == 1)
{
double constraint = 0;
if (DA.GetData(i, ref constraint))
var constraint = new List<double>();
if (DA.GetDataList(i, constraint))
{
dict.Add(key, constraint);
}
Expand Down
95 changes: 67 additions & 28 deletions Tunny/Storage/JournalStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,64 +102,103 @@ public StudySummary[] GetStudySummaries(string storagePath)

JournalStorage[] storage = Deserialize(journalStorageString);
var studyName = storage.Where(x => x.OpCode == 0).Select(x => x.StudyName).Distinct().ToList();
IEnumerable<IGrouping<int?, JournalStorage>> userAttr = storage.Where(x => x.OpCode == 2)
.GroupBy(x => x.StudyId);
IEnumerable<IGrouping<int?, JournalStorage>> systemAttr = storage.Where(x => x.OpCode == 3).GroupBy(x => x.StudyId);
IEnumerable<IGrouping<int?, JournalStorage>> userAttr = storage.Where(x => x.OpCode == 2).GroupBy(x => x.StudyId);
var studySummaries = new StudySummary[studyName.Count];

SetStudySummaries(studyName, userAttr, studySummaries);
SetStudySummaries(studyName, systemAttr, userAttr, studySummaries);

return studySummaries;
}

private static void SetStudySummaries(IReadOnlyList<string> studyName, IEnumerable<IGrouping<int?, JournalStorage>> userAttr, IList<StudySummary> studySummaries)
private static void SetStudySummaries(IReadOnlyList<string> studyName, IEnumerable<IGrouping<int?, JournalStorage>> systemAttr, IEnumerable<IGrouping<int?, JournalStorage>> userAttr, IList<StudySummary> studySummaries)
{
int i = 0;
foreach (IGrouping<int?, JournalStorage> group in userAttr)
foreach (var group in userAttr.Zip(systemAttr, (user, system) => new { user, system }))
{
if (group.Key == null)
if (group.user.Key == null)
{
continue;
}

var studySummary = new StudySummary
{
StudyId = group.Key.Value,
StudyId = group.user.Key.Value,
StudyName = studyName[i],
UserAttributes = new Dictionary<string, string[]>(),
SystemAttributes = new Dictionary<string, string[]>(),
Trials = new List<Trial>()
};
SetStudySummaryValue(group, studySummary);
SetStudySummaryValue(group.user, group.system, studySummary);
studySummaries[i++] = studySummary;
}
}

private static void SetStudySummaryValue(IEnumerable<JournalStorage> group, StudySummary studySummary)
private static void SetStudySummaryValue(IEnumerable<JournalStorage> user, IEnumerable<JournalStorage> system, StudySummary studySummary)
{
foreach (JournalStorage journal in group)
foreach (var journal in user.Zip(system, (u, s) => new { u, s }))
{
foreach (KeyValuePair<string, object> item in journal.UserAttr)
foreach (KeyValuePair<string, object> item in journal.u.UserAttr)
{
string[] values = Array.Empty<string>();
if (item.Value is string str)
{
values = str.Split(',');
}
else if (item.Value is string[] strArray)
{
values = strArray;
}
SetUserAttr(studySummary, item);
}
foreach (KeyValuePair<string, object> item in journal.s.SystemAttr)
{
SetSystemAttr(studySummary, item);
}
}
}

if (studySummary.UserAttributes.TryGetValue(item.Key, out string[] value))
{
_ = value.Union(values);
}
else
{
studySummary.UserAttributes.Add(item.Key, values);
}
private static void SetSystemAttr(StudySummary studySummary, KeyValuePair<string, object> item)
{
string[] values = Array.Empty<string>();
if (item.Value is string str)
{
values = str.Split(',');
}
else if (item.Value is string[] strArray)
{
values = strArray;
}
else if (item.Value is Newtonsoft.Json.Linq.JArray strJArray)
{
values = new string[strJArray.Count];
for (int i = 0; i < strJArray.Count; i++)
{
values[i] = strJArray[i].ToString();
}
}

if (studySummary.SystemAttributes.TryGetValue(item.Key, out string[] value))
{
_ = value.Union(values);
}
else
{
studySummary.SystemAttributes.Add(item.Key, values);
}
}

private static void SetUserAttr(StudySummary studySummary, KeyValuePair<string, object> item)
{
string[] values = Array.Empty<string>();
if (item.Value is string str)
{
values = str.Split(',');
}
else if (item.Value is string[] strArray)
{
values = strArray;
}

if (studySummary.UserAttributes.TryGetValue(item.Key, out string[] value))
{
_ = value.Union(values);
}
else
{
studySummary.UserAttributes.Add(item.Key, values);
}
}

public dynamic CreateNewStorage(bool useInnerPythonEngine, Settings.Storage storageSetting)
Expand Down
3 changes: 3 additions & 0 deletions Tunny/Type/GH_FishAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public override string ToString()
case List<string> str:
valueStrings = string.Join(", ", str);
break;
case List<double> num:
valueStrings = string.Join(", ", num);
break;
case List<GeometryBase> geo:
IEnumerable<string> geometryStrings = geo.Select(g => Converter.GeometryBaseToGoo(g).ToString());
valueStrings = string.Join(", ", geometryStrings);
Expand Down
2 changes: 2 additions & 0 deletions Tunny/UI/OptimizationWindow.Designer.cs

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

24 changes: 19 additions & 5 deletions Tunny/UI/OptimizeWindowTab/SettingsTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private void Nsga2CrossoverCheckBox_CheckedChanged(object sender, EventArgs e)

private void Nsga2DefaultButton_Click(object sender, EventArgs e)
{
SetNsgaSettings(new NSGAII());
SetNsga2Settings(new NSGAII());
}

private void Nsga3MutationProbCheckedChanged(object sender, EventArgs e)
Expand All @@ -39,7 +39,7 @@ private void Nsga3CrossoverCheckBox_CheckedChanged(object sender, EventArgs e)

private void Nsga3DefaultButton_Click(object sender, EventArgs e)
{
SetNsgaSettings(new NSGAIII());
SetNsga3Settings(new NSGAIII());
}

private void CmaEsSigmaCheckedChanged(object sender, EventArgs e)
Expand Down Expand Up @@ -90,8 +90,8 @@ private void InitializeSamplerSettings()
SamplerSettings sampler = _settings.Optimize.Sampler;
SetTpeSettings(sampler.Tpe);
SetBoTorchSettings(sampler.BoTorch);
SetNsgaSettings(sampler.NsgaII);
SetNsgaSettings(sampler.NsgaIII);
SetNsga2Settings(sampler.NsgaII);
SetNsga3Settings(sampler.NsgaIII);
SetCmaEsSettings(sampler.CmaEs);
SetQMCSettings(sampler.QMC);
}
Expand All @@ -116,7 +116,7 @@ private void SetBoTorchSettings(BoTorch boTorch)
boTorchStartupNumUpDown.Value = boTorch.NStartupTrials;
}

private void SetNsgaSettings(NSGAII nsga)
private void SetNsga2Settings(NSGAII nsga)
{
nsga2MutationProbCheckBox.Checked = nsga.MutationProb != null;
nsga2MutationProbUpDown.Enabled = nsga2MutationProbCheckBox.Checked;
Expand All @@ -130,6 +130,20 @@ private void SetNsgaSettings(NSGAII nsga)
nsga2CrossoverComboBox.SelectedIndex = SetNsgaCrossoverSetting(nsga.Crossover);
}

private void SetNsga3Settings(NSGAIII nsga)
{
nsga3MutationProbCheckBox.Checked = nsga.MutationProb != null;
nsga3MutationProbUpDown.Enabled = nsga3MutationProbCheckBox.Checked;
nsga3MutationProbUpDown.Value = nsga.MutationProb != null
? (decimal)nsga.MutationProb : 0;
nsga3CrossoverProbUpDown.Value = (decimal)nsga.CrossoverProb;
nsga3SwappingProbUpDown.Value = (decimal)nsga.SwappingProb;
nsga3PopulationSizeUpDown.Value = nsga.PopulationSize;
nsga3CrossoverCheckBox.Checked = nsga.Crossover != string.Empty;
nsga3CrossoverComboBox.Enabled = nsga.Crossover != string.Empty;
nsga3CrossoverComboBox.SelectedIndex = SetNsgaCrossoverSetting(nsga.Crossover);
}

private static int SetNsgaCrossoverSetting(string crossover)
{
switch (crossover)
Expand Down
4 changes: 2 additions & 2 deletions Tunny/Util/GrasshopperInOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,9 @@ public Dictionary<string, List<string>> GetAttributes()
if (key == "Constraint")
{
object obj = _attributes.Value[key];
if (obj is double val)
if (obj is List<double> val)
{
value.Add(val.ToString(CultureInfo.InvariantCulture));
value.AddRange(val.Select(v => v.ToString(CultureInfo.InvariantCulture)));
}
}
else
Expand Down

0 comments on commit c417dd2

Please sign in to comment.