Fix crashes

This commit is contained in:
Asriel Camora
2024-05-06 00:05:49 -07:00
parent 869a27bf3a
commit b5ed17dc15
5 changed files with 11 additions and 10 deletions
+6 -5
View File
@@ -176,9 +176,8 @@ public sealed class MCTS
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
[SkipLocalsInit]
private (Node ExpandedNode, float Score) ExpandAndRollout(Random random, Simulator simulator, Node initialNode)
private (Node ExpandedNode, float Score) ExpandAndRollout(Random random, Simulator simulator, Node initialNode, Span<ActionType> actionBuffer)
{
ref var initialState = ref initialNode.State;
// expand once
@@ -194,7 +193,7 @@ public sealed class MCTS
var currentActions = expandedNode.State.AvailableActions;
byte actionCount = 0;
Span<ActionType> actions = stackalloc ActionType[Math.Min(config.MaxStepCount - currentState.ActionCount, config.MaxRolloutStepCount)];
var actions = actionBuffer[..Math.Min(config.MaxStepCount - currentState.ActionCount, config.MaxRolloutStepCount)];
while (SimulationNode.GetCompletionState(currentCompletionState, currentActions) == CompletionState.Incomplete &&
actionCount < actions.Length)
{
@@ -260,16 +259,18 @@ public sealed class MCTS
return !NodesIncomplete(rootNode, new());
}
public void Search(int iterations, ref int progress, CancellationToken token)
[SkipLocalsInit]
public unsafe void Search(int iterations, ref int progress, CancellationToken token)
{
var simulator = new Simulator(config.ActionPool, config.MaxStepCount, rootNode.State.State);
var random = rootNode.State.State.Input.Random;
var staleCounter = 0;
var i = 0;
Span<ActionType> actionBuffer = stackalloc ActionType[Math.Min(config.MaxStepCount, config.MaxRolloutStepCount)];
for (; i < iterations || MaxScore == 0; i++)
{
var selectedNode = Select();
var (endNode, score) = ExpandAndRollout(random, simulator, selectedNode);
var (endNode, score) = ExpandAndRollout(random, simulator, selectedNode, actionBuffer);
if (MaxScore == 0)
{
if (endNode == selectedNode)
+1 -1
View File
@@ -29,7 +29,7 @@ internal sealed class Simulator : SimulatorNoRandom
State = state;
pool = pool.Where(x => x.Item1.IsPossible(this));
}
actionPoolObjects = pool.OrderBy(x => x.x).ToArray();
actionPoolObjects = [.. pool.OrderBy(x => x.x)];
this.maxStepCount = maxStepCount;
}
+1 -1
View File
@@ -62,7 +62,7 @@ public readonly record struct SolverConfig
}
public static ActionType[] OptimizeActionPool(IEnumerable<ActionType> actions) =>
actions.Order().ToArray();
[.. actions.Order()];
public static readonly ActionType[] DeterministicActionPool = OptimizeActionPool(new[]
{