From e6d27b328bed029b591f7ec7580ba8e07a1a5874 Mon Sep 17 00:00:00 2001 From: Jon Bergland Date: Tue, 6 Feb 2024 22:35:09 +0100 Subject: [PATCH] Fix(#28): Remove bug that didn't sort the priorityActions before evaluating and deepcopied depth --- ...terativeDeepeningAlphaBetaPruningMinimax.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/backend/src/main/java/board/master/model/agents/IterativeDeepeningAlphaBetaPruningMinimax.java b/backend/src/main/java/board/master/model/agents/IterativeDeepeningAlphaBetaPruningMinimax.java index 00c3d4f..e5af2c0 100644 --- a/backend/src/main/java/board/master/model/agents/IterativeDeepeningAlphaBetaPruningMinimax.java +++ b/backend/src/main/java/board/master/model/agents/IterativeDeepeningAlphaBetaPruningMinimax.java @@ -47,10 +47,11 @@ public Action getAction(StateHandler state) { int depth = 1; - List priorityActions = state.getActions(); - priorityActions.stream() + List priorityActions = state.getActions().stream() .sorted(Comparator.comparingInt(action -> state.result(action).utility(agentPlayerId))) .collect(Collectors.toList()); + + Collections.reverse(priorityActions); while (!isTimeUp()) { //Find the best move ordering @@ -72,16 +73,18 @@ private float evaluateState(StateHandler state, float alpha, float beta, int dep return state.utility(agentPlayerId); } - List priorityActions = state.getActions(); - priorityActions.stream() + List priorityActions = state.getActions().stream() .sorted(Comparator.comparingInt(action -> state.result(action).utility(agentPlayerId))) .collect(Collectors.toList()); float value; + int minusDepth = depth - 1; + if (isMaximizingPlayer) { + Collections.reverse(priorityActions); value = alpha; for (Action action : priorityActions) { - value = Math.max(value, evaluateState(state.result(action), value, beta, depth--, !isMaximizingPlayer)); + value = Math.max(value, evaluateState(state.result(action), value, beta, minusDepth, !isMaximizingPlayer)); if (value >= beta) { break; } @@ -90,10 +93,9 @@ private float evaluateState(StateHandler state, float alpha, float beta, int dep return value; } else { - Collections.reverse(priorityActions); value = beta; for (Action action : priorityActions) { - value = Math.min(value, evaluateState(state.result(action), alpha, value, depth--, !isMaximizingPlayer)); + value = Math.min(value, evaluateState(state.result(action), alpha, value, minusDepth, !isMaximizingPlayer)); if (alpha >= value) { break; }