Solver optimizations & allow action pools
This commit is contained in:
+7
-29
@@ -3,7 +3,6 @@ using Craftimizer.Simulator;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using Node = Craftimizer.Solver.ArenaNode<Craftimizer.Solver.SimulationNode>;
|
||||
|
||||
namespace Craftimizer.Solver;
|
||||
@@ -23,7 +22,7 @@ public sealed class MCTS
|
||||
public MCTS(in MCTSConfig config, in SimulationState state)
|
||||
{
|
||||
this.config = config;
|
||||
var sim = new Simulator(config.ActionPool, config.MaxStepCount) { State = state };
|
||||
var sim = new Simulator(config.ActionPool, config.MaxStepCount, state);
|
||||
rootNode = new(new(
|
||||
state,
|
||||
null,
|
||||
@@ -35,7 +34,7 @@ public sealed class MCTS
|
||||
|
||||
private static SimulationNode Execute(Simulator simulator, in SimulationState state, ActionType action, bool strict)
|
||||
{
|
||||
(_, var newState) = simulator.Execute(state, action);
|
||||
var newState = simulator.ExecuteUnchecked(state, action);
|
||||
return new(
|
||||
newState,
|
||||
action,
|
||||
@@ -192,7 +191,6 @@ public sealed class MCTS
|
||||
var currentCompletionState = expandedNode.State.SimulationCompletionState;
|
||||
var currentActions = expandedNode.State.AvailableActions;
|
||||
|
||||
|
||||
byte actionCount = 0;
|
||||
Span<ActionType> actions = stackalloc ActionType[Math.Min(config.MaxStepCount - currentState.ActionCount, config.MaxRolloutStepCount)];
|
||||
while (SimulationNode.GetCompletionState(currentCompletionState, currentActions) == CompletionState.Incomplete &&
|
||||
@@ -200,7 +198,7 @@ public sealed class MCTS
|
||||
{
|
||||
var nextAction = currentActions.SelectRandom(random);
|
||||
actions[actionCount++] = nextAction;
|
||||
(_, currentState) = simulator.Execute(currentState, nextAction);
|
||||
currentState = simulator.ExecuteUnchecked(currentState, nextAction);
|
||||
currentCompletionState = simulator.CompletionState;
|
||||
if (currentCompletionState != CompletionState.Incomplete)
|
||||
break;
|
||||
@@ -235,26 +233,6 @@ public sealed class MCTS
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowAllNodes()
|
||||
{
|
||||
static void ShowNodes(StringBuilder b, Node node, Stack<Node> path)
|
||||
{
|
||||
path.Push(node);
|
||||
b.AppendLine($"{new string(' ', path.Count)}{node.State.Action}");
|
||||
{
|
||||
for (var i = 0; i < node.Children.Count; ++i)
|
||||
{
|
||||
var n = node.ChildAt((i >> 3, i & 7))!;
|
||||
ShowNodes(b, n, path);
|
||||
}
|
||||
path.Pop();
|
||||
}
|
||||
}
|
||||
var b = new StringBuilder();
|
||||
ShowNodes(b, rootNode, new());
|
||||
Console.WriteLine(b.ToString());
|
||||
}
|
||||
|
||||
private bool AllNodesComplete()
|
||||
{
|
||||
static bool NodesIncomplete(Node node, Stack<Node> path)
|
||||
@@ -280,17 +258,14 @@ public sealed class MCTS
|
||||
return !NodesIncomplete(rootNode, new());
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Search(int iterations, ref int progress, CancellationToken token)
|
||||
{
|
||||
Simulator simulator = new(config.ActionPool, config.MaxStepCount);
|
||||
var simulator = new Simulator(config.ActionPool, config.MaxStepCount, rootNode.State.State);
|
||||
var random = rootNode.State.State.Input.Random;
|
||||
var staleCounter = 0;
|
||||
var i = 0;
|
||||
for (; i < iterations || MaxScore == 0; i++)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
var selectedNode = Select();
|
||||
var (endNode, score) = ExpandAndRollout(random, simulator, selectedNode);
|
||||
if (MaxScore == 0)
|
||||
@@ -315,7 +290,10 @@ public sealed class MCTS
|
||||
Backpropagate(endNode, score);
|
||||
|
||||
if ((i & (ProgressUpdateFrequency - 1)) == ProgressUpdateFrequency - 1)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
Interlocked.Add(ref progress, ProgressUpdateFrequency);
|
||||
}
|
||||
}
|
||||
Interlocked.Add(ref progress, i & (ProgressUpdateFrequency - 1));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user