Customizable solver config, remove simulator emplace/displace methods
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Craftimizer.Solver.Crafty;
|
||||
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
public struct NodeScores
|
||||
{
|
||||
public float ScoreSum;
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using Craftimizer.Simulator;
|
||||
using Craftimizer.Simulator.Actions;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Craftimizer.Solver.Crafty;
|
||||
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
public struct SimulationNode
|
||||
{
|
||||
public readonly SimulationState State;
|
||||
@@ -12,9 +14,9 @@ public struct SimulationNode
|
||||
public ActionSet AvailableActions;
|
||||
public NodeScores Scores;
|
||||
|
||||
public CompletionState CompletionState => GetCompletionState(SimulationCompletionState, AvailableActions);
|
||||
public readonly CompletionState CompletionState => GetCompletionState(SimulationCompletionState, AvailableActions);
|
||||
|
||||
public bool IsComplete => CompletionState != CompletionState.Incomplete;
|
||||
public readonly bool IsComplete => CompletionState != CompletionState.Incomplete;
|
||||
|
||||
public SimulationNode(SimulationState state, ActionType? action, CompletionState completionState, ActionSet actions)
|
||||
{
|
||||
@@ -29,9 +31,9 @@ public struct SimulationNode
|
||||
CompletionState.NoMoreActions :
|
||||
simCompletionState;
|
||||
|
||||
public float? CalculateScore() => CalculateScoreForState(State, SimulationCompletionState);
|
||||
public readonly float? CalculateScore(int maxStepCount) => CalculateScoreForState(State, SimulationCompletionState, maxStepCount);
|
||||
|
||||
public static float? CalculateScoreForState(SimulationState state, CompletionState completionState)
|
||||
public static float? CalculateScoreForState(SimulationState state, CompletionState completionState, int maxStepCount)
|
||||
{
|
||||
if (completionState != CompletionState.ProgressComplete)
|
||||
return null;
|
||||
@@ -70,7 +72,7 @@ public struct SimulationNode
|
||||
);
|
||||
|
||||
var fewerStepsScore =
|
||||
fewerStepsBonus * (1f - ((float)(state.ActionCount + 1) / Solver.MaxStepCount));
|
||||
fewerStepsBonus * (1f - ((float)(state.ActionCount + 1) / maxStepCount));
|
||||
|
||||
return progressScore + qualityScore + durabilityScore + cpScore + fewerStepsScore;
|
||||
}
|
||||
|
||||
@@ -6,14 +6,17 @@ namespace Craftimizer.Solver.Crafty;
|
||||
|
||||
public class Simulator : Sim
|
||||
{
|
||||
private readonly int maxStepCount;
|
||||
|
||||
public new CompletionState CompletionState =>
|
||||
(ActionCount + 1) >= Solver.MaxStepCount ?
|
||||
(ActionCount + 1) >= maxStepCount ?
|
||||
CompletionState.MaxActionCountReached :
|
||||
(CompletionState)base.CompletionState;
|
||||
public override bool IsComplete => CompletionState != CompletionState.Incomplete;
|
||||
|
||||
public Simulator(SimulationState state) : base(state)
|
||||
public Simulator(SimulationState state, int maxStepCount) : base(state)
|
||||
{
|
||||
this.maxStepCount = maxStepCount;
|
||||
}
|
||||
|
||||
// Disable randomization
|
||||
|
||||
+17
-21
@@ -10,20 +10,16 @@ namespace Craftimizer.Solver.Crafty;
|
||||
// https://github.com/alostsock/crafty/blob/cffbd0cad8bab3cef9f52a3e3d5da4f5e3781842/crafty/src/simulator.rs
|
||||
public class Solver
|
||||
{
|
||||
public SolverConfig Config;
|
||||
public Simulator Simulator;
|
||||
public Node RootNode;
|
||||
|
||||
// public Random Random => Simulator.Input.Random;
|
||||
|
||||
public const int Iterations = 30000;
|
||||
public const float ScoreStorageThreshold = 1f;
|
||||
public const float MaxScoreWeightingConstant = 0.1f;
|
||||
public const float ExplorationConstant = 4f;
|
||||
public const int MaxStepCount = 25;
|
||||
|
||||
public Solver(SimulationState state, bool strict)
|
||||
public Solver(SolverConfig config, SimulationState state, bool strict)
|
||||
{
|
||||
Simulator = new(state);
|
||||
Config = config;
|
||||
Simulator = new(state, config.MaxStepCount);
|
||||
RootNode = new(new(
|
||||
state,
|
||||
null,
|
||||
@@ -32,7 +28,7 @@ public class Solver
|
||||
));
|
||||
}
|
||||
|
||||
public Solver(SimulationInput input, bool strict) : this(new SimulationState(input), strict)
|
||||
public Solver(SolverConfig config, SimulationInput input, bool strict) : this(config, new SimulationState(input), strict)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -104,8 +100,8 @@ public class Solver
|
||||
{
|
||||
var length = children.Length;
|
||||
|
||||
var C = ExplorationConstant * MathF.Log(parentVisits);
|
||||
var w = MaxScoreWeightingConstant;
|
||||
var C = Config.ExplorationConstant * MathF.Log(parentVisits);
|
||||
var w = Config.MaxScoreWeightingConstant;
|
||||
var W = 1f - w;
|
||||
var CVector = new Vector<float>(C);
|
||||
|
||||
@@ -162,7 +158,7 @@ public class Solver
|
||||
ref var initialState = ref initialNode.State;
|
||||
// expand once
|
||||
if (initialState.IsComplete)
|
||||
return (initialNode, initialState.CompletionState, initialState.CalculateScore() ?? 0);
|
||||
return (initialNode, initialState.CompletionState, initialState.CalculateScore(Config.MaxStepCount) ?? 0);
|
||||
|
||||
var randomAction = initialState.AvailableActions.First();
|
||||
initialState.AvailableActions.RemoveAction(randomAction);
|
||||
@@ -174,7 +170,7 @@ public class Solver
|
||||
var currentActions = expandedNode.State.AvailableActions;
|
||||
|
||||
byte actionCount = 0;
|
||||
Span<ActionType> actions = stackalloc ActionType[MaxStepCount];
|
||||
Span<ActionType> actions = stackalloc ActionType[Config.MaxStepCount];
|
||||
while (true)
|
||||
{
|
||||
if (SimulationNode.GetCompletionState(currentCompletionState, currentActions) != CompletionState.Incomplete)
|
||||
@@ -186,10 +182,10 @@ public class Solver
|
||||
|
||||
// store the result if a max score was reached
|
||||
currentCompletionState = SimulationNode.GetCompletionState(currentCompletionState, currentActions);
|
||||
var score = SimulationNode.CalculateScoreForState(currentState, currentCompletionState) ?? 0;
|
||||
var score = SimulationNode.CalculateScoreForState(currentState, currentCompletionState, Config.MaxStepCount) ?? 0;
|
||||
if (currentCompletionState == CompletionState.ProgressComplete)
|
||||
{
|
||||
if (score >= ScoreStorageThreshold && score >= RootNode.State.Scores.MaxScore)
|
||||
if (score >= Config.ScoreStorageThreshold && score >= RootNode.State.Scores.MaxScore)
|
||||
{
|
||||
(var terminalNode, _) = ExecuteActions(expandedNode, actions[..actionCount], true);
|
||||
return (terminalNode, currentCompletionState, score);
|
||||
@@ -213,7 +209,7 @@ public class Solver
|
||||
|
||||
public void Search(Node startNode)
|
||||
{
|
||||
for (var i = 0; i < Iterations; i++)
|
||||
for (var i = 0; i < Config.Iterations; i++)
|
||||
{
|
||||
var selectedNode = Select(startNode);
|
||||
var (endNode, _, score) = ExpandAndRollout(selectedNode);
|
||||
@@ -236,11 +232,11 @@ public class Solver
|
||||
return (actions, node.State);
|
||||
}
|
||||
|
||||
public static (List<ActionType> Actions, SimulationState State) SearchStepwise(SimulationInput input, Action<ActionType>? actionCallback)
|
||||
public static (List<ActionType> Actions, SimulationState State) SearchStepwise(SolverConfig config, SimulationInput input, Action<ActionType>? actionCallback)
|
||||
{
|
||||
var state = new SimulationState(input);
|
||||
var actions = new List<ActionType>();
|
||||
var solver = new Solver(state, true);
|
||||
var solver = new Solver(config, state, true);
|
||||
while (!solver.Simulator.IsComplete)
|
||||
{
|
||||
solver.Search(solver.RootNode);
|
||||
@@ -258,15 +254,15 @@ public class Solver
|
||||
|
||||
actionCallback?.Invoke(chosen_action);
|
||||
|
||||
solver = new Solver(state, true);
|
||||
solver = new Solver(config, state, true);
|
||||
}
|
||||
|
||||
return (actions, state);
|
||||
}
|
||||
|
||||
public static (List<ActionType> Actions, SimulationState State) SearchOneshot(SimulationInput input)
|
||||
public static (List<ActionType> Actions, SimulationState State) SearchOneshot(SolverConfig config, SimulationInput input)
|
||||
{
|
||||
var solver = new Solver(input, false);
|
||||
var solver = new Solver(config, input, false);
|
||||
solver.Search(solver.RootNode);
|
||||
var (solution_actions, solution_node) = solver.Solution();
|
||||
return (solution_actions, solution_node.State);
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Craftimizer.Solver.Crafty;
|
||||
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
public readonly struct SolverConfig
|
||||
{
|
||||
public readonly int Iterations;
|
||||
public readonly float ScoreStorageThreshold;
|
||||
public readonly float MaxScoreWeightingConstant;
|
||||
public readonly float ExplorationConstant;
|
||||
public readonly int MaxStepCount;
|
||||
|
||||
public SolverConfig() : this(30000, 1f, 0.1f, 4, 25) { }
|
||||
|
||||
public SolverConfig(
|
||||
int iterations = 30000,
|
||||
float scoreStorageThreshold = 1f,
|
||||
float maxScoreWeightingConstant = 0.1f,
|
||||
float explorationConstant = 4f,
|
||||
int maxStepCount = 25)
|
||||
{
|
||||
Iterations = iterations;
|
||||
ScoreStorageThreshold = scoreStorageThreshold;
|
||||
MaxScoreWeightingConstant = maxScoreWeightingConstant;
|
||||
ExplorationConstant = explorationConstant;
|
||||
MaxStepCount = maxStepCount;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user