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

Add support artifacts #210

Merged
merged 12 commits into from
Dec 28, 2023
15 changes: 7 additions & 8 deletions PYTHON_PACKAGE_LICENSES
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ summary
|-------------------|---------|---------------------------------------------------|
| Mako | 1.2.4 | MIT License |
| MarkupSafe | 2.1.3 | BSD License |
| PyYAML | 6.0 | MIT License |
| SQLAlchemy | 2.0.20 | MIT License |
| PyYAML | 6.0.1 | MIT License |
| SQLAlchemy | 2.0.21 | MIT License |
| alembic | 1.12.0 | MIT License |
| botorch | 0.7.3 | MIT License |
| bottle | 0.12.25 | MIT License |
Expand All @@ -24,26 +24,25 @@ summary
| joblib | 1.3.2 | BSD License |
| linear-operator | 0.2.0 | MIT |
| multipledispatch | 0.6.0 | BSD |
| numpy | 1.25.2 | BSD License |
| numpy | 1.26.0 | BSD License |
| opt-einsum | 3.3.0 | MIT |
| optuna | 3.3.0 | MIT License |
| optuna-dashboard | 0.12.0 | MIT License |
| optuna-dashboard | 0.13.0b1| MIT License |
| packaging | 23.1 | Apache Software License; BSD License |
| pip | 23.0 | MIT License |
| pip-licenses | 4.0.3 | MIT License |
| plotly | 5.16.1 | MIT |
| pyro-api | 0.1.2 | Apache Software License |
| pyro-ppl | 1.8.4 | Apache Software License |
| scikit-learn | 1.3.0 | BSD License |
| scipy | 1.11.2 | BSD License |
| scikit-learn | 1.3.1 | BSD License |
| scipy | 1.11.3 | BSD License |
| setuptools | 65.5.0 | MIT License |
| six | 1.16.0 | MIT License |
| tenacity | 8.2.3 | Apache Software License |
| threadpoolctl | 3.2.0 | BSD License |
| torch | 1.13.1 | BSD License |
| tqdm | 4.66.1 | MIT License; Mozilla Public License 2.0 (MPL 2.0) |
| typing_extensions | 4.4.0 | Python Software Foundation License |
| wcwidth | 0.2.6 | MIT License |
| typing_extensions | 4.8.0 | Python Software Foundation License |

----------------------------------------------------------------------------------------------------

Expand Down
Binary file added Samples/Grasshopper/save_artifact_to_trial.gh
Binary file not shown.
22 changes: 21 additions & 1 deletion Tunny/Component/Optimizer/FishingComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ protected override void RegisterInputParams(GH_InputParamManager pManager)
pManager.AddGenericParameter("Variables", "Vars", "Connect variable number slider here.", GH_ParamAccess.tree);
pManager.AddGenericParameter("Objectives", "Objs", "Connect objective number component here.", GH_ParamAccess.tree);
pManager.AddParameter(new Param_FishAttribute(), "Attributes", "Attrs", "Connect model attribute like some geometry or values here. Not required.", GH_ParamAccess.item);
pManager.AddGenericParameter("Artifacts", "Artfs", "Connect artifacts here. Not required.", GH_ParamAccess.item);
Params.Input[0].Optional = true;
Params.Input[1].Optional = true;
Params.Input[2].Optional = true;
Params.Input[3].Optional = true;
}

protected override void RegisterOutputParams(GH_OutputParamManager pManager)
Expand All @@ -54,6 +56,7 @@ protected override void SolveInstance(IGH_DataAccess DA)
{
CheckVariablesInput(Params.Input[0].Sources.Select(ghParam => ghParam.InstanceGuid));
CheckObjectivesInput(Params.Input[1].Sources.Select(ghParam => ghParam.InstanceGuid));
CheckArtifactsInput(Params.Input[3].Sources.Select(ghParam => ghParam.InstanceGuid));

DA.SetDataList(0, Fishes);
}
Expand Down Expand Up @@ -82,7 +85,6 @@ private void CheckObjectivesInput(IEnumerable<Guid> inputGuids)
switch (docObject)
{
case Param_Number number:
case Param_FishPrint fPrint:
break;
default:
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"{docObject} input is not a valid objective.");
Expand All @@ -91,6 +93,24 @@ private void CheckObjectivesInput(IEnumerable<Guid> inputGuids)
}
}

