From 0f2267dabf6fbdf2ea7841f3b2dec0a10d729dc2 Mon Sep 17 00:00:00 2001 From: Asriel Camora Date: Wed, 21 Jun 2023 11:42:22 -0700 Subject: [PATCH] Re enable random actions --- Benchmark/Program.cs | 9 +++++++-- Solver/Crafty/Solver.cs | 6 +++--- Solver/Crafty/SolverConfig.cs | 31 ++++++++++++------------------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/Benchmark/Program.cs b/Benchmark/Program.cs index 1360b36..bb9201a 100644 --- a/Benchmark/Program.cs +++ b/Benchmark/Program.cs @@ -31,12 +31,17 @@ internal static class Program } ); + var config = new SolverConfig() + { + Iterations = 1_000_000 + }; + var s = Stopwatch.StartNew(); if (true) - _ = Solver.Crafty.Solver.SearchStepwise(new(), input, a => Console.WriteLine(a)); + _ = Solver.Crafty.Solver.SearchStepwise(config, input, a => Console.WriteLine(a)); else { - (var actions, _) = Solver.Crafty.Solver.SearchOneshot(new(), input); + (var actions, _) = Solver.Crafty.Solver.SearchOneshot(config, input); foreach (var action in actions) Console.Write($">{action.IntName()}"); Console.WriteLine(); diff --git a/Solver/Crafty/Solver.cs b/Solver/Crafty/Solver.cs index d2611c8..ddae25d 100644 --- a/Solver/Crafty/Solver.cs +++ b/Solver/Crafty/Solver.cs @@ -14,7 +14,7 @@ public class Solver public Simulator Simulator; public Node RootNode; - // public Random Random => Simulator.Input.Random; + public Random Random => Simulator.Input.Random; public Solver(SolverConfig config, SimulationState state, bool strict) { @@ -160,7 +160,7 @@ public class Solver if (initialState.IsComplete) return (initialNode, initialState.CompletionState, initialState.CalculateScore(Config.MaxStepCount) ?? 0); - var randomAction = initialState.AvailableActions.First(); + var randomAction = initialState.AvailableActions.SelectRandom(Random); initialState.AvailableActions.RemoveAction(randomAction); var expandedNode = initialNode.Add(Execute(initialState.State, randomAction, true)); @@ -175,7 +175,7 @@ public class Solver { if (SimulationNode.GetCompletionState(currentCompletionState, currentActions) != CompletionState.Incomplete) break; - randomAction = currentActions.First(); + randomAction = currentActions.SelectRandom(Random); actions[actionCount++] = randomAction; (currentState, currentCompletionState, currentActions) = ExecuteSimple(currentState, randomAction, true); } diff --git a/Solver/Crafty/SolverConfig.cs b/Solver/Crafty/SolverConfig.cs index b4643cf..f4a4aa4 100644 --- a/Solver/Crafty/SolverConfig.cs +++ b/Solver/Crafty/SolverConfig.cs @@ -3,27 +3,20 @@ using System.Runtime.InteropServices; namespace Craftimizer.Solver.Crafty; [StructLayout(LayoutKind.Auto)] -public readonly struct SolverConfig +public readonly record struct SolverConfig { - public readonly int Iterations; - public readonly float ScoreStorageThreshold; - public readonly float MaxScoreWeightingConstant; - public readonly float ExplorationConstant; - public readonly int MaxStepCount; + public int Iterations { get; init; } + public float ScoreStorageThreshold { get; init; } + public float MaxScoreWeightingConstant { get; init; } + public float ExplorationConstant { get; init; } + public int MaxStepCount { get; init; } - public SolverConfig() : this(30000, 1f, 0.1f, 4, 25) { } - - public SolverConfig( - int iterations = 30000, - float scoreStorageThreshold = 1f, - float maxScoreWeightingConstant = 0.1f, - float explorationConstant = 4f, - int maxStepCount = 25) + public SolverConfig() { - Iterations = iterations; - ScoreStorageThreshold = scoreStorageThreshold; - MaxScoreWeightingConstant = maxScoreWeightingConstant; - ExplorationConstant = explorationConstant; - MaxStepCount = maxStepCount; + Iterations = 30000; + ScoreStorageThreshold = 1f; + MaxScoreWeightingConstant = 0.1f; + ExplorationConstant = 4f; + MaxStepCount = 25; } }