some changes
This commit is contained in:
+69
-11
@@ -1,5 +1,5 @@
|
|||||||
using Craftimizer.Simulator.Actions;
|
using Craftimizer.Simulator.Actions;
|
||||||
using System;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.Intrinsics.X86;
|
using System.Runtime.Intrinsics.X86;
|
||||||
@@ -9,15 +9,38 @@ namespace Craftimizer.Simulator;
|
|||||||
public record ActionSet
|
public record ActionSet
|
||||||
{
|
{
|
||||||
public ulong Bits { get; set; }
|
public ulong Bits { get; set; }
|
||||||
|
public List<ActionType> SavedActions { get; set; } = new();
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
private bool HasFlagA(ActionType action) => (Bits & (1ul << ((int)action + 1))) != 0;
|
||||||
public bool HasFlag(ActionType action) => (Bits & (1ul << ((int)action + 1))) != 0;
|
private bool HasFlagB(ActionType action) => SavedActions.Contains(action);
|
||||||
|
public bool HasFlag(ActionType action)
|
||||||
|
{
|
||||||
|
var a = HasFlagA(action);
|
||||||
|
var b = HasFlagB(action);
|
||||||
|
if (a != b)
|
||||||
|
throw new Exception($"Action {action} has different flags: {a} vs {b}");
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
private void SetFlagA(ActionType action) => Bits |= 1ul << ((int)action + 1);
|
||||||
public void SetFlag(ActionType action) => Bits |= 1ul << ((int)action + 1);
|
private void SetFlagB(ActionType action)
|
||||||
|
{
|
||||||
|
if (!SavedActions.Contains(action))
|
||||||
|
SavedActions.Add(action);
|
||||||
|
}
|
||||||
|
public void SetFlag(ActionType action)
|
||||||
|
{
|
||||||
|
SetFlagA(action);
|
||||||
|
SetFlagB(action);
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
private void ClearFlagA(ActionType action) => Bits &= ~(1ul << ((int)action + 1));
|
||||||
public void ClearFlag(ActionType action) => Bits &= ~(1ul << ((int)action + 1));
|
private void ClearFlagB(ActionType action) => SavedActions.RemoveAll(a => a == action);
|
||||||
|
public void ClearFlag(ActionType action)
|
||||||
|
{
|
||||||
|
ClearFlagA(action);
|
||||||
|
ClearFlagB(action);
|
||||||
|
}
|
||||||
|
|
||||||
public IEnumerable<ActionType> Actions => GetActions();
|
public IEnumerable<ActionType> Actions => GetActions();
|
||||||
|
|
||||||
@@ -60,10 +83,45 @@ public record ActionSet
|
|||||||
return _base;
|
return _base;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
private ActionType ActionAtA(int index) => Actions.ElementAt(index);//(ActionType)(NthBitSet(Bits, index) - 1);
|
||||||
public ActionType ActionAt(int index) => (ActionType)(NthBitSet(Bits, index) - 1);
|
private ActionType ActionAtB(int index) => SavedActions.ElementAt(index);
|
||||||
|
public ActionType ActionAt(int index)
|
||||||
|
{
|
||||||
|
return ActionAtB(index);
|
||||||
|
var a = ActionAtA(index);
|
||||||
|
var a2 = (ActionType)(NthBitSet(Bits, index) - 1);
|
||||||
|
var b = ActionAtB(index);
|
||||||
|
if (a != a2)
|
||||||
|
throw new Exception($"A2: Action {index} has different flags: {a} vs {a2}");
|
||||||
|
if (a != b)
|
||||||
|
throw new Exception($"Action {index} has different flags: {a} vs {b}");
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
public int ActionCount => BitOperations.PopCount(Bits);
|
private int ActionCountA => BitOperations.PopCount(Bits);
|
||||||
|
private int ActionCountB => SavedActions.Count;
|
||||||
|
public int ActionCount { get
|
||||||
|
{
|
||||||
|
return ActionCountB;
|
||||||
|
var a = ActionCountA;
|
||||||
|
var b = ActionCountB;
|
||||||
|
if (a != b)
|
||||||
|
throw new Exception($"Action count has different flags: {a} vs {b}");
|
||||||
|
return a;
|
||||||
|
} }
|
||||||
|
|
||||||
public bool IsEmpty => Bits == 0;
|
private bool IsEmptyA => Bits == 0;
|
||||||
|
private bool IsEmptyB => SavedActions.Count == 0;
|
||||||
|
public bool IsEmpty
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return IsEmptyB;
|
||||||
|
var a = IsEmptyA;
|
||||||
|
var b = IsEmptyB;
|
||||||
|
if (a != b)
|
||||||
|
throw new Exception($"IsEmpty has different flags: {a} vs {b}");
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ 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
|
||||||
|
|||||||
+11
-14
@@ -49,8 +49,14 @@ public class Simulator : Sim
|
|||||||
ActionType.BasicTouch,
|
ActionType.BasicTouch,
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/craft_state.rs#L146
|
// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/craft_state.rs#L137
|
||||||
private bool CanUseAction(ActionType action, bool strict)
|
public ActionSet AvailableActionsHeuristic(bool strict)
|
||||||
|
{
|
||||||
|
if (IsComplete)
|
||||||
|
return new();
|
||||||
|
|
||||||
|
ActionUtils.SetSimulation(this);
|
||||||
|
var a = AcceptedActions.Where(action =>
|
||||||
{
|
{
|
||||||
var baseAction = action.WithUnsafe();
|
var baseAction = action.WithUnsafe();
|
||||||
|
|
||||||
@@ -146,19 +152,10 @@ public class Simulator : Sim
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
});
|
||||||
|
|
||||||
// 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();
|
var ret = new ActionSet();
|
||||||
foreach(var action in AcceptedActions)
|
foreach (var ac in a)
|
||||||
if (CanUseAction(action, strict))
|
ret.SetFlag(ac);
|
||||||
ret.SetFlag(action);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-6
@@ -1,5 +1,6 @@
|
|||||||
using Craftimizer.Simulator;
|
using Craftimizer.Simulator;
|
||||||
using Craftimizer.Simulator.Actions;
|
using Craftimizer.Simulator.Actions;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace Craftimizer.Solver.Crafty;
|
namespace Craftimizer.Solver.Crafty;
|
||||||
|
|
||||||
@@ -107,7 +108,8 @@ public class Solver
|
|||||||
|
|
||||||
var expandable = !selectedNode.State.AvailableActions.IsEmpty;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,7 +126,7 @@ public class Solver
|
|||||||
if (initialNode.IsComplete)
|
if (initialNode.IsComplete)
|
||||||
return (initialIndex, initialNode.CompletionState, initialNode.CalculateScore() ?? 0);
|
return (initialIndex, initialNode.CompletionState, initialNode.CalculateScore() ?? 0);
|
||||||
|
|
||||||
var randomIdx = random.Next(initialNode.AvailableActions.ActionCount);
|
var randomIdx = 0;// random.Next(initialNode.AvailableActions.ActionCount);
|
||||||
var randomAction = initialNode.AvailableActions.ActionAt(randomIdx);
|
var randomAction = initialNode.AvailableActions.ActionAt(randomIdx);
|
||||||
initialNode.AvailableActions.ClearFlag(randomAction);
|
initialNode.AvailableActions.ClearFlag(randomAction);
|
||||||
var expandedState = Execute(initialNode.State, randomAction, true);
|
var expandedState = Execute(initialNode.State, randomAction, true);
|
||||||
@@ -137,7 +139,7 @@ public class Solver
|
|||||||
{
|
{
|
||||||
if (currentState.IsComplete)
|
if (currentState.IsComplete)
|
||||||
break;
|
break;
|
||||||
randomIdx = random.Next(currentState.AvailableActions.ActionCount);
|
randomIdx = 0;// random.Next(currentState.AvailableActions.ActionCount);
|
||||||
randomAction = currentState.AvailableActions.ActionAt(randomIdx);
|
randomAction = currentState.AvailableActions.ActionAt(randomIdx);
|
||||||
currentState = Execute(currentState.State, randomAction, true);
|
currentState = Execute(currentState.State, randomAction, true);
|
||||||
}
|
}
|
||||||
@@ -188,7 +190,8 @@ public class Solver
|
|||||||
{
|
{
|
||||||
var actions = new List<ActionType>();
|
var actions = new List<ActionType>();
|
||||||
var node = Tree.Get(0);
|
var node = Tree.Get(0);
|
||||||
while (node.Children.Count != 0) {
|
while (node.Children.Count != 0)
|
||||||
|
{
|
||||||
var next_index = RustMaxBy(node.Children, n => Tree.Get(n).State.Scores.MaxScore);
|
var next_index = RustMaxBy(node.Children, n => Tree.Get(n).State.Scores.MaxScore);
|
||||||
node = Tree.Get(next_index);
|
node = Tree.Get(next_index);
|
||||||
if (node.State.Action != null)
|
if (node.State.Action != null)
|
||||||
@@ -210,7 +213,8 @@ public class Solver
|
|||||||
public static (List<ActionType> Actions, SimulationState State) SearchStepwise(SimulationInput input, List<ActionType> actions, Action<ActionType>? actionCallback)
|
public static (List<ActionType> Actions, SimulationState State) SearchStepwise(SimulationInput input, List<ActionType> actions, Action<ActionType>? actionCallback)
|
||||||
{
|
{
|
||||||
var (state, result) = Simulate(input, actions);
|
var (state, result) = Simulate(input, actions);
|
||||||
if (result != CompletionState.Incomplete) {
|
if (result != CompletionState.Incomplete)
|
||||||
|
{
|
||||||
return (actions, state);
|
return (actions, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,7 +224,8 @@ public class Solver
|
|||||||
solver.Search(0);
|
solver.Search(0);
|
||||||
var (solution_actions, solution_node) = solver.Solution();
|
var (solution_actions, solution_node) = solver.Solution();
|
||||||
|
|
||||||
if (solution_node.Scores.MaxScore >= 1.0) {
|
if (solution_node.Scores.MaxScore >= 1.0)
|
||||||
|
{
|
||||||
actions.AddRange(solution_actions);
|
actions.AddRange(solution_actions);
|
||||||
return (actions, solution_node.State);
|
return (actions, solution_node.State);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user