Merge solver code with static interface
- Breaks backwards compat solver code with last version. Concurrent broke backwards compat because of race conditions with rng, but single is now broken too, despite it being 2x faster (!!!!) - Literally twice as fast as Rust now in single thread - Concurrent doesn't work yet, deadlocks somewhere..?
This commit is contained in:
@@ -55,10 +55,10 @@ internal static class Program
|
|||||||
Debugger.Break();
|
Debugger.Break();
|
||||||
var s = Stopwatch.StartNew();
|
var s = Stopwatch.StartNew();
|
||||||
if (true)
|
if (true)
|
||||||
_ = Solver.Crafty.Solver.SearchStepwise(config, input, a => Console.WriteLine(a));
|
_ = SolverUtils.SearchStepwise<SolverSingle>(config, input, a => Console.WriteLine(a));
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
(var actions, _) = Solver.Crafty.Solver.SearchOneshot(config, input);
|
(var actions, _) = SolverUtils.SearchOneshot<SolverConcurrent>(config, input);
|
||||||
foreach (var action in actions)
|
foreach (var action in actions)
|
||||||
Console.Write($">{action.IntName()}");
|
Console.Write($">{action.IntName()}");
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Craftimizer.Simulator;
|
using Craftimizer.Simulator;
|
||||||
using Craftimizer.Simulator.Actions;
|
using Craftimizer.Simulator.Actions;
|
||||||
|
using Craftimizer.Solver.Crafty;
|
||||||
using Dalamud.Interface.Windowing;
|
using Dalamud.Interface.Windowing;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
@@ -71,7 +72,7 @@ public sealed partial class SimulatorWindow : Window, IDisposable
|
|||||||
|
|
||||||
SolverInitialActionCount = Actions.Count;
|
SolverInitialActionCount = Actions.Count;
|
||||||
SolverTaskToken = new();
|
SolverTaskToken = new();
|
||||||
SolverTask = Task.Run(() => Solver.Crafty.Solver.SearchStepwise(Service.Configuration.SolverConfig, solverState, SolverActionQueue.Enqueue, SolverTaskToken.Token));
|
SolverTask = Task.Run(() => SolverUtils.SearchStepwise<SolverConcurrent>(Service.Configuration.SolverConfig, solverState, SolverActionQueue.Enqueue, SolverTaskToken.Token));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|||||||
@@ -17,11 +17,11 @@ public struct ActionSet
|
|||||||
private static ActionType ToAction(int index) => Simulator.AcceptedActions[index];
|
private static ActionType ToAction(int index) => Simulator.AcceptedActions[index];
|
||||||
[Pure]
|
[Pure]
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static uint ToMask(ActionType action) => 1u << (FromAction(action) + 1);
|
private static uint ToMask(ActionType action) => 1u << FromAction(action) + 1;
|
||||||
|
|
||||||
// Return true if action was newly added and not there before.
|
// Return true if action was newly added and not there before.
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public bool AddAction(ActionType action)
|
public bool AddActionConcurrent(ActionType action)
|
||||||
{
|
{
|
||||||
var mask = ToMask(action);
|
var mask = ToMask(action);
|
||||||
var old = Interlocked.Or(ref bits, mask);
|
var old = Interlocked.Or(ref bits, mask);
|
||||||
@@ -30,13 +30,33 @@ public struct ActionSet
|
|||||||
|
|
||||||
// Return true if action was newly removed and not already gone.
|
// Return true if action was newly removed and not already gone.
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public bool RemoveAction(ActionType action)
|
public bool RemoveActionConcurrent(ActionType action)
|
||||||
{
|
{
|
||||||
var mask = ToMask(action);
|
var mask = ToMask(action);
|
||||||
var old = Interlocked.And(ref bits, ~mask);
|
var old = Interlocked.And(ref bits, ~mask);
|
||||||
return (old & mask) != 0;
|
return (old & mask) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return true if action was newly added and not there before.
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool AddAction(ActionType action)
|
||||||
|
{
|
||||||
|
var mask = ToMask(action);
|
||||||
|
var old = bits;
|
||||||
|
bits |= mask;
|
||||||
|
return (old & mask) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return true if action was newly removed and not already gone.
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool RemoveAction(ActionType action)
|
||||||
|
{
|
||||||
|
var mask = ToMask(action);
|
||||||
|
var old = bits;
|
||||||
|
bits &= ~mask;
|
||||||
|
return (old & mask) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
[Pure]
|
[Pure]
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public readonly bool HasAction(ActionType action) => (bits & ToMask(action)) != 0;
|
public readonly bool HasAction(ActionType action) => (bits & ToMask(action)) != 0;
|
||||||
@@ -51,10 +71,10 @@ public struct ActionSet
|
|||||||
public readonly bool IsEmpty => bits == 0;
|
public readonly bool IsEmpty => bits == 0;
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public readonly ActionType SelectRandom(Random random) => ElementAt(random.Next(Count));
|
public readonly ActionType SelectRandom(Random random) => ElementAt(0);// random.Next(Count));
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public ActionType? PopRandom(Random random)
|
public ActionType? PopRandomConcurrent(Random random)
|
||||||
{
|
{
|
||||||
uint snapshot;
|
uint snapshot;
|
||||||
uint newValue;
|
uint newValue;
|
||||||
@@ -76,7 +96,7 @@ public struct ActionSet
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public ActionType? PopFirst()
|
public ActionType? PopFirstConcurrent()
|
||||||
{
|
{
|
||||||
uint snapshot;
|
uint snapshot;
|
||||||
uint newValue;
|
uint newValue;
|
||||||
@@ -94,6 +114,23 @@ public struct ActionSet
|
|||||||
return action;
|
return action;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public ActionType PopRandom(Random random)
|
||||||
|
{
|
||||||
|
return PopFirst();
|
||||||
|
var action = ElementAt(random.Next(Count));
|
||||||
|
RemoveAction(action);
|
||||||
|
return action;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public ActionType PopFirst()
|
||||||
|
{
|
||||||
|
var action = First();
|
||||||
|
RemoveAction(action);
|
||||||
|
return action;
|
||||||
|
}
|
||||||
|
|
||||||
[Pure]
|
[Pure]
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public readonly ActionType First() => ElementAt(0);
|
public readonly ActionType First() => ElementAt(0);
|
||||||
|
|||||||
@@ -20,17 +20,17 @@ public sealed class ArenaNode<T> where T : struct
|
|||||||
private static int BatchCount = MaxSize / BatchSize;
|
private static int BatchCount = MaxSize / BatchSize;
|
||||||
|
|
||||||
public ArenaNode<T>[][] Data;
|
public ArenaNode<T>[][] Data;
|
||||||
private int index;
|
private int index; // Unused in single threaded workload
|
||||||
private int count;
|
private int count;
|
||||||
|
|
||||||
public readonly int Count => count;
|
public readonly int Count => count;
|
||||||
|
|
||||||
public void Add(ArenaNode<T> node)
|
public void AddConcurrent(ArenaNode<T> node)
|
||||||
{
|
{
|
||||||
if (Data == null)
|
if (Data == null)
|
||||||
Interlocked.CompareExchange(ref Data, new ArenaNode<T>[BatchCount][], null);
|
Interlocked.CompareExchange(ref Data, new ArenaNode<T>[BatchCount][], null);
|
||||||
|
|
||||||
var idx = Interlocked.Increment(ref this.index) - 1;
|
var idx = Interlocked.Increment(ref index) - 1;
|
||||||
|
|
||||||
var (arrayIdx, subIdx) = GetArrayIndex(idx);
|
var (arrayIdx, subIdx) = GetArrayIndex(idx);
|
||||||
|
|
||||||
@@ -42,6 +42,19 @@ public sealed class ArenaNode<T> where T : struct
|
|||||||
Interlocked.Increment(ref count);
|
Interlocked.Increment(ref count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Add(ArenaNode<T> node)
|
||||||
|
{
|
||||||
|
Data ??= new ArenaNode<T>[BatchCount][];
|
||||||
|
|
||||||
|
var idx = count++;
|
||||||
|
|
||||||
|
var (arrayIdx, subIdx) = GetArrayIndex(idx);
|
||||||
|
|
||||||
|
Data[arrayIdx] ??= new ArenaNode<T>[BatchSize];
|
||||||
|
|
||||||
|
Data[arrayIdx][subIdx] = node;
|
||||||
|
}
|
||||||
|
|
||||||
[Pure]
|
[Pure]
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static (int arrayIdx, int subIdx) GetArrayIndex(int idx) =>
|
private static (int arrayIdx, int subIdx) GetArrayIndex(int idx) =>
|
||||||
@@ -59,6 +72,14 @@ public sealed class ArenaNode<T> where T : struct
|
|||||||
Parent = parent;
|
Parent = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public ArenaNode<T> AddConcurrent(T state)
|
||||||
|
{
|
||||||
|
var node = new ArenaNode<T>(state, this);
|
||||||
|
Children.AddConcurrent(node);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public ArenaNode<T> Add(T state)
|
public ArenaNode<T> Add(T state)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using Node = Craftimizer.Solver.Crafty.ArenaNode<Craftimizer.Solver.Crafty.SimulationNode>;
|
||||||
|
|
||||||
|
namespace Craftimizer.Solver.Crafty;
|
||||||
|
|
||||||
|
public interface ISolver
|
||||||
|
{
|
||||||
|
abstract static void LoadChildData(Span<float> scoreSums, Span<int> visits, Span<float> maxScores, ref Node[] chunk, int iterCount);
|
||||||
|
|
||||||
|
abstract static bool SearchIter(ref SolverConfig config, Node rootNode, Random random, Simulator simulator);
|
||||||
|
|
||||||
|
abstract static void Search(ref SolverConfig config, Node rootNode, CancellationToken token);
|
||||||
|
}
|
||||||
@@ -9,10 +9,17 @@ public struct NodeScores
|
|||||||
public float MaxScore;
|
public float MaxScore;
|
||||||
public int Visits;
|
public int Visits;
|
||||||
|
|
||||||
public void Visit(float score)
|
public void VisitConcurrent(float score)
|
||||||
{
|
{
|
||||||
Intrinsics.CASAdd(ref ScoreSum, score);
|
Intrinsics.CASAdd(ref ScoreSum, score);
|
||||||
Intrinsics.CASMax(ref MaxScore, score);
|
Intrinsics.CASMax(ref MaxScore, score);
|
||||||
Interlocked.Increment(ref Visits);
|
Interlocked.Increment(ref Visits);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Visit(float score)
|
||||||
|
{
|
||||||
|
ScoreSum += score;
|
||||||
|
MaxScore = Math.Max(MaxScore, score);
|
||||||
|
Visits++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
using System.Diagnostics.Contracts;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using Node = Craftimizer.Solver.Crafty.ArenaNode<Craftimizer.Solver.Crafty.SimulationNode>;
|
||||||
|
|
||||||
|
namespace Craftimizer.Solver.Crafty;
|
||||||
|
|
||||||
|
// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/simulator.rs
|
||||||
|
public sealed class SolverConcurrent : ISolver
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||||
|
public static void LoadChildData(Span<float> scoreSums, Span<int> visits, Span<float> maxScores, ref Node[] chunk, int iterCount)
|
||||||
|
{
|
||||||
|
for (var j = 0; j < iterCount; ++j)
|
||||||
|
{
|
||||||
|
var node = chunk[j]?.State.Scores ?? new();
|
||||||
|
scoreSums[j] = node.ScoreSum;
|
||||||
|
visits[j] = node.Visits;
|
||||||
|
maxScores[j] = node.MaxScore;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Pure]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Node? EvalBestChild(ref SolverConfig config, int parentVisits, ref Node.ChildBuffer children) =>
|
||||||
|
parentVisits == 0 ?
|
||||||
|
null :
|
||||||
|
SolverUtils.EvalBestChild<SolverConcurrent>(ref config, parentVisits, ref children);
|
||||||
|
|
||||||
|
[Pure]
|
||||||
|
public static Node Select(ref SolverConfig config, Node rootNode)
|
||||||
|
{
|
||||||
|
var node = rootNode;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
var expandable = !node.State.AvailableActions.IsEmpty;
|
||||||
|
var likelyTerminal = node.Children.Count == 0;
|
||||||
|
if (expandable || likelyTerminal)
|
||||||
|
return node;
|
||||||
|
|
||||||
|
// select the node with the highest score
|
||||||
|
// if null (current node is invalid & not backpropagated just yet), try again from root
|
||||||
|
node = EvalBestChild(ref config, node.State.Scores.Visits, ref node.Children) ?? rootNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static (Node ExpandedNode, float Score)? ExpandAndRollout(ref SolverConfig config, Node rootNode, Random random, Simulator simulator, Node initialNode)
|
||||||
|
{
|
||||||
|
ref var initialState = ref initialNode.State;
|
||||||
|
// expand once
|
||||||
|
if (initialState.IsComplete)
|
||||||
|
return (initialNode, initialState.CalculateScore(config.MaxStepCount) ?? 0);
|
||||||
|
|
||||||
|
var poppedAction = initialState.AvailableActions.PopRandomConcurrent(random);
|
||||||
|
if (!poppedAction.HasValue)
|
||||||
|
return null;
|
||||||
|
var expandedNode = initialNode.Add(SolverUtils.Execute(simulator, initialState.State, poppedAction.Value, true));
|
||||||
|
|
||||||
|
return SolverUtils.Rollout(ref config, rootNode, expandedNode, random, simulator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Backpropagate(Node rootNode, Node startNode, float score)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
startNode.State.Scores.VisitConcurrent(score);
|
||||||
|
|
||||||
|
if (startNode == rootNode)
|
||||||
|
break;
|
||||||
|
|
||||||
|
startNode = startNode.Parent!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool SearchIter(ref SolverConfig config, Node rootNode, Random random, Simulator simulator)
|
||||||
|
{
|
||||||
|
var selectedNode = Select(ref config, rootNode);
|
||||||
|
var rolledOut = ExpandAndRollout(ref config, rootNode, random, simulator, selectedNode);
|
||||||
|
if (!rolledOut.HasValue)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var (endNode, score) = rolledOut.Value;
|
||||||
|
Backpropagate(rootNode, endNode, score);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SearchThread(SolverConfig config, Node rootNode, CancellationToken token) =>
|
||||||
|
SolverUtils.Search<SolverConcurrent>(ref config, rootNode, token);
|
||||||
|
|
||||||
|
public static void Search(ref SolverConfig config, Node rootNode, CancellationToken token)
|
||||||
|
{
|
||||||
|
var configP = config;
|
||||||
|
var tasks = new Task[config.ThreadCount];
|
||||||
|
for (var i = 0; i < config.ThreadCount; ++i)
|
||||||
|
tasks[i] = Task.Run(() => SearchThread(configP, rootNode, token), token);
|
||||||
|
Task.WaitAll(tasks, CancellationToken.None);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
using System.Diagnostics.Contracts;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using Node = Craftimizer.Solver.Crafty.ArenaNode<Craftimizer.Solver.Crafty.SimulationNode>;
|
||||||
|
|
||||||
|
namespace Craftimizer.Solver.Crafty;
|
||||||
|
|
||||||
|
// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/simulator.rs
|
||||||
|
public sealed class SolverSingle : ISolver
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||||
|
public static void LoadChildData(Span<float> scoreSums, Span<int> visits, Span<float> maxScores, ref Node[] chunk, int iterCount)
|
||||||
|
{
|
||||||
|
for (var j = 0; j < iterCount; ++j)
|
||||||
|
{
|
||||||
|
ref var node = ref chunk[j].State.Scores;
|
||||||
|
scoreSums[j] = node.ScoreSum;
|
||||||
|
visits[j] = node.Visits;
|
||||||
|
maxScores[j] = node.MaxScore;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Pure]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Node EvalBestChild(ref SolverConfig config, int parentVisits, ref Node.ChildBuffer children) =>
|
||||||
|
SolverUtils.EvalBestChild<SolverSingle>(ref config, parentVisits, ref children);
|
||||||
|
|
||||||
|
[Pure]
|
||||||
|
public static Node Select(ref SolverConfig config, Node node)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
var expandable = !node.State.AvailableActions.IsEmpty;
|
||||||
|
var likelyTerminal = node.Children.Count == 0;
|
||||||
|
if (expandable || likelyTerminal)
|
||||||
|
return node;
|
||||||
|
|
||||||
|
// select the node with the highest score
|
||||||
|
node = EvalBestChild(ref config, node.State.Scores.Visits, ref node.Children);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static (Node ExpandedNode, float Score) ExpandAndRollout(ref SolverConfig config, Node rootNode, Random random, Simulator simulator, Node initialNode)
|
||||||
|
{
|
||||||
|
ref var initialState = ref initialNode.State;
|
||||||
|
// expand once
|
||||||
|
if (initialState.IsComplete)
|
||||||
|
return (initialNode, initialState.CalculateScore(config.MaxStepCount) ?? 0);
|
||||||
|
|
||||||
|
var poppedAction = initialState.AvailableActions.PopRandom(random);
|
||||||
|
var expandedNode = initialNode.Add(SolverUtils.Execute(simulator, initialState.State, poppedAction, true));
|
||||||
|
|
||||||
|
return SolverUtils.Rollout(ref config, rootNode, expandedNode, random, simulator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Backpropagate(Node rootNode, Node startNode, float score)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
startNode.State.Scores.Visit(score);
|
||||||
|
|
||||||
|
if (startNode == rootNode)
|
||||||
|
break;
|
||||||
|
|
||||||
|
startNode = startNode.Parent!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool SearchIter(ref SolverConfig config, Node rootNode, Random random, Simulator simulator)
|
||||||
|
{
|
||||||
|
var selectedNode = Select(ref config, rootNode);
|
||||||
|
var (endNode, score) = ExpandAndRollout(ref config, rootNode, random, simulator, selectedNode);
|
||||||
|
|
||||||
|
Backpropagate(rootNode, endNode, score);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Search(ref SolverConfig config, Node rootNode, CancellationToken token) =>
|
||||||
|
SolverUtils.Search<SolverSingle>(ref config, rootNode, token);
|
||||||
|
}
|
||||||
@@ -1,38 +1,14 @@
|
|||||||
using Craftimizer.Simulator;
|
|
||||||
using Craftimizer.Simulator.Actions;
|
using Craftimizer.Simulator.Actions;
|
||||||
|
using Craftimizer.Simulator;
|
||||||
|
using Node = Craftimizer.Solver.Crafty.ArenaNode<Craftimizer.Solver.Crafty.SimulationNode>;
|
||||||
using System.Diagnostics.Contracts;
|
using System.Diagnostics.Contracts;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using Node = Craftimizer.Solver.Crafty.ArenaNode<Craftimizer.Solver.Crafty.SimulationNode>;
|
|
||||||
|
|
||||||
namespace Craftimizer.Solver.Crafty;
|
namespace Craftimizer.Solver.Crafty;
|
||||||
|
public static class SolverUtils
|
||||||
// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/simulator.rs
|
|
||||||
public sealed class Solver
|
|
||||||
{
|
{
|
||||||
public SolverConfig Config;
|
public static SimulationNode Execute(Simulator simulator, SimulationState state, ActionType action, bool strict)
|
||||||
public Node RootNode;
|
|
||||||
|
|
||||||
public Random Random;
|
|
||||||
|
|
||||||
public Solver(SolverConfig config, SimulationState state, bool strict)
|
|
||||||
{
|
|
||||||
Config = config;
|
|
||||||
var sim = new Simulator(state, config.MaxStepCount);
|
|
||||||
RootNode = new(new(
|
|
||||||
state,
|
|
||||||
null,
|
|
||||||
sim.CompletionState,
|
|
||||||
sim.AvailableActionsHeuristic(strict)
|
|
||||||
));
|
|
||||||
Random = state.Input.Random;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Solver(SolverConfig config, SimulationInput input, bool strict) : this(config, new SimulationState(input), strict)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private static SimulationNode Execute(Simulator simulator, SimulationState state, ActionType action, bool strict)
|
|
||||||
{
|
{
|
||||||
(_, var newState) = simulator.Execute(state, action);
|
(_, var newState) = simulator.Execute(state, action);
|
||||||
return new(
|
return new(
|
||||||
@@ -53,7 +29,7 @@ public sealed class Solver
|
|||||||
|
|
||||||
if (!state.AvailableActions.HasAction(action))
|
if (!state.AvailableActions.HasAction(action))
|
||||||
return (startNode, CompletionState.InvalidAction);
|
return (startNode, CompletionState.InvalidAction);
|
||||||
state.AvailableActions.RemoveAction(action);
|
state.AvailableActions.RemoveActionConcurrent(action);
|
||||||
|
|
||||||
startNode = startNode.Add(Execute(simulator, state.State, action, strict));
|
startNode = startNode.Add(Execute(simulator, state.State, action, strict));
|
||||||
}
|
}
|
||||||
@@ -63,7 +39,7 @@ public sealed class Solver
|
|||||||
|
|
||||||
[Pure]
|
[Pure]
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static Node ChildMaxScore(ref Node.ChildBuffer children)
|
public static Node ChildMaxScore(ref Node.ChildBuffer children)
|
||||||
{
|
{
|
||||||
var length = children.Count;
|
var length = children.Count;
|
||||||
var vecLength = Vector<float>.Count;
|
var vecLength = Vector<float>.Count;
|
||||||
@@ -95,17 +71,29 @@ public sealed class Solver
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Pure]
|
[Pure]
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
public static (List<ActionType> Actions, SimulationNode Node) Solution(Node node)
|
||||||
private Node? EvalBestChild(int parentVisits, ref Node.ChildBuffer children)
|
|
||||||
{
|
{
|
||||||
if (parentVisits == 0)
|
var actions = new List<ActionType>();
|
||||||
return null;
|
while (node.Children.Count != 0)
|
||||||
|
{
|
||||||
|
node = ChildMaxScore(ref node.Children);
|
||||||
|
|
||||||
|
if (node.State.Action != null)
|
||||||
|
actions.Add(node.State.Action.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (actions, node.State);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Pure]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||||
|
public static Node EvalBestChild<S>(ref SolverConfig config, int parentVisits, ref Node.ChildBuffer children) where S : ISolver
|
||||||
|
{
|
||||||
var length = children.Count;
|
var length = children.Count;
|
||||||
var vecLength = Vector<float>.Count;
|
var vecLength = Vector<float>.Count;
|
||||||
|
|
||||||
var C = MathF.Sqrt(Config.ExplorationConstant * MathF.Log(parentVisits));
|
var C = MathF.Sqrt(config.ExplorationConstant * MathF.Log(parentVisits));
|
||||||
var w = Config.MaxScoreWeightingConstant;
|
var w = config.MaxScoreWeightingConstant;
|
||||||
var W = 1f - w;
|
var W = 1f - w;
|
||||||
var CVector = new Vector<float>(C);
|
var CVector = new Vector<float>(C);
|
||||||
|
|
||||||
@@ -119,14 +107,7 @@ public sealed class Solver
|
|||||||
{
|
{
|
||||||
var iterCount = Math.Min(vecLength, length);
|
var iterCount = Math.Min(vecLength, length);
|
||||||
|
|
||||||
ref var chunk = ref children.Data[i];
|
S.LoadChildData(scoreSums, visits, maxScores, ref children.Data[i], iterCount);
|
||||||
for (var j = 0; j < iterCount; ++j)
|
|
||||||
{
|
|
||||||
var node = chunk[j]?.State.Scores ?? new();
|
|
||||||
scoreSums[j] = node.ScoreSum;
|
|
||||||
visits[j] = node.Visits;
|
|
||||||
maxScores[j] = node.MaxScore;
|
|
||||||
}
|
|
||||||
|
|
||||||
var s = new Vector<float>(scoreSums);
|
var s = new Vector<float>(scoreSums);
|
||||||
var m = new Vector<float>(maxScores);
|
var m = new Vector<float>(maxScores);
|
||||||
@@ -151,47 +132,21 @@ public sealed class Solver
|
|||||||
return children.Data[max.Item1][max.Item2];
|
return children.Data[max.Item1][max.Item2];
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public Node Select()
|
public static (Node ExpandedNode, float Score) Rollout(ref SolverConfig config, Node rootNode, Node expandedNode, Random random, Simulator simulator)
|
||||||
{
|
{
|
||||||
var node = RootNode;
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
var expandable = !node.State.AvailableActions.IsEmpty;
|
|
||||||
var likelyTerminal = node.Children.Count == 0;
|
|
||||||
if (expandable || likelyTerminal)
|
|
||||||
return node;
|
|
||||||
|
|
||||||
// select the node with the highest score
|
|
||||||
// if null (current node is invalid & not backpropagated just yet), try again from root
|
|
||||||
node = EvalBestChild(node.State.Scores.Visits, ref node.Children) ?? RootNode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public (Node ExpandedNode, float Score)? ExpandAndRollout(Simulator simulator, Node initialNode)
|
|
||||||
{
|
|
||||||
ref var initialState = ref initialNode.State;
|
|
||||||
// expand once
|
|
||||||
if (initialState.IsComplete)
|
|
||||||
return (initialNode, initialState.CalculateScore(Config.MaxStepCount) ?? 0);
|
|
||||||
|
|
||||||
var poppedAction = initialState.AvailableActions.PopRandom(Random);
|
|
||||||
if (!poppedAction.HasValue)
|
|
||||||
return null;
|
|
||||||
var expandedNode = initialNode.Add(Execute(simulator, initialState.State, poppedAction.Value, true));
|
|
||||||
|
|
||||||
// playout to a terminal state
|
// playout to a terminal state
|
||||||
var currentState = expandedNode.State.State;
|
var currentState = expandedNode.State.State;
|
||||||
var currentCompletionState = expandedNode.State.SimulationCompletionState;
|
var currentCompletionState = expandedNode.State.SimulationCompletionState;
|
||||||
var currentActions = expandedNode.State.AvailableActions;
|
var currentActions = expandedNode.State.AvailableActions;
|
||||||
|
|
||||||
byte actionCount = 0;
|
byte actionCount = 0;
|
||||||
Span<ActionType> actions = stackalloc ActionType[Config.MaxStepCount];
|
Span<ActionType> actions = stackalloc ActionType[config.MaxStepCount - currentState.ActionCount];
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (SimulationNode.GetCompletionState(currentCompletionState, currentActions) != CompletionState.Incomplete)
|
if (SimulationNode.GetCompletionState(currentCompletionState, currentActions) != CompletionState.Incomplete)
|
||||||
break;
|
break;
|
||||||
var nextAction = currentActions.SelectRandom(Random);
|
var nextAction = currentActions.SelectRandom(random);
|
||||||
actions[actionCount++] = nextAction;
|
actions[actionCount++] = nextAction;
|
||||||
(_, currentState) = simulator.Execute(currentState, nextAction);
|
(_, currentState) = simulator.Execute(currentState, nextAction);
|
||||||
currentCompletionState = simulator.CompletionState;
|
currentCompletionState = simulator.CompletionState;
|
||||||
@@ -199,10 +154,10 @@ public sealed class Solver
|
|||||||
}
|
}
|
||||||
|
|
||||||
// store the result if a max score was reached
|
// store the result if a max score was reached
|
||||||
var score = SimulationNode.CalculateScoreForState(currentState, currentCompletionState, Config.MaxStepCount) ?? 0;
|
var score = SimulationNode.CalculateScoreForState(currentState, currentCompletionState, config.MaxStepCount) ?? 0;
|
||||||
if (currentCompletionState == CompletionState.ProgressComplete)
|
if (currentCompletionState == CompletionState.ProgressComplete)
|
||||||
{
|
{
|
||||||
if (score >= Config.ScoreStorageThreshold && score >= RootNode.State.Scores.MaxScore)
|
if (score >= config.ScoreStorageThreshold && score >= rootNode.State.Scores.MaxScore)
|
||||||
{
|
{
|
||||||
(var terminalNode, _) = ExecuteActions(simulator, expandedNode, actions[..actionCount], true);
|
(var terminalNode, _) = ExecuteActions(simulator, expandedNode, actions[..actionCount], true);
|
||||||
return (terminalNode, score);
|
return (terminalNode, score);
|
||||||
@@ -211,80 +166,58 @@ public sealed class Solver
|
|||||||
return (expandedNode, score);
|
return (expandedNode, score);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Backpropagate(Node startNode, float score)
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static void Search<S>(ref SolverConfig config, Node rootNode, CancellationToken token) where S : ISolver
|
||||||
{
|
{
|
||||||
while (true)
|
Simulator simulator = new(rootNode.State.State, config.MaxStepCount);
|
||||||
{
|
var random = rootNode.State.State.Input.Random;
|
||||||
startNode.State.Scores.Visit(score);
|
for (var i = 0; i < config.Iterations; i++)
|
||||||
|
|
||||||
if (startNode == RootNode)
|
|
||||||
break;
|
|
||||||
|
|
||||||
startNode = startNode.Parent!;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SearchThread(CancellationToken token)
|
|
||||||
{
|
|
||||||
Simulator simulator = new(RootNode.State.State, Config.MaxStepCount);
|
|
||||||
for (var i = 0; i < Config.Iterations; i++)
|
|
||||||
{
|
{
|
||||||
if (token.IsCancellationRequested)
|
if (token.IsCancellationRequested)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
var selectedNode = Select();
|
if (!S.SearchIter(ref config, rootNode, random, simulator))
|
||||||
var rolledOut = ExpandAndRollout(simulator, selectedNode);
|
|
||||||
if (!rolledOut.HasValue)
|
|
||||||
{
|
{
|
||||||
// Retry, count this iteration as moot
|
// Retry, count this iteration as moot
|
||||||
i--;
|
i--;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var (endNode, score) = rolledOut.Value;
|
|
||||||
Backpropagate(endNode, score);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Search(CancellationToken token)
|
|
||||||
{
|
|
||||||
var tasks = new Task[Config.ThreadCount];
|
|
||||||
for (var i = 0; i < Config.ThreadCount; ++i)
|
|
||||||
tasks[i] = Task.Run(() => SearchThread(token), token);
|
|
||||||
Task.WaitAll(tasks, CancellationToken.None);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Pure]
|
[Pure]
|
||||||
public (List<ActionType> Actions, SimulationNode Node) Solution()
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Node CreateRootNode(SolverConfig config, SimulationInput input, bool strict) =>
|
||||||
|
CreateRootNode(config, new SimulationState(input), strict);
|
||||||
|
|
||||||
|
[Pure]
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Node CreateRootNode(SolverConfig config, SimulationState state, bool strict)
|
||||||
{
|
{
|
||||||
var actions = new List<ActionType>();
|
var sim = new Simulator(state, config.MaxStepCount);
|
||||||
var node = RootNode;
|
return new(new(
|
||||||
while (node.Children.Count != 0)
|
state,
|
||||||
{
|
null,
|
||||||
node = ChildMaxScore(ref node.Children);
|
sim.CompletionState,
|
||||||
|
sim.AvailableActionsHeuristic(strict)
|
||||||
if (node.State.Action != null)
|
));
|
||||||
actions.Add(node.State.Action.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (actions, node.State);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static (List<ActionType> Actions, SimulationState State) SearchStepwise(SolverConfig config, SimulationInput input, Action<ActionType>? actionCallback, CancellationToken token = default) =>
|
public static (List<ActionType> Actions, SimulationState State) SearchStepwise<S>(SolverConfig config, SimulationInput input, Action<ActionType>? actionCallback, CancellationToken token = default) where S : ISolver =>
|
||||||
SearchStepwise(config, new SimulationState(input), actionCallback, token);
|
SearchStepwise<S>(config, new SimulationState(input), actionCallback, token);
|
||||||
|
|
||||||
public static (List<ActionType> Actions, SimulationState State) SearchStepwise(SolverConfig config, SimulationState state, Action<ActionType>? actionCallback, CancellationToken token = default)
|
public static (List<ActionType> Actions, SimulationState State) SearchStepwise<S>(SolverConfig config, SimulationState state, Action<ActionType>? actionCallback, CancellationToken token = default) where S : ISolver
|
||||||
{
|
{
|
||||||
var actions = new List<ActionType>();
|
var actions = new List<ActionType>();
|
||||||
Simulator sim = new(state, config.MaxStepCount);
|
var sim = new Simulator(state, config.MaxStepCount);
|
||||||
var solver = new Solver(config, state, true);
|
var rootNode = CreateRootNode(config, state, true);
|
||||||
while (!sim.IsComplete)
|
while (!sim.IsComplete)
|
||||||
{
|
{
|
||||||
if (token.IsCancellationRequested)
|
if (token.IsCancellationRequested)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
solver.Search(token);
|
S.Search(ref config, rootNode, token);
|
||||||
var (solution_actions, solution_node) = solver.Solution();
|
var (solution_actions, solution_node) = Solution(rootNode);
|
||||||
|
|
||||||
if (solution_node.Scores.MaxScore >= 1.0)
|
if (solution_node.Scores.MaxScore >= 1.0)
|
||||||
{
|
{
|
||||||
@@ -298,20 +231,20 @@ public sealed class Solver
|
|||||||
|
|
||||||
actionCallback?.Invoke(chosen_action);
|
actionCallback?.Invoke(chosen_action);
|
||||||
|
|
||||||
solver = new Solver(config, state, true);
|
rootNode = CreateRootNode(config, state, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (actions, state);
|
return (actions, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static (List<ActionType> Actions, SimulationState State) SearchOneshot(SolverConfig config, SimulationInput input, CancellationToken token = default) =>
|
public static (List<ActionType> Actions, SimulationState State) SearchOneshot<S>(SolverConfig config, SimulationInput input, CancellationToken token = default) where S : ISolver =>
|
||||||
SearchOneshot(config, new SimulationState(input), token);
|
SearchOneshot<S>(config, new SimulationState(input), token);
|
||||||
|
|
||||||
public static (List<ActionType> Actions, SimulationState State) SearchOneshot(SolverConfig config, SimulationState state, CancellationToken token = default)
|
public static (List<ActionType> Actions, SimulationState State) SearchOneshot<S>(SolverConfig config, SimulationState state, CancellationToken token = default) where S : ISolver
|
||||||
{
|
{
|
||||||
var solver = new Solver(config, state, false);
|
var rootNode = CreateRootNode(config, state, false);
|
||||||
solver.Search(token);
|
S.Search(ref config, rootNode, token);
|
||||||
var (solution_actions, solution_node) = solver.Solution();
|
var (solution_actions, solution_node) = Solution(rootNode);
|
||||||
return (solution_actions, solution_node.State);
|
return (solution_actions, solution_node.State);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user