From c344815e50910a085f6ac8fc4ebf78c97d9596ed Mon Sep 17 00:00:00 2001 From: Asriel Camora Date: Sun, 23 Jul 2023 02:27:08 +0400 Subject: [PATCH] Moved algorithm into SolverConfig, multi configuration support --- Craftimizer/Configuration.cs | 25 ++++----------------- Craftimizer/Windows/CraftSolver.cs | 2 +- Craftimizer/Windows/SimulatorSolver.cs | 2 +- Solver/Crafty/Solver.cs | 17 +++++++++++++++ Solver/Crafty/SolverConfig.cs | 30 ++++++++++++++++++++++++-- 5 files changed, 51 insertions(+), 25 deletions(-) diff --git a/Craftimizer/Configuration.cs b/Craftimizer/Configuration.cs index 58b34a7..d1f67aa 100644 --- a/Craftimizer/Configuration.cs +++ b/Craftimizer/Configuration.cs @@ -15,30 +15,13 @@ public class Macro public List Actions { get; set; } = new(); } -public enum SolverAlgorithm -{ - Oneshot, - OneshotForked, - Stepwise, - StepwiseForked, - StepwiseFurcated, -} - public static class AlgorithmUtils { - public static void Invoke(this SolverAlgorithm me, SolverConfig config, SimulationState state, Action? actionCallback = null, CancellationToken token = default) + public static void Invoke(this SolverConfig me, SimulationState state, Action? actionCallback = null, CancellationToken token = default) { - Func?, CancellationToken, SolverSolution> func = me switch - { - SolverAlgorithm.Oneshot => Solver.Crafty.Solver.SearchOneshot, - SolverAlgorithm.OneshotForked => Solver.Crafty.Solver.SearchOneshotForked, - SolverAlgorithm.Stepwise => Solver.Crafty.Solver.SearchStepwise, - SolverAlgorithm.StepwiseForked => Solver.Crafty.Solver.SearchStepwiseForked, - SolverAlgorithm.StepwiseFurcated or _ => Solver.Crafty.Solver.SearchStepwiseFurcated, - }; try { - func(config, state, actionCallback, token); + Solver.Crafty.Solver.Search(me, state, actionCallback, token); } catch (AggregateException e) { @@ -59,8 +42,8 @@ public class Configuration : IPluginConfiguration public bool OverrideUncraftability { get; set; } = true; public bool HideUnlearnedActions { get; set; } = true; public List Macros { get; set; } = new(); - public SolverConfig SolverConfig { get; set; } = new(); - public SolverAlgorithm SolverAlgorithm { get; set; } = SolverAlgorithm.StepwiseFurcated; + public SolverConfig SimulatorSolverConfig { get; set; } = SolverConfig.SimulatorDefault; + public SolverConfig SynthHelperSolverConfig { get; set; } = SolverConfig.SynthHelperDefault; public bool ConditionRandomness { get; set; } = true; public bool EnableSynthesisHelper { get; set; } = true; public int SynthesisHelperStepCount { get; set; } = 5; diff --git a/Craftimizer/Windows/CraftSolver.cs b/Craftimizer/Windows/CraftSolver.cs index 7a7cce0..9a518ed 100644 --- a/Craftimizer/Windows/CraftSolver.cs +++ b/Craftimizer/Windows/CraftSolver.cs @@ -48,7 +48,7 @@ public sealed unsafe partial class Craft : Window, IDisposable SolverSim = new(state); SolverTaskToken = new(); - SolverTask = Task.Run(() => Config.SolverAlgorithm.Invoke(Config.SolverConfig, state, SolverActionQueue.Enqueue, SolverTaskToken.Token)); + SolverTask = Task.Run(() => Config.SynthHelperSolverConfig.Invoke(state, SolverActionQueue.Enqueue, SolverTaskToken.Token)); } private void SolveTick() diff --git a/Craftimizer/Windows/SimulatorSolver.cs b/Craftimizer/Windows/SimulatorSolver.cs index f4a97b8..d4df22c 100644 --- a/Craftimizer/Windows/SimulatorSolver.cs +++ b/Craftimizer/Windows/SimulatorSolver.cs @@ -83,7 +83,7 @@ public sealed partial class Simulator : Window, IDisposable SolverInitialActionCount = Actions.Count; SolverTaskToken = new(); - SolverTask = Task.Run(() => Config.SolverAlgorithm.Invoke(Config.SolverConfig, solverState, SolverActionQueue.Enqueue, SolverTaskToken.Token)); + SolverTask = Task.Run(() => Config.SimulatorSolverConfig.Invoke(solverState, SolverActionQueue.Enqueue, SolverTaskToken.Token)); } public void Dispose() diff --git a/Solver/Crafty/Solver.cs b/Solver/Crafty/Solver.cs index 25041fc..52f7628 100644 --- a/Solver/Crafty/Solver.cs +++ b/Solver/Crafty/Solver.cs @@ -570,4 +570,21 @@ public sealed class Solver return solution; } + + public static SolverSolution Search(SolverConfig config, SimulationInput input, Action? actionCallback = null, CancellationToken token = default) => + Search(config, new SimulationState(input), actionCallback, token); + + public static SolverSolution Search(SolverConfig config, SimulationState state, Action? actionCallback = null, CancellationToken token = default) + { + Func?, CancellationToken, SolverSolution> func = config.Algorithm switch + { + SolverAlgorithm.Oneshot => SearchOneshot, + SolverAlgorithm.OneshotForked => SearchOneshotForked, + SolverAlgorithm.Stepwise => SearchStepwise, + SolverAlgorithm.StepwiseForked => SearchStepwiseForked, + SolverAlgorithm.StepwiseFurcated or _ => SearchStepwiseFurcated, + }; + return func(config, state, actionCallback, token); + } + } diff --git a/Solver/Crafty/SolverConfig.cs b/Solver/Crafty/SolverConfig.cs index 552a302..259946b 100644 --- a/Solver/Crafty/SolverConfig.cs +++ b/Solver/Crafty/SolverConfig.cs @@ -2,6 +2,15 @@ using System.Runtime.InteropServices; namespace Craftimizer.Solver.Crafty; +public enum SolverAlgorithm +{ + Oneshot, + OneshotForked, + Stepwise, + StepwiseForked, + StepwiseFurcated, +} + [StructLayout(LayoutKind.Auto)] public readonly record struct SolverConfig { @@ -21,14 +30,16 @@ public readonly record struct SolverConfig public float ScoreCPBonus { get; init; } public float ScoreFewerStepsBonus { get; init; } + public SolverAlgorithm Algorithm { get; init; } + public SolverConfig() { Iterations = 100000; ScoreStorageThreshold = 1f; MaxScoreWeightingConstant = 0.1f; ExplorationConstant = 4; - MaxStepCount = 25; - MaxRolloutStepCount = MaxStepCount; + MaxStepCount = 30; + MaxRolloutStepCount = 99; ForkCount = Math.Max(Environment.ProcessorCount, 32); FurcatedActionCount = ForkCount / 2; StrictActions = true; @@ -38,5 +49,20 @@ public readonly record struct SolverConfig ScoreDurabilityBonus = .05f; ScoreCPBonus = .05f; ScoreFewerStepsBonus = .05f; + + Algorithm = SolverAlgorithm.StepwiseFurcated; } + + public static readonly SolverConfig SimulatorDefault = new SolverConfig() with + { + + }; + + public static readonly SolverConfig SynthHelperDefault = new SolverConfig() with + { + Iterations = 300000, + ForkCount = Environment.ProcessorCount - 1, // Keep one for the game thread + FurcatedActionCount = Environment.ProcessorCount / 2, + Algorithm = SolverAlgorithm.StepwiseForked + }; }