diff --git a/Simulator/CompletionState.cs b/Simulator/CompletionState.cs index 84f2881..88856d0 100644 --- a/Simulator/CompletionState.cs +++ b/Simulator/CompletionState.cs @@ -1,6 +1,6 @@ namespace Craftimizer.Simulator; -public enum CompletionState +public enum CompletionState : byte { Incomplete, ProgressComplete, diff --git a/Simulator/SimulationInput.cs b/Simulator/SimulationInput.cs index 75962ce..692768c 100644 --- a/Simulator/SimulationInput.cs +++ b/Simulator/SimulationInput.cs @@ -1,6 +1,6 @@ namespace Craftimizer.Simulator; -public readonly record struct SimulationInput +public record SimulationInput { public CharacterStats Stats { get; } public RecipeInfo Recipe { get; } diff --git a/Solver/Crafty/Arena.cs b/Solver/Crafty/Arena.cs index 8661b78..c80695f 100644 --- a/Solver/Crafty/Arena.cs +++ b/Solver/Crafty/Arena.cs @@ -6,24 +6,23 @@ public class Arena where T : struct { public readonly record struct Node { - public int? Parent { get; init; } - public int Index { get; init; } - public List Children { get; init; } public T State { get; init; } + public List Children { get; init; } + public int Parent { get; init; } } private readonly List nodes = new(); public Arena(T initialState = default) { - nodes.Add(new() { Parent = null, Index = 0, Children = new(), State = initialState }); + nodes.Add(new() { Parent = -1, Children = new(), State = initialState }); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int Insert(int parentIndex, T state) { var index = nodes.Count; - nodes.Add(new() { Parent = parentIndex, Index = index, Children = new(), State = state }); + nodes.Add(new() { Parent = parentIndex, Children = new(), State = state }); nodes[parentIndex].Children.Add(index); return index; } diff --git a/Solver/Crafty/CompletionState.cs b/Solver/Crafty/CompletionState.cs index 09284ae..b7aabab 100644 --- a/Solver/Crafty/CompletionState.cs +++ b/Solver/Crafty/CompletionState.cs @@ -2,7 +2,7 @@ using CompState = Craftimizer.Simulator.CompletionState; namespace Craftimizer.Solver.Crafty; -public enum CompletionState +public enum CompletionState : byte { Incomplete, ProgressComplete, diff --git a/Solver/Crafty/Solver.cs b/Solver/Crafty/Solver.cs index e9551fc..01b5a77 100644 --- a/Solver/Crafty/Solver.cs +++ b/Solver/Crafty/Solver.cs @@ -70,20 +70,6 @@ public class Solver return (currentIndex, currentNode.CompletionState); } - public static float Eval(NodeScores node, NodeScores parent) - { - var w = MaxScoreWeightingConstant; - var c = ExplorationConstant; - - var visits = node.Visits; - var average_score = node.ScoreSum / visits; - - var exploitation = ((1f - w) * average_score) + (w * node.MaxScore); - var exploration = MathF.Sqrt(c * MathF.Log(parent.Visits) / visits); - - return exploitation + exploration; - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] private static int RustMaxBy(List source, Func into) { @@ -112,52 +98,6 @@ public class Solver private static int AlignToVectorLength(int length) => (length + (Vector.Count - 1)) & ~(Vector.Count - 1); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - // Requires a multiple of Vector.Count - private static void EvalBestChildMultiple(float parentVisits, ReadOnlySpan scoreSums, ReadOnlySpan visits, ReadOnlySpan maxScores, Span evalScores) - { - var C = ExplorationConstant * MathF.Log(parentVisits); - var w = MaxScoreWeightingConstant; - var W = 1f - w; - var CVector = new Vector(C); - - var length = scoreSums.Length; - for (var i = 0; i < length; i += Vector.Count) - { - var scoreSumsVector = new Vector(scoreSums[i..(i + Vector.Count)]); - var visitsVector = new Vector(visits[i..(i + Vector.Count)]); - var maxScoresVector = new Vector(maxScores[i..(i + Vector.Count)]); - var evalVector = EvalBestChildVectorized(w, W, CVector, scoreSumsVector, visitsVector, maxScoresVector); - evalVector.CopyTo(evalScores[i..(i + Vector.Count)]); - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private int EvalBestChildAlternative(float parentVisits, List children) - { - var length = children.Count; - var alignedLength = AlignToVectorLength(length); - Span scoreSums = stackalloc float[alignedLength]; - Span visits = stackalloc float[alignedLength]; - Span maxScores = stackalloc float[alignedLength]; - Span evalScores = stackalloc float[alignedLength]; - - for (var i = 0; i < length; ++i) - { - var node = Tree.Get(children[i]).State.Scores; - scoreSums[i] = node.ScoreSum; - visits[i] = node.Visits; - maxScores[i] = node.MaxScore; - } - - EvalBestChildMultiple(parentVisits, scoreSums, visits, maxScores, evalScores); - var max = 0; - for (var i = 1; i < length; ++i) - if (evalScores[i] >= evalScores[max]) - max = i; - return children[max]; - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] private int EvalBestChild(float parentVisits, List children) {