Remove unused NodeData instantiations
This commit is contained in:
@@ -56,4 +56,9 @@ public struct ActionSet
|
|||||||
public readonly ActionType ElementAt(int index) => ToAction(NthBitSet(bits, index) - 1);
|
public readonly ActionType ElementAt(int index) => ToAction(NthBitSet(bits, index) - 1);
|
||||||
|
|
||||||
public readonly int Count => BitOperations.PopCount(bits);
|
public readonly int Count => BitOperations.PopCount(bits);
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public readonly ActionType SelectRandom(Random random) => ElementAt(random.Next(Count));
|
||||||
|
|
||||||
|
public readonly ActionType First() => ElementAt(0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,10 +10,7 @@ public readonly struct SimulationNode
|
|||||||
public readonly CompletionState SimulationCompletionState;
|
public readonly CompletionState SimulationCompletionState;
|
||||||
public readonly NodeData Data;
|
public readonly NodeData Data;
|
||||||
|
|
||||||
public CompletionState CompletionState =>
|
public CompletionState CompletionState => GetCompletionState(SimulationCompletionState, Data.AvailableActions);
|
||||||
Data.AvailableActions.Count == 0 && SimulationCompletionState == CompletionState.Incomplete ?
|
|
||||||
CompletionState.NoMoreActions :
|
|
||||||
SimulationCompletionState;
|
|
||||||
|
|
||||||
public bool IsComplete => CompletionState != CompletionState.Incomplete;
|
public bool IsComplete => CompletionState != CompletionState.Incomplete;
|
||||||
|
|
||||||
@@ -25,9 +22,16 @@ public readonly struct SimulationNode
|
|||||||
Data = data;
|
Data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float? CalculateScore()
|
public static CompletionState GetCompletionState(CompletionState simCompletionState, ActionSet actions) =>
|
||||||
|
actions.Count == 0 && simCompletionState == CompletionState.Incomplete ?
|
||||||
|
CompletionState.NoMoreActions :
|
||||||
|
simCompletionState;
|
||||||
|
|
||||||
|
public float? CalculateScore() => CalculateScoreForState(State, SimulationCompletionState);
|
||||||
|
|
||||||
|
public static float? CalculateScoreForState(SimulationState state, CompletionState completionState)
|
||||||
{
|
{
|
||||||
if (CompletionState != CompletionState.ProgressComplete)
|
if (completionState != CompletionState.ProgressComplete)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
static float Apply(float bonus, float value, float target) =>
|
static float Apply(float bonus, float value, float target) =>
|
||||||
@@ -41,30 +45,30 @@ public readonly struct SimulationNode
|
|||||||
|
|
||||||
var progressScore = Apply(
|
var progressScore = Apply(
|
||||||
progressBonus,
|
progressBonus,
|
||||||
State.Progress,
|
state.Progress,
|
||||||
State.Input.Recipe.MaxProgress
|
state.Input.Recipe.MaxProgress
|
||||||
);
|
);
|
||||||
|
|
||||||
var qualityScore = Apply(
|
var qualityScore = Apply(
|
||||||
qualityBonus,
|
qualityBonus,
|
||||||
State.Quality,
|
state.Quality,
|
||||||
State.Input.Recipe.MaxQuality
|
state.Input.Recipe.MaxQuality
|
||||||
);
|
);
|
||||||
|
|
||||||
var durabilityScore = Apply(
|
var durabilityScore = Apply(
|
||||||
durabilityBonus,
|
durabilityBonus,
|
||||||
State.Durability,
|
state.Durability,
|
||||||
State.Input.Recipe.MaxDurability
|
state.Input.Recipe.MaxDurability
|
||||||
);
|
);
|
||||||
|
|
||||||
var cpScore = Apply(
|
var cpScore = Apply(
|
||||||
cpBonus,
|
cpBonus,
|
||||||
State.CP,
|
state.CP,
|
||||||
State.Input.Stats.CP
|
state.Input.Stats.CP
|
||||||
);
|
);
|
||||||
|
|
||||||
var fewerStepsScore =
|
var fewerStepsScore =
|
||||||
fewerStepsBonus * (1f - ((float)(State.ActionCount + 1) / Solver.MaxStepCount));
|
fewerStepsBonus * (1f - ((float)(state.ActionCount + 1) / Solver.MaxStepCount));
|
||||||
|
|
||||||
return progressScore + qualityScore + durabilityScore + cpScore + fewerStepsScore;
|
return progressScore + qualityScore + durabilityScore + cpScore + fewerStepsScore;
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-16
@@ -36,14 +36,20 @@ public class Solver
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
private SimulationNode Execute(SimulationState state, ActionType action, bool strict)
|
private (SimulationState NewState, CompletionState SimulatorCompletionState, ActionSet AvailableActions) ExecuteSimple(SimulationState state, ActionType action, bool strict)
|
||||||
{
|
{
|
||||||
(_, var newState) = Simulator.Execute(state, action);
|
(_, var newState) = Simulator.Execute(state, action);
|
||||||
|
return (newState, Simulator.CompletionState, Simulator.AvailableActionsHeuristic(strict));
|
||||||
|
}
|
||||||
|
|
||||||
|
private SimulationNode Execute(SimulationState state, ActionType action, bool strict)
|
||||||
|
{
|
||||||
|
(var newState, var completionState, var newActions) = ExecuteSimple(state, action, strict);
|
||||||
return new(
|
return new(
|
||||||
newState,
|
newState,
|
||||||
action,
|
action,
|
||||||
Simulator.CompletionState,
|
completionState,
|
||||||
new() { AvailableActions = Simulator.AvailableActionsHeuristic(strict) }
|
new() { AvailableActions = newActions }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,37 +164,38 @@ public class Solver
|
|||||||
if (initialState.IsComplete)
|
if (initialState.IsComplete)
|
||||||
return (initialNode, initialState.CompletionState, initialState.CalculateScore() ?? 0);
|
return (initialNode, initialState.CompletionState, initialState.CalculateScore() ?? 0);
|
||||||
|
|
||||||
var randomIdx = 0;// Random.Next(initialState.Data.AvailableActions.Count);
|
var randomAction = initialState.Data.AvailableActions.First();
|
||||||
var randomAction = initialState.Data.AvailableActions.ElementAt(randomIdx);
|
|
||||||
initialState.Data.AvailableActions.RemoveAction(randomAction);
|
initialState.Data.AvailableActions.RemoveAction(randomAction);
|
||||||
var expandedState = Execute(initialState.State, randomAction, true);
|
var expandedNode = initialNode.Add(Execute(initialState.State, randomAction, true));
|
||||||
var expandedNode = initialNode.Add(expandedState);
|
|
||||||
|
|
||||||
// playout to a terminal state
|
// playout to a terminal state
|
||||||
var currentState = expandedNode.State;
|
var currentState = expandedNode.State.State;
|
||||||
|
var currentCompletionState = expandedNode.State.SimulationCompletionState;
|
||||||
|
var currentActions = expandedNode.State.Data.AvailableActions;
|
||||||
|
|
||||||
byte actionCount = 0;
|
byte actionCount = 0;
|
||||||
Span<ActionType> actions = stackalloc ActionType[MaxStepCount];
|
Span<ActionType> actions = stackalloc ActionType[MaxStepCount];
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (currentState.IsComplete)
|
if (SimulationNode.GetCompletionState(currentCompletionState, currentActions) != CompletionState.Incomplete)
|
||||||
break;
|
break;
|
||||||
randomIdx = 0;// Random.Next(currentState.Data.AvailableActions.Count);
|
randomAction = currentActions.First();
|
||||||
randomAction = currentState.Data.AvailableActions.ElementAt(randomIdx);
|
|
||||||
actions[actionCount++] = randomAction;
|
actions[actionCount++] = randomAction;
|
||||||
currentState = Execute(currentState.State, randomAction, true);
|
(currentState, currentCompletionState, currentActions) = ExecuteSimple(currentState, randomAction, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// store the result if a max score was reached
|
// store the result if a max score was reached
|
||||||
var score = currentState.CalculateScore() ?? 0;
|
currentCompletionState = SimulationNode.GetCompletionState(currentCompletionState, currentActions);
|
||||||
if (currentState.CompletionState == CompletionState.ProgressComplete)
|
var score = SimulationNode.CalculateScoreForState(currentState, currentCompletionState) ?? 0;
|
||||||
|
if (currentCompletionState == CompletionState.ProgressComplete)
|
||||||
{
|
{
|
||||||
if (score >= ScoreStorageThreshold && score >= RootNode.State.Data.Scores.MaxScore)
|
if (score >= ScoreStorageThreshold && score >= RootNode.State.Data.Scores.MaxScore)
|
||||||
{
|
{
|
||||||
(var terminalNode, _) = ExecuteActions(expandedNode, actions[..actionCount], true);
|
(var terminalNode, _) = ExecuteActions(expandedNode, actions[..actionCount], true);
|
||||||
return (terminalNode, currentState.CompletionState, score);
|
return (terminalNode, currentCompletionState, score);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (expandedNode, currentState.CompletionState, score);
|
return (expandedNode, currentCompletionState, score);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Backpropagate(Node startNode, Node targetNode, float score)
|
public static void Backpropagate(Node startNode, Node targetNode, float score)
|
||||||
|
|||||||
Reference in New Issue
Block a user