Get rid of ActionPool, use ulong for ActionSet

This commit is contained in:
Asriel Camora
2024-02-29 02:47:59 -08:00
parent 85922b225c
commit c8d3161eb1
7 changed files with 112 additions and 226 deletions
-97
View File
@@ -1,97 +0,0 @@
using Craftimizer.Simulator.Actions;
using System.Diagnostics.Contracts;
using System.Numerics;
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;
// Bitmask of accepted actions
private readonly ulong acceptedActions;
internal ActionType[] AcceptedActions => GetActions();
internal int Count => BitOperations.PopCount(acceptedActions);
public ActionPool(ReadOnlySpan<ActionType> actions)
{
acceptedActions = 0;
foreach (var action in actions)
acceptedActions |= 1ul << ((byte)action);
if (Count > MaskSize)
throw new ArgumentOutOfRangeException(nameof(actions), actions.Length, $"ActionPool only supports up to {MaskSize} actions");
}
private ActionType[] GetActions()
{
var ret = new ActionType[Count];
var i = 0;
foreach (var v in (byte[])Enum.GetValuesAsUnderlyingType<ActionType>())
{
if ((acceptedActions & (1ul << v)) != 0)
ret[i++] = (ActionType)v;
}
return ret;
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal int FromAction(ActionType action)
{
if ((acceptedActions & (1ul << (byte)action)) == 0)
throw new ArgumentOutOfRangeException(nameof(action), action, "Action is not accepted by this pool.");
// Get number of 1s before action
return BitOperations.PopCount(acceptedActions & ((1ul << (byte)action) - 1));
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ActionType ToAction(int index)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index), index, $"Index {index} is out of range for this pool.");
// Get index of (index+1)th 1 in set
return (ActionType)Intrinsics.NthBitSet(acceptedActions, index);
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal uint ToMask(ActionType action) => 1u << (FromAction(action) + 1);
}
+30 -18
View File
@@ -7,13 +7,25 @@ namespace Craftimizer.Solver;
public struct ActionSet
{
internal uint bits;
internal ulong bits;
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int FromAction(ActionType action) => (byte)action;
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ActionType ToAction(int index) => (ActionType)index;
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ulong ToMask(ActionType action) => 1ul << FromAction(action);
// Return true if action was newly added and not there before.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool AddAction(in ActionPool pool, ActionType action)
public bool AddAction(ActionType action)
{
var mask = pool.ToMask(action);
var mask = ToMask(action);
var old = bits;
bits |= mask;
return (old & mask) == 0;
@@ -21,9 +33,9 @@ public struct ActionSet
// Return true if action was newly removed and not already gone.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool RemoveAction(in ActionPool pool, ActionType action)
public bool RemoveAction(ActionType action)
{
var mask = pool.ToMask(action);
var mask = ToMask(action);
var old = bits;
bits &= ~mask;
return (old & mask) != 0;
@@ -31,10 +43,10 @@ public struct ActionSet
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool HasAction(in ActionPool pool, ActionType action) => (bits & pool.ToMask(action)) != 0;
public readonly bool HasAction(ActionType action) => (bits & ToMask(action)) != 0;
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType ElementAt(in ActionPool pool, int index) => pool.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);
@@ -43,38 +55,38 @@ public struct ActionSet
public readonly bool IsEmpty => bits == 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ActionType SelectRandom(in ActionPool pool, Random random)
public readonly ActionType SelectRandom(Random random)
{
#if IS_DETERMINISTIC
return First(in pool);
return First();
#else
return ElementAt(in pool, random.Next(Count));
return ElementAt(random.Next(Count));
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ActionType PopRandom(in ActionPool pool, Random random)
public ActionType PopRandom(Random random)
{
#if IS_DETERMINISTIC
return PopFirst(in pool);
return PopFirst();
#else
var action = ElementAt(in pool, random.Next(Count));
RemoveAction(in pool, action);
var action = ElementAt(random.Next(Count));
RemoveAction(action);
return action;
#endif
}
#if IS_DETERMINISTIC
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ActionType PopFirst(in pool)
private ActionType PopFirst()
{
var action = First(in pool);
RemoveAction(in pool, action);
var action = First();
RemoveAction(action);
return action;
}
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly ActionType First(in pool) => ElementAt(in pool, 0);
private readonly ActionType First() => ElementAt(0);
#endif
}
+4 -4
View File
@@ -52,9 +52,9 @@ public sealed class MCTS
if (state.IsComplete)
return startNode;
if (!state.AvailableActions.HasAction(in simulator.Pool, action))
if (!state.AvailableActions.HasAction(action))
return startNode;
state.AvailableActions.RemoveAction(in simulator.Pool, action);
state.AvailableActions.RemoveAction(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(in simulator.Pool, random);
var poppedAction = initialState.AvailableActions.PopRandom(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(in simulator.Pool, random);
var nextAction = currentActions.SelectRandom(random);
actions[actionCount++] = nextAction;
(_, currentState) = simulator.Execute(currentState, nextAction);
currentCompletionState = simulator.CompletionState;
+2 -1
View File
@@ -1,3 +1,4 @@
using Craftimizer.Simulator.Actions;
using System.Runtime.InteropServices;
namespace Craftimizer.Solver;
@@ -21,7 +22,7 @@ public readonly record struct MCTSConfig
public float ScoreCP { get; init; }
public float ScoreSteps { get; init; }
public ActionPool ActionPool { get; init; }
public ActionType[] ActionPool { get; init; }
public MCTSConfig(in SolverConfig config)
{
+5 -7
View File
@@ -7,8 +7,7 @@ namespace Craftimizer.Solver;
internal sealed class Simulator : SimulatorNoRandom
{
public readonly ActionPool Pool;
private readonly ActionType[] poolActions;
private readonly ActionType[] actionPool;
private readonly int maxStepCount;
public override CompletionState CompletionState
@@ -22,10 +21,9 @@ internal sealed class Simulator : SimulatorNoRandom
}
}
public Simulator(in ActionPool pool, int maxStepCount)
public Simulator(ActionType[] actionPool, int maxStepCount)
{
Pool = pool;
poolActions = Pool.AcceptedActions;
this.actionPool = actionPool;
this.maxStepCount = maxStepCount;
}
@@ -137,9 +135,9 @@ internal sealed class Simulator : SimulatorNoRandom
return new();
var ret = new ActionSet();
foreach (var action in poolActions)
foreach (var action in actionPool)
if (CanUseAction(action, strict))
ret.AddAction(in Pool, action);
ret.AddAction(action);
return ret;
}
+36 -2
View File
@@ -1,3 +1,4 @@
using Craftimizer.Simulator.Actions;
using System.Runtime.InteropServices;
namespace Craftimizer.Solver;
@@ -31,7 +32,7 @@ public readonly record struct SolverConfig
public float ScoreCP { get; init; }
public float ScoreSteps { get; init; }
public ActionPool ActionPool { get; init; }
public ActionType[] ActionPool { get; init; }
public SolverAlgorithm Algorithm { get; init; }
public SolverConfig()
@@ -55,10 +56,43 @@ public readonly record struct SolverConfig
ScoreCP = .05f;
ScoreSteps = .05f;
ActionPool = ActionPool.Default;
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 SolverConfig SimulatorDefault = new SolverConfig() with
{