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
+1 -4
View File
@@ -620,10 +620,7 @@ internal static class ImGuiUtils
var lineSize = font.CalcWordWrapPositionA(1, textBuf, currentWrapWidth) ?? textBuf.Length;
var lineBuf = textBuf[..lineSize];
ImGui.Text(lineBuf.ToString());
var remainingBuf = textBuf[lineSize..];
while (!remainingBuf.IsEmpty && char.IsWhiteSpace(remainingBuf[0]))
remainingBuf = remainingBuf[1..];
var remainingBuf = textBuf[lineSize..].TrimStart();
if (!remainingBuf.IsEmpty)
{
+1 -2
View File
@@ -843,16 +843,15 @@ public sealed unsafe class RecipeNote : Window, IDisposable
if (state.MacroName is { } macroName)
{
using var _ = ImRaii2.TextWrapPos(panelWidth);
if (state.MacroUrl is { } macroUrl)
{
ImGuiUtils.AlignCentered(ImGui.CalcTextSize(macroName).X, panelWidth);
ImGuiUtils.Hyperlink(macroName, macroUrl, false);
}
else
{
ImGuiUtils.TextCentered(macroName, panelWidth);
}
}
using var table = ImRaii.Table("table", 3, ImGuiTableFlags.BordersInnerV | ImGuiTableFlags.SizingStretchSame);
if (table)
+133
View File
@@ -1,3 +1,5 @@
using Craftimizer.Simulator;
using Craftimizer.Simulator.Actions;
using Craftimizer.Solver;
using Dalamud.Interface;
using Dalamud.Interface.Colors;
@@ -8,8 +10,10 @@ using Dalamud.Interface.Windowing;
using FFXIVClientStructs.FFXIV.Client.UI;
using ImGuiNET;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
namespace Craftimizer.Plugin.Windows;
@@ -558,6 +562,21 @@ public sealed class Settings : Window, IDisposable
);
}
using (var panel = ImRaii2.GroupPanel("Action Pool", -1, out var poolWidth))
{
poolWidth -= ImGui.GetStyle().ItemSpacing.X * 2;
ImGui.Text("Select the actions you want the solver to choose from.");
var pool = config.ActionPool;
DrawActionPool(ref pool, poolWidth, out var isPoolDirty);
if (isPoolDirty)
{
config = config with { ActionPool = pool };
isDirty = true;
}
}
using (var panel = ImRaii2.GroupPanel("Advanced", -1, out _))
{
DrawOption(
@@ -676,6 +695,120 @@ public sealed class Settings : Window, IDisposable
configRef = config;
}
private static void DrawActionPool(ref ActionType[] actionPool, float poolWidth, out bool isDirty)
{
isDirty = false;
var recipeData = Service.Plugin.GetDefaultStats().Recipe;
HashSet<ActionType> pool = new(actionPool);
var imageSize = ImGui.GetFrameHeight() * 2;
var spacing = ImGui.GetStyle().ItemSpacing.Y;
using var _color = ImRaii.PushColor(ImGuiCol.Button, Vector4.Zero);
using var _color3 = ImRaii.PushColor(ImGuiCol.ButtonHovered, Vector4.Zero);
using var _color2 = ImRaii.PushColor(ImGuiCol.ButtonActive, Vector4.Zero);
using var _alpha = ImRaii.PushStyle(ImGuiStyleVar.DisabledAlpha, ImGui.GetStyle().DisabledAlpha * .5f);
foreach (var category in Enum.GetValues<ActionCategory>())
{
if (category == ActionCategory.Combo)
continue;
var actions = category.GetActions();
using var panel = ImRaii2.GroupPanel(category.GetDisplayName(), poolWidth, out var availSpace);
var itemsPerRow = (int)MathF.Floor((availSpace + spacing) / (imageSize + spacing));
var itemCount = actions.Count;
var iterCount = (int)(Math.Ceiling((float)itemCount / itemsPerRow) * itemsPerRow);
for (var i = 0; i < iterCount; i++)
{
if (i % itemsPerRow != 0)
ImGui.SameLine(0, spacing);
if (i < itemCount)
{
var actionBase = actions[i].Base();
var isEnabled = pool.Contains(actions[i]);
var isInefficient = SolverConfig.InefficientActions.Contains(actions[i]);
var isRisky = SolverConfig.RiskyActions.Contains(actions[i]);
var iconTint = Vector4.One;
if (!isEnabled)
iconTint = new(1, 1, 1, ImGui.GetStyle().DisabledAlpha);
else if (isInefficient)
iconTint = new(1, 1f, .5f, 1);
else if (isRisky)
iconTint = new(1, .5f, .5f, 1);
if (ImGui.ImageButton(actions[i].GetIcon(recipeData.ClassJob).ImGuiHandle, new(imageSize), default, Vector2.One, 0, default, iconTint))
{
isDirty = true;
if (isEnabled)
pool.Remove(actions[i]);
else
pool.Add(actions[i]);
}
if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled))
{
var s = new StringBuilder();
s.AppendLine(actions[i].GetName(recipeData.ClassJob));
if (isInefficient)
s.AppendLine(
"Not recommended. This action may be randomly used in a " +
"detrimental way to the rest of the craft. Always use " +
"your best judgement if enabling this action.");
if (isRisky)
s.AppendLine(
"Useless; the solver currently doesn't take any risks in " +
"its crafts. It only takes steps that have a 100% chance of " +
"succeeding. If you want have a moment where you want to take " +
"risks in your craft (like in expert recipes), don't rely " +
"on the solver during that time.");
ImGuiUtils.TooltipWrapped(s.ToString());
}
}
else
ImGui.Dummy(new(imageSize));
}
}
if (isDirty)
{
bool InPool(BaseComboAction action)
{
if (action.ActionTypeA.Base() is BaseComboAction { } aCombo)
{
if (!InPool(aCombo))
return false;
}
else
{
if (!pool.Contains(action.ActionTypeA))
return false;
}
if (action.ActionTypeB.Base() is BaseComboAction { } bCombo)
{
if (!InPool(bCombo))
return false;
}
else
{
if (!pool.Contains(action.ActionTypeB))
return false;
}
return true;
}
foreach(var combo in ActionCategory.Combo.GetActions())
{
if (combo.Base() is BaseComboAction { } comboAction)
{
if (!InPool(comboAction))
pool.Remove(combo);
else
pool.Add(combo);
}
}
actionPool = pool.ToArray();
}
}
private void DrawTabSimulator()
{
using var tab = TabItem("Simulator");
-1
View File
@@ -22,7 +22,6 @@ public static class ActionCategoryUtils
{
SortedActions = new(
Enum.GetValues<ActionType>()
.Where(a => a.Category() != ActionCategory.Combo)
.GroupBy(a => a.Category())
.ToDictionary(g => g.Key, g => g.OrderBy(a => a.Level()).ToArray()));
}
-6
View File
@@ -63,12 +63,6 @@ public static class ActionUtils
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static BaseAction Base(this ActionType me) => Actions[(int)me];
public static IEnumerable<ActionType> AvailableActions(Simulator simulation) =>
simulation.IsComplete
? Enumerable.Empty<ActionType>()
: Enum.GetValues<ActionType>()
.Where(a => a.Base().CanUse(simulation));
public static int Level(this ActionType me) =>
me.Base().Level;
+2
View File
@@ -4,4 +4,6 @@ internal sealed class AdvancedTouchCombo : BaseComboAction<StandardTouchCombo, A
{
public override ActionType ActionTypeA => ActionType.StandardTouchCombo;
public override ActionType ActionTypeB => ActionType.AdvancedTouch;
public override int CPCost(Simulator s) => 18 * 3;
}
+13 -2
View File
@@ -25,8 +25,19 @@ public abstract class BaseAction
public virtual int Efficiency(Simulator s) => 0;
public virtual float SuccessRate(Simulator s) => 1f;
public virtual bool CanUse(Simulator s) =>
s.Input.Stats.Level >= Level && s.CP >= CPCost(s);
// Return true if it can be in the action pool now or in the future
// e.g. if Heart and Soul is already used, it is impossible to use it again
// or if it's a first step action and IsFirstStep is false
public virtual bool IsPossible(Simulator s) =>
s.Input.Stats.Level >= Level;
// Return true if it can be used now
// This already assumes that IsPossible returns true *at some point before*
public virtual bool CouldUse(Simulator s) =>
s.CP >= CPCost(s);
public bool CanUse(Simulator s) =>
IsPossible(s) && CouldUse(s);
public virtual void Use(Simulator s)
{
+2 -2
View File
@@ -7,8 +7,8 @@ public abstract class BaseComboAction : BaseAction
public sealed override ActionCategory Category => ActionCategory.Combo;
protected bool BaseCanUse(Simulator s) =>
base.CanUse(s);
protected bool BaseCouldUse(Simulator s) =>
base.CouldUse(s);
private static bool VerifyDurability2(int durabilityA, int durability, in Effects effects)
{
+4 -2
View File
@@ -13,8 +13,10 @@ internal abstract class BaseComboAction<A, B> : BaseComboAction where A : BaseAc
public override int CPCost(Simulator s) => ActionA.CPCost(s) + ActionB.CPCost(s);
public override bool CanUse(Simulator s) =>
BaseCanUse(s) && VerifyDurability2(s, ActionA.DurabilityCost);
public override bool IsPossible(Simulator s) => ActionA.IsPossible(s) && ActionB.IsPossible(s);
public override bool CouldUse(Simulator s) =>
BaseCouldUse(s) && VerifyDurability2(s, ActionA.DurabilityCost);
public override void Use(Simulator s)
{
+1 -1
View File
@@ -11,7 +11,7 @@ internal sealed class ByregotsBlessing : BaseAction
public override int CPCost(Simulator s) => 24;
public override int Efficiency(Simulator s) => 100 + (20 * s.GetEffectStrength(EffectType.InnerQuiet));
public override bool CanUse(Simulator s) => s.HasEffect(EffectType.InnerQuiet) && base.CanUse(s);
public override bool CouldUse(Simulator s) => s.HasEffect(EffectType.InnerQuiet) && base.CouldUse(s);
public override void UseSuccess(Simulator s)
{
+5 -2
View File
@@ -12,10 +12,13 @@ internal sealed class CarefulObservation : BaseAction
public override int CPCost(Simulator s) => 0;
public override bool CanUse(Simulator s) => s.Input.Stats.IsSpecialist && s.ActionStates.CarefulObservationCount < 3;
public override bool IsPossible(Simulator s) =>
base.IsPossible(s) && s.Input.Stats.IsSpecialist && s.ActionStates.CarefulObservationCount < 3;
public override bool CouldUse(Simulator s) => s.ActionStates.CarefulObservationCount < 3;
public override void UseSuccess(Simulator s) => s.StepCondition();
public override string GetTooltip(Simulator s, bool addUsability) =>
$"{base.GetTooltip(s, addUsability)}Specialist Only";
$"{base.GetTooltip(s, addUsability)}Specialist Only\n";
}
+5 -2
View File
@@ -13,8 +13,11 @@ internal sealed class HeartAndSoul : BaseBuffAction
public override int CPCost(Simulator s) => 0;
public override bool CanUse(Simulator s) => s.Input.Stats.IsSpecialist && !s.ActionStates.UsedHeartAndSoul;
public override bool IsPossible(Simulator s) =>
base.IsPossible(s) && s.Input.Stats.IsSpecialist && !s.ActionStates.UsedHeartAndSoul;
public override bool CouldUse(Simulator s) => !s.ActionStates.UsedHeartAndSoul;
public override string GetTooltip(Simulator s, bool addUsability) =>
$"{GetBaseTooltip(s, addUsability)}Specialist Only";
$"{GetBaseTooltip(s, addUsability)}Specialist Only\n";
}
+2 -2
View File
@@ -11,9 +11,9 @@ internal sealed class IntensiveSynthesis : BaseAction
public override int CPCost(Simulator s) => 6;
public override int Efficiency(Simulator s) => 400;
public override bool CanUse(Simulator s) =>
public override bool CouldUse(Simulator s) =>
(s.Condition == Condition.Good || s.Condition == Condition.Excellent || s.HasEffect(EffectType.HeartAndSoul))
&& base.CanUse(s);
&& base.CouldUse(s);
public override void UseSuccess(Simulator s)
{
+3 -1
View File
@@ -10,7 +10,9 @@ internal sealed class Manipulation : BaseBuffAction
public override byte Duration => 8;
public override int CPCost(Simulator s) => 96;
public override bool CanUse(Simulator s) => s.Input.Stats.CanUseManipulation && base.CanUse(s);
public override bool IsPossible(Simulator s) =>
s.Input.Stats.CanUseManipulation && base.IsPossible(s);
public override void Use(Simulator s)
{
+3 -1
View File
@@ -11,7 +11,9 @@ internal sealed class MuscleMemory : BaseAction
public override int CPCost(Simulator s) => 6;
public override int Efficiency(Simulator s) => 300;
public override bool CanUse(Simulator s) => s.IsFirstStep && base.CanUse(s);
public override bool IsPossible(Simulator s) => s.IsFirstStep && base.IsPossible(s);
public override bool CouldUse(Simulator s) => s.IsFirstStep && base.CouldUse(s);
public override void UseSuccess(Simulator s)
{
+2 -2
View File
@@ -11,9 +11,9 @@ internal sealed class PreciseTouch : BaseAction
public override int CPCost(Simulator s) => 18;
public override int Efficiency(Simulator s) => 150;
public override bool CanUse(Simulator s) =>
public override bool CouldUse(Simulator s) =>
(s.Condition == Condition.Good || s.Condition == Condition.Excellent || s.HasEffect(EffectType.HeartAndSoul))
&& base.CanUse(s);
&& base.CouldUse(s);
public override void UseSuccess(Simulator s)
{
+2 -2
View File
@@ -12,7 +12,7 @@ internal sealed class PrudentSynthesis : BaseAction
public override int CPCost(Simulator s) => 18;
public override int Efficiency(Simulator s) => 180;
public override bool CanUse(Simulator s) =>
public override bool CouldUse(Simulator s) =>
!(s.HasEffect(EffectType.WasteNot) || s.HasEffect(EffectType.WasteNot2))
&& base.CanUse(s);
&& base.CouldUse(s);
}
+2 -2
View File
@@ -12,7 +12,7 @@ internal sealed class PrudentTouch : BaseAction
public override int CPCost(Simulator s) => 25;
public override int Efficiency(Simulator s) => 100;
public override bool CanUse(Simulator s) =>
public override bool CouldUse(Simulator s) =>
!(s.HasEffect(EffectType.WasteNot) || s.HasEffect(EffectType.WasteNot2))
&& base.CanUse(s);
&& base.CouldUse(s);
}
+3 -1
View File
@@ -11,7 +11,9 @@ internal sealed class Reflect : BaseAction
public override int CPCost(Simulator s) => 6;
public override int Efficiency(Simulator s) => 100;
public override bool CanUse(Simulator s) => s.IsFirstStep && base.CanUse(s);
public override bool IsPossible(Simulator s) => s.IsFirstStep && base.IsPossible(s);
public override bool CouldUse(Simulator s) => s.IsFirstStep && base.CouldUse(s);
public override void UseSuccess(Simulator s)
{
+2
View File
@@ -4,4 +4,6 @@ internal sealed class StandardTouchCombo : BaseComboAction<BasicTouch, StandardT
{
public override ActionType ActionTypeA => ActionType.BasicTouch;
public override ActionType ActionTypeB => ActionType.StandardTouch;
public override int CPCost(Simulator s) => 18 * 2;
}
+4 -3
View File
@@ -10,11 +10,12 @@ internal sealed class TrainedEye : BaseAction
public override int CPCost(Simulator s) => 250;
public override bool CanUse(Simulator s) =>
s.IsFirstStep &&
public override bool IsPossible(Simulator s) => s.IsFirstStep &&
!s.Input.Recipe.IsExpert &&
s.Input.Stats.Level >= (s.Input.Recipe.ClassJobLevel + 10) &&
base.CanUse(s);
base.IsPossible(s);
public override bool CouldUse(Simulator s) => s.IsFirstStep && base.CouldUse(s);
public override void UseSuccess(Simulator s) =>
s.IncreaseQualityRaw(s.Input.Recipe.MaxQuality - s.Quality);
+2 -2
View File
@@ -12,7 +12,7 @@ internal sealed class TrainedFinesse : BaseAction
public override int CPCost(Simulator s) => 32;
public override int Efficiency(Simulator s) => 100;
public override bool CanUse(Simulator s) =>
public override bool CouldUse(Simulator s) =>
s.GetEffectStrength(EffectType.InnerQuiet) == 10
&& base.CanUse(s);
&& base.CouldUse(s);
}
+2 -2
View File
@@ -10,9 +10,9 @@ internal sealed class TricksOfTheTrade : BaseAction
public override int CPCost(Simulator s) => 0;
public override bool CanUse(Simulator s) =>
public override bool CouldUse(Simulator s) =>
(s.Condition == Condition.Good || s.Condition == Condition.Excellent || s.HasEffect(EffectType.HeartAndSoul))
&& base.CanUse(s);
&& base.CouldUse(s);
public override void UseSuccess(Simulator s)
{
+28 -12
View File
@@ -1,22 +1,38 @@
namespace Craftimizer.Simulator;
public enum Condition : ushort
public enum Condition : byte
{
Poor = 0x0008,
Normal = 0x0001,
Good = 0x0002,
Excellent = 0x0004,
Normal,
Good,
Excellent,
Poor,
Centered = 0x0010,
Sturdy = 0x0020,
Pliant = 0x0040,
Malleable = 0x0080,
Primed = 0x0100,
GoodOmen = 0x0200,
Centered,
Sturdy,
Pliant,
Malleable,
Primed,
GoodOmen,
}
public static class ConditionUtils
{
[Flags]
private enum ConditionMask : ushort
{
Normal = 1 << 0, // 0x0001
Good = 1 << 1, // 0x0002
Excellent = 1 << 2, // 0x0004
Poor = 1 << 3, // 0x0008
Centered = 1 << 4, // 0x0010
Sturdy = 1 << 5, // 0x0020
Pliant = 1 << 6, // 0x0040
Malleable = 1 << 7, // 0x0080
Primed = 1 << 8, // 0x0100
GoodOmen = 1 << 9, // 0x0200
}
public static Condition[] GetPossibleConditions(ushort conditionsFlag) =>
Enum.GetValues<Condition>().Where(c => ((Condition)conditionsFlag).HasFlag(c)).ToArray();
Enum.GetValues<Condition>().Where(c => ((ConditionMask)conditionsFlag).HasFlag((ConditionMask)(1 << (ushort)c))).ToArray();
}
+10 -1
View File
@@ -35,7 +35,12 @@ public class Simulator
}
public bool IsComplete => CompletionState != CompletionState.Incomplete;
public IEnumerable<ActionType> AvailableActions => ActionUtils.AvailableActions(this);
public SimulationState ExecuteUnchecked(in SimulationState state, ActionType action)
{
this.state = state;
ExecuteUnchecked(action);
return this.state;
}
public (ActionResponse Response, SimulationState NewState) Execute(in SimulationState state, ActionType action)
{
@@ -43,6 +48,10 @@ public class Simulator
return (Execute(action), this.state);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ExecuteUnchecked(ActionType action) =>
action.Base().Use(this);
private ActionResponse Execute(ActionType action)
{
if (IsComplete)
+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,8 +288,11 @@ 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
{
+6 -21
View File
@@ -3,21 +3,6 @@ namespace Craftimizer.Test.Solver;
[TestClass]
public class ActionSetTests
{
[TestMethod]
public void TestAcceptedActions()
{
var actions = ActionSet.AcceptedActions;
var lut = ActionSet.AcceptedActionsLUT;
Assert.IsTrue(actions.Length <= 32);
foreach (var i in Enum.GetValues<ActionType>())
{
var idx = lut[(byte)i];
if (idx != -1)
Assert.AreEqual(i, actions[idx]);
}
}
[TestMethod]
public void TestSize()
{
@@ -87,18 +72,18 @@ public class ActionSetTests
Assert.AreEqual(4, set.Count);
Assert.AreEqual(ActionType.DelicateSynthesis, set.ElementAt(0));
Assert.AreEqual(ActionType.Reflect, set.ElementAt(1));
Assert.AreEqual(ActionType.ByregotsBlessing, set.ElementAt(2));
Assert.AreEqual(ActionType.BasicSynthesis, set.ElementAt(3));
Assert.AreEqual(ActionType.BasicSynthesis, set.ElementAt(0));
Assert.AreEqual(ActionType.ByregotsBlessing, set.ElementAt(1));
Assert.AreEqual(ActionType.DelicateSynthesis, set.ElementAt(2));
Assert.AreEqual(ActionType.Reflect, set.ElementAt(3));
set.RemoveAction(ActionType.Reflect);
Assert.AreEqual(3, set.Count);
Assert.AreEqual(ActionType.DelicateSynthesis, set.ElementAt(0));
Assert.AreEqual(ActionType.BasicSynthesis, set.ElementAt(0));
Assert.AreEqual(ActionType.ByregotsBlessing, set.ElementAt(1));
Assert.AreEqual(ActionType.BasicSynthesis, set.ElementAt(2));
Assert.AreEqual(ActionType.DelicateSynthesis, set.ElementAt(2));
}
[TestMethod]