Big changes 2

- Split into several projects
- All dalamud/lumina deps are in the plugin
- Crafty/craftingway sim implemented!
- optimizations to follow
This commit is contained in:
Asriel Camora
2023-06-17 08:50:46 -07:00
parent 15d416ef2a
commit e190368d62
76 changed files with 1284 additions and 435 deletions
+13
View File
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Simulator\Craftimizer.Simulator.csproj" />
</ItemGroup>
</Project>
+29
View File
@@ -0,0 +1,29 @@
namespace Craftimizer.Solver.Crafty;
public class Arena<T> where T : struct
{
public readonly record struct Node
{
public int? Parent { get; init; }
public int Index { get; init; }
public List<int> Children { get; init; }
public T State { get; init; }
}
public List<Node> Nodes { get; } = new();
public Arena(T initialState = default)
{
Nodes.Add(new() { Parent = null, Index = 0, Children = new(), State = initialState });
}
public int Insert(int parentIndex, T state)
{
var index = Nodes.Count;
Nodes.Add(new() { Parent = parentIndex, Index = index, Children = new(), State = state });
Nodes[parentIndex].Children.Add(index);
return index;
}
public Node Get(int index) => Nodes[index];
}
+20
View File
@@ -0,0 +1,20 @@
using CompState = Craftimizer.Simulator.CompletionState;
namespace Craftimizer.Solver.Crafty;
public enum CompletionState
{
Incomplete,
ProgressComplete,
NoMoreDurability,
InvalidAction,
MaxActionCountReached,
NoMoreActions
}
internal static class CompletionStateUtils
{
public static CompState IntoBase(this CompletionState me) =>
(CompState)me >= CompState.Other ? CompState.Other : (CompState)me;
}
+8
View File
@@ -0,0 +1,8 @@
namespace Craftimizer.Solver.Crafty;
public class NodeScores
{
public float ScoreSum { get; set; } = 0;
public float MaxScore { get; set; } = 0;
public float Visits { get; set; } = 0;
}
+66
View File
@@ -0,0 +1,66 @@
using Craftimizer.Simulator;
using Craftimizer.Simulator.Actions;
namespace Craftimizer.Solver.Crafty;
public readonly record struct SimulationNode
{
public SimulationState State { get; init; }
public ActionType? Action { get; init; }
public List<ActionType> AvailableActions { get; init; }
public CompletionState SimulationCompletionState { get; init; }
public CompletionState CompletionState =>
AvailableActions.Count == 0 && SimulationCompletionState == CompletionState.Incomplete ?
CompletionState.NoMoreActions :
SimulationCompletionState;
public NodeScores Scores { get; init; }
public bool IsComplete => CompletionState != CompletionState.Incomplete;
public float? CalculateScore()
{
if (CompletionState != CompletionState.ProgressComplete)
return null;
static float Apply(float bonus, float value, float target) =>
bonus * Math.Min(1f, value / target);
var progressBonus = 0.20f;
var qualityBonus = 0.65f;
var durabilityBonus = 0.05f;
var cpBonus = 0.05f;
var fewerStepsBonus = 0.05f;
var progressScore = Apply(
progressBonus,
State.Progress,
State.Input.Recipe.MaxProgress
);
var qualityScore = Apply(
qualityBonus,
State.Quality,
State.Input.Recipe.MaxQuality
);
var durabilityScore = Apply(
durabilityBonus,
State.Durability,
State.Input.Recipe.MaxDurability
);
var cpScore = Apply(
cpBonus,
State.CP,
State.Input.Stats.CP
);
var fewerStepsScore =
fewerStepsBonus * (1f - ((float)(State.ActionCount + 1) / Solver.MaxStepCount));
Solver.WriteLine($"score: {progressScore:0.00000} {qualityScore:0.00000} {durabilityScore:0.00000} {cpScore:0.00000} {fewerStepsScore:0.00000}");
return progressScore + qualityScore + durabilityScore + cpScore + fewerStepsScore;
}
}
+161
View File
@@ -0,0 +1,161 @@
using Craftimizer.Simulator;
using Craftimizer.Simulator.Actions;
using Sim = Craftimizer.Simulator.Simulator;
namespace Craftimizer.Solver.Crafty;
public class Simulator : Sim
{
public new CompletionState CompletionState =>
ActionHistory.Count >= Solver.MaxStepCount ?
CompletionState.MaxActionCountReached :
(CompletionState)base.CompletionState;
public override bool IsComplete => CompletionState != CompletionState.Incomplete;
public Simulator(SimulationState state) : base(state)
{
}
// Disable randomization
public override bool RollSuccessRaw(float successRate) => successRate == 1;
public override void StepCondition() { }
private static readonly ActionType[] AcceptedActions = new[]
{
ActionType.TrainedFinesse,
ActionType.PrudentSynthesis,
ActionType.Groundwork,
ActionType.AdvancedTouch,
ActionType.CarefulSynthesis,
ActionType.TrainedEye,
ActionType.DelicateSynthesis,
ActionType.PreparatoryTouch,
ActionType.Reflect,
ActionType.FocusedTouch,
ActionType.FocusedSynthesis,
ActionType.PrudentTouch,
ActionType.Manipulation,
ActionType.MuscleMemory,
ActionType.ByregotsBlessing,
ActionType.WasteNot2,
ActionType.BasicSynthesis,
ActionType.Innovation,
ActionType.GreatStrides,
ActionType.StandardTouch,
ActionType.Veneration,
ActionType.WasteNot,
ActionType.Observe,
ActionType.MastersMend,
ActionType.BasicTouch,
};
// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/craft_state.rs#L137
public List<ActionType> AvailableActionsHeuristic(bool strict)
{
if (IsComplete)
return new();
ActionUtils.SetSimulation(this);
return AcceptedActions.Where(action =>
{
var baseAction = action.WithUnsafe();
if (!baseAction.CanUse)
return false;
if (CalculateSuccessRate(baseAction.SuccessRate) != 1)
return false;
// don't allow quality moves at max quality
if (Quality >= Input.Recipe.MaxQuality && baseAction.IncreasesQuality)
return false;
if (action == ActionType.Observe &&
IsPreviousAction(ActionType.Observe))
return false;
if (action == ActionType.Groundwork &&
Durability < baseAction.DurabilityCost)
return false;
if (action == ActionType.FinalAppraisal)
return false;
if (strict)
{
// always used Trained Eye if it's available
if (action == ActionType.TrainedEye)
return true;
// only allow Focused moves after Observe
if (IsPreviousAction(ActionType.Observe) &&
action != ActionType.FocusedSynthesis &&
action != ActionType.FocusedTouch)
return false;
// don't allow quality moves under Muscle Memory for difficult crafts
if (Input.Recipe.ClassJobLevel == 90 &&
HasEffect(EffectType.MuscleMemory) &&
baseAction.IncreasesQuality)
return false;
// don't allow pure quality moves under Veneration
if (HasEffect(EffectType.Veneration) &&
!baseAction.IncreasesProgress &&
baseAction.IncreasesQuality)
return false;
if (baseAction.IncreasesProgress)
{
var progress_increase = CalculateProgressGain(baseAction.Efficiency);
var would_finish = Progress + progress_increase >= Input.Recipe.MaxProgress;
if (would_finish)
{
// don't allow finishing the craft if there is significant quality remaining
if (Quality < (Input.Recipe.MaxQuality / 5))
return false;
}
else
{
// don't allow pure progress moves under Innovation, if it wouldn't finish the craft
if (HasEffect(EffectType.Innovation) &&
!baseAction.IncreasesQuality &&
baseAction.IncreasesProgress)
return false;
}
}
if (action == ActionType.ByregotsBlessing &&
GetEffect(EffectType.InnerQuiet)?.Strength <= 1)
return false;
if ((action == ActionType.WasteNot || action == ActionType.WasteNot2) &&
(HasEffect(EffectType.WasteNot) || HasEffect(EffectType.WasteNot2)))
return false;
if (action == ActionType.Observe &&
CP < 5)
return false;
if (action == ActionType.MastersMend &&
Input.Recipe.MaxDurability - Durability < 25)
return false;
if (action == ActionType.Manipulation &&
HasEffect(EffectType.Manipulation))
return false;
if (action == ActionType.GreatStrides &&
HasEffect(EffectType.GreatStrides))
return false;
if ((action == ActionType.Veneration || action == ActionType.Innovation) &&
(GetEffect(EffectType.Veneration)?.Duration > 1 || GetEffect(EffectType.Innovation)?.Duration > 1))
return false;
}
return true;
}).ToList();
}
}
+304
View File
@@ -0,0 +1,304 @@
using Craftimizer.Simulator;
using Craftimizer.Simulator.Actions;
namespace Craftimizer.Solver.Crafty;
// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/simulator.rs
public class Solver
{
public Simulator Simulator;
public Arena<SimulationNode> Tree;
//public Random Random => Simulator.Input.Random;
public const int Iterations = 100000;
public const float ScoreStorageThreshold = 1f;
public const float MaxScoreWeightingConstant = 0.1f;
public const float ExplorationConstant = 4f;
public const int MaxStepCount = 25;
public static void Write(string data)
{
if (false)
Console.Write(data);
}
public static void WriteLine(string data)
{
if (false)
Console.WriteLine(data);
}
public Solver(SimulationState state, bool strict)
{
Simulator = new(state);
Tree = new(new()
{
State = state,
Action = null,
SimulationCompletionState = Simulator.CompletionState,
AvailableActions = Simulator.AvailableActionsHeuristic(strict),
Scores = new()
});
}
public Solver(SimulationInput input) : this(new(input), false)
{
}
private SimulationNode Execute(SimulationState state, ActionType action, bool strict)
{
(_, var newState) = Simulator.Execute(state, action);
return new()
{
State = newState,
Action = action,
SimulationCompletionState = Simulator.CompletionState,
AvailableActions = Simulator.AvailableActionsHeuristic(strict),
Scores = new()
};
}
public (int Index, CompletionState State) ExecuteActions(int startIndex, List<ActionType> actions, bool strict = false)
{
var currentIndex = startIndex;
foreach (var action in actions)
{
var node = Tree.Get(currentIndex).State;
if (node.IsComplete)
return (currentIndex, node.CompletionState);
if (!node.AvailableActions.Remove(action))
return (currentIndex, CompletionState.InvalidAction);
currentIndex = Tree.Insert(currentIndex, Execute(node.State, action, strict));
}
var currentNode = Tree.Get(currentIndex).State;
return (currentIndex, currentNode.CompletionState);
}
public static float Eval(NodeScores node, NodeScores parent)
{
var w = MaxScoreWeightingConstant;
var c = ExplorationConstant;
var visits = node.Visits;
var average_score = node.ScoreSum / visits;
var exploitation = ((1f - w) * average_score) + (w * node.MaxScore);
var exploration = MathF.Sqrt(c * MathF.Log(parent.Visits) / visits);
WriteLine($"a {node.ScoreSum} {node.MaxScore}");
WriteLine($"b {exploitation} {exploration}");
return exploitation + exploration;
}
private enum Ordering
{
Less,
Equal,
Greater
}
private static V? RustMaxBy<V, T>(List<V> source, Func<V, T> into)
{
static Func<V, V, Ordering> compare_into(Func<T, T, Ordering> compare, Func<V, T> into) =>
(a, b) => compare(into(a), into(b));
static Func<T, T, Ordering> compare(IComparer<T> comparer) =>
(x, y) => comparer.Compare(x, y) switch
{
< 0 => Ordering.Less,
0 => Ordering.Equal,
> 0 => Ordering.Greater,
};
static Func<V, V, V> max_by_fold(Func<V, V, Ordering> compare) =>
(x, y) => compare(x, y) switch
{
Ordering.Less or Ordering.Equal => y,
Ordering.Greater => x,
_ => x
};
static V? reduce(List<V> d, Func<V, V, V> f)
{
V? accum = default!;
for (var i = 0; i < d.Count; ++i)
accum = i == 0 ? d[i] : f(accum, d[i]);
return accum;
}
var comparer = compare_into(compare(Comparer<T>.Default), into);
return reduce(source, max_by_fold(comparer));
}
public int Select(int currentIndex)
{
var selectedIndex = currentIndex;
while (true)
{
var selectedNode = Tree.Get(selectedIndex);
var expandable = selectedNode.State.AvailableActions.Count != 0;
var likelyTerminal = selectedNode.Children.Count == 0;
WriteLine("select:");
WriteLine($"{expandable} {likelyTerminal}".ToLower());
if (expandable || likelyTerminal) {
break;
}
// select the node with the highest score
selectedIndex = RustMaxBy(selectedNode.Children, n => Eval(Tree.Get(n).State.Scores, selectedNode.State.Scores));
WriteLine($"{selectedIndex}");
}
return selectedIndex;
}
public (int Index, CompletionState State, float Score) ExpandAndRollout(int initialIndex)
{
WriteLine("expand_and_rollout");
WriteLine($"{initialIndex}");
// expand once
var initialNode = Tree.Get(initialIndex).State;
if (initialNode.IsComplete)
{
WriteLine($"ret {initialIndex} {initialNode.CompletionState}");
return (initialIndex, initialNode.CompletionState, initialNode.CalculateScore() ?? 0);
}
var randomAction = initialNode.AvailableActions.ElementAt(0);
initialNode.AvailableActions.Remove(randomAction);
WriteLine($"pick {randomAction.IntName()}");
var expandedState = Execute(initialNode.State, randomAction, true);
var expandedIndex = Tree.Insert(initialIndex, expandedState);
WriteLine($"ins {expandedIndex}");
// playout to a terminal state
var currentState = Tree.Get(expandedIndex).State;
var preCount = currentState.State.ActionCount;
while (true)
{
if (currentState.IsComplete)
break;
randomAction = currentState.AvailableActions.ElementAt(0);
currentState = Execute(currentState.State, randomAction, true);
}
// store the result if a max score was reached
var score = currentState.CalculateScore() ?? 0;
if (currentState.CompletionState == CompletionState.ProgressComplete)
{
WriteLine($"calc: {score:0.00000}");
if (score >= ScoreStorageThreshold && score >= Tree.Get(0).State.Scores.MaxScore)
{
WriteLine("exp_a");
foreach (var action in currentState.State.ActionHistory.Skip(preCount))
Write($">{action.IntName()}");
WriteLine("");
(var terminalIndex, _) = ExecuteActions(expandedIndex, currentState.State.ActionHistory.Skip(preCount).ToList(), true);
return (terminalIndex, currentState.CompletionState, score);
}
}
return (expandedIndex, currentState.CompletionState, score);
}
public void Backpropagate(int startIndex, int targetIndex, float score)
{
WriteLine($"back {startIndex}->{targetIndex} {score}");
var currentIndex = startIndex;
while (true)
{
var currentNode = Tree.Get(currentIndex);
var currentScores = currentNode.State.Scores;
currentScores.Visits++;
currentScores.ScoreSum += score;
currentScores.MaxScore = Math.Max(currentScores.MaxScore, score);
WriteLine($"bak {currentIndex} {currentScores.Visits} {currentScores.ScoreSum} {currentScores.MaxScore}");
if (currentIndex == targetIndex)
break;
currentIndex = currentNode.Parent!.Value;
}
}
public void Search(int startIndex)
{
for (var i = 0; i < Iterations; i++)
{
WriteLine($"search {i}");
var selectedIndex = Select(startIndex);
var (endIndex, state, score) = ExpandAndRollout(selectedIndex);
WriteLine($"backp {endIndex} {score}");
Backpropagate(endIndex, startIndex, score);
}
}
public (List<ActionType> Actions, SimulationNode Node) Solution()
{
WriteLine("sol");
var actions = new List<ActionType>();
var node = Tree.Get(0);
while (node.Children.Count != 0) {
var next_index = RustMaxBy(node.Children, n => Tree.Get(n).State.Scores.MaxScore);
WriteLine($"next: {next_index}");
node = Tree.Get(next_index);
if (node.State.Action != null)
{
WriteLine($"act: {node.State.Action.Value.IntName()}");
actions.Add(node.State.Action.Value);
}
}
return (actions, node.State);
}
public static (SimulationState SimState, CompletionState State) Simulate(SimulationInput input, List<ActionType> actions)
{
var solver = new Solver(input);
var (index, result) = solver.ExecuteActions(0, actions);
return (solver.Tree.Get(index).State.State, result);
}
public static (List<ActionType> Actions, SimulationState State) SearchStepwise(SimulationInput input, List<ActionType> actions, Action<ActionType>? actionCallback)
{
var (state, result) = Simulate(input, actions);
if (result != CompletionState.Incomplete) {
return (actions, state);
}
var solver = new Solver(state, true);
while (!solver.Simulator.IsComplete)
{
solver.Search(0);
var (solution_actions, solution_node) = solver.Solution();
if (solution_node.Scores.MaxScore >= 1.0) {
actions.AddRange(solution_actions);
return (actions, solution_node.State);
}
var chosen_action = solution_actions[0];
(_, state) = solver.Simulator.Execute(state, chosen_action);
actions.Add(chosen_action);
actionCallback?.Invoke(chosen_action);
solver = new Solver(state, true);
}
return (actions, state);
}
public static (List<ActionType> Actions, SimulationState State) SearchOneshot(SimulationInput input, List<ActionType> actions)
{
var solver = new Solver(input);
solver.Search(0);
var (solution_actions, solution_node) = solver.Solution();
actions.AddRange(solution_actions);
return (actions, solution_node.State);
}
}