Remove redundant initial state in sims

This commit is contained in:
Asriel Camora
2023-11-13 22:58:10 -08:00
parent ea220fa33d
commit b9c871f2b2
10 changed files with 20 additions and 34 deletions
+1 -1
View File
@@ -106,7 +106,7 @@ internal static class Program
MaxStepCount = 30,
};
var sim = new SimulatorNoRandom(new(input));
var sim = new SimulatorNoRandom();
(_, var state) = sim.Execute(new(input), ActionType.MuscleMemory);
(_, state) = sim.Execute(state, ActionType.PrudentTouch);
//(_, state) = sim.Execute(state, ActionType.Manipulation);
+9 -9
View File
@@ -143,7 +143,7 @@ public sealed class MacroEditor : Window, IDisposable
for (var i = 0; i < iterCount; ++i)
{
var sim = new Sim(startState);
var sim = new Sim();
var (_, state, _) = sim.ExecuteMultiple(startState, actions);
Progress.Add(state.Progress);
Quality.Add(state.Quality);
@@ -1018,7 +1018,7 @@ public sealed class MacroEditor : Window, IDisposable
private void DrawActionHotbars()
{
var sim = CreateSim(State);
var sim = CreateSim();
var imageSize = ImGui.GetFrameHeight() * 2;
var spacing = ImGui.GetStyle().ItemSpacing.Y;
@@ -1283,7 +1283,7 @@ public sealed class MacroEditor : Window, IDisposable
}
if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled))
{
var sim = CreateSim(lastState);
var sim = CreateSim();
ImGui.SetTooltip($"{action.GetName(RecipeData!.ClassJob)}\n{actionBase.GetTooltip(sim, true)}");
}
lastState = state;
@@ -1644,14 +1644,14 @@ public sealed class MacroEditor : Window, IDisposable
private void RecalculateState()
{
InitialState = new SimulationState(new(CharacterStats, RecipeData.RecipeInfo, StartingQuality));
var sim = CreateSim(InitialState);
var sim = CreateSim();
var lastState = InitialState;
for (var i = 0; i < Macro.Count; i++)
lastState = Macro[i].Recalculate(sim, lastState);
}
private static Sim CreateSim(in SimulationState state) =>
Service.Configuration.ConditionRandomness ? new Sim(state) : new SimNoRandom(state);
private static Sim CreateSim() =>
Service.Configuration.ConditionRandomness ? new Sim() : new SimNoRandom();
private void AddStep(ActionType action, int index = -1, bool isSolver = false)
{
@@ -1664,13 +1664,13 @@ public sealed class MacroEditor : Window, IDisposable
if (index == -1)
{
var sim = CreateSim(State);
var sim = CreateSim();
Macro.Add(new(action, sim, State, out _));
}
else
{
var state = index == 0 ? InitialState : Macro[index - 1].State;
var sim = CreateSim(state);
var sim = CreateSim();
Macro.Insert(index, new(action, sim, state, out state));
for (var i = index + 1; i < Macro.Count; i++)
@@ -1689,7 +1689,7 @@ public sealed class MacroEditor : Window, IDisposable
Macro.RemoveAt(index);
var state = index == 0 ? InitialState : Macro[index - 1].State;
var sim = CreateSim(state);
var sim = CreateSim();
for (var i = index; i < Macro.Count; i++)
state = Macro[i].Recalculate(sim, state);
}
+1 -1
View File
@@ -329,7 +329,7 @@ public sealed class MacroList : Window, IDisposable
return state;
state = new SimulationState(new(CharacterStats, RecipeData.RecipeInfo));
var sim = new Sim(state);
var sim = new Sim();
(_, state, _) = sim.ExecuteMultiple(state, macro.Actions);
return MacroStateCache[macro] = state;
}
+1 -1
View File
@@ -824,7 +824,7 @@ public sealed unsafe class RecipeNote : Window, IDisposable
var state = new SimulationState(input);
var config = Service.Configuration.SimulatorSolverConfig;
var mctsConfig = new MCTSConfig(config);
var simulator = new SimulatorNoRandom(state);
var simulator = new SimulatorNoRandom();
List<Macro> macros = new(Service.Configuration.Macros);
token.ThrowIfCancellationRequested();
-10
View File
@@ -35,16 +35,6 @@ public class Simulator
public IEnumerable<ActionType> AvailableActions => ActionUtils.AvailableActions(this);
public Simulator(in SimulationState state)
{
State = state;
}
public void SetState(in SimulationState state)
{
State = state;
}
public (ActionResponse Response, SimulationState NewState) Execute(in SimulationState state, ActionType action)
{
State = state;
-4
View File
@@ -2,10 +2,6 @@ namespace Craftimizer.Simulator;
public class SimulatorNoRandom : Simulator
{
public SimulatorNoRandom(in SimulationState state) : base(state)
{
}
public sealed override bool RollSuccessRaw(float successRate) => successRate == 1;
public sealed override Condition GetNextRandomCondition() => Condition.Normal;
}
+2 -2
View File
@@ -20,7 +20,7 @@ public sealed class MCTS
public MCTS(in MCTSConfig config, in SimulationState state)
{
this.config = config;
var sim = new Simulator(state, config.MaxStepCount);
var sim = new Simulator(config.MaxStepCount);
rootNode = new(new(
state,
null,
@@ -280,7 +280,7 @@ public sealed class MCTS
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Search(int iterations, CancellationToken token)
{
Simulator simulator = new(rootNode.State.State, config.MaxStepCount);
Simulator simulator = new(config.MaxStepCount);
var random = rootNode.State.State.Input.Random;
var n = 0;
for (var i = 0; i < iterations || MaxScore == 0; i++)
+1 -1
View File
@@ -20,7 +20,7 @@ internal sealed class Simulator : SimulatorNoRandom
}
}
public Simulator(in SimulationState state, int maxStepCount) : base(state)
public Simulator(int maxStepCount)
{
this.maxStepCount = maxStepCount;
}
+3 -3
View File
@@ -116,7 +116,7 @@ public sealed class Solver : IDisposable
var bestSims = new List<(float Score, SolverSolution Result)>();
var state = State;
var sim = new Simulator(state, Config.MaxStepCount);
var sim = new Simulator(Config.MaxStepCount);
var activeStates = new List<SolverSolution>() { new(Array.Empty<ActionType>(), state) };
@@ -240,7 +240,7 @@ public sealed class Solver : IDisposable
{
var actions = new List<ActionType>();
var state = State;
var sim = new Simulator(state, Config.MaxStepCount);
var sim = new Simulator(Config.MaxStepCount);
while (true)
{
Token.ThrowIfCancellationRequested();
@@ -303,7 +303,7 @@ public sealed class Solver : IDisposable
{
var actions = new List<ActionType>();
var state = State;
var sim = new Simulator(state, Config.MaxStepCount);
var sim = new Simulator(Config.MaxStepCount);
while (true)
{
Token.ThrowIfCancellationRequested();
+2 -2
View File
@@ -68,7 +68,7 @@ public class SimulatorTests
int progress, int quality,
int durability, int cp)
{
var simulator = new SimulatorNoRandom(new(input));
var simulator = new SimulatorNoRandom();
var (_, state, _) = simulator.ExecuteMultiple(new(input), actions);
Assert.AreEqual(progress, state.Progress);
Assert.AreEqual(quality, state.Quality);
@@ -170,7 +170,7 @@ public class SimulatorTests
},
0, 4064, 15, 332);
Assert.AreEqual(10, state.ActiveEffects.InnerQuiet);
Assert.IsTrue(ActionType.TrainedFinesse.Base().CanUse(new SimulatorNoRandom(state)));
Assert.IsTrue(ActionType.TrainedFinesse.Base().CanUse(new SimulatorNoRandom()));
}
[TestMethod]