Merge branch 'action-pool'

This commit is contained in:
Asriel Camora
2024-03-14 20:10:59 -07:00
36 changed files with 393 additions and 183 deletions
+7 -57
View File
@@ -7,69 +7,19 @@ namespace Craftimizer.Solver;
public struct ActionSet
{
private uint bits;
internal static ReadOnlySpan<ActionType> AcceptedActions => new[]
{
ActionType.StandardTouchCombo,
ActionType.AdvancedTouchCombo,
ActionType.FocusedTouchCombo,
ActionType.FocusedSynthesisCombo,
ActionType.TrainedFinesse,
ActionType.PrudentSynthesis,
ActionType.Groundwork,
ActionType.AdvancedTouch,
ActionType.CarefulSynthesis,
ActionType.TrainedEye,
ActionType.DelicateSynthesis,
ActionType.PreparatoryTouch,
ActionType.Reflect,
ActionType.PrudentTouch,
ActionType.Manipulation,
ActionType.MuscleMemory,
ActionType.ByregotsBlessing,
ActionType.WasteNot2,
ActionType.BasicSynthesis,
ActionType.Innovation,
ActionType.GreatStrides,
ActionType.StandardTouch,
ActionType.Veneration,
ActionType.WasteNot,
ActionType.MastersMend,
ActionType.BasicTouch,
};
public static readonly int[] AcceptedActionsLUT;
static ActionSet()
{
AcceptedActionsLUT = new int[Enum.GetValues<ActionType>().Length];
for (var i = 0; i < AcceptedActionsLUT.Length; i++)
AcceptedActionsLUT[i] = -1;
for (var i = 0; i < AcceptedActions.Length; i++)
AcceptedActionsLUT[(byte)AcceptedActions[i]] = i;
}
private ulong bits;
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int FromAction(ActionType action)
{
var ret = AcceptedActionsLUT[(byte)action];
if (ret == -1)
throw new ArgumentOutOfRangeException(nameof(action), action, $"Action {action} is unsupported in {nameof(ActionSet)}.");
return ret;
}
private static int FromAction(ActionType action) => (byte)action;
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ActionType ToAction(int index)
{
if (index < 0 || index >= AcceptedActions.Length)
throw new ArgumentOutOfRangeException(nameof(index), index, $"Index {index} is out of range for {nameof(ActionSet)}.");
return AcceptedActions[index];
}
private static ActionType ToAction(int index) => (ActionType)index;
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint ToMask(ActionType action) => 1u << (FromAction(action) + 1);
private static ulong ToMask(ActionType action) => 1ul << FromAction(action);
// Return true if action was newly added and not there before.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -96,7 +46,7 @@ public struct ActionSet
public readonly bool HasAction(ActionType action) => (bits & ToMask(action)) != 0;
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType ElementAt(int index) => ToAction(Intrinsics.NthBitSet(bits, index) - 1);
public readonly ActionType ElementAt(int index) => ToAction(Intrinsics.NthBitSet(bits, index));
[Pure]
public readonly int Count => BitOperations.PopCount(bits);
+13 -10
View File
@@ -4,31 +4,34 @@ using System.Runtime.CompilerServices;
namespace Craftimizer.Solver;
// Adapted from https://github.com/dtao/ConcurrentList/blob/4fcf1c76e93021a41af5abb2d61a63caeba2adad/ConcurrentList/ConcurrentList.cs
public struct ArenaBuffer<T> where T : struct
public struct ArenaBuffer
{
// Technically 25, but it's very unlikely to actually get to there.
// The benchmark reaches 20 at most, but here we have a little leeway just in case.
private const int MaxSize = 24;
internal const int MaxSize = 32;
private static readonly int BatchSize = Vector<float>.Count;
private static readonly int BatchSizeBits = int.Log2(BatchSize);
private static readonly int BatchSizeMask = BatchSize - 1;
internal static readonly int BatchSize = Vector<float>.Count;
internal static readonly int BatchSizeBits = int.Log2(BatchSize);
internal static readonly int BatchSizeMask = BatchSize - 1;
private static readonly int BatchCount = MaxSize / BatchSize;
internal static readonly int BatchCount = MaxSize / BatchSize;
}
// Adapted from https://github.com/dtao/ConcurrentList/blob/4fcf1c76e93021a41af5abb2d61a63caeba2adad/ConcurrentList/ConcurrentList.cs
public struct ArenaBuffer<T> where T : struct
{
public ArenaNode<T>[][] Data;
public int Count { get; private set; }
public void Add(ArenaNode<T> node)
{
Data ??= new ArenaNode<T>[BatchCount][];
Data ??= new ArenaNode<T>[ArenaBuffer.BatchCount][];
var idx = Count++;
var (arrayIdx, subIdx) = GetArrayIndex(idx);
Data[arrayIdx] ??= new ArenaNode<T>[BatchSize];
Data[arrayIdx] ??= new ArenaNode<T>[ArenaBuffer.BatchSize];
node.ChildIdx = (arrayIdx, subIdx);
Data[arrayIdx][subIdx] = node;
@@ -37,5 +40,5 @@ public struct ArenaBuffer<T> where T : struct
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static (int arrayIdx, int subIdx) GetArrayIndex(int idx) =>
(idx >> BatchSizeBits, idx & BatchSizeMask);
(idx >> ArenaBuffer.BatchSizeBits, idx & ArenaBuffer.BatchSizeMask);
}
+3 -3
View File
@@ -12,7 +12,7 @@ public sealed class ArenaNode<T> where T : struct
public NodeScoresBuffer? ParentScores => Parent?.ChildScores;
public ArenaNode(T state, ArenaNode<T>? parent = null)
public ArenaNode(in T state, ArenaNode<T>? parent = null)
{
State = state;
Children = new();
@@ -24,9 +24,9 @@ public sealed class ArenaNode<T> where T : struct
Children.Data?[at.arrayIdx]?[at.subIdx];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ArenaNode<T> Add(T state)
public ArenaNode<T> Add(in T state)
{
var node = new ArenaNode<T>(state, this);
var node = new ArenaNode<T>(in state, this);
ChildScores.Add();
Children.Add(node);
return node;
+47
View File
@@ -92,11 +92,46 @@ internal static class Intrinsics
return _base;
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int NthBitSetScalar(ulong value, int n)
{
var mask = 0x00000000FFFFFFFFul;
var size = 32;
var _base = 0;
if (n++ >= BitOperations.PopCount(value))
return 64;
while (size > 0)
{
var count = BitOperations.PopCount(value & mask);
if (n > count)
{
_base += size;
size >>= 1;
mask |= mask << size;
}
else
{
size >>= 1;
mask >>= size;
}
}
return _base;
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int NthBitSetBMI2(uint value, int n) =>
BitOperations.TrailingZeroCount(Bmi2.ParallelBitDeposit(1u << n, value));
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int NthBitSetBMI2(ulong value, int n) =>
BitOperations.TrailingZeroCount(Bmi2.X64.ParallelBitDeposit(1ul << n, value));
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int NthBitSet(uint value, int n)
@@ -109,6 +144,18 @@ internal static class Intrinsics
NthBitSetScalar(value, n);
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int NthBitSet(ulong value, int n)
{
if (n >= BitOperations.PopCount(value))
return 64;
return Bmi2.X64.IsSupported ?
NthBitSetBMI2(value, n) :
NthBitSetScalar(value, n);
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector<float> ReciprocalSqrt(Vector<float> data)
+7 -9
View File
@@ -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.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,
@@ -194,7 +193,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 &&
@@ -202,7 +200,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;
@@ -262,17 +260,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.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)
@@ -293,7 +288,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));
}
+5
View File
@@ -1,3 +1,4 @@
using Craftimizer.Simulator.Actions;
using System.Runtime.InteropServices;
namespace Craftimizer.Solver;
@@ -21,6 +22,8 @@ public readonly record struct MCTSConfig
public float ScoreCP { get; init; }
public float ScoreSteps { get; init; }
public ActionType[] ActionPool { get; init; }
public MCTSConfig(in SolverConfig config)
{
MaxStepCount = config.MaxStepCount;
@@ -36,5 +39,7 @@ public readonly record struct MCTSConfig
ScoreDurability = config.ScoreDurability;
ScoreCP = config.ScoreCP;
ScoreSteps = config.ScoreSteps;
ActionPool = config.ActionPool;
}
}
+7 -16
View File
@@ -1,12 +1,13 @@
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Craftimizer.Solver;
// Adapted from https://github.com/dtao/ConcurrentList/blob/4fcf1c76e93021a41af5abb2d61a63caeba2adad/ConcurrentList/ConcurrentList.cs
public struct NodeScoresBuffer
{
[StructLayout(LayoutKind.Auto)]
public readonly struct ScoresBatch
{
public readonly Memory<float> ScoreSum;
@@ -15,28 +16,18 @@ public struct NodeScoresBuffer
public ScoresBatch()
{
ScoreSum = new float[BatchSize];
MaxScore = new float[BatchSize];
Visits = new int[BatchSize];
ScoreSum = new float[ArenaBuffer.BatchSize];
MaxScore = new float[ArenaBuffer.BatchSize];
Visits = new int[ArenaBuffer.BatchSize];
}
}
// Technically 25, but it's very unlikely to actually get to there.
// The benchmark reaches 20 at most, but here we have a little leeway just in case.
private const int MaxSize = 24;
private static readonly int BatchSize = Vector<float>.Count;
private static readonly int BatchSizeBits = int.Log2(BatchSize);
private static readonly int BatchSizeMask = BatchSize - 1;
private static readonly int BatchCount = MaxSize / BatchSize;
public ScoresBatch[] Data;
public int Count { get; private set; }
public void Add()
{
Data ??= new ScoresBatch[BatchCount];
Data ??= new ScoresBatch[ArenaBuffer.BatchCount];
var idx = Count++;
@@ -59,5 +50,5 @@ public struct NodeScoresBuffer
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static (int arrayIdx, int subIdx) GetArrayIndex(int idx) =>
(idx >> BatchSizeBits, idx & BatchSizeMask);
(idx >> ArenaBuffer.BatchSizeBits, idx & ArenaBuffer.BatchSizeMask);
}
+14 -9
View File
@@ -7,6 +7,7 @@ namespace Craftimizer.Solver;
internal sealed class Simulator : SimulatorNoRandom
{
private readonly (BaseAction Data, ActionType Action)[] actionPoolObjects;
private readonly int maxStepCount;
public override CompletionState CompletionState
@@ -20,8 +21,15 @@ internal sealed class Simulator : SimulatorNoRandom
}
}
public Simulator(int maxStepCount)
public Simulator(ActionType[] actionPool, int maxStepCount, SimulationState? filteringState = null)
{
var pool = actionPool.Select(x => (x.Base(), x));
if (filteringState is { } state)
{
State = state;
pool = pool.Where(x => x.Item1.IsPossible(this));
}
actionPoolObjects = pool.OrderBy(x => x.x).ToArray();
this.maxStepCount = maxStepCount;
}
@@ -30,11 +38,9 @@ internal sealed class Simulator : SimulatorNoRandom
[MethodImpl(MethodImplOptions.AggressiveInlining)]
// It's just a bunch of if statements, I would assume this is actually quite simple to follow
#pragma warning disable MA0051 // Method is too long
private bool CanUseAction(ActionType action, bool strict)
private bool CouldUseAction(ActionType action, BaseAction baseAction, bool strict)
#pragma warning restore MA0051 // Method is too long
{
var baseAction = action.Base();
if (CalculateSuccessRate(baseAction.SuccessRate(this)) != 1)
return false;
@@ -46,7 +52,7 @@ internal sealed class Simulator : SimulatorNoRandom
{
// always use Trained Eye if it's available
if (action == ActionType.TrainedEye)
return baseAction.CanUse(this);
return baseAction.CouldUse(this);
// don't allow quality moves under Muscle Memory for difficult crafts
if (Input.Recipe.ClassJobLevel == 90 &&
@@ -123,7 +129,7 @@ internal sealed class Simulator : SimulatorNoRandom
return false;
}
return baseAction.CanUse(this);
return baseAction.CouldUse(this);
}
// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/craft_state.rs#L137
@@ -133,10 +139,9 @@ internal sealed class Simulator : SimulatorNoRandom
return new();
var ret = new ActionSet();
foreach (var action in ActionSet.AcceptedActions)
if (CanUseAction(action, strict))
foreach (var (data, action) in actionPoolObjects)
if (CouldUseAction(action, data, strict))
ret.AddAction(action);
return ret;
}
}
+3 -3
View File
@@ -145,7 +145,7 @@ public sealed class Solver : IDisposable
var bestSims = new List<(float Score, SolverSolution Result)>();
var state = State;
var sim = new Simulator(Config.MaxStepCount);
var sim = new Simulator(Config.ActionPool, Config.MaxStepCount);
var activeStates = new List<SolverSolution>() { new(Array.Empty<ActionType>(), state) };
@@ -272,7 +272,7 @@ public sealed class Solver : IDisposable
var actions = new List<ActionType>();
var state = State;
var sim = new Simulator(Config.MaxStepCount) { State = state };
var sim = new Simulator(Config.ActionPool, Config.MaxStepCount, state);
while (true)
{
Token.ThrowIfCancellationRequested();
@@ -338,7 +338,7 @@ public sealed class Solver : IDisposable
var actions = new List<ActionType>();
var state = State;
var sim = new Simulator(Config.MaxStepCount) { State = state };
var sim = new Simulator(Config.ActionPool, Config.MaxStepCount, state);
while (true)
{
Token.ThrowIfCancellationRequested();
+49
View File
@@ -1,3 +1,4 @@
using Craftimizer.Simulator.Actions;
using System.Runtime.InteropServices;
namespace Craftimizer.Solver;
@@ -31,6 +32,7 @@ public readonly record struct SolverConfig
public float ScoreCP { get; init; }
public float ScoreSteps { get; init; }
public ActionType[] ActionPool { get; init; }
public SolverAlgorithm Algorithm { get; init; }
public SolverConfig()
@@ -54,9 +56,56 @@ public readonly record struct SolverConfig
ScoreCP = .05f;
ScoreSteps = .05f;
ActionPool = DefaultActionPool;
Algorithm = SolverAlgorithm.StepwiseFurcated;
}
public static ActionType[] OptimizeActionPool(IEnumerable<ActionType> actions) =>
actions.Order().ToArray();
public static readonly ActionType[] DefaultActionPool = OptimizeActionPool(new[]
{
ActionType.StandardTouchCombo,
ActionType.AdvancedTouchCombo,
ActionType.FocusedTouchCombo,
ActionType.FocusedSynthesisCombo,
ActionType.TrainedFinesse,
ActionType.PrudentSynthesis,
ActionType.Groundwork,
ActionType.AdvancedTouch,
ActionType.CarefulSynthesis,
ActionType.TrainedEye,
ActionType.DelicateSynthesis,
ActionType.PreparatoryTouch,
ActionType.Reflect,
ActionType.PrudentTouch,
ActionType.Manipulation,
ActionType.MuscleMemory,
ActionType.ByregotsBlessing,
ActionType.WasteNot2,
ActionType.BasicSynthesis,
ActionType.Innovation,
ActionType.GreatStrides,
ActionType.StandardTouch,
ActionType.Veneration,
ActionType.WasteNot,
ActionType.MastersMend,
ActionType.BasicTouch,
});
public static readonly IReadOnlySet<ActionType> InefficientActions = new HashSet<ActionType>(new[]
{
ActionType.CarefulObservation,
ActionType.HeartAndSoul,
ActionType.FinalAppraisal
});
public static readonly IReadOnlySet<ActionType> RiskyActions = new HashSet<ActionType>(new[]
{
ActionType.RapidSynthesis,
ActionType.HastyTouch,
});
public static readonly SolverConfig SimulatorDefault = new SolverConfig() with
{