Skip to content

Commit

Permalink
Merge pull request #383 from hrntsm/Fix/result-update-timing
Browse files Browse the repository at this point in the history
Fix get gh result timing
  • Loading branch information
hrntsm authored Feb 7, 2025
2 parents 0f3a07f + 5b68a7b commit 7f5f480
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 33 deletions.
5 changes: 2 additions & 3 deletions Tunny/PostProcess/TrialGrasshopperItems.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;

using Tunny.Input;
Expand All @@ -8,7 +7,7 @@ namespace Tunny.PostProcess
public class TrialGrasshopperItems
{
public Objective Objectives { get; set; }
public Dictionary<string, List<string>> Attribute { get; set; }
public Dictionary<string, List<string>> Attributes { get; set; }
public Artifact Artifacts { get; set; }

public TrialGrasshopperItems()
Expand All @@ -18,7 +17,7 @@ public TrialGrasshopperItems()
public TrialGrasshopperItems(double[] values)
{
Objectives = new Objective(values);
Attribute = new Dictionary<string, List<string>>();
Attributes = new Dictionary<string, List<string>>();
Artifacts = new Artifact();
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tunny/Process/OptimizeProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private static TrialGrasshopperItems EvaluateFunction(ProgressState pState, int
return new TrialGrasshopperItems
{
Objectives = component.GhInOut.Objectives,
Attribute = component.GhInOut.GetAttributes(),
Attributes = component.GhInOut.Attributes,
Artifacts = component.GhInOut.Artifacts,
};
}
Expand Down
6 changes: 3 additions & 3 deletions Tunny/Solver/Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ private void TellResultToOptuna(OptimizationHandlingInfo optInfo, Parameter[] pa
{
TellPruned(optInfo, trialNum, trial);
}
else if (result.Attribute.TryGetValue("IsFAIL", out List<string> isFail) && isFail.Contains("True"))
else if (result.Attributes.TryGetValue("IsFAIL", out List<string> isFail) && isFail.Contains("True"))
{
TellFailed(optInfo, trialNum, trial);
}
Expand Down Expand Up @@ -616,7 +616,7 @@ private void SetStudyUserAttr(StudyWrapper study, string[] variableName, bool se
private static void SetTrialUserAttr(TrialGrasshopperItems result, TrialWrapper trial, OptimizationHandlingInfo optSet)
{
TLog.MethodStart();
if (result.Attribute != null)
if (result.Attributes != null)
{
SetNonGeometricAttr(result, trial);
}
Expand All @@ -643,7 +643,7 @@ private static void SetTrialUserAttr(TrialGrasshopperItems result, TrialWrapper
private static void SetNonGeometricAttr(TrialGrasshopperItems result, TrialWrapper trial)
{
TLog.MethodStart();
foreach (KeyValuePair<string, List<string>> pair in result.Attribute)
foreach (KeyValuePair<string, List<string>> pair in result.Attributes)
{
PyObject pyReportValues;
if (pair.Key == "Constraint")
Expand Down
83 changes: 57 additions & 26 deletions Tunny/Util/GrasshopperInOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,48 @@ public class GrasshopperInOut
private readonly List<Guid> _inputGuids;
private readonly GH_Component _component;
private List<GalapagosGeneListObject> _genePool;
private GH_FishAttribute _attributes;
private List<GH_NumberSlider> _sliders;
private List<TunnyValueList> _valueLists;

public Objective Objectives { get; private set; }
public List<VariableBase> Variables { get; private set; }
public Artifact Artifacts { get; private set; }
public List<FishEgg> FishEggs { get; private set; }
public bool HasConstraint { get; private set; }
public bool IsMultiObjective => Objectives.Length > 1;
public bool IsLoadCorrectly { get; }
private Objective _objectives;
public Objective Objectives
{
get
{
GetObjectives();
return _objectives;
}
}
private GH_FishAttribute _fishAttributes;
public Dictionary<string, List<string>> Attributes
{
get
{
GetFishAttributes();
return FishAttributesToDict();
}
}
private Artifact _artifacts;
public Artifact Artifacts
{
get
{
GetArtifacts();
return _artifacts;
}
}
private bool _hasConstraint;
public bool HasConstraint
{
get
{
GetFishAttributes();
return _hasConstraint;
}
}

public GrasshopperInOut(GH_Component component, bool getVariableOnly = false)
{
Expand All @@ -52,7 +83,7 @@ public GrasshopperInOut(GH_Component component, bool getVariableOnly = false)

IsLoadCorrectly = getVariableOnly
? SetVariables()
: SetVariables() && SetObjectives() && SetAttributes() && SetArtifacts();
: SetVariables() && GetObjectives() && GetFishAttributes() && GetArtifacts();
}

private bool SetVariables()
Expand Down Expand Up @@ -229,7 +260,7 @@ private void SetInputValueItem(List<VariableBase> variables)
}
}

private bool SetObjectives()
private bool GetObjectives()
{
TLog.MethodStart();
if (_component.Params.Input[1].SourceCount == 0)
Expand All @@ -254,7 +285,7 @@ private bool SetObjectives()
return ShowIncorrectObjectiveInputMessage(unsupportedObjectives);
}
if (!CheckObjectiveNicknameDuplication(_component.Params.Input[1].Sources.ToArray())) { return false; }
Objectives = new Objective(_component.Params.Input[1].Sources.ToList());
_objectives = new Objective(_component.Params.Input[1].Sources.ToList());
return true;
}

Expand Down Expand Up @@ -284,10 +315,10 @@ private bool ShowIncorrectObjectiveInputMessage(List<IGH_Param> unsupportedSourc
return false;
}

private bool SetAttributes()
private bool GetFishAttributes()
{
TLog.MethodStart();
_attributes = new GH_FishAttribute();
_fishAttributes = new GH_FishAttribute();
if (_component.Params.Input[2].SourceCount == 0)
{
return true;
Expand All @@ -302,8 +333,8 @@ private bool SetAttributes()
{
if (goo is GH_FishAttribute fishAttr)
{
_attributes = fishAttr;
HasConstraint = fishAttr.Value.ContainsKey("Constraint");
_fishAttributes = fishAttr;
_hasConstraint = fishAttr.Value.ContainsKey("Constraint");
break;
}
}
Expand Down Expand Up @@ -420,9 +451,9 @@ public async void NewSolution(IList<Parameter> parameters)
ExpireInput(_component.Params.Input[1]); // objectives
ExpireInput(_component.Params.Input[3]); // artifacts
await RecalculateAsync();
SetObjectives();
SetAttributes();
SetArtifacts();
GetObjectives();
GetFishAttributes();
GetArtifacts();
}

private void ExpireInput(IGH_Param input)
Expand All @@ -442,21 +473,21 @@ private void ExpireInput(IGH_Param input)
}
}

public Dictionary<string, List<string>> GetAttributes()
private Dictionary<string, List<string>> FishAttributesToDict()
{
TLog.MethodStart();
var attrs = new Dictionary<string, List<string>>();
if (_attributes.Value == null)
if (_fishAttributes.Value == null)
{
return attrs;
}

foreach (string key in _attributes.Value.Keys)
foreach (string key in _fishAttributes.Value.Keys)
{
var value = new List<string>();
if (key == "Constraint")
{
object obj = _attributes.Value[key];
object obj = _fishAttributes.Value[key];
if (obj is List<double> val)
{
value.AddRange(val.Select(v => v.ToString(CultureInfo.InvariantCulture)));
Expand All @@ -475,7 +506,7 @@ public Dictionary<string, List<string>> GetAttributes()
private void AddGooValues(string key, List<string> value)
{
TLog.MethodStart();
if (_attributes.Value[key] is List<object> objList)
if (_fishAttributes.Value[key] is List<object> objList)
{
foreach (object param in objList)
{
Expand All @@ -487,10 +518,10 @@ private void AddGooValues(string key, List<string> value)
}
}

private bool SetArtifacts()
private bool GetArtifacts()
{
TLog.MethodStart();
Artifacts = new Artifact();
_artifacts = new Artifact();
if (_component.Params.Input[3].SourceCount == 0)
{
return true;
Expand All @@ -504,24 +535,24 @@ private bool SetArtifacts()
bool result = goo.CastTo(out GeometryBase geometry);
if (result)
{
Artifacts.Geometries.Add(geometry);
_artifacts.Geometries.Add(geometry);
continue;
}

if (goo is GH_FishPrint fishPrint)
{
Artifacts.Images.Add(fishPrint.Value);
_artifacts.Images.Add(fishPrint.Value);
continue;
}

result = goo.CastTo(out string path);
if (result)
{
Artifacts.AddFilePathToArtifact(path);
_artifacts.AddFilePathToArtifact(path);
}
}
}
return Artifacts.Count() != 0;
return _artifacts.Count() != 0;
}
}
}

0 comments on commit 7f5f480

Please sign in to comment.