Implement ActionPool (backend only)

This commit is contained in:
Asriel Camora
2024-02-29 00:01:55 -08:00
parent 44ae3791f1
commit ecabc24517
8 changed files with 204 additions and 134 deletions
+110
View File
@@ -0,0 +1,110 @@
using Craftimizer.Simulator.Actions;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Craftimizer.Solver;
[StructLayout(LayoutKind.Auto)]
public readonly struct ActionPool
{
public static ActionPool Default { get; } = new(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 const int MaskSize = 32;
public const int EnumSize = 37;
private unsafe struct EnumBuffer
{
public fixed byte Data[MaskSize];
public ref ActionType this[int index] => ref Unsafe.As<byte, ActionType>(ref Data[index]);
public Span<ActionType> AsSpan() => new(Unsafe.AsPointer(ref this[0]), MaskSize);
}
private unsafe struct LUTBuffer
{
public fixed byte Data[EnumSize];
public ref byte this[ActionType index] => ref Data[(byte)index];
#pragma warning disable MA0099
public Span<byte> AsSpan() => new(Unsafe.AsPointer(ref this[0]), EnumSize);
#pragma warning restore MA0099
}
// List of accepted actions (max 32)
private readonly EnumBuffer acceptedActions;
// Lookup table for accepted actions (ActionType as idx -> idx in acceptedActions)
private readonly LUTBuffer acceptedActionsLUT;
private readonly byte size;
internal ReadOnlySpan<ActionType> AcceptedActions => acceptedActions.AsSpan().Slice(0, size);
public ActionPool(ReadOnlySpan<ActionType> actions)
{
if (actions.Length > MaskSize)
throw new ArgumentOutOfRangeException(nameof(actions), actions.Length, $"ActionPool only supports up to {MaskSize} actions");
size = (byte)actions.Length;
acceptedActions.AsSpan().Fill((ActionType)0xFF);
acceptedActionsLUT.AsSpan().Fill(0xFF);
actions.CopyTo(acceptedActions.AsSpan());
for (var i = 0; i < size; i++)
acceptedActionsLUT[acceptedActions[i]] = (byte)i;
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal byte FromAction(ActionType action)
{
var ret = acceptedActionsLUT[action];
if (ret == 0xFF)
throw new ArgumentOutOfRangeException(nameof(action), action, $"Action {action} is unsupported in this pool.");
return ret;
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ActionType ToAction(byte index)
{
if (index < 0 || index >= size)
throw new ArgumentOutOfRangeException(nameof(index), index, $"Index {index} is out of range for this pool.");
return acceptedActions[index];
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal uint ToMask(ActionType action) => 1u << (FromAction(action) + 1);
}
+18 -80
View File
@@ -7,75 +7,13 @@ 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;
}
[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;
}
[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];
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint ToMask(ActionType action) => 1u << (FromAction(action) + 1);
internal uint bits;
// Return true if action was newly added and not there before.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool AddAction(ActionType action)
public bool AddAction(in ActionPool pool, ActionType action)
{
var mask = ToMask(action);
var mask = pool.ToMask(action);
var old = bits;
bits |= mask;
return (old & mask) == 0;
@@ -83,9 +21,9 @@ public struct ActionSet
// Return true if action was newly removed and not already gone.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool RemoveAction(ActionType action)
public bool RemoveAction(in ActionPool pool, ActionType action)
{
var mask = ToMask(action);
var mask = pool.ToMask(action);
var old = bits;
bits &= ~mask;
return (old & mask) != 0;
@@ -93,10 +31,10 @@ public struct ActionSet
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool HasAction(ActionType action) => (bits & ToMask(action)) != 0;
public readonly bool HasAction(in ActionPool pool, ActionType action) => (bits & pool.ToMask(action)) != 0;
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType ElementAt(int index) => ToAction(Intrinsics.NthBitSet(bits, index) - 1);
public readonly ActionType ElementAt(in ActionPool pool, int index) => pool.ToAction((byte)(Intrinsics.NthBitSet(bits, index) - 1));
[Pure]
public readonly int Count => BitOperations.PopCount(bits);
@@ -105,38 +43,38 @@ public struct ActionSet
public readonly bool IsEmpty => bits == 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType SelectRandom(Random random)
public readonly ActionType SelectRandom(in ActionPool pool, Random random)
{
#if IS_DETERMINISTIC
return First();
return First(in pool);
#else
return ElementAt(random.Next(Count));
return ElementAt(in pool, random.Next(Count));
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType PopRandom(Random random)
public ActionType PopRandom(in ActionPool pool, Random random)
{
#if IS_DETERMINISTIC
return PopFirst();
return PopFirst(in pool);
#else
var action = ElementAt(random.Next(Count));
RemoveAction(action);
var action = ElementAt(in pool, random.Next(Count));
RemoveAction(in pool, action);
return action;
#endif
}
#if IS_DETERMINISTIC
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ActionType PopFirst()
private ActionType PopFirst(in pool)
{
var action = First();
RemoveAction(action);
var action = First(in pool);
RemoveAction(in pool, action);
return action;
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly ActionType First() => ElementAt(0);
private readonly ActionType First(in pool) => ElementAt(in pool, 0);
#endif
}
+6 -6
View File
@@ -23,7 +23,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 = state };
rootNode = new(new(
state,
null,
@@ -52,9 +52,9 @@ public sealed class MCTS
if (state.IsComplete)
return startNode;
if (!state.AvailableActions.HasAction(action))
if (!state.AvailableActions.HasAction(in simulator.Pool, action))
return startNode;
state.AvailableActions.RemoveAction(action);
state.AvailableActions.RemoveAction(in simulator.Pool, action);
startNode = startNode.Add(Execute(simulator, state.State, action, strict));
}
@@ -184,7 +184,7 @@ public sealed class MCTS
if (initialState.IsComplete)
return (initialNode, initialState.CalculateScore(config) ?? 0);
var poppedAction = initialState.AvailableActions.PopRandom(random);
var poppedAction = initialState.AvailableActions.PopRandom(in simulator.Pool, random);
var expandedNode = initialNode.Add(Execute(simulator, initialState.State, poppedAction, true));
// playout to a terminal state
@@ -198,7 +198,7 @@ public sealed class MCTS
while (SimulationNode.GetCompletionState(currentCompletionState, currentActions) == CompletionState.Incomplete &&
actionCount < actions.Length)
{
var nextAction = currentActions.SelectRandom(random);
var nextAction = currentActions.SelectRandom(in simulator.Pool, random);
actions[actionCount++] = nextAction;
(_, currentState) = simulator.Execute(currentState, nextAction);
currentCompletionState = simulator.CompletionState;
@@ -283,7 +283,7 @@ public sealed class MCTS
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Search(int iterations, ref int progress, CancellationToken token)
{
Simulator simulator = new(config.MaxStepCount);
Simulator simulator = new(config.ActionPool, config.MaxStepCount);
var random = rootNode.State.State.Input.Random;
var staleCounter = 0;
var i = 0;
+4
View File
@@ -21,6 +21,8 @@ public readonly record struct MCTSConfig
public float ScoreCP { get; init; }
public float ScoreSteps { get; init; }
public ActionPool ActionPool { get; init; }
public MCTSConfig(in SolverConfig config)
{
MaxStepCount = config.MaxStepCount;
@@ -36,5 +38,7 @@ public readonly record struct MCTSConfig
ScoreDurability = config.ScoreDurability;
ScoreCP = config.ScoreCP;
ScoreSteps = config.ScoreSteps;
ActionPool = config.ActionPool;
}
}
+5 -3
View File
@@ -7,6 +7,7 @@ namespace Craftimizer.Solver;
internal sealed class Simulator : SimulatorNoRandom
{
public readonly ActionPool Pool;
private readonly int maxStepCount;
public override CompletionState CompletionState
@@ -20,8 +21,9 @@ internal sealed class Simulator : SimulatorNoRandom
}
}
public Simulator(int maxStepCount)
public Simulator(in ActionPool pool, int maxStepCount)
{
Pool = pool;
this.maxStepCount = maxStepCount;
}
@@ -133,9 +135,9 @@ internal sealed class Simulator : SimulatorNoRandom
return new();
var ret = new ActionSet();
foreach (var action in ActionSet.AcceptedActions)
foreach (var action in Pool.AcceptedActions)
if (CanUseAction(action, strict))
ret.AddAction(action);
ret.AddAction(in Pool, 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 = 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 = state };
while (true)
{
Token.ThrowIfCancellationRequested();
+2
View File
@@ -31,6 +31,7 @@ public readonly record struct SolverConfig
public float ScoreCP { get; init; }
public float ScoreSteps { get; init; }
public ActionPool ActionPool { get; init; }
public SolverAlgorithm Algorithm { get; init; }
public SolverConfig()
@@ -54,6 +55,7 @@ public readonly record struct SolverConfig
ScoreCP = .05f;
ScoreSteps = .05f;
ActionPool = ActionPool.Default;
Algorithm = SolverAlgorithm.StepwiseFurcated;
}