Skip to content

Commit

Permalink
Add ability to set initialized sim state if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
WorkingRobot committed Nov 14, 2023
1 parent b9c871f commit 9806139
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
8 changes: 5 additions & 3 deletions Craftimizer/Windows/MacroEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ private void DrawIngredientHQEntry(int idx)

private void DrawActionHotbars()
{
var sim = CreateSim();
var sim = CreateSim(State);

var imageSize = ImGui.GetFrameHeight() * 2;
var spacing = ImGui.GetStyle().ItemSpacing.Y;
Expand Down Expand Up @@ -1283,8 +1283,7 @@ private void DrawMacro()
}
if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled))
{
var sim = CreateSim();
ImGui.SetTooltip($"{action.GetName(RecipeData!.ClassJob)}\n{actionBase.GetTooltip(sim, true)}");
ImGui.SetTooltip($"{action.GetName(RecipeData!.ClassJob)}\n{actionBase.GetTooltip(CreateSim(lastState), true)}");
}
lastState = state;
}
Expand Down Expand Up @@ -1653,6 +1652,9 @@ private void RecalculateState()
private static Sim CreateSim() =>
Service.Configuration.ConditionRandomness ? new Sim() : new SimNoRandom();

private static Sim CreateSim(in SimulationState state) =>
Service.Configuration.ConditionRandomness ? new Sim() { State = state } : new SimNoRandom() { State = state };

private void AddStep(ActionType action, int index = -1, bool isSolver = false)
{
if (index < -1 || index >= Macro.Count)
Expand Down
36 changes: 19 additions & 17 deletions Simulator/Simulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ namespace Craftimizer.Simulator;

public class Simulator
{
protected SimulationState State;
public SimulationState State { init => state = value; }

public SimulationInput Input => State.Input;
public ref int ActionCount => ref State.ActionCount;
public ref int StepCount => ref State.StepCount;
public ref int Progress => ref State.Progress;
public ref int Quality => ref State.Quality;
public ref int Durability => ref State.Durability;
public ref int CP => ref State.CP;
public ref Condition Condition => ref State.Condition;
public ref Effects ActiveEffects => ref State.ActiveEffects;
public ref ActionStates ActionStates => ref State.ActionStates;
private SimulationState state;

public bool IsFirstStep => State.StepCount == 0;
public SimulationInput Input => state.Input;
public ref int ActionCount => ref state.ActionCount;
public ref int StepCount => ref state.StepCount;
public ref int Progress => ref state.Progress;
public ref int Quality => ref state.Quality;
public ref int Durability => ref state.Durability;
public ref int CP => ref state.CP;
public ref Condition Condition => ref state.Condition;
public ref Effects ActiveEffects => ref state.ActiveEffects;
public ref ActionStates ActionStates => ref state.ActionStates;

public bool IsFirstStep => state.StepCount == 0;

public virtual CompletionState CompletionState {
get
Expand All @@ -37,8 +39,8 @@ public virtual CompletionState CompletionState {

public (ActionResponse Response, SimulationState NewState) Execute(in SimulationState state, ActionType action)
{
State = state;
return (Execute(action), State);
this.state = state;
return (Execute(action), this.state);
}

private ActionResponse Execute(ActionType action)
Expand Down Expand Up @@ -67,16 +69,16 @@ private ActionResponse Execute(ActionType action)

public (ActionResponse Response, SimulationState NewState, int FailedActionIdx) ExecuteMultiple(in SimulationState state, IEnumerable<ActionType> actions)
{
State = state;
this.state = state;
var i = 0;
foreach(var action in actions)
{
var resp = Execute(action);
if (resp != ActionResponse.UsedAction)
return (resp, State, i);
return (resp, this.state, i);
i++;
}
return (ActionResponse.UsedAction, State, -1);
return (ActionResponse.UsedAction, this.state, -1);
}

[Pure]
Expand Down
2 changes: 1 addition & 1 deletion Solver/MCTS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public sealed class MCTS
public MCTS(in MCTSConfig config, in SimulationState state)
{
this.config = config;
var sim = new Simulator(config.MaxStepCount);
var sim = new Simulator(config.MaxStepCount) { State = state };
rootNode = new(new(
state,
null,
Expand Down
4 changes: 2 additions & 2 deletions Solver/Solver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private async Task<SolverSolution> SearchStepwiseForked()
{
var actions = new List<ActionType>();
var state = State;
var sim = new Simulator(Config.MaxStepCount);
var sim = new Simulator(Config.MaxStepCount) { State = state };
while (true)
{
Token.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -303,7 +303,7 @@ private Task<SolverSolution> SearchStepwise()
{
var actions = new List<ActionType>();
var state = State;
var sim = new Simulator(Config.MaxStepCount);
var sim = new Simulator(Config.MaxStepCount) { State = state };
while (true)
{
Token.ThrowIfCancellationRequested();
Expand Down
2 changes: 1 addition & 1 deletion Test/Simulator/Simulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void TrainedFinesseProcs()
},
0, 4064, 15, 332);
Assert.AreEqual(10, state.ActiveEffects.InnerQuiet);
Assert.IsTrue(ActionType.TrainedFinesse.Base().CanUse(new SimulatorNoRandom()));
Assert.IsTrue(ActionType.TrainedFinesse.Base().CanUse(new SimulatorNoRandom() { State = state }));
}

[TestMethod]
Expand Down

1 comment on commit 9806139

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 9806139 Previous: 4bf7397 Ratio
Craftimizer.Benchmark.Bench.Solve(State: 5372D31C98FA4C357F54029912394B0F5ECBE94AEC9D12C1C2B7F453C62ACD2F, Config: B75845A5DEDC79F3C98FE35F28206855B0EF4E065555FAF2AA129AED115B7744) 1404302816.6666667 ns (± 11373673.533594495) 1480306700 ns (± 23679878.252854258) 0.95
Craftimizer.Benchmark.Bench.Solve(State: 5372D31C98FA4C357F54029912394B0F5ECBE94AEC9D12C1C2B7F453C62ACD2F, Config: B75845A5DEDC79F3C98FE35F28206855B0EF4E065555FAF2AA129AED115B7744) 1092072800 ns (± 21029574.844460122) 1480306700 ns (± 23679878.252854258) 0.74
Craftimizer.Benchmark.Bench.Solve(State: 99B0F1AD46A18B4D8262F9BA75ABE23507217C2F20FBF895A49282DDFEF50190, Config: B75845A5DEDC79F3C98FE35F28206855B0EF4E065555FAF2AA129AED115B7744) 1468244846.6666667 ns (± 3631721.0516437492) 1450385066.6666667 ns (± 6712458.243197247) 1.01
Craftimizer.Benchmark.Bench.Solve(State: 99B0F1AD46A18B4D8262F9BA75ABE23507217C2F20FBF895A49282DDFEF50190, Config: B75845A5DEDC79F3C98FE35F28206855B0EF4E065555FAF2AA129AED115B7744) 1156725742.857143 ns (± 2945804.5252696085) 1450385066.6666667 ns (± 6712458.243197247) 0.80

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.