fix to be identical to crafty, remove debugs

This commit is contained in:
Asriel Camora
2023-06-18 03:50:48 -07:00
parent e190368d62
commit bae48844b5
23 changed files with 203 additions and 148 deletions
+8
View File
@@ -27,6 +27,14 @@ internal class Program
}; };
var actions = new List<ActionType>(); var actions = new List<ActionType>();
if (true)
(actions, _) = Solver.Crafty.Solver.SearchStepwise(input, actions, a => Console.WriteLine(a)); (actions, _) = Solver.Crafty.Solver.SearchStepwise(input, actions, a => Console.WriteLine(a));
else
{
(actions, _) = Solver.Crafty.Solver.SearchOneshot(input, actions);
foreach (var action in actions)
Console.Write($">{action.IntName()}");
Console.WriteLine();
}
} }
} }
+12 -12
View File
@@ -138,27 +138,27 @@ internal static class EffectExtensions
public static Status Status(this EffectType me) => public static Status Status(this EffectType me) =>
LuminaSheets.StatusSheet.GetRow(me.StatusId())!; LuminaSheets.StatusSheet.GetRow(me.StatusId())!;
public static ushort GetIconId(this Effect me) public static ushort GetIconId(this EffectType me, int strength)
{ {
var status = me.Type.Status(); var status = me.Status();
uint iconId = status.Icon; uint iconId = status.Icon;
if (status.MaxStacks != 0 && me.Strength != null) if (status.MaxStacks != 0)
iconId += (uint)Math.Clamp(me.Strength!.Value, 1, status.MaxStacks) - 1; iconId += (uint)Math.Clamp(strength, 1, status.MaxStacks) - 1;
return (ushort)iconId; return (ushort)iconId;
} }
public static TextureWrap GetIcon(this Effect me) => public static TextureWrap GetIcon(this EffectType me, int strength) =>
Icons.GetIconFromId(me.GetIconId()); Icons.GetIconFromId(me.GetIconId(strength));
public static string GetTooltip(this Effect me) public static string GetTooltip(this EffectType me, int strength, int duration)
{ {
var status = me.Type.Status(); var status = me.Status();
var name = new StringBuilder(); var name = new StringBuilder();
name.Append(status.Name.ToDalamudString().TextValue); name.Append(status.Name.ToDalamudString().TextValue);
if (status.MaxStacks != 0 && me.Strength != null) if (status.MaxStacks != 0)
name.Append($" {me.Strength}"); name.Append($" {strength}");
if (!status.IsPermanent && me.Duration != null) if (!status.IsPermanent)
name.Append($" > {me.Duration}"); name.Append($" > {duration}");
return name.ToString(); return name.ToString();
} }
} }
+5 -3
View File
@@ -109,14 +109,16 @@ public class SimulatorWindow : Window
ImGui.PopStyleColor(); ImGui.PopStyleColor();
ImGuiHelpers.ScaledDummy(5); ImGuiHelpers.ScaledDummy(5);
ImGui.Text($"Effects:"); ImGui.Text($"Effects:");
foreach (var effect in State.ActiveEffects) foreach (var effect in Enum.GetValues<EffectType>())
{ {
var icon = effect.GetIcon(); var strength = Simulation.GetEffectStrength(effect);
var duration = Simulation.GetEffectDuration(effect);
var icon = effect.GetIcon(strength);
var h = ImGui.GetFontSize() * 1.25f; var h = ImGui.GetFontSize() * 1.25f;
var w = icon.Width * h / icon.Height; var w = icon.Width * h / icon.Height;
ImGui.Image(icon.ImGuiHandle, new Vector2(w, h)); ImGui.Image(icon.ImGuiHandle, new Vector2(w, h));
ImGui.SameLine(); ImGui.SameLine();
ImGui.Text(effect.GetTooltip()); ImGui.Text(effect.GetTooltip(strength, duration));
} }
ImGuiHelpers.ScaledDummy(5); ImGuiHelpers.ScaledDummy(5);
{ {
+6 -3
View File
@@ -32,14 +32,17 @@ public abstract class BaseAction
public virtual void Use() public virtual void Use()
{ {
if (Simulation.RollSuccess(SuccessRate))
UseSuccess();
Simulation.ReduceCP(CPCost); Simulation.ReduceCP(CPCost);
Simulation.ReduceDurability(DurabilityCost); Simulation.ReduceDurability(DurabilityCost);
if (Simulation.Durability > 0)
{
if (Simulation.HasEffect(EffectType.Manipulation)) if (Simulation.HasEffect(EffectType.Manipulation))
Simulation.RestoreDurability(5); Simulation.RestoreDurability(5);
}
if (Simulation.RollSuccess(SuccessRate))
UseSuccess();
if (IncreasesStepCount) if (IncreasesStepCount)
Simulation.IncreaseStepCount(); Simulation.IncreaseStepCount();
+4 -3
View File
@@ -4,7 +4,8 @@ namespace Craftimizer.Simulator.Actions;
internal abstract class BaseBuffAction : BaseAction internal abstract class BaseBuffAction : BaseAction
{ {
public abstract Effect Effect { get; } public abstract EffectType Effect { get; }
public virtual byte Duration => 1;
public virtual EffectType[] ConflictingEffects => Array.Empty<EffectType>(); public virtual EffectType[] ConflictingEffects => Array.Empty<EffectType>();
public override int DurabilityCost => 0; public override int DurabilityCost => 0;
@@ -14,13 +15,13 @@ internal abstract class BaseBuffAction : BaseAction
if (ConflictingEffects.Length != 0) if (ConflictingEffects.Length != 0)
foreach(var effect in ConflictingEffects) foreach(var effect in ConflictingEffects)
Simulation.RemoveEffect(effect); Simulation.RemoveEffect(effect);
Simulation.AddEffect(Effect.Type, Effect.Duration, Effect.Strength); Simulation.AddEffect(Effect, Duration);
} }
public override string GetTooltip(bool addUsability) public override string GetTooltip(bool addUsability)
{ {
var builder = new StringBuilder(base.GetTooltip(addUsability)); var builder = new StringBuilder(base.GetTooltip(addUsability));
builder.AppendLine($"{Effect.Duration} Steps"); builder.AppendLine($"{Duration} Steps");
return builder.ToString(); return builder.ToString();
} }
} }
+1 -1
View File
@@ -7,7 +7,7 @@ internal class ByregotsBlessing : BaseAction
public override uint ActionId => 100339; public override uint ActionId => 100339;
public override int CPCost => 24; public override int CPCost => 24;
public override float Efficiency => 1.00f + (0.20f * (Simulation.GetEffect(EffectType.InnerQuiet)?.Strength ?? 0)); public override float Efficiency => 1.00f + (0.20f * Simulation.GetEffectStrength(EffectType.InnerQuiet));
public override bool IncreasesQuality => true; public override bool IncreasesQuality => true;
public override bool CanUse => Simulation.HasEffect(EffectType.InnerQuiet) && base.CanUse; public override bool CanUse => Simulation.HasEffect(EffectType.InnerQuiet) && base.CanUse;
+2 -1
View File
@@ -9,5 +9,6 @@ internal class FinalAppraisal : BaseBuffAction
public override int CPCost => 1; public override int CPCost => 1;
public override bool IncreasesStepCount => false; public override bool IncreasesStepCount => false;
public override Effect Effect => new() { Type = EffectType.FinalAppraisal, Duration = 5 }; public override EffectType Effect => EffectType.FinalAppraisal;
public override byte Duration => 5;
} }
+2 -1
View File
@@ -8,5 +8,6 @@ internal class GreatStrides : BaseBuffAction
public override int CPCost => 32; public override int CPCost => 32;
public override Effect Effect => new() { Type = EffectType.GreatStrides, Duration = 3 }; public override EffectType Effect => EffectType.GreatStrides;
public override byte Duration => 3;
} }
+1 -2
View File
@@ -13,8 +13,7 @@ internal class Groundwork : BaseAction
get get
{ {
var ret = Simulation.Input.Stats.Level >= 86 ? 3.60f : 3.00f; var ret = Simulation.Input.Stats.Level >= 86 ? 3.60f : 3.00f;
// TODO: does not account for waste not return Simulation.Durability < Simulation.CalculateDurabilityCost(DurabilityCost) ? ret / 2 : ret;
return Simulation.Durability < DurabilityCost ? ret / 2 : ret;
} }
} }
public override bool IncreasesProgress => true; public override bool IncreasesProgress => true;
+1 -1
View File
@@ -9,7 +9,7 @@ internal class HeartAndSoul : BaseBuffAction
public override int CPCost => 0; public override int CPCost => 0;
public override bool IncreasesStepCount => false; public override bool IncreasesStepCount => false;
public override Effect Effect => new() { Type = EffectType.HeartAndSoul }; public override EffectType Effect => EffectType.HeartAndSoul;
public override bool CanUse => Simulation.Input.Stats.IsSpecialist && Simulation.CountPreviousAction(ActionType.HeartAndSoul) == 0; public override bool CanUse => Simulation.Input.Stats.IsSpecialist && Simulation.CountPreviousAction(ActionType.HeartAndSoul) == 0;
} }
+2 -1
View File
@@ -8,5 +8,6 @@ internal class Innovation : BaseBuffAction
public override int CPCost => 18; public override int CPCost => 18;
public override Effect Effect => new() { Type = EffectType.Innovation, Duration = 4 }; public override EffectType Effect => EffectType.Innovation;
public override byte Duration => 4;
} }
+15 -1
View File
@@ -8,5 +8,19 @@ internal class Manipulation : BaseBuffAction
public override int CPCost => 96; public override int CPCost => 96;
public override Effect Effect => new() { Type = EffectType.Manipulation, Duration = 8 }; public override EffectType Effect => EffectType.Manipulation;
public override byte Duration => 8;
public override void Use()
{
if (Simulation.HasEffect(EffectType.Manipulation))
Simulation.RestoreDurability(5);
Simulation.ReduceCP(CPCost);
Simulation.ReduceDurability(DurabilityCost);
UseSuccess();
Simulation.IncreaseStepCount();
}
} }
+1 -1
View File
@@ -12,6 +12,6 @@ internal class TrainedFinesse : BaseAction
public override int DurabilityCost => 0; public override int DurabilityCost => 0;
public override bool CanUse => public override bool CanUse =>
(Simulation.GetEffect(EffectType.InnerQuiet)?.Strength ?? 0) == 10 Simulation.GetEffectStrength(EffectType.InnerQuiet) == 10
&& base.CanUse; && base.CanUse;
} }
+2 -1
View File
@@ -9,5 +9,6 @@ internal class Veneration : BaseBuffAction
public override int CPCost => 18; public override int CPCost => 18;
public override int DurabilityCost => 0; public override int DurabilityCost => 0;
public override Effect Effect => new() { Type = EffectType.Veneration, Duration = 4 }; public override EffectType Effect => EffectType.Veneration;
public override byte Duration => 4;
} }
+2 -1
View File
@@ -8,6 +8,7 @@ internal class WasteNot : BaseBuffAction
public override int CPCost => 56; public override int CPCost => 56;
public override Effect Effect => new() { Type = EffectType.WasteNot, Duration = 4 }; public override EffectType Effect => EffectType.WasteNot;
public override byte Duration => 4;
public override EffectType[] ConflictingEffects => new[] { EffectType.WasteNot2 }; public override EffectType[] ConflictingEffects => new[] { EffectType.WasteNot2 };
} }
+2 -1
View File
@@ -8,6 +8,7 @@ internal class WasteNot2 : BaseBuffAction
public override int CPCost => 98; public override int CPCost => 98;
public override Effect Effect => new() { Type = EffectType.WasteNot2, Duration = 8 }; public override EffectType Effect => EffectType.WasteNot2;
public override byte Duration => 8;
public override EffectType[] ConflictingEffects => new[] { EffectType.WasteNot }; public override EffectType[] ConflictingEffects => new[] { EffectType.WasteNot };
} }
-14
View File
@@ -1,14 +0,0 @@
namespace Craftimizer.Simulator;
public readonly record struct Effect
{
public EffectType Type { get; init; }
public int? Duration { get; init; }
public int? Strength { get; init; }
public bool HasDuration => Duration != null;
public bool HasStrength => Strength != null;
public Effect DecrementDuration() => this with { Duration = Duration - 1 };
public Effect IncrementStrength() => this with { Strength = Strength + 1 };
}
+102
View File
@@ -0,0 +1,102 @@
namespace Craftimizer.Simulator;
public record struct Effects
{
public byte InnerQuiet { get; set; }
public byte WasteNot { get; set; }
public byte Veneration { get; set; }
public byte GreatStrides { get; set; }
public byte Innovation { get; set; }
public byte FinalAppraisal { get; set; }
public byte WasteNot2 { get; set; }
public byte MuscleMemory { get; set; }
public byte Manipulation { get; set; }
public bool HeartAndSoul { get; set; }
public void SetDuration(EffectType effect, byte duration)
{
switch (effect)
{
case EffectType.InnerQuiet:
if (duration == 0)
InnerQuiet = 0;
break;
case EffectType.WasteNot:
WasteNot = duration;
break;
case EffectType.Veneration:
Veneration = duration;
break;
case EffectType.GreatStrides:
GreatStrides = duration;
break;
case EffectType.Innovation:
Innovation = duration;
break;
case EffectType.FinalAppraisal:
FinalAppraisal = duration;
break;
case EffectType.WasteNot2:
WasteNot2 = duration;
break;
case EffectType.MuscleMemory:
MuscleMemory = duration;
break;
case EffectType.Manipulation:
Manipulation = duration;
break;
case EffectType.HeartAndSoul:
HeartAndSoul = duration != 0;
break;
}
}
public void Strengthen(EffectType effect)
{
if (effect == EffectType.InnerQuiet && InnerQuiet < 10)
InnerQuiet++;
}
public byte GetDuration(EffectType effect) =>
effect switch
{
EffectType.InnerQuiet => (byte)(InnerQuiet != 0 ? 1 : 0),
EffectType.WasteNot => WasteNot,
EffectType.Veneration => Veneration,
EffectType.GreatStrides => GreatStrides,
EffectType.Innovation => Innovation,
EffectType.FinalAppraisal => FinalAppraisal,
EffectType.WasteNot2 => WasteNot2,
EffectType.MuscleMemory => MuscleMemory,
EffectType.Manipulation => Manipulation,
EffectType.HeartAndSoul => (byte)(HeartAndSoul ? 1 : 0),
_ => 0
};
public byte GetStrength(EffectType effect) =>
effect == EffectType.InnerQuiet ? InnerQuiet :
(byte)(GetDuration(effect) != 0 ? 1 : 0);
public bool HasEffect(EffectType effect) =>
GetDuration(effect) != 0;
public void DecrementDuration()
{
if (WasteNot > 0)
WasteNot--;
if (WasteNot2 > 0)
WasteNot2--;
if (Veneration > 0)
Veneration--;
if (GreatStrides > 0)
GreatStrides--;
if (Innovation > 0)
Innovation--;
if (FinalAppraisal > 0)
FinalAppraisal--;
if (MuscleMemory > 0)
MuscleMemory--;
if (Manipulation > 0)
Manipulation--;
}
}
+1 -3
View File
@@ -1,6 +1,4 @@
using Craftimizer.Simulator.Actions; using Craftimizer.Simulator.Actions;
using System;
using System.Collections.Generic;
namespace Craftimizer.Simulator; namespace Craftimizer.Simulator;
@@ -16,7 +14,7 @@ public readonly record struct SimulationState
public int Durability { get; init; } public int Durability { get; init; }
public int CP { get; init; } public int CP { get; init; }
public Condition Condition { get; init; } public Condition Condition { get; init; }
public List<Effect> ActiveEffects { get; init; } public Effects ActiveEffects { get; init; }
public List<ActionType> ActionHistory { get; init; } public List<ActionType> ActionHistory { get; init; }
// https://github.com/ffxiv-teamcraft/simulator/blob/0682dfa76043ff4ccb38832c184d046ceaff0733/src/model/tables.ts#L2 // https://github.com/ffxiv-teamcraft/simulator/blob/0682dfa76043ff4ccb38832c184d046ceaff0733/src/model/tables.ts#L2
+16 -48
View File
@@ -11,7 +11,7 @@ public class Simulator
public int Durability { get; private set; } public int Durability { get; private set; }
public int CP { get; private set; } public int CP { get; private set; }
public Condition Condition { get; private set; } public Condition Condition { get; private set; }
public List<Effect> ActiveEffects { get; private set; } public Effects ActiveEffects;
public List<ActionType> ActionHistory { get; private set; } public List<ActionType> ActionHistory { get; private set; }
public bool IsFirstStep => StepCount == 0; public bool IsFirstStep => StepCount == 0;
@@ -47,7 +47,7 @@ public class Simulator
Durability = state.Durability; Durability = state.Durability;
CP = state.CP; CP = state.CP;
Condition = state.Condition; Condition = state.Condition;
ActiveEffects = new(state.ActiveEffects); ActiveEffects = state.ActiveEffects;
ActionHistory = new(state.ActionHistory); ActionHistory = new(state.ActionHistory);
} }
@@ -60,7 +60,7 @@ public class Simulator
Durability = Durability, Durability = Durability,
CP = CP, CP = CP,
Condition = Condition, Condition = Condition,
ActiveEffects = ActiveEffects!, ActiveEffects = ActiveEffects,
ActionHistory = ActionHistory!, ActionHistory = ActionHistory!,
}; };
@@ -88,68 +88,36 @@ public class Simulator
baseAction.Use(); baseAction.Use();
ActionHistory!.Add(action); ActionHistory!.Add(action);
for (var i = 0; i < ActiveEffects!.Count; ++i) ActiveEffects.DecrementDuration();
{
var effect = ActiveEffects[i].DecrementDuration();
if (effect.Duration == 0)
{
ActiveEffects.RemoveAt(i);
--i;
}
else
ActiveEffects[i] = effect;
}
return ActionResponse.UsedAction; return ActionResponse.UsedAction;
} }
private int GetEffectIdx(EffectType effect) => public int GetEffectStrength(EffectType effect) =>
ActiveEffects!.FindIndex(e => e.Type == effect); ActiveEffects.GetStrength(effect);
public Effect? GetEffect(EffectType effect) public int GetEffectDuration(EffectType effect) =>
{ ActiveEffects.GetDuration(effect);
var idx = GetEffectIdx(effect);
return idx == -1 ? null : ActiveEffects![idx];
}
public void AddEffect(EffectType effect, int? duration = null, int? strength = null) public void AddEffect(EffectType effect, int duration)
{ {
if (Condition == Condition.Primed && duration != null) if (Condition == Condition.Primed)
duration += 2; duration += 2;
// Duration will be decreased in the next step, so we need to add 1 // Duration will be decreased in the next step, so we need to add 1
if (duration != null)
duration++; duration++;
var newEffect = new Effect { Type = effect, Duration = duration, Strength = strength }; ActiveEffects.SetDuration(effect, (byte)duration);
var effectIdx = GetEffectIdx(effect);
if (effectIdx != -1)
ActiveEffects![effectIdx] = newEffect;
else
ActiveEffects!.Add(newEffect);
} }
public void StrengthenEffect(EffectType effect, int? duration = null) public void StrengthenEffect(EffectType effect) =>
{ ActiveEffects.Strengthen(effect);
if (duration != null)
duration += 1;
var effectIdx = GetEffectIdx(effect);
if (effectIdx != -1)
{
if (effect == EffectType.InnerQuiet && ActiveEffects![effectIdx].Strength < 10)
ActiveEffects[effectIdx] = ActiveEffects[effectIdx].IncrementStrength();
}
else
ActiveEffects!.Add(new Effect { Type = effect, Duration = duration, Strength = 1 });
}
public void RemoveEffect(EffectType effect) => public void RemoveEffect(EffectType effect) =>
ActiveEffects!.RemoveAll(e => e.Type == effect); ActiveEffects.SetDuration(effect, 0);
public bool HasEffect(EffectType effect) => public bool HasEffect(EffectType effect) =>
ActiveEffects!.Any(e => e.Type == effect); ActiveEffects.HasEffect(effect);
public bool IsPreviousAction(ActionType action, int stepsBack = 1) => public bool IsPreviousAction(ActionType action, int stepsBack = 1) =>
ActionHistory!.Count >= stepsBack && ActionHistory[^stepsBack] == action; ActionHistory!.Count >= stepsBack && ActionHistory[^stepsBack] == action;
@@ -287,7 +255,7 @@ public class Simulator
if (HasEffect(EffectType.Innovation)) if (HasEffect(EffectType.Innovation))
buffModifier += 0.50f; buffModifier += 0.50f;
buffModifier *= 1 + ((GetEffect(EffectType.InnerQuiet)?.Strength ?? 0) * 0.10f); buffModifier *= 1 + (GetEffectStrength(EffectType.InnerQuiet) * 0.10f);
var conditionModifier = Condition switch var conditionModifier = Condition switch
{ {
-2
View File
@@ -59,8 +59,6 @@ public readonly record struct SimulationNode
var fewerStepsScore = var fewerStepsScore =
fewerStepsBonus * (1f - ((float)(State.ActionCount + 1) / Solver.MaxStepCount)); 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; return progressScore + qualityScore + durabilityScore + cpScore + fewerStepsScore;
} }
} }
+10 -4
View File
@@ -7,7 +7,7 @@ namespace Craftimizer.Solver.Crafty;
public class Simulator : Sim public class Simulator : Sim
{ {
public new CompletionState CompletionState => public new CompletionState CompletionState =>
ActionHistory.Count >= Solver.MaxStepCount ? (ActionHistory.Count + 1) >= Solver.MaxStepCount ?
CompletionState.MaxActionCountReached : CompletionState.MaxActionCountReached :
(CompletionState)base.CompletionState; (CompletionState)base.CompletionState;
public override bool IsComplete => CompletionState != CompletionState.Incomplete; public override bool IsComplete => CompletionState != CompletionState.Incomplete;
@@ -63,6 +63,12 @@ public class Simulator : Sim
if (!baseAction.CanUse) if (!baseAction.CanUse)
return false; return false;
if (action == ActionType.StandardTouch && CP < 32)
return false;
if (action == ActionType.AdvancedTouch && CP < 46)
return false;
if (CalculateSuccessRate(baseAction.SuccessRate) != 1) if (CalculateSuccessRate(baseAction.SuccessRate) != 1)
return false; return false;
@@ -75,7 +81,7 @@ public class Simulator : Sim
return false; return false;
if (action == ActionType.Groundwork && if (action == ActionType.Groundwork &&
Durability < baseAction.DurabilityCost) Durability < CalculateDurabilityCost(baseAction.DurabilityCost))
return false; return false;
if (action == ActionType.FinalAppraisal) if (action == ActionType.FinalAppraisal)
@@ -127,7 +133,7 @@ public class Simulator : Sim
} }
if (action == ActionType.ByregotsBlessing && if (action == ActionType.ByregotsBlessing &&
GetEffect(EffectType.InnerQuiet)?.Strength <= 1) GetEffectStrength(EffectType.InnerQuiet) <= 1)
return false; return false;
if ((action == ActionType.WasteNot || action == ActionType.WasteNot2) && if ((action == ActionType.WasteNot || action == ActionType.WasteNot2) &&
@@ -151,7 +157,7 @@ public class Simulator : Sim
return false; return false;
if ((action == ActionType.Veneration || action == ActionType.Innovation) && if ((action == ActionType.Veneration || action == ActionType.Innovation) &&
(GetEffect(EffectType.Veneration)?.Duration > 1 || GetEffect(EffectType.Innovation)?.Duration > 1)) (GetEffectDuration(EffectType.Veneration) > 1 || GetEffectDuration(EffectType.Innovation) > 1))
return false; return false;
} }
+4 -40
View File
@@ -11,23 +11,12 @@ public class Solver
//public Random Random => Simulator.Input.Random; //public Random Random => Simulator.Input.Random;
public const int Iterations = 100000; public const int Iterations = 1000;
public const float ScoreStorageThreshold = 1f; public const float ScoreStorageThreshold = 1f;
public const float MaxScoreWeightingConstant = 0.1f; public const float MaxScoreWeightingConstant = 0.1f;
public const float ExplorationConstant = 4f; public const float ExplorationConstant = 4f;
public const int MaxStepCount = 25; 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) public Solver(SimulationState state, bool strict)
{ {
Simulator = new(state); Simulator = new(state);
@@ -88,9 +77,6 @@ public class Solver
var exploitation = ((1f - w) * average_score) + (w * node.MaxScore); var exploitation = ((1f - w) * average_score) + (w * node.MaxScore);
var exploration = MathF.Sqrt(c * MathF.Log(parent.Visits) / visits); var exploration = MathF.Sqrt(c * MathF.Log(parent.Visits) / visits);
WriteLine($"a {node.ScoreSum} {node.MaxScore}");
WriteLine($"b {exploitation} {exploration}");
return exploitation + exploration; return exploitation + exploration;
} }
@@ -143,36 +129,27 @@ public class Solver
var expandable = selectedNode.State.AvailableActions.Count != 0; var expandable = selectedNode.State.AvailableActions.Count != 0;
var likelyTerminal = selectedNode.Children.Count == 0; var likelyTerminal = selectedNode.Children.Count == 0;
WriteLine("select:");
WriteLine($"{expandable} {likelyTerminal}".ToLower());
if (expandable || likelyTerminal) { if (expandable || likelyTerminal) {
break; break;
} }
// select the node with the highest score // select the node with the highest score
selectedIndex = RustMaxBy(selectedNode.Children, n => Eval(Tree.Get(n).State.Scores, selectedNode.State.Scores)); selectedIndex = RustMaxBy(selectedNode.Children, n => Eval(Tree.Get(n).State.Scores, selectedNode.State.Scores));
WriteLine($"{selectedIndex}");
} }
return selectedIndex; return selectedIndex;
} }
public (int Index, CompletionState State, float Score) ExpandAndRollout(int initialIndex) public (int Index, CompletionState State, float Score) ExpandAndRollout(int initialIndex)
{ {
WriteLine("expand_and_rollout");
WriteLine($"{initialIndex}");
// expand once // expand once
var initialNode = Tree.Get(initialIndex).State; var initialNode = Tree.Get(initialIndex).State;
if (initialNode.IsComplete) if (initialNode.IsComplete)
{
WriteLine($"ret {initialIndex} {initialNode.CompletionState}");
return (initialIndex, initialNode.CompletionState, initialNode.CalculateScore() ?? 0); return (initialIndex, initialNode.CompletionState, initialNode.CalculateScore() ?? 0);
}
var randomAction = initialNode.AvailableActions.ElementAt(0); var randomAction = initialNode.AvailableActions.ElementAt(0);
initialNode.AvailableActions.Remove(randomAction); initialNode.AvailableActions.RemoveAt(0);
WriteLine($"pick {randomAction.IntName()}");
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);
WriteLine($"ins {expandedIndex}");
// playout to a terminal state // playout to a terminal state
var currentState = Tree.Get(expandedIndex).State; var currentState = Tree.Get(expandedIndex).State;
@@ -189,14 +166,8 @@ public class Solver
var score = currentState.CalculateScore() ?? 0; var score = currentState.CalculateScore() ?? 0;
if (currentState.CompletionState == CompletionState.ProgressComplete) if (currentState.CompletionState == CompletionState.ProgressComplete)
{ {
WriteLine($"calc: {score:0.00000}");
if (score >= ScoreStorageThreshold && score >= Tree.Get(0).State.Scores.MaxScore) 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); (var terminalIndex, _) = ExecuteActions(expandedIndex, currentState.State.ActionHistory.Skip(preCount).ToList(), true);
return (terminalIndex, currentState.CompletionState, score); return (terminalIndex, currentState.CompletionState, score);
} }
@@ -206,7 +177,6 @@ public class Solver
public void Backpropagate(int startIndex, int targetIndex, float score) public void Backpropagate(int startIndex, int targetIndex, float score)
{ {
WriteLine($"back {startIndex}->{targetIndex} {score}");
var currentIndex = startIndex; var currentIndex = startIndex;
while (true) while (true)
{ {
@@ -215,7 +185,6 @@ public class Solver
currentScores.Visits++; currentScores.Visits++;
currentScores.ScoreSum += score; currentScores.ScoreSum += score;
currentScores.MaxScore = Math.Max(currentScores.MaxScore, score); currentScores.MaxScore = Math.Max(currentScores.MaxScore, score);
WriteLine($"bak {currentIndex} {currentScores.Visits} {currentScores.ScoreSum} {currentScores.MaxScore}");
if (currentIndex == targetIndex) if (currentIndex == targetIndex)
break; break;
@@ -228,27 +197,22 @@ public class Solver
{ {
for (var i = 0; i < Iterations; i++) for (var i = 0; i < Iterations; i++)
{ {
WriteLine($"search {i}");
var selectedIndex = Select(startIndex); var selectedIndex = Select(startIndex);
var (endIndex, state, score) = ExpandAndRollout(selectedIndex); var (endIndex, _, score) = ExpandAndRollout(selectedIndex);
WriteLine($"backp {endIndex} {score}");
Backpropagate(endIndex, startIndex, score); Backpropagate(endIndex, startIndex, score);
} }
} }
public (List<ActionType> Actions, SimulationNode Node) Solution() public (List<ActionType> Actions, SimulationNode Node) Solution()
{ {
WriteLine("sol");
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);
WriteLine($"next: {next_index}");
node = Tree.Get(next_index); node = Tree.Get(next_index);
if (node.State.Action != null) if (node.State.Action != null)
{ {
WriteLine($"act: {node.State.Action.Value.IntName()}");
actions.Add(node.State.Action.Value); actions.Add(node.State.Action.Value);
} }
} }