Created
May 28, 2018 15:22
-
-
Save jcbribeiro/a74406aabc94136cce08bd68a37456bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Minimax extends GameAlgorithm { | |
| //MINIMAX-DECISION(state) returns an action | |
| // max = -∞ | |
| // For each a from ACTIONS(state) do | |
| // v = MIN-VALUE(RESULT(state, a), 1) | |
| // If (v > max) max = v; action = a | |
| // returns action | |
| @Override | |
| public Action takeDecision(AgentState currentState) { | |
| Action nextAction = null; | |
| double max = Double.NEGATIVE_INFINITY; // pior valor possível | |
| double actionValue; | |
| List<Action> actions = currentState.getActions(); | |
| for (Action action : | |
| actions) { | |
| AgentState resultState = currentState.result(action); | |
| actionValue = minValue(resultState, 1); | |
| if (nextAction == null || actionValue > max) { | |
| max = actionValue; | |
| nextAction = action; | |
| } | |
| } | |
| return nextAction; | |
| } | |
| //MAX-VALUE(state, depth) returns the minimax value of state | |
| // If TERMINAL-TEST(state, depth) returns EVALUATION-FUNCION(state) | |
| // v = -∞ | |
| // For each a from ACTIONS(state) do | |
| // v = max(v, MIN-VALUE(RESULT(state, a), depth + 1)) | |
| // returns v | |
| private double maxValue(AgentState state, int depth) { | |
| if (depth == depthLimit || state.isEndOfGameState()) { | |
| return state.evaluate(); | |
| } | |
| double max = Double.NEGATIVE_INFINITY; | |
| List<Action> actions = state.getActions(); | |
| for (Action action : | |
| actions) { | |
| AgentState resultState = state.result(action); | |
| double actionValue = minValue(resultState, depth + 1); | |
| max = Math.max(max, actionValue); | |
| } | |
| return max; | |
| } | |
| //MIN-VALUE(state, depth) returns the minimax value of state | |
| // If TERMINAL-TEST(state, depth) returns EVALUATION-FUNCION(state) | |
| // v = +∞ | |
| // For each a from ACTIONS(state) do | |
| // v = min(v, MAX-VALUE(RESULT(state, a), depth + 1)) | |
| // returns v | |
| private double minValue(AgentState state, int depth) { | |
| if (depth == depthLimit || state.isEndOfGameState()) { | |
| return state.evaluate(); | |
| } | |
| double min = Double.POSITIVE_INFINITY; | |
| List<Action> actions = state.getActions(); | |
| for (Action action : | |
| actions) { | |
| AgentState resultState = state.result(action); | |
| double actionValue = maxValue(resultState, depth + 1); | |
| min = Math.min(min, actionValue); | |
| } | |
| return min; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment