From 9d8e29f069ab5b183dd864713fb86b6e61961e1a Mon Sep 17 00:00:00 2001 From: Asriel Camora Date: Tue, 29 Apr 2025 14:28:50 -0700 Subject: [PATCH] Update raphael (now threaded and loggable) --- Craftimizer/Windows/Settings.cs | 32 ++++++++++++++++---------------- Craftimizer/packages.lock.json | 10 +++++----- Solver/Craftimizer.Solver.csproj | 4 ++-- Solver/Solver.cs | 17 +++++++++++++---- Solver/SolverConfig.cs | 2 +- 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/Craftimizer/Windows/Settings.cs b/Craftimizer/Windows/Settings.cs index 82d1acd..54b62d7 100644 --- a/Craftimizer/Windows/Settings.cs +++ b/Craftimizer/Windows/Settings.cs @@ -534,6 +534,22 @@ public sealed class Settings : Window, IDisposable disableOptimal ? [SolverAlgorithm.Raphael] : [] ); + using (ImRaii.Disabled(config.Algorithm is not (SolverAlgorithm.OneshotForked or SolverAlgorithm.StepwiseForked or SolverAlgorithm.StepwiseGenetic or SolverAlgorithm.Raphael))) + DrawOption( + "Max Core Count", + "The number of cores to use when solving. You should use as many " + + "as you can. If it's too high, it will have an effect on your gameplay " + + $"experience. A good estimate would be 1 or 2 cores less than your " + + $"system (FYI, you have {Environment.ProcessorCount} cores), but make sure to accomodate " + + $"for any other tasks you have in the background, if you have any.\n" + + "(Only used in the Forked, Genetic, and Optimal algorithms)", + config.MaxThreadCount, + 1, + Environment.ProcessorCount, + v => config = config with { MaxThreadCount = v }, + ref isDirty + ); + if (config.Algorithm != SolverAlgorithm.Raphael) { DrawOption( @@ -602,22 +618,6 @@ public sealed class Settings : Window, IDisposable ref isDirty ); - using (ImRaii.Disabled(config.Algorithm is not (SolverAlgorithm.OneshotForked or SolverAlgorithm.StepwiseForked or SolverAlgorithm.StepwiseGenetic))) - DrawOption( - "Max Core Count", - "The number of cores to use when solving. You should use as many " + - "as you can. If it's too high, it will have an effect on your gameplay " + - $"experience. A good estimate would be 1 or 2 cores less than your " + - $"system (FYI, you have {Environment.ProcessorCount} cores), but make sure to accomodate " + - $"for any other tasks you have in the background, if you have any.\n" + - "(Only used in the Forked and Genetic algorithms)", - config.MaxThreadCount, - 1, - Environment.ProcessorCount, - v => config = config with { MaxThreadCount = v }, - ref isDirty - ); - using (ImRaii.Disabled(config.Algorithm is not (SolverAlgorithm.OneshotForked or SolverAlgorithm.StepwiseForked or SolverAlgorithm.StepwiseGenetic))) DrawOption( "Fork Count", diff --git a/Craftimizer/packages.lock.json b/Craftimizer/packages.lock.json index f60fcaa..6b54cdd 100644 --- a/Craftimizer/packages.lock.json +++ b/Craftimizer/packages.lock.json @@ -36,8 +36,8 @@ }, "Raphael.Net": { "type": "Transitive", - "resolved": "2.1.0", - "contentHash": "MDXWh/jSuwTctU3lMs76jC37KY6mERCBIip4+hl4aqvuyxNkX8WOKyrr0fP0EiV3QHFWoQCn7wxWA9U1or78IA==" + "resolved": "2.1.1", + "contentHash": "4+7HyDa7lVokXObqv8ADeTgi74Cz7W99N07DYtzuOXEt6tJtMlJt8RxLGoPtakVf1mURDMuD5kDKw0oz3EaoDA==" }, "System.IO.Hashing": { "type": "Transitive", @@ -52,15 +52,15 @@ "dependencies": { "Craftimizer.Simulator": "[1.0.0, )", "DotNext": "[5.21.0, )", - "Raphael.Net": "[2.1.0, )" + "Raphael.Net": "[2.1.1, )" } } }, "net9.0-windows7.0/win-x64": { "Raphael.Net": { "type": "Transitive", - "resolved": "2.1.0", - "contentHash": "MDXWh/jSuwTctU3lMs76jC37KY6mERCBIip4+hl4aqvuyxNkX8WOKyrr0fP0EiV3QHFWoQCn7wxWA9U1or78IA==" + "resolved": "2.1.1", + "contentHash": "4+7HyDa7lVokXObqv8ADeTgi74Cz7W99N07DYtzuOXEt6tJtMlJt8RxLGoPtakVf1mURDMuD5kDKw0oz3EaoDA==" } } } diff --git a/Solver/Craftimizer.Solver.csproj b/Solver/Craftimizer.Solver.csproj index c2b39b5..f22f653 100644 --- a/Solver/Craftimizer.Solver.csproj +++ b/Solver/Craftimizer.Solver.csproj @@ -1,4 +1,4 @@ - + net9.0 @@ -15,7 +15,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/Solver/Solver.cs b/Solver/Solver.cs index 4f7c0dd..16392ba 100644 --- a/Solver/Solver.cs +++ b/Solver/Solver.cs @@ -160,8 +160,8 @@ public sealed class Solver : IDisposable var pool = RaphaelUtils.ConvertToRawActions(Config.ActionPool.Where(a => a.Base().IsPossible(s)).ToArray()); var input = new Raphael.SolverInput() { - CP = checked((short)State.Input.Stats.CP), - Durability = checked((sbyte)State.Input.Recipe.MaxDurability), + CP = checked((ushort)State.Input.Stats.CP), + Durability = checked((ushort)State.Input.Recipe.MaxDurability), Progress = checked((ushort)State.Input.Recipe.MaxProgress), Quality = checked((ushort)(State.Input.Recipe.MaxQuality - State.Input.StartingQuality)), BaseProgressGain = checked((ushort)State.Input.BaseProgressGain), @@ -202,13 +202,18 @@ public sealed class Solver : IDisposable progress = prog % maxProgress; } + void Log(string s) => + OnLog?.Invoke(s); + if (!Config.MinimizeSteps) { Raphael.SolverConfig config = new() { Adversarial = Config.Adversarial, BackloadProgress = true, - UnsoundBranchPruning = true + UnsoundBranchPruning = true, + LogLevel = Raphael.LevelFilter.Debug, + ThreadCount = Config.MaxThreadCount, }; using var solver = new Raphael.Solver(in config, in input, pool); @@ -216,6 +221,7 @@ public sealed class Solver : IDisposable solver.OnFinish += OnFinish; solver.OnSuggestSolution += OnSuggestSolution; solver.OnProgress += OnProgress; + solver.OnLog += Log; progressStage = 0; progress = 0; @@ -231,7 +237,9 @@ public sealed class Solver : IDisposable { Adversarial = Config.Adversarial, BackloadProgress = Config.BackloadProgress, - UnsoundBranchPruning = false + UnsoundBranchPruning = false, + LogLevel = Raphael.LevelFilter.Debug, + ThreadCount = Config.MaxThreadCount, }; using var solver = new Raphael.Solver(in config, in input, pool); @@ -239,6 +247,7 @@ public sealed class Solver : IDisposable solver.OnFinish += OnFinish; solver.OnSuggestSolution += OnSuggestSolution; solver.OnProgress += OnProgress; + solver.OnLog += Log; progressStage = 0; progress = 0; diff --git a/Solver/SolverConfig.cs b/Solver/SolverConfig.cs index d7abbe7..dad5f46 100644 --- a/Solver/SolverConfig.cs +++ b/Solver/SolverConfig.cs @@ -24,7 +24,6 @@ public readonly record struct SolverConfig public float ExplorationConstant { get; init; } public int MaxStepCount { get; init; } public int MaxRolloutStepCount { get; init; } - public int MaxThreadCount { get; init; } public int ForkCount { get; init; } public int FurcatedActionCount { get; init; } public bool StrictActions { get; init; } @@ -41,6 +40,7 @@ public readonly record struct SolverConfig public bool BackloadProgress { get; init; } public bool MinimizeSteps { get; init; } + public int MaxThreadCount { get; init; } public ActionType[] ActionPool { get; init; } public SolverAlgorithm Algorithm { get; init; }