Moved algorithm into SolverConfig, multi configuration support

This commit is contained in:
Asriel Camora
2023-07-23 02:27:08 +04:00
parent bac96201d2
commit c344815e50
5 changed files with 51 additions and 25 deletions
+4 -21
View File
@@ -15,30 +15,13 @@ public class Macro
public List<ActionType> Actions { get; set; } = new(); public List<ActionType> Actions { get; set; } = new();
} }
public enum SolverAlgorithm
{
Oneshot,
OneshotForked,
Stepwise,
StepwiseForked,
StepwiseFurcated,
}
public static class AlgorithmUtils public static class AlgorithmUtils
{ {
public static void Invoke(this SolverAlgorithm me, SolverConfig config, SimulationState state, Action<ActionType>? actionCallback = null, CancellationToken token = default) public static void Invoke(this SolverConfig me, SimulationState state, Action<ActionType>? actionCallback = null, CancellationToken token = default)
{ {
Func<SolverConfig, SimulationState, Action<ActionType>?, CancellationToken, SolverSolution> func = me switch
{
SolverAlgorithm.Oneshot => Solver.Crafty.Solver.SearchOneshot,
SolverAlgorithm.OneshotForked => Solver.Crafty.Solver.SearchOneshotForked,
SolverAlgorithm.Stepwise => Solver.Crafty.Solver.SearchStepwise,
SolverAlgorithm.StepwiseForked => Solver.Crafty.Solver.SearchStepwiseForked,
SolverAlgorithm.StepwiseFurcated or _ => Solver.Crafty.Solver.SearchStepwiseFurcated,
};
try try
{ {
func(config, state, actionCallback, token); Solver.Crafty.Solver.Search(me, state, actionCallback, token);
} }
catch (AggregateException e) catch (AggregateException e)
{ {
@@ -59,8 +42,8 @@ public class Configuration : IPluginConfiguration
public bool OverrideUncraftability { get; set; } = true; public bool OverrideUncraftability { get; set; } = true;
public bool HideUnlearnedActions { get; set; } = true; public bool HideUnlearnedActions { get; set; } = true;
public List<Macro> Macros { get; set; } = new(); public List<Macro> Macros { get; set; } = new();
public SolverConfig SolverConfig { get; set; } = new(); public SolverConfig SimulatorSolverConfig { get; set; } = SolverConfig.SimulatorDefault;
public SolverAlgorithm SolverAlgorithm { get; set; } = SolverAlgorithm.StepwiseFurcated; public SolverConfig SynthHelperSolverConfig { get; set; } = SolverConfig.SynthHelperDefault;
public bool ConditionRandomness { get; set; } = true; public bool ConditionRandomness { get; set; } = true;
public bool EnableSynthesisHelper { get; set; } = true; public bool EnableSynthesisHelper { get; set; } = true;
public int SynthesisHelperStepCount { get; set; } = 5; public int SynthesisHelperStepCount { get; set; } = 5;
+1 -1
View File
@@ -48,7 +48,7 @@ public sealed unsafe partial class Craft : Window, IDisposable
SolverSim = new(state); SolverSim = new(state);
SolverTaskToken = new(); SolverTaskToken = new();
SolverTask = Task.Run(() => Config.SolverAlgorithm.Invoke(Config.SolverConfig, state, SolverActionQueue.Enqueue, SolverTaskToken.Token)); SolverTask = Task.Run(() => Config.SynthHelperSolverConfig.Invoke(state, SolverActionQueue.Enqueue, SolverTaskToken.Token));
} }
private void SolveTick() private void SolveTick()
+1 -1
View File
@@ -83,7 +83,7 @@ public sealed partial class Simulator : Window, IDisposable
SolverInitialActionCount = Actions.Count; SolverInitialActionCount = Actions.Count;
SolverTaskToken = new(); SolverTaskToken = new();
SolverTask = Task.Run(() => Config.SolverAlgorithm.Invoke(Config.SolverConfig, solverState, SolverActionQueue.Enqueue, SolverTaskToken.Token)); SolverTask = Task.Run(() => Config.SimulatorSolverConfig.Invoke(solverState, SolverActionQueue.Enqueue, SolverTaskToken.Token));
} }
public void Dispose() public void Dispose()
+17
View File
@@ -570,4 +570,21 @@ public sealed class Solver
return solution; return solution;
} }
public static SolverSolution Search(SolverConfig config, SimulationInput input, Action<ActionType>? actionCallback = null, CancellationToken token = default) =>
Search(config, new SimulationState(input), actionCallback, token);
public static SolverSolution Search(SolverConfig config, SimulationState state, Action<ActionType>? actionCallback = null, CancellationToken token = default)
{
Func<SolverConfig, SimulationState, Action<ActionType>?, CancellationToken, SolverSolution> func = config.Algorithm switch
{
SolverAlgorithm.Oneshot => SearchOneshot,
SolverAlgorithm.OneshotForked => SearchOneshotForked,
SolverAlgorithm.Stepwise => SearchStepwise,
SolverAlgorithm.StepwiseForked => SearchStepwiseForked,
SolverAlgorithm.StepwiseFurcated or _ => SearchStepwiseFurcated,
};
return func(config, state, actionCallback, token);
}
} }
+28 -2
View File
@@ -2,6 +2,15 @@ using System.Runtime.InteropServices;
namespace Craftimizer.Solver.Crafty; namespace Craftimizer.Solver.Crafty;
public enum SolverAlgorithm
{
Oneshot,
OneshotForked,
Stepwise,
StepwiseForked,
StepwiseFurcated,
}
[StructLayout(LayoutKind.Auto)] [StructLayout(LayoutKind.Auto)]
public readonly record struct SolverConfig public readonly record struct SolverConfig
{ {
@@ -21,14 +30,16 @@ public readonly record struct SolverConfig
public float ScoreCPBonus { get; init; } public float ScoreCPBonus { get; init; }
public float ScoreFewerStepsBonus { get; init; } public float ScoreFewerStepsBonus { get; init; }
public SolverAlgorithm Algorithm { get; init; }
public SolverConfig() public SolverConfig()
{ {
Iterations = 100000; Iterations = 100000;
ScoreStorageThreshold = 1f; ScoreStorageThreshold = 1f;
MaxScoreWeightingConstant = 0.1f; MaxScoreWeightingConstant = 0.1f;
ExplorationConstant = 4; ExplorationConstant = 4;
MaxStepCount = 25; MaxStepCount = 30;
MaxRolloutStepCount = MaxStepCount; MaxRolloutStepCount = 99;
ForkCount = Math.Max(Environment.ProcessorCount, 32); ForkCount = Math.Max(Environment.ProcessorCount, 32);
FurcatedActionCount = ForkCount / 2; FurcatedActionCount = ForkCount / 2;
StrictActions = true; StrictActions = true;
@@ -38,5 +49,20 @@ public readonly record struct SolverConfig
ScoreDurabilityBonus = .05f; ScoreDurabilityBonus = .05f;
ScoreCPBonus = .05f; ScoreCPBonus = .05f;
ScoreFewerStepsBonus = .05f; ScoreFewerStepsBonus = .05f;
Algorithm = SolverAlgorithm.StepwiseFurcated;
} }
public static readonly SolverConfig SimulatorDefault = new SolverConfig() with
{
};
public static readonly SolverConfig SynthHelperDefault = new SolverConfig() with
{
Iterations = 300000,
ForkCount = Environment.ProcessorCount - 1, // Keep one for the game thread
FurcatedActionCount = Environment.ProcessorCount / 2,
Algorithm = SolverAlgorithm.StepwiseForked
};
} }