From 7f9e7ec4e0e4a21610b297e482b769e0adc887db Mon Sep 17 00:00:00 2001 From: Piers <pwillic@essex.ac.uk> Date: Tue, 16 Jun 2015 16:38:32 +0100 Subject: [PATCH] Piers MCTS Work --- src/battle/controllers/Piers/BetterMCTSNode.java | 11 +++++++---- src/battle/controllers/Piers/PiersMCTS.java | 8 ++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/battle/controllers/Piers/BetterMCTSNode.java b/src/battle/controllers/Piers/BetterMCTSNode.java index 727442d..d50424c 100644 --- a/src/battle/controllers/Piers/BetterMCTSNode.java +++ b/src/battle/controllers/Piers/BetterMCTSNode.java @@ -26,13 +26,15 @@ public class BetterMCTSNode { private int numberOfVisits = 1; private double explorationConstant; + private PiersMCTS mcts; - public BetterMCTSNode(double explorationConstant, int playerID) { + public BetterMCTSNode(double explorationConstant, int playerID, PiersMCTS mcts) { this.explorationConstant = explorationConstant; currentDepth = 0; this.playerID = playerID; ourNode = true; children = new BetterMCTSNode[allActions.length]; + this.mcts = mcts; } private BetterMCTSNode(BetterMCTSNode parent, Action ourMoveToThisState) { @@ -43,6 +45,7 @@ public class BetterMCTSNode { this.currentDepth = parent.currentDepth + 1; this.playerID = parent.playerID; this.ourNode = parent.ourNode; + this.mcts = parent.mcts; } public static void setAllActions() { @@ -63,7 +66,7 @@ public class BetterMCTSNode { while (current.currentDepth <= maxDepth) { if (current.fullyExpanded()) { current = current.selectBestChild(); - for (int i = 0; i < PiersMCTS.ACTIONS_PER_MACRO; i++) { + for (int i = 0; i < mcts.ACTIONS_PER_MACRO; i++) { state.update(current.ourMoveToThisState, allActions[random.nextInt(allActions.length)]); } } else { @@ -85,7 +88,7 @@ public class BetterMCTSNode { childToExpand = random.nextInt(allActions.length); } children[childToExpand] = new BetterMCTSNode(this, allActions[childToExpand]); - for (int i = 0; i < PiersMCTS.ACTIONS_PER_MACRO; i++) { + for (int i = 0; i < mcts.ACTIONS_PER_MACRO; i++) { state.update(allActions[childToExpand], allActions[random.nextInt(allActions.length)]); } numberOfChildrenExpanded++; @@ -131,7 +134,7 @@ public class BetterMCTSNode { while (maxDepth > currentRolloutDepth && !state.isGameOver()) { Action first = allActions[random.nextInt(allActions.length)]; Action second = allActions[random.nextInt(allActions.length)]; - for(int i = 0; i < PiersMCTS.ACTIONS_PER_MACRO; i++) { + for (int i = 0; i < mcts.ACTIONS_PER_MACRO; i++) { state.update(first, second); } currentRolloutDepth++; diff --git a/src/battle/controllers/Piers/PiersMCTS.java b/src/battle/controllers/Piers/PiersMCTS.java index 047e477..aa4e071 100644 --- a/src/battle/controllers/Piers/PiersMCTS.java +++ b/src/battle/controllers/Piers/PiersMCTS.java @@ -25,19 +25,19 @@ public class PiersMCTS implements BattleController { @Override public Action getAction(SimpleBattle gameStateCopy, int playerId) { GameTimer timer = new GameTimer(); - timer.setTimeBudgetMilliseconds(40); + timer.setTimeBudgetMilliseconds(25); Action action = currentBestAction.getAction(); double distance = gameStateCopy.getShip(0).s.dist(gameStateCopy.getShip(1).s); ACTIONS_PER_MACRO = (distance > DISTANCE_THRESHOLD) ? ACTIONS_PER_MACRO_ENEMY_FAR : ACTIONS_PER_MACRO_ENEMY_CLOSE; - if (root == null) root = new BetterMCTSNode(2.0, playerId); - if (currentBestAction.getTimesUsed() >= ACTIONS_PER_MACRO) root = new BetterMCTSNode(2.0, playerId); + if (root == null) root = new BetterMCTSNode(2.0, playerId, this); + if (currentBestAction.getTimesUsed() >= ACTIONS_PER_MACRO) root = new BetterMCTSNode(2.0, playerId, this); int i = 0; while (timer.remainingTimePercent() > 10) { SimpleBattle copy = gameStateCopy.clone(); - BetterMCTSNode travel = root.select(copy, 6); + BetterMCTSNode travel = root.select(copy, 3); double result = travel.rollout(copy, 10); travel.updateValues(result); i++; -- GitLab