Skip to content

Commit

Permalink
Add PythonEngine logging and Serilog console sink
Browse files Browse the repository at this point in the history
  • Loading branch information
NATSUME Hiroaki committed Jan 30, 2024
1 parent 813dc32 commit a28cd8b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
21 changes: 18 additions & 3 deletions Tunny/Solver/Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Tunny.Storage;
using Tunny.Type;
using Tunny.UI;
using Tunny.Util;

namespace Tunny.Solver
{
Expand Down Expand Up @@ -58,10 +59,12 @@ public void Solve()
string[] directions = SetDirectionValues(Objective.Length);
Log.Information($"Optimization started with {nTrials} trials and {timeout} seconds timeout and {samplerType} sampler.");

PythonEngine.Initialize();
IntPtr allowThreadsPtr = PythonEngine.BeginAllowThreads();
InitializePythonEngine();
using (Py.GIL())
{
Log.Debug("Wake Python GIL.");
PythonEngine.Exec("a == 1");

dynamic optuna = Py.Import("optuna");
dynamic sampler = SetSamplerSettings(samplerType, optuna, HasConstraints);
dynamic storage = Settings.Storage.CreateNewOptunaStorage(false);
Expand All @@ -88,8 +91,20 @@ public void Solve()
SetResultValues(Objective.Length, study, parameter);
}
}
PythonEngine.EndAllowThreads(allowThreadsPtr);
PythonEngine.Shutdown();
Log.Debug("Shutdown PythonEngine.");
}

private static void InitializePythonEngine()
{
Log.Debug("Check PythonEngine status.");
if (PythonEngine.IsInitialized)
{
PythonEngine.Shutdown();
Log.Warning("PythonEngine is unintentionally initialized and therefore shut it down.");
}
PythonEngine.Initialize();
Log.Debug("Initialize PythonEngine.");
}

private void PreferentialOptimization(int nBatch, dynamic storage, dynamic artifactBackend, out double[] parameter, out TrialGrasshopperItems result, out dynamic study)
Expand Down
2 changes: 1 addition & 1 deletion Tunny/Solver/Optuna.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static void ShowEndMessages(EndState endState)

private static void ShowErrorMessages(Exception e)
{
Log.Error(e.Message);
Log.Error("Optimization end with exception: {0}", e.Message);
TunnyMessageBox.Show(
"Tunny runtime error:\n" +
"Please send below message (& gh file if possible) to Tunny support.\n" +
Expand Down
1 change: 1 addition & 0 deletions Tunny/Tunny.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

<ItemGroup>
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Grasshopper" Version="7.13.21348.13001" IncludeAssets="compile;build" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="7.0.0" />
Expand Down
7 changes: 6 additions & 1 deletion Tunny/UI/LoadingInstruction.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Windows.Forms;

using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;

using Python.Runtime;

using Serilog;

using Tunny.Resources;
Expand All @@ -32,7 +35,9 @@ public override GH_LoadingInstruction PriorityLoad()
private static void InitializeLogger()
{
Log.Logger = new LoggerConfiguration()
.WriteTo.File(path: TunnyVariables.LogPath + "/log_.txt", rollingInterval: RollingInterval.Day, formatProvider: System.Globalization.CultureInfo.InvariantCulture)
.MinimumLevel.Verbose()
.WriteTo.Console(formatProvider: CultureInfo.InvariantCulture)
.WriteTo.File(path: TunnyVariables.LogPath + "/log_.txt", rollingInterval: RollingInterval.Day, formatProvider: CultureInfo.InvariantCulture)
.CreateLogger();
Log.Information("Tunny is loaded.");
CheckAndDeleteOldLogFiles();
Expand Down

0 comments on commit a28cd8b

Please sign in to comment.