Remove all unncessesary data instantiations
This commit is contained in:
@@ -4,7 +4,7 @@ namespace Craftimizer.Solver.Crafty;
|
|||||||
|
|
||||||
public class ArenaNode<T> where T : struct
|
public class ArenaNode<T> where T : struct
|
||||||
{
|
{
|
||||||
public readonly T State;
|
public T State;
|
||||||
public readonly List<ArenaNode<T>> Children;
|
public readonly List<ArenaNode<T>> Children;
|
||||||
public readonly ArenaNode<T>? Parent;
|
public readonly ArenaNode<T>? Parent;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace Craftimizer.Solver.Crafty;
|
|
||||||
|
|
||||||
public class NodeData
|
|
||||||
{
|
|
||||||
public ActionSet AvailableActions;
|
|
||||||
public NodeScores Scores;
|
|
||||||
}
|
|
||||||
@@ -3,23 +3,25 @@ using Craftimizer.Simulator.Actions;
|
|||||||
|
|
||||||
namespace Craftimizer.Solver.Crafty;
|
namespace Craftimizer.Solver.Crafty;
|
||||||
|
|
||||||
public readonly struct SimulationNode
|
public struct SimulationNode
|
||||||
{
|
{
|
||||||
public readonly SimulationState State;
|
public readonly SimulationState State;
|
||||||
public readonly ActionType? Action;
|
public readonly ActionType? Action;
|
||||||
public readonly CompletionState SimulationCompletionState;
|
public readonly CompletionState SimulationCompletionState;
|
||||||
public readonly NodeData Data;
|
|
||||||
|
|
||||||
public CompletionState CompletionState => GetCompletionState(SimulationCompletionState, Data.AvailableActions);
|
public ActionSet AvailableActions;
|
||||||
|
public NodeScores Scores;
|
||||||
|
|
||||||
|
public CompletionState CompletionState => GetCompletionState(SimulationCompletionState, AvailableActions);
|
||||||
|
|
||||||
public bool IsComplete => CompletionState != CompletionState.Incomplete;
|
public bool IsComplete => CompletionState != CompletionState.Incomplete;
|
||||||
|
|
||||||
public SimulationNode(SimulationState state, ActionType? action, CompletionState completionState, NodeData data)
|
public SimulationNode(SimulationState state, ActionType? action, CompletionState completionState, ActionSet actions)
|
||||||
{
|
{
|
||||||
State = state;
|
State = state;
|
||||||
Action = action;
|
Action = action;
|
||||||
SimulationCompletionState = completionState;
|
SimulationCompletionState = completionState;
|
||||||
Data = data;
|
AvailableActions = actions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CompletionState GetCompletionState(CompletionState simCompletionState, ActionSet actions) =>
|
public static CompletionState GetCompletionState(CompletionState simCompletionState, ActionSet actions) =>
|
||||||
|
|||||||
+15
-15
@@ -28,7 +28,7 @@ public class Solver
|
|||||||
state,
|
state,
|
||||||
null,
|
null,
|
||||||
Simulator.CompletionState,
|
Simulator.CompletionState,
|
||||||
new() { AvailableActions = Simulator.AvailableActionsHeuristic(strict) }
|
Simulator.AvailableActionsHeuristic(strict)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ public class Solver
|
|||||||
newState,
|
newState,
|
||||||
action,
|
action,
|
||||||
completionState,
|
completionState,
|
||||||
new() { AvailableActions = newActions }
|
newActions
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,9 +61,9 @@ public class Solver
|
|||||||
if (state.IsComplete)
|
if (state.IsComplete)
|
||||||
return (startNode, state.CompletionState);
|
return (startNode, state.CompletionState);
|
||||||
|
|
||||||
if (!state.Data.AvailableActions.HasAction(action))
|
if (!state.AvailableActions.HasAction(action))
|
||||||
return (startNode, CompletionState.InvalidAction);
|
return (startNode, CompletionState.InvalidAction);
|
||||||
state.Data.AvailableActions.RemoveAction(action);
|
state.AvailableActions.RemoveAction(action);
|
||||||
|
|
||||||
startNode = startNode.Add(Execute(state.State, action, strict));
|
startNode = startNode.Add(Execute(state.State, action, strict));
|
||||||
}
|
}
|
||||||
@@ -123,7 +123,7 @@ public class Solver
|
|||||||
|
|
||||||
for (var j = 0; j < iterCount; ++j)
|
for (var j = 0; j < iterCount; ++j)
|
||||||
{
|
{
|
||||||
var node = children[i + j].State.Data.Scores;
|
var node = children[i + j].State.Scores;
|
||||||
scoreSums[j] = node.ScoreSum;
|
scoreSums[j] = node.ScoreSum;
|
||||||
visits[j] = node.Visits;
|
visits[j] = node.Visits;
|
||||||
maxScores[j] = node.MaxScore;
|
maxScores[j] = node.MaxScore;
|
||||||
@@ -147,31 +147,31 @@ public class Solver
|
|||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
var expandable = selectedNode.State.Data.AvailableActions.Count != 0;
|
var expandable = selectedNode.State.AvailableActions.Count != 0;
|
||||||
var likelyTerminal = selectedNode.Children.Count == 0;
|
var likelyTerminal = selectedNode.Children.Count == 0;
|
||||||
if (expandable || likelyTerminal)
|
if (expandable || likelyTerminal)
|
||||||
return selectedNode;
|
return selectedNode;
|
||||||
|
|
||||||
// select the node with the highest score
|
// select the node with the highest score
|
||||||
selectedNode = EvalBestChild(selectedNode.State.Data.Scores.Visits, CollectionsMarshal.AsSpan(selectedNode.Children));
|
selectedNode = EvalBestChild(selectedNode.State.Scores.Visits, CollectionsMarshal.AsSpan(selectedNode.Children));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public (Node ExpandedNode, CompletionState State, float Score) ExpandAndRollout(Node initialNode)
|
public (Node ExpandedNode, CompletionState State, float Score) ExpandAndRollout(Node initialNode)
|
||||||
{
|
{
|
||||||
var initialState = initialNode.State;
|
ref var initialState = ref initialNode.State;
|
||||||
// expand once
|
// expand once
|
||||||
if (initialState.IsComplete)
|
if (initialState.IsComplete)
|
||||||
return (initialNode, initialState.CompletionState, initialState.CalculateScore() ?? 0);
|
return (initialNode, initialState.CompletionState, initialState.CalculateScore() ?? 0);
|
||||||
|
|
||||||
var randomAction = initialState.Data.AvailableActions.First();
|
var randomAction = initialState.AvailableActions.First();
|
||||||
initialState.Data.AvailableActions.RemoveAction(randomAction);
|
initialState.AvailableActions.RemoveAction(randomAction);
|
||||||
var expandedNode = initialNode.Add(Execute(initialState.State, randomAction, true));
|
var expandedNode = initialNode.Add(Execute(initialState.State, randomAction, true));
|
||||||
|
|
||||||
// playout to a terminal state
|
// playout to a terminal state
|
||||||
var currentState = expandedNode.State.State;
|
var currentState = expandedNode.State.State;
|
||||||
var currentCompletionState = expandedNode.State.SimulationCompletionState;
|
var currentCompletionState = expandedNode.State.SimulationCompletionState;
|
||||||
var currentActions = expandedNode.State.Data.AvailableActions;
|
var currentActions = expandedNode.State.AvailableActions;
|
||||||
|
|
||||||
byte actionCount = 0;
|
byte actionCount = 0;
|
||||||
Span<ActionType> actions = stackalloc ActionType[MaxStepCount];
|
Span<ActionType> actions = stackalloc ActionType[MaxStepCount];
|
||||||
@@ -189,7 +189,7 @@ public class Solver
|
|||||||
var score = SimulationNode.CalculateScoreForState(currentState, currentCompletionState) ?? 0;
|
var score = SimulationNode.CalculateScoreForState(currentState, currentCompletionState) ?? 0;
|
||||||
if (currentCompletionState == CompletionState.ProgressComplete)
|
if (currentCompletionState == CompletionState.ProgressComplete)
|
||||||
{
|
{
|
||||||
if (score >= ScoreStorageThreshold && score >= RootNode.State.Data.Scores.MaxScore)
|
if (score >= ScoreStorageThreshold && score >= RootNode.State.Scores.MaxScore)
|
||||||
{
|
{
|
||||||
(var terminalNode, _) = ExecuteActions(expandedNode, actions[..actionCount], true);
|
(var terminalNode, _) = ExecuteActions(expandedNode, actions[..actionCount], true);
|
||||||
return (terminalNode, currentCompletionState, score);
|
return (terminalNode, currentCompletionState, score);
|
||||||
@@ -202,7 +202,7 @@ public class Solver
|
|||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
startNode.State.Data.Scores.Visit(score);
|
startNode.State.Scores.Visit(score);
|
||||||
|
|
||||||
if (startNode == targetNode)
|
if (startNode == targetNode)
|
||||||
break;
|
break;
|
||||||
@@ -228,7 +228,7 @@ public class Solver
|
|||||||
var node = RootNode;
|
var node = RootNode;
|
||||||
while (node.Children.Count != 0)
|
while (node.Children.Count != 0)
|
||||||
{
|
{
|
||||||
node = RustMaxBy<Node>(CollectionsMarshal.AsSpan(node.Children), n => n.State.Data.Scores.MaxScore);
|
node = RustMaxBy<Node>(CollectionsMarshal.AsSpan(node.Children), n => n.State.Scores.MaxScore);
|
||||||
if (node.State.Action != null)
|
if (node.State.Action != null)
|
||||||
actions.Add(node.State.Action.Value);
|
actions.Add(node.State.Action.Value);
|
||||||
}
|
}
|
||||||
@@ -246,7 +246,7 @@ public class Solver
|
|||||||
solver.Search(solver.RootNode);
|
solver.Search(solver.RootNode);
|
||||||
var (solution_actions, solution_node) = solver.Solution();
|
var (solution_actions, solution_node) = solver.Solution();
|
||||||
|
|
||||||
if (solution_node.Data.Scores.MaxScore >= 1.0)
|
if (solution_node.Scores.MaxScore >= 1.0)
|
||||||
{
|
{
|
||||||
actions.AddRange(solution_actions);
|
actions.AddRange(solution_actions);
|
||||||
return (actions, solution_node.State);
|
return (actions, solution_node.State);
|
||||||
|
|||||||
Reference in New Issue
Block a user