Use ActionSet instead of a List
This commit is contained in:
@@ -0,0 +1,69 @@
|
|||||||
|
using Craftimizer.Simulator.Actions;
|
||||||
|
using System;
|
||||||
|
using System.Numerics;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.Intrinsics.X86;
|
||||||
|
|
||||||
|
namespace Craftimizer.Simulator;
|
||||||
|
|
||||||
|
public record ActionSet
|
||||||
|
{
|
||||||
|
public ulong Bits { get; set; }
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool HasFlag(ActionType action) => (Bits & (1ul << ((int)action + 1))) != 0;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void SetFlag(ActionType action) => Bits |= 1ul << ((int)action + 1);
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void ClearFlag(ActionType action) => Bits &= ~(1ul << ((int)action + 1));
|
||||||
|
|
||||||
|
public IEnumerable<ActionType> Actions => GetActions();
|
||||||
|
|
||||||
|
private IEnumerable<ActionType> GetActions()
|
||||||
|
{
|
||||||
|
foreach (var action in Enum.GetValues<ActionType>())
|
||||||
|
if (HasFlag(action))
|
||||||
|
yield return action;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
private static int NthBitSet(ulong value, int n)
|
||||||
|
{
|
||||||
|
if (Bmi2.X64.IsSupported)
|
||||||
|
return BitOperations.TrailingZeroCount(Bmi2.X64.ParallelBitDeposit(1ul << n, value));
|
||||||
|
|
||||||
|
ulong mask = 0x0000FFFFFFFFu;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public ActionType ActionAt(int index) => (ActionType)(NthBitSet(Bits, index) - 1);
|
||||||
|
|
||||||
|
public int ActionCount => BitOperations.PopCount(Bits);
|
||||||
|
|
||||||
|
public bool IsEmpty => Bits == 0;
|
||||||
|
}
|
||||||
@@ -29,8 +29,6 @@ public class Simulator
|
|||||||
}
|
}
|
||||||
public virtual bool IsComplete => CompletionState != CompletionState.Incomplete;
|
public virtual bool IsComplete => CompletionState != CompletionState.Incomplete;
|
||||||
|
|
||||||
public IEnumerable<ActionType> AvailableActions => ActionUtils.AvailableActions(this);
|
|
||||||
|
|
||||||
#pragma warning disable CS8618 // Emplace sets all the fields already
|
#pragma warning disable CS8618 // Emplace sets all the fields already
|
||||||
public Simulator(SimulationState state)
|
public Simulator(SimulationState state)
|
||||||
#pragma warning restore CS8618
|
#pragma warning restore CS8618
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ public readonly record struct SimulationNode
|
|||||||
{
|
{
|
||||||
public SimulationState State { get; init; }
|
public SimulationState State { get; init; }
|
||||||
public ActionType? Action { get; init; }
|
public ActionType? Action { get; init; }
|
||||||
public List<ActionType> AvailableActions { get; init; }
|
public ActionSet AvailableActions { get; init; }
|
||||||
public CompletionState SimulationCompletionState { get; init; }
|
public CompletionState SimulationCompletionState { get; init; }
|
||||||
public CompletionState CompletionState =>
|
public CompletionState CompletionState =>
|
||||||
AvailableActions.Count == 0 && SimulationCompletionState == CompletionState.Incomplete ?
|
AvailableActions.IsEmpty && SimulationCompletionState == CompletionState.Incomplete ?
|
||||||
CompletionState.NoMoreActions :
|
CompletionState.NoMoreActions :
|
||||||
SimulationCompletionState;
|
SimulationCompletionState;
|
||||||
|
|
||||||
|
|||||||
@@ -49,14 +49,8 @@ public class Simulator : Sim
|
|||||||
ActionType.BasicTouch,
|
ActionType.BasicTouch,
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/craft_state.rs#L137
|
// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/craft_state.rs#L146
|
||||||
public List<ActionType> AvailableActionsHeuristic(bool strict)
|
private bool CanUseAction(ActionType action, bool strict)
|
||||||
{
|
|
||||||
if (IsComplete)
|
|
||||||
return new();
|
|
||||||
|
|
||||||
ActionUtils.SetSimulation(this);
|
|
||||||
return AcceptedActions.Where(action =>
|
|
||||||
{
|
{
|
||||||
var baseAction = action.WithUnsafe();
|
var baseAction = action.WithUnsafe();
|
||||||
|
|
||||||
@@ -152,6 +146,19 @@ public class Simulator : Sim
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}).ToList();
|
}
|
||||||
|
|
||||||
|
// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/craft_state.rs#L137
|
||||||
|
public ActionSet AvailableActionsHeuristic(bool strict)
|
||||||
|
{
|
||||||
|
if (IsComplete)
|
||||||
|
return new();
|
||||||
|
|
||||||
|
ActionUtils.SetSimulation(this);
|
||||||
|
var ret = new ActionSet();
|
||||||
|
foreach(var action in AcceptedActions)
|
||||||
|
if (CanUseAction(action, strict))
|
||||||
|
ret.SetFlag(action);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,8 +56,9 @@ public class Solver
|
|||||||
if (node.IsComplete)
|
if (node.IsComplete)
|
||||||
return (currentIndex, node.CompletionState);
|
return (currentIndex, node.CompletionState);
|
||||||
|
|
||||||
if (!node.AvailableActions.Remove(action))
|
if (!node.AvailableActions.HasFlag(action))
|
||||||
return (currentIndex, CompletionState.InvalidAction);
|
return (currentIndex, CompletionState.InvalidAction);
|
||||||
|
node.AvailableActions.ClearFlag(action);
|
||||||
|
|
||||||
currentIndex = Tree.Insert(currentIndex, Execute(node.State, action, strict));
|
currentIndex = Tree.Insert(currentIndex, Execute(node.State, action, strict));
|
||||||
}
|
}
|
||||||
@@ -104,7 +105,7 @@ public class Solver
|
|||||||
{
|
{
|
||||||
var selectedNode = Tree.Get(selectedIndex);
|
var selectedNode = Tree.Get(selectedIndex);
|
||||||
|
|
||||||
var expandable = selectedNode.State.AvailableActions.Count != 0;
|
var expandable = !selectedNode.State.AvailableActions.IsEmpty;
|
||||||
var likelyTerminal = selectedNode.Children.Count == 0;
|
var likelyTerminal = selectedNode.Children.Count == 0;
|
||||||
if (expandable || likelyTerminal) {
|
if (expandable || likelyTerminal) {
|
||||||
break;
|
break;
|
||||||
@@ -123,8 +124,9 @@ public class Solver
|
|||||||
if (initialNode.IsComplete)
|
if (initialNode.IsComplete)
|
||||||
return (initialIndex, initialNode.CompletionState, initialNode.CalculateScore() ?? 0);
|
return (initialIndex, initialNode.CompletionState, initialNode.CalculateScore() ?? 0);
|
||||||
|
|
||||||
var randomAction = initialNode.AvailableActions.ElementAt(0);
|
var randomIdx = random.Next(initialNode.AvailableActions.ActionCount);
|
||||||
initialNode.AvailableActions.RemoveAt(0);
|
var randomAction = initialNode.AvailableActions.ActionAt(randomIdx);
|
||||||
|
initialNode.AvailableActions.ClearFlag(randomAction);
|
||||||
var expandedState = Execute(initialNode.State, randomAction, true);
|
var expandedState = Execute(initialNode.State, randomAction, true);
|
||||||
var expandedIndex = Tree.Insert(initialIndex, expandedState);
|
var expandedIndex = Tree.Insert(initialIndex, expandedState);
|
||||||
|
|
||||||
@@ -135,7 +137,8 @@ public class Solver
|
|||||||
{
|
{
|
||||||
if (currentState.IsComplete)
|
if (currentState.IsComplete)
|
||||||
break;
|
break;
|
||||||
randomAction = currentState.AvailableActions.ElementAt(0);
|
randomIdx = random.Next(currentState.AvailableActions.ActionCount);
|
||||||
|
randomAction = currentState.AvailableActions.ActionAt(randomIdx);
|
||||||
currentState = Execute(currentState.State, randomAction, true);
|
currentState = Execute(currentState.State, randomAction, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user