Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added triangle and dog features
  • Loading branch information
Elaine Tsun authored and Elaine Tsun committed Apr 26, 2017
2 parents 85d62db + fa40493 commit 81c065e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/BaseEvaluator.java
Expand Up @@ -28,7 +28,7 @@ public class BaseEvaluator implements Evaluator{
// return 1000; // what should this be?
// }
// else{
// return 0; // assuming only positive evalutions
// return -1000; // assuming only positive evalutions
// }
//}
double[] params = s.getFeatures(player);
Expand Down
59 changes: 57 additions & 2 deletions src/CheckersGameState3.java
Expand Up @@ -388,16 +388,22 @@ public class CheckersGameState3 implements CheckersGameState{
11: ^ same but for the two smaller diagonals
]
*/
private boolean king(int piece){
return (piece == 3 || piece == 4);
}

public double[] getFeatures(int player){
double[] features = new double[12];
double total = 0.0;
double mypieces = 0.0;
for(int i = 0; i<this.board.length; i++){
if(i%9!=8){ //valid square
if(this.board[i] != 0 ) total+=1.0;
if(this.board[i] != 0 ) total+=2.0;
if(king(this.board[i])) total+=1.0; // pawn is 2, king is 3
/****my pieces (pawns and kings)*****/
if(myPiece(this.board[i], player)){
mypieces+=1.0;
mypieces+=2.0;
if(king(this.board[i])) mypieces+=1.0; // pawn is 2, king is 3
if(isLoner(i)) features[1] +=1.0;
if(isSafe(i)) features[2] +=1.0;
/*****pawns features****/
Expand Down Expand Up @@ -430,6 +436,50 @@ public class CheckersGameState3 implements CheckersGameState{
features[11] = numOnDiag1(player) + numOnDiag2(player);
return features;
}

/* computes feature vector:
[0: piece-ratio,
1: # of moveable pawns + 2*#of moveable kings
2: num attacking pieces
3: central pieces
4: # pawns on diagonal + 2 * # kings on diagonal
5: ^ same but for the two smaller diagonals
]
*/
public double[] getEndGameFeatures(int player){
double[] features = new double[6];
double total = 0.0;
double mypieces = 0.0;
for(int i = 0; i<this.board.length; i++){
if(i%9!=8){ //valid square
if(this.board[i] != 0 ) total+=1.0;
/****my pieces (pawns and kings)*****/
if(myPiece(this.board[i], player)){
mypieces+=1.0;
/*****pawns features****/
if(this.board[i] == player){
if(pawn_can_move(i)) features[1] += 1.0; //moveable pawns
if(i == 10 || i == 11 || i == 14 || i == 15 || i == 19 || i == 20 || i == 23 || i ==24){
features[3] +=1.0; //central pawns
}
}
/****kings features****/
else if(this.board[i] == player+2){
if(king_can_move(i)) features[1] += 2.0; //add to aggregate distance of the kings
if(i == 10 || i == 11 || i == 14 || i == 15 || i == 19 || i == 20 || i == 23 || i ==24){
features[3] +=2.0; //central kings
}
}
}
}
}
features[0] = mypieces/total; //piece ratio
features[2] = numAttacking(player);
features[4] = numOnMainDiag(player);
features[5] = numOnDiag1(player) + numOnDiag2(player);
return features;
}

/* number of pawns and kings on the long diagonal*/
public int numOnMainDiag(int player){
int count = 0;
Expand Down Expand Up @@ -552,6 +602,7 @@ public class CheckersGameState3 implements CheckersGameState{
return false;
}
/* feature: Dog pattern*/
<<<<<<< HEAD
public boolean isDog(int player){
if(player==1){
if((this.board[0]==1 || this.board[0]==3) && (this.board[4]==2||this.board[4]==2)){
Expand All @@ -563,6 +614,10 @@ public class CheckersGameState3 implements CheckersGameState{
return true;
}
}
=======
public boolean isDog(int player){

>>>>>>> fa404932a69ef682b050341bcfdb0b3f7f825bf2
return false;
}
public boolean isTerminal(){
Expand Down
33 changes: 17 additions & 16 deletions src/Learn.java
Expand Up @@ -22,30 +22,29 @@ public class Learn{
final int num_games = 30;
final int iterations = 3;

Random rand = new Random();
for(int j = 0; j < iterations; j++){
for(int i = 1; i <= num_games; i++){ // play num_games amount of games
System.out.println("playing game " + i);
play(alpha, beta, le, true); // alpha and beta play a game
int player = rand.nextInt(2) + 1; // choose which player alpha plays as
play(alpha, beta, le, player, true); // alpha and beta play a game
le.updateWeights(.1); // get new weights using data from game
}
faceBeta(alpha, beta, le, be);
}
}

public static void faceBeta(CheckersAI alpha, CheckersAI beta, LearningEvaluator le, BaseEvaluator be){
int won = 0;
boolean w;
boolean w1;
boolean w2;
CheckersGameState s;
System.out.println("facing beta");
for(int i = 0; i < 10; i++){
s = new CheckersGameState3();
w = play(alpha, beta, le, false);
if(w){
won++;
}
}
System.out.println("alpha won " + won + " times");
if(won >= 7){
s = new CheckersGameState3();
w1 = play(alpha, beta, le, 1, false);
w2 = play(alpha, beta, le, 2, false);

System.out.println("alpha won " + w1 + " " + w2);
if(w1 && w2){
System.out.println("updating beta");
le.commitWeights("../src/weights/beta.csv");
be.refreshWeights();
Expand All @@ -61,21 +60,22 @@ public class Learn{



public static boolean play(CheckersAI alpha, CheckersAI beta, LearningEvaluator le, boolean learning){
public static boolean play(CheckersAI alpha, CheckersAI beta, LearningEvaluator le, int player, boolean learning){
CheckersGameState current = new CheckersGameState3();
Random rand = new Random();
int player = rand.nextInt(2) + 1; // choose which player alpha plays as
int other = 1 - (player - 1) + 1;
alpha.setPlayer(player);
beta.setPlayer(other);
int moves = 0;
if(other == 1){ // if beta goes first, make a move
current = current.result(beta.minimax(current, 7));
moves++;
}
int same_moves = 0;
Move lastmove = null;
Move secondlast = null;
while(!current.isTerminal() && same_moves <= 3){
while(!current.isTerminal() && same_moves <= 3 && moves <= 100){
Move next = alpha.minimax(current, 7); // get alpha's move
moves++;
if(secondlast != null && next.toString().equals(secondlast.toString())){
same_moves++;
}
Expand All @@ -85,6 +85,7 @@ public class Learn{
le.addData(current.getFeatures(alpha.getPlayer()), next.getValue()); // add this moves data to the data set (the value of the state is stored in the move. there is probably a better way to do this)
}
current = current.result(next); // make the move
moves++;
//current.printState();
if(current.isTerminal()){ // if alpha won, then break
break;
Expand Down

0 comments on commit 81c065e

Please sign in to comment.