Skip to content

Commit

Permalink
Replace unused worker progress delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
WorkingRobot committed Nov 14, 2023
1 parent 8f5f199 commit 930cbcc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
10 changes: 7 additions & 3 deletions Craftimizer/Windows/MacroEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public SimulationReliablity GetReliability(in SimulationState initialState, IEnu
Reliability ??=
new(initialState, actionSet, Service.Configuration.ReliabilitySimulationCount, recipeData);
};

private List<SimulatedActionStep> Macro { get; set; } = new();
private SimulationState InitialState { get; set; }
private SimulationState State => Macro.Count > 0 ? Macro[^1].State : InitialState;
Expand Down Expand Up @@ -1664,7 +1664,11 @@ private void CalculateBestMacroTask(SimulationState state, CancellationToken tok
var solver = new Solver.Solver(config, state) { Token = token };
solver.OnLog += Log.Debug;
solver.OnNewAction += QueueSolverStep;
solver.OnProgress += (s, p, m) => { Interlocked.Exchange(ref solverProgress, p); Interlocked.Exchange(ref maxSolverProgress, m); };
solver.OnProgress += (p, m) =>
{
Interlocked.Exchange(ref solverProgress, p);
Interlocked.Exchange(ref maxSolverProgress, m);
};
solver.Start();
_ = solver.GetTask().GetAwaiter().GetResult();

Expand Down Expand Up @@ -1716,7 +1720,7 @@ private void AddStep(ActionType action, int index = -1)
var state = index == 0 ? InitialState : Macro[index - 1].State;
var sim = CreateSim();
Macro.Insert(index, new(action, sim, state, out state));

for (var i = index + 1; i < Macro.Count; i++)
state = Macro[i].Recalculate(sim, state);
}
Expand Down
23 changes: 6 additions & 17 deletions Solver/Solver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,21 @@ public sealed class Solver : IDisposable
private MCTSConfig MCTSConfig => new(Config);
private Task? CompletionTask { get; set; }

private int progressSequence;
private int progress;
private int maxProgress;

public delegate void LogDelegate(string text);
public delegate void WorkerProgressDelegate(SolverSolution solution, float score);
public delegate void ProgressDelegate(int sequence, int value, int maxValue);
public delegate void ProgressDelegate(int value, int maxValue);
public delegate void NewActionDelegate(ActionType action);
public delegate void SolutionDelegate(SolverSolution solution);

// Print to console or plugin log.
public event LogDelegate? OnLog;

// Isn't always called. This is just meant to show as an indicator to the user.
// Solution contains the best terminal state, and its actions to get there exclude the ones provided by OnNewAction.
// For example, to get to the terminal state, execute all OnNewAction actions, then execute all Solution actions.
public event WorkerProgressDelegate? OnWorkerProgress;

// Always called in some form in every algorithm.
// In iterative algorithms, the sequence can increment and reset the value back to 0.
// In other algorithms, the sequence is always 0 and the value increases monotonically.
// In iterative algorithms, the value can be reset back to 0.
// In other algorithms, the value increases monotonically.
public event ProgressDelegate? OnProgress;

// Always called when a new step is generated.
Expand Down Expand Up @@ -73,7 +67,7 @@ private async Task RunTask()
{
Token.ThrowIfCancellationRequested();

progressSequence = progress = 0;
progress = 0;
Solution = await SearchFunc().ConfigureAwait(false);
}

Expand Down Expand Up @@ -129,14 +123,13 @@ private void IncrementRemainingProgress(int iterations) =>

private void IncrementProgressBy(int value)
{
OnProgress?.Invoke(progressSequence, Interlocked.Add(ref progress, value), maxProgress);
OnProgress?.Invoke(Interlocked.Add(ref progress, value), maxProgress);
}

private void IncrementProgressSequence()
{
Interlocked.Exchange(ref progress, 0);
progressSequence++;
OnProgress?.Invoke(progressSequence, 0, maxProgress);
OnProgress?.Invoke(0, maxProgress);
}

private void SearchWithIncrement(MCTS mcts, int iterations)
Expand Down Expand Up @@ -185,8 +178,6 @@ private async Task<SolverSolution> SearchStepwiseFurcated()
catch (ObjectDisposedException) { }
}
var solution = solver.Solution();
var progressActions = activeStates[stateIdx].Actions.Concat(solution.Actions).Skip(definiteActionCount).ToList();
OnWorkerProgress?.Invoke(solution with { Actions = progressActions }, solver.MaxScore);
return (solver.MaxScore, stateIdx, solution);
}, Token);
}
Expand Down Expand Up @@ -312,7 +303,6 @@ private async Task<SolverSolution> SearchStepwiseForked()
catch (ObjectDisposedException) { }
}
var solution = solver.Solution();
OnWorkerProgress?.Invoke(solution, solver.MaxScore);
return (solver.MaxScore, solution);
}, Token);
semaphore.Release(Config.MaxThreadCount);
Expand Down Expand Up @@ -412,7 +402,6 @@ private async Task<SolverSolution> SearchOneshotForked()
catch (ObjectDisposedException) { }
}
var solution = solver.Solution();
OnWorkerProgress?.Invoke(solution, solver.MaxScore);
return (solver.MaxScore, solution);
}, Token);
semaphore.Release(Config.MaxThreadCount);
Expand Down

1 comment on commit 930cbcc

@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: 930cbcc Previous: a8c3f34 Ratio
Craftimizer.Benchmark.Bench.Solve(State: 5372D31C98FA4C357F54029912394B0F5ECBE94AEC9D12C1C2B7F453C62ACD2F, Config: E1C895829E5BB1533C41D3C71AD65B6B3E1A60B5EE2D35A845744C251E71EFA5) 2206630026.6666665 ns (± 23106191.596640643)
Craftimizer.Benchmark.Bench.Solve(State: 5372D31C98FA4C357F54029912394B0F5ECBE94AEC9D12C1C2B7F453C62ACD2F, Config: E1C895829E5BB1533C41D3C71AD65B6B3E1A60B5EE2D35A845744C251E71EFA5) 1748238246.6666667 ns (± 13187484.38167615)
Craftimizer.Benchmark.Bench.Solve(State: 99B0F1AD46A18B4D8262F9BA75ABE23507217C2F20FBF895A49282DDFEF50190, Config: E1C895829E5BB1533C41D3C71AD65B6B3E1A60B5EE2D35A845744C251E71EFA5) 2216484366.6666665 ns (± 22057124.64338472)
Craftimizer.Benchmark.Bench.Solve(State: 99B0F1AD46A18B4D8262F9BA75ABE23507217C2F20FBF895A49282DDFEF50190, Config: E1C895829E5BB1533C41D3C71AD65B6B3E1A60B5EE2D35A845744C251E71EFA5) 1733379284.6153846 ns (± 17483836.077114493)

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

Please sign in to comment.