Replace unused worker progress delegate

This commit is contained in:
Asriel Camora
2023-11-14 02:24:28 -08:00
parent 8f5f199682
commit 930cbcce25
2 changed files with 13 additions and 20 deletions
+7 -3
View File
@@ -183,7 +183,7 @@ public sealed class MacroEditor : Window, IDisposable
Reliability ??= Reliability ??=
new(initialState, actionSet, Service.Configuration.ReliabilitySimulationCount, recipeData); new(initialState, actionSet, Service.Configuration.ReliabilitySimulationCount, recipeData);
}; };
private List<SimulatedActionStep> Macro { get; set; } = new(); private List<SimulatedActionStep> Macro { get; set; } = new();
private SimulationState InitialState { get; set; } private SimulationState InitialState { get; set; }
private SimulationState State => Macro.Count > 0 ? Macro[^1].State : InitialState; private SimulationState State => Macro.Count > 0 ? Macro[^1].State : InitialState;
@@ -1664,7 +1664,11 @@ public sealed class MacroEditor : Window, IDisposable
var solver = new Solver.Solver(config, state) { Token = token }; var solver = new Solver.Solver(config, state) { Token = token };
solver.OnLog += Log.Debug; solver.OnLog += Log.Debug;
solver.OnNewAction += QueueSolverStep; 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.Start();
_ = solver.GetTask().GetAwaiter().GetResult(); _ = solver.GetTask().GetAwaiter().GetResult();
@@ -1716,7 +1720,7 @@ public sealed class MacroEditor : Window, IDisposable
var state = index == 0 ? InitialState : Macro[index - 1].State; var state = index == 0 ? InitialState : Macro[index - 1].State;
var sim = CreateSim(); var sim = CreateSim();
Macro.Insert(index, new(action, sim, state, out state)); Macro.Insert(index, new(action, sim, state, out state));
for (var i = index + 1; i < Macro.Count; i++) for (var i = index + 1; i < Macro.Count; i++)
state = Macro[i].Recalculate(sim, state); state = Macro[i].Recalculate(sim, state);
} }
+6 -17
View File
@@ -19,27 +19,21 @@ public sealed class Solver : IDisposable
private MCTSConfig MCTSConfig => new(Config); private MCTSConfig MCTSConfig => new(Config);
private Task? CompletionTask { get; set; } private Task? CompletionTask { get; set; }
private int progressSequence;
private int progress; private int progress;
private int maxProgress; private int maxProgress;
public delegate void LogDelegate(string text); public delegate void LogDelegate(string text);
public delegate void WorkerProgressDelegate(SolverSolution solution, float score); 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 NewActionDelegate(ActionType action);
public delegate void SolutionDelegate(SolverSolution solution); public delegate void SolutionDelegate(SolverSolution solution);
// Print to console or plugin log. // Print to console or plugin log.
public event LogDelegate? OnLog; 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. // Always called in some form in every algorithm.
// In iterative algorithms, the sequence can increment and reset the value back to 0. // In iterative algorithms, the value can be reset back to 0.
// In other algorithms, the sequence is always 0 and the value increases monotonically. // In other algorithms, the value increases monotonically.
public event ProgressDelegate? OnProgress; public event ProgressDelegate? OnProgress;
// Always called when a new step is generated. // Always called when a new step is generated.
@@ -73,7 +67,7 @@ public sealed class Solver : IDisposable
{ {
Token.ThrowIfCancellationRequested(); Token.ThrowIfCancellationRequested();
progressSequence = progress = 0; progress = 0;
Solution = await SearchFunc().ConfigureAwait(false); Solution = await SearchFunc().ConfigureAwait(false);
} }
@@ -129,14 +123,13 @@ public sealed class Solver : IDisposable
private void IncrementProgressBy(int value) 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() private void IncrementProgressSequence()
{ {
Interlocked.Exchange(ref progress, 0); Interlocked.Exchange(ref progress, 0);
progressSequence++; OnProgress?.Invoke(0, maxProgress);
OnProgress?.Invoke(progressSequence, 0, maxProgress);
} }
private void SearchWithIncrement(MCTS mcts, int iterations) private void SearchWithIncrement(MCTS mcts, int iterations)
@@ -185,8 +178,6 @@ public sealed class Solver : IDisposable
catch (ObjectDisposedException) { } catch (ObjectDisposedException) { }
} }
var solution = solver.Solution(); 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); return (solver.MaxScore, stateIdx, solution);
}, Token); }, Token);
} }
@@ -312,7 +303,6 @@ public sealed class Solver : IDisposable
catch (ObjectDisposedException) { } catch (ObjectDisposedException) { }
} }
var solution = solver.Solution(); var solution = solver.Solution();
OnWorkerProgress?.Invoke(solution, solver.MaxScore);
return (solver.MaxScore, solution); return (solver.MaxScore, solution);
}, Token); }, Token);
semaphore.Release(Config.MaxThreadCount); semaphore.Release(Config.MaxThreadCount);
@@ -412,7 +402,6 @@ public sealed class Solver : IDisposable
catch (ObjectDisposedException) { } catch (ObjectDisposedException) { }
} }
var solution = solver.Solution(); var solution = solver.Solution();
OnWorkerProgress?.Invoke(solution, solver.MaxScore);
return (solver.MaxScore, solution); return (solver.MaxScore, solution);
}, Token); }, Token);
semaphore.Release(Config.MaxThreadCount); semaphore.Release(Config.MaxThreadCount);