diff --git a/Craftimizer/Windows/MacroEditor.cs b/Craftimizer/Windows/MacroEditor.cs index 68220bf..4b1a198 100644 --- a/Craftimizer/Windows/MacroEditor.cs +++ b/Craftimizer/Windows/MacroEditor.cs @@ -183,7 +183,7 @@ public SimulationReliablity GetReliability(in SimulationState initialState, IEnu Reliability ??= new(initialState, actionSet, Service.Configuration.ReliabilitySimulationCount, recipeData); }; - + private List Macro { get; set; } = new(); private SimulationState InitialState { get; set; } private SimulationState State => Macro.Count > 0 ? Macro[^1].State : InitialState; @@ -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(); @@ -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); } diff --git a/Solver/Solver.cs b/Solver/Solver.cs index b144f6c..6716fad 100644 --- a/Solver/Solver.cs +++ b/Solver/Solver.cs @@ -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. @@ -73,7 +67,7 @@ private async Task RunTask() { Token.ThrowIfCancellationRequested(); - progressSequence = progress = 0; + progress = 0; Solution = await SearchFunc().ConfigureAwait(false); } @@ -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) @@ -185,8 +178,6 @@ private async Task 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); } @@ -312,7 +303,6 @@ private async Task SearchStepwiseForked() catch (ObjectDisposedException) { } } var solution = solver.Solution(); - OnWorkerProgress?.Invoke(solution, solver.MaxScore); return (solver.MaxScore, solution); }, Token); semaphore.Release(Config.MaxThreadCount); @@ -412,7 +402,6 @@ private async Task SearchOneshotForked() catch (ObjectDisposedException) { } } var solution = solver.Solution(); - OnWorkerProgress?.Invoke(solution, solver.MaxScore); return (solver.MaxScore, solution); }, Token); semaphore.Release(Config.MaxThreadCount);