Skip to content

Commit

Permalink
Better node selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
khardey committed Feb 13, 2012
1 parent b414c92 commit 726119d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Axiom.java
Expand Up @@ -772,9 +772,9 @@ public static void main(String args[]) {
Axiom g = new Axiom();
//Player p1 = new HumanPlayer(Cube.BLACK, Cube.WHITE);
//Player p1 = new AlphaBetaPlayer(Cube.BLACK, Cube.WHITE, 4);
Player p1 = new MonteCarloPlayer(Cube.BLACK, Cube.WHITE, 100);
//Player p2 = new AlphaBetaPlayer(Cube.WHITE, Cube.BLACK, 2);
Player p2 = new RandomPlayer(Cube.WHITE, Cube.BLACK);
Player p1 = new MonteCarloPlayer(Cube.BLACK, Cube.WHITE, 2500);
Player p2 = new AlphaBetaPlayer(Cube.WHITE, Cube.BLACK, 2);
// Player p2 = new RandomPlayer(Cube.WHITE, Cube.BLACK);
g.firstPlayer(p1);
g.secondPlayer(p2);
Host.hostGame(g, p1, p2, true);
Expand Down
2 changes: 1 addition & 1 deletion MonteCarloPlayer.java
Expand Up @@ -25,7 +25,7 @@ public int monteCarloMove(BoardGame board){
moveTree.createRoot();
for(int i =0; i < simnum; i++){
moveTree.findMove(board, this);
System.out.print(".");
// System.out.print(".");
}
return moveTree.getBestMove();
}
Expand Down
30 changes: 27 additions & 3 deletions MonteCarloTree.java
Expand Up @@ -41,7 +41,8 @@ public void findMove(BoardGame board, MonteCarloPlayer player){
//Make better method to choose node to explore
//Just trying to get it to select some move, not generating children if visited visitLimit yet.
Random random = new Random();
int move = random.nextInt(root.children.size());
// int move = random.nextInt(root.children.size());
int move = selectNode();
root.children.get(move).visits+= 1;
BoardGame nextBoard = (BoardGame)board.clone();
nextBoard.makeMove(player, move);
Expand All @@ -59,13 +60,36 @@ public void findMove(BoardGame board, MonteCarloPlayer player){
ArrayList<Integer> t = nextBoard.legalMoves(pointer);
int move2 = t.get((int)(Math.random() * t.size()));
nextBoard.makeMove(pointer, move2);
count += 1;
count++;
}
if (nextBoard.hasWon(player.num)) {
root.children.get(move).wins+=1;
}
}

public int selectNode(){
double bestFoundValue = 0;
int bestFoundMove = 0;
for(Node c : root.children){
double value;
if(c.visits > 10){
double winrate = ((double)c.wins / c.visits);
//figure out formula for choosing nodes. Using example for now.
value = winrate + (0.44 * Math.sqrt(Math.log(c.visits) / c.visits));
}
else{
value = 10000 + 1000*Math.random();
}
if(value > bestFoundValue){
bestFoundValue = value;
bestFoundMove = root.children.indexOf(c);
}
}
//System.out.println("Move:" + bestFoundMove + " Value:" + bestFoundValue);
System.out.print(".");
return bestFoundMove;
}

public void generateChildren(Node parent, BoardGame board, MonteCarloPlayer player) {
List<Node> children_list = new ArrayList<Node>();
for (Integer m : board.legalMoves(player)) {
Expand All @@ -81,7 +105,7 @@ public int getBestMove(){
int winning_move = 0;
for (Node c : root.children){
System.out.println(c.wins);
if(c.wins > winner.wins){
if(c.visits > winner.visits){
winner = c;
winning_move = root.children.indexOf(c);
}
Expand Down

0 comments on commit 726119d

Please sign in to comment.