private void CheckArtifactsInput(IEnumerable<Guid> inputGuids)
{
foreach ((IGH_DocumentObject docObject, int _) in inputGuids.Select((guid, i) => (OnPingDocument().FindObject(guid, false), i)))
{
switch (docObject)
{
case Param_Geometry geometry:
case Param_FishPrint fPrint:
case Param_String text:
case Param_FilePath filePath:
break;
default:
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"{docObject} input is not a valid artifact.");
break;
}
}
}

public void GhInOutInstantiate()
{
GhInOut = new GrasshopperInOut(this);
Expand Down
9 changes: 6 additions & 3 deletions Tunny/Component/Optimizer/FishingComponentAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ private void DrawWires(GH_Canvas canvas, Graphics graphics)
new Wire(3, Color.DarkBlue),
new Wire(3, Color.Green),
new Wire(3, Color.DarkMagenta),
new Wire(3, Color.Crimson),
})
: (new[]
{
new Wire(2, Color.FromArgb(Convert.ToInt32("3300008B", 16))),
new Wire(2, Color.FromArgb(Convert.ToInt32("33008000", 16))),
new Wire(2, Color.FromArgb(Convert.ToInt32("338B008B", 16))),
new Wire(2, Color.FromArgb(Convert.ToInt32("33DC143C", 16))),
});
for (int i = 0; i < 3; i++)
for (int i = 0; i < 4; i++)
{
DrawPath(canvas, graphics, Owner.Params.Input[i], wires[i]);
}
Expand All @@ -85,9 +87,10 @@ private void RenderInputComponentBoxes(Graphics graphics)
new SolidBrush(Color.FromArgb(Convert.ToInt32("9900008B", 16))),
new SolidBrush(Color.FromArgb(Convert.ToInt32("99008000", 16))),
new SolidBrush(Color.FromArgb(Convert.ToInt32("998B008B", 16))),
new SolidBrush(Color.FromArgb(Convert.ToInt32("99DC143C", 16))),
};
Pen[] edge = new[] { Pens.DarkBlue, Pens.Green, Pens.DarkMagenta };
for (int i = 0; i < 3; i++)
Pen[] edge = new[] { Pens.DarkBlue, Pens.Green, Pens.DarkMagenta, Pens.Crimson };
for (int i = 0; i < 4; i++)
{
foreach (Guid guid in Owner.Params.Input[i].Sources.Select(s => s.InstanceGuid))
{
Expand Down
1 change: 1 addition & 0 deletions Tunny/Component/Util/ConstructFishEgg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Grasshopper.Kernel;

using Tunny.Component.Params;
using Tunny.PreProcess;
using Tunny.Type;
using Tunny.Util;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Diagnostics;

namespace Tunny.Util
namespace Tunny.Handler
{
public static class DashboardHandler
{
Expand Down
8 changes: 5 additions & 3 deletions Tunny/Handler/OptimizeLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
using Grasshopper.Kernel;

using Tunny.Component.Optimizer;
using Tunny.PostProcess;
using Tunny.PreProcess;
using Tunny.Settings;
using Tunny.Solver;
using Tunny.Type;
using Tunny.UI;
using Tunny.Util;

namespace Tunny.Handler
{
Expand Down Expand Up @@ -67,7 +68,7 @@ private static double[] RunOptimizationLoop(BackgroundWorker worker)
return solverStarted ? optunaSolver.XOpt : new[] { double.NaN };
}

private static EvaluatedGHResult EvaluateFunction(ProgressState pState, int progress)
private static TrialGrasshopperItems EvaluateFunction(ProgressState pState, int progress)
{
s_component.OptimizationWindow.GrasshopperStatus = OptimizationWindow.GrasshopperStates.RequestSent;

Expand All @@ -76,12 +77,13 @@ private static EvaluatedGHResult EvaluateFunction(ProgressState pState, int prog
{ /*just wait*/ }

TunnyObjective objective = s_component.GhInOut.GetObjectiveValues();
return new EvaluatedGHResult
return new TrialGrasshopperItems
{
ObjectiveValues = objective.Numbers,
ObjectiveImages = objective.Images,
GeometryJson = s_component.GhInOut.GetGeometryJson(),
Attribute = s_component.GhInOut.GetAttributes(),
Artifacts = s_component.GhInOut.Artifacts,
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tunny/Handler/OutputLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
using Rhino.Runtime;

using Tunny.Component.Optimizer;
using Tunny.PostProcess;
using Tunny.Settings;
using Tunny.Solver;
using Tunny.Type;
using Tunny.UI;
using Tunny.Util;

namespace Tunny.Handler
{
Expand Down
12 changes: 6 additions & 6 deletions Tunny/Lib/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ linear-operator==0.2.0
Mako==1.2.4
MarkupSafe==2.1.3
multipledispatch==0.6.0
numpy==1.25.2
numpy==1.26.0
opt-einsum==3.3.0
optuna==3.3.0
optuna-dashboard==0.12.0
optuna-dashboard==0.13.0b1
packaging==23.1
plotly==5.16.1
pyro-api==0.1.2
pyro-ppl==1.8.4
PyYAML==6.0.1
scikit-learn==1.3.0
scipy==1.11.2
scikit-learn==1.3.1
scipy==1.11.3
six==1.16.0
SQLAlchemy==2.0.20
SQLAlchemy==2.0.21
tenacity==8.2.3
threadpoolctl==3.2.0
torch==1.13.1
tqdm==4.66.1
typing_extensions==4.7.1
typing_extensions==4.8.0
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace Tunny.Util
namespace Tunny.PostProcess
{
public class ModelResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Tunny.Util
namespace Tunny.PostProcess
{
public enum OutputMode
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Tunny.Util
namespace Tunny.PostProcess
{
public enum PlotActionType
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Tunny.Util
namespace Tunny.PostProcess
{
public class PlotSettings
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System.Collections.Generic;
using System.Drawing;

namespace Tunny.Util
using Tunny.PreProcess;

namespace Tunny.PostProcess
{
public class EvaluatedGHResult
public class TrialGrasshopperItems
{
public double[] ObjectiveValues { get; set; }
public string[] GeometryJson { get; set; }
public Dictionary<string, List<string>> Attribute { get; set; }
public Bitmap[] ObjectiveImages { get; set; }
public Artifact Artifacts { get; set; }
}
}
74 changes: 74 additions & 0 deletions Tunny/PreProcess/Artifact.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

using Rhino;
using Rhino.DocObjects;
using Rhino.Geometry;

namespace Tunny.PreProcess
{
public class Artifact
{
public List<GeometryBase> Geometries { get; set; } = new List<GeometryBase>();
public List<Bitmap> Images { get; set; } = new List<Bitmap>();
public List<string> ArtifactPaths { get; private set; } = new List<string>();

public void AddFilePathToArtifact(string path)
{
if (File.Exists(path))
{
ArtifactPaths.Add(path);
}
}

public int Count()
{
return Geometries.Count + Images.Count;
}

public void SaveAllArtifacts(string basePath)
{
SaveRhino3dm(basePath);
SaveImage(basePath);
}

public void SaveRhino3dm(string basePath)
{
string path = basePath + "_model.3dm";
var rhinoDoc = RhinoDoc.CreateHeadless("");
foreach (GeometryBase geom in Geometries)
{
rhinoDoc.Objects.Add(geom);
}

foreach (RhinoObject obj in rhinoDoc.Objects)
{
obj.CreateMeshes(MeshType.Render, new MeshingParameters(), false);
}

var option = new Rhino.FileIO.FileWriteOptions
{
FileVersion = 7,
IncludeRenderMeshes = true
};

rhinoDoc.Write3dmFile(path, option);
rhinoDoc.Dispose();

ArtifactPaths.Add(path);
}

public void SaveImage(string basePath)
{
for (int i = 0; i < Images.Count; i++)
{
Bitmap bitmap = Images[i];
string path = basePath + $"_image_{i}.png";
bitmap?.Save(path, ImageFormat.Png);
ArtifactPaths.Add(path);
}
}
}
}
2 changes: 1 addition & 1 deletion Tunny/Util/Variables.cs → Tunny/PreProcess/Variables.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Tunny.Util
namespace Tunny.PreProcess
{
[Serializable]
public class Variable
Expand Down
Loading
Loading