Skip to content
Snippets Groups Projects
Commit dbb4ec6b authored by Piers Williams's avatar Piers Williams
Browse files

Some work on scoring for MCTS

parent 66579ffa
No related branches found
No related tags found
No related merge requests found
package battle.controllers.Piers;
import asteroids.Action;
import asteroids.GameState;
import battle.SimpleBattle;
import java.util.Random;
......@@ -19,17 +19,21 @@ public class MCTSNode {
private MCTSNode parent;
private MCTSNode[] children;
private int numberOfChildrenExpanded;
private boolean ourNode;
private int currentDepth;
private int playerID;
private double totalValue;
private int numberOfVisits = 1;
private double explorationConstant;
public MCTSNode(double explorationConstant) {
public MCTSNode(double explorationConstant, int playerID) {
this.explorationConstant = explorationConstant;
currentDepth = 0;
this.playerID = playerID;
ourNode = true;
}
private MCTSNode(MCTSNode parent, Action moveToThisState) {
......@@ -38,6 +42,8 @@ public class MCTSNode {
this.parent = parent;
this.children = new MCTSNode[MCTSNode.allActions.length];
this.currentDepth = parent.currentDepth + 1;
this.playerID = parent.playerID;
this.ourNode = parent.ourNode;
}
public static void setAllActions() {
......@@ -53,7 +59,7 @@ public class MCTSNode {
}
}
public MCTSNode select(GameState state, int maxDepth) {
public MCTSNode select(SimpleBattle state, int maxDepth) {
MCTSNode current = this;
while (current.currentDepth <= maxDepth) {
if (current.fullyExpanded()) {
......@@ -93,17 +99,22 @@ public class MCTSNode {
(explorationConstant * (Math.sqrt(Math.log(parent.numberOfVisits) / numberOfVisits)));
}
public void updateValues(double value) {
public void updateValues(double value, double enemyScore) {
MCTSNode current = this;
while (current.parent != null) {
current.numberOfVisits++;
// todo work out scoring system.
current.totalValue += (current.ourNode) ? value : enemyScore;
current = current.parent;
}
}
public double rollout(GameState state) {
return 0.0d;
public double[] rollout(SimpleBattle state) {
while (!state.isGameOver()) {
Action first = allActions[random.nextInt(allActions.length)];
Action second = allActions[random.nextInt(allActions.length)];
state.update(first, second);
}
return new double[]{state.getPoints(playerID), state.getPoints((playerID == 1) ? 0 : 1)};
}
public Action getBestAction() {
......
package battle.controllers.Piers;
/**
* Created by pwillic on 12/06/2015.
*/
public class PiersBattleTest {
public static void main(String[] args) {
}
}
......@@ -11,7 +11,7 @@ public class PiersMCTS implements BattleController {
@Override
public Action getAction(SimpleBattle gameStateCopy, int playerId) {
MCTSNode root = new MCTSNode(2.0);
MCTSNode root = new MCTSNode(2.0, playerId);
return null;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment