Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
end game
  • Loading branch information
sos13004 committed Apr 29, 2017
1 parent abd82b4 commit 58ed898
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 43 deletions.
2 changes: 2 additions & 0 deletions src/CheckersGameState.java
Expand Up @@ -7,4 +7,6 @@ boolean isTerminal();
int winner();
void printState ();
public double[] getFeatures(int player);
public boolean isEndGame();
public double[] getEndGameFeatures(int player);
}
52 changes: 28 additions & 24 deletions src/CheckersGameState3.java
Expand Up @@ -388,9 +388,9 @@ public class CheckersGameState3 implements CheckersGameState{

/* computes feature vector:
[0: piece-ratio,
1: loners,
2: safes,
3: 1*#pawns+ 2*#kings
1: loners, //toss?
2: safes, /toss?
3: 1*#pawns+ 2*#kings //toss?
4: # of moveable pawns + 2*#of moveable kings
5: aggregate distance of all pawns to promotion line
6: promotion line opening
Expand All @@ -399,6 +399,9 @@ public class CheckersGameState3 implements CheckersGameState{
9: central pieces
10: # pawns on diagonal + 2 * # kings on diagonal
11: ^ same but for the two smaller diagonals
12: bridge pattern TODO!!!
13: triangle pattern TODO
14: dog pattern TODO
]
*/
private boolean king(int piece){
Expand Down Expand Up @@ -452,18 +455,13 @@ public class CheckersGameState3 implements CheckersGameState{

/* 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: bridge pattern
6: triangle pattern
7: dog pattern
8: opponents kings are on the side.
1: num attacking pieces
2: central pieces
4: opponents kings are on the side.
]
*/
public double[] getEndGameFeatures(int player){
double[] features = new double[9];
double[] features = new double[4];
double total = 0.0;
double mypieces = 0.0;
for(int i = 0; i<this.board.length; i++){
Expand All @@ -474,28 +472,22 @@ public class CheckersGameState3 implements CheckersGameState{
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
features[2] +=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[2] +=2.0; //central kings
}
}
}
}
}
features[0] = mypieces/total; //piece ratio
features[2] = numAttacking(player);
features[4] = numOnDiag1(player) + numOnDiag2(player);
features[5] = bridge(player);
features[6] = triangle(player);
features[7] = dog(player);
features[8] = opponKingsOnSide(player);
features[1] = numAttacking(player);
features[3] = opponKingsOnSide(player);
return features;
}

Expand Down Expand Up @@ -626,7 +618,7 @@ public class CheckersGameState3 implements CheckersGameState{
}
return 0.0;
}
/* feature: triangle patter*/
/* feature: triangle pattern*/
public double triangle(int player){
if(player==2){
if((this.board[33]==2 || this.board[33]==4)
Expand Down Expand Up @@ -682,7 +674,19 @@ public class CheckersGameState3 implements CheckersGameState{
return tot;
}


public boolean isEndGame(){
int mypieces = 0, others = 0, maxPieces =4;
for(int i=0; i<board.length; i++){
if(board[i]!=0){
if(myPiece(board[i],player)) mypieces+=1;
else others+=1;
}
}
if(mypieces <= maxPieces || others <= maxPieces){
return true;
}
else return false;
}

public void printState(){
boolean leading = false;
Expand Down
63 changes: 45 additions & 18 deletions src/Learn.java
@@ -1,14 +1,24 @@
import java.util.Random;

public class Learn{
CheckersAI alpha;
CheckersAI beta;
LearningEvaluator le;
LearningEvaluator endle;
BaseEvaluator be;
BaseEvaluator endbe;

public static void main(String[] args){
LearningEvaluator le = new LearningEvaluator("../src/weights/alpha.csv");
BaseEvaluator be = new BaseEvaluator("../src/weights/beta.csv");
CheckersAI alpha = new CheckersAI(le, 1);
CheckersAI beta = new CheckersAI(be, 2);
learn(alpha, beta, le, be);

Learn learn = new Learn();
learn.learn();
}
public Learn(){
alpha = new CheckersAI(le, 1);
beta = new CheckersAI(be, 2);
le = new LearningEvaluator("../src/weights/alpha.csv");
be = new BaseEvaluator("../src/weights/beta.csv");
endle = new LearningEvaluator("../src/weights/endalpha.csv");
endbe = new BaseEvaluator("../src/weights/endbeta.csv");
}

// need to decide what to do if we are going on the wrong track
Expand All @@ -18,20 +28,23 @@ public class Learn{
// for draws, make function called is_improved that checks if piece count is greater than 4 (king is worth 2)
// for learning rate, first 30 with .1, next 30 with .05, then final 30 with .01 and see what happens

public static void learn(CheckersAI alpha, CheckersAI beta, LearningEvaluator le, BaseEvaluator be){
public void learn(){
final int num_games = 30;
final int iterations = 7;

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
alpha.eval = le;
beta.eval = be;
System.out.println("playing game " + i);
int player = rand.nextInt(2) + 1; // choose which player alpha plays as
play(alpha, beta, le, player, true); // alpha and beta play a game
play(player, true); // alpha and beta play a game
le.updateWeights(learningParameter(i, num_games)); // get new weights using data from game
endle.updateWeights(learningParameter(i, num_games)); // get new weights using data from game
//le.updateWeights(.1); // get new weights using data from game
}
faceBeta(alpha, beta, le, be);
faceBeta();
}
}

Expand All @@ -46,33 +59,38 @@ public class Learn{
}
}

public static void faceBeta(CheckersAI alpha, CheckersAI beta, LearningEvaluator le, BaseEvaluator be){
public void faceBeta(){
boolean w1;
boolean w2;
CheckersGameState s;
System.out.println("facing beta");
s = new CheckersGameState3();
w1 = play(alpha, beta, le, 1, false);
w2 = play(alpha, beta, le, 2, false);
w1 = play(1, false);
w2 = play(2, false);

System.out.println("alpha won " + w1 + " " + w2);
if(w1 && w2){
System.out.println("updating beta");
le.commitWeights("../src/weights/beta.csv");
endle.commitWeights("../src/weights/endbeta.csv");
be.refreshWeights();
endbe.refreshWeights();
}
else{
be.commitWeights("../src/weights/alpha.csv");
le.refreshWeights();
endbe.commitWeights("../src/weights/endalpha.csv");
endle.refreshWeights();
}



}



public static boolean play(CheckersAI alpha, CheckersAI beta, LearningEvaluator le, int player, boolean learning){
public boolean play(int player, boolean learning){
int min_ply = 7;
int incr_ply = 0;
boolean switchedToEndGame = false;
CheckersGameState current = new CheckersGameState3();
int other = 1 - (player - 1) + 1;
alpha.setPlayer(player);
Expand All @@ -86,23 +104,32 @@ public class Learn{
Move lastmove = null;
Move secondlast = null;
while(!current.isTerminal() && same_moves <= 3 && moves <= 200){
Move next = alpha.minimax(current, 7); // get alpha's move
if(current.isEndGame() && !switchedToEndGame){
switchedToEndGame = true;
alpha.eval = endle;
beta.eval = endbe;
min_ply += incr_ply;
}
Move next = alpha.minimax(current, min_ply); // get alpha's move
moves++;
if(secondlast != null && next.toString().equals(secondlast.toString())){
same_moves++;
}
secondlast = lastmove;
lastmove = next;
if(learning){
if(learning && !switchedToEndGame){
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)
}
else if(switchedToEndGame){
endle.addData(current.getEndGameFeatures(alpha.getPlayer()), next.getValue());
}
current = current.result(next); // make the move
moves++;
//current.printState();
if(current.isTerminal()){ // if alpha won, then break
break;
}
current = current.result(beta.minimax(current, 7)); // beta's move
current = current.result(beta.minimax(current, min_ply)); // beta's move

}
current.printState();
Expand Down
7 changes: 6 additions & 1 deletion src/RmCheckersClient.java
Expand Up @@ -49,7 +49,7 @@ public class RmCheckersClient {

public Evaluator e;
public CheckersAI ai;
public CheckersGameState currentState;
public CheckersGameState3 currentState;

public RmCheckersClient(){
_socket = openSocket();
Expand Down Expand Up @@ -150,6 +150,8 @@ public class RmCheckersClient {

public void playGame(int player) {
int minPly = 8;
int maxPly = 12;
int incPly = 7;
try {
String msg = readAndEcho(); // initial message
if(player == 1) { // black
Expand All @@ -161,6 +163,9 @@ public class RmCheckersClient {
readAndEcho(); // move query
}
while(currentState.actions().size()>0){
if(currentState.isEndGame() && minPly < maxPly){
minPly+=incPly;
}
currentState.printState();
Move myMove = ai.minimax(currentState, minPly);
writeMessageAndEcho(myMove.toString());
Expand Down
25 changes: 25 additions & 0 deletions src/weights/alpha.csv
Expand Up @@ -211,3 +211,28 @@
1636.0393033795276, -20.55866486325862, 23.025889823117954, -75.23311382312917, 49.441644873926144, 6.656444927678631, 47.95237701100202, 30.800919183379587, 8.737729875300932, 15.591634738689757, -30.83579903277091, -4.031769509694296
1638.2330874898776, -20.221504696957055, 21.744347638396736, -75.05160182683231, 48.83751050422706, 6.5445299914688295, 47.92011127154682, 30.690614802855197, 9.967706216168144, 13.976829859173037, -29.54516847523072, -3.3459437553953135
2799.5131611566608, -1.944668143409672, -6.07582076732366, -15.020040153383759, -0.38978985234549934, 5.102487827528079, 45.55197753549653, -7.961856581907668, -11.80237286431448, 1.2111733368909983, -3.063047124671007, 1.986352732898951
2819.123748375204, -6.3502535298600105, -12.782558494485961, -7.292852848933818, -0.34712863449470116, 4.917424359698483, 51.52717394754892, -9.716311931796048, -20.60575146529882, 4.690311528963438, -6.6904730690626675, -4.885008568879172
2823.7446931293407, -6.29819815688049, -10.661822965976196, -6.7179388970924725, 0.43509391896474064, 5.377385681697625, 51.04893695097747, -13.79295452751498, -26.5558837919409, 6.703762568051382, -11.747926161739754, -5.270919246804665
2840.2410977224113, -6.982205055127192, -14.270865238837677, 0.0649634752429078, 0.983244881353248, 5.424374030317277, 50.859465066349514, -23.368679207396433, -32.490947173688326, 5.813021305023193, -16.315816745530043, -3.3824852380287576
2930.0759464993153, -7.475710209838357, -9.612411441279239, -5.235937792581098, 1.3838623977516014, 4.999786158654082, 47.85803299232243, -21.860026155742975, -26.281150310291814, 7.629445546858122, -16.679703156639718, -7.54927040318127
2915.634547791944, -6.155932093735857, -7.437770238798674, -5.982694556061574, 2.406985715038655, 5.5070306373241005, 49.263643689530404, -24.094411217071627, -29.806335014955074, 8.868834095293115, -21.544784781138258, -8.007468729955562
2916.3063867724754, -5.090974263375936, -5.7475356179133685, -3.0000089120278237, 2.961712708417279, 5.202094822036985, 49.73462697381358, -25.65311612596431, -31.692968955259055, 10.093493760085153, -23.36610562733877, -11.318307728443557
2910.6037930335715, -4.131480703581557, -3.384797837430957, -2.4614155649218983, 3.96525058934255, 5.130895295637674, 51.3901096405062, -26.45318923869011, -32.77151303224894, 10.445634416200377, -24.88664222837126, -13.441509241110161
2826.156464386087, 0.120654506825419, -7.921606712227613, -0.007493788059202089, 3.6459310804814087, 2.943790575230708, 51.309264952328874, -12.272800488023764, -20.526722380754606, 4.447955817145174, -24.404895648453053, -9.977299510528816
2890.0509629519224, -0.24841380284418302, -10.06800238045122, -11.89635912346341, 4.248187314434739, 5.538302742307144, 46.00945500731964, -22.951226349297645, -18.53689178814173, -3.78533171766298, -21.686305337007788, -7.97403472924687
3053.6351606943517, -4.535600585874046, -2.2983305806248966, -40.361736552612264, 7.425974961410926, 7.41221443110567, 54.553462416869316, -13.738248190693737, -13.356873363084443, 4.896381913265503, -24.399191578047574, -12.746350650863311
3048.005198736501, -4.735716530236788, 3.663875680414705, -37.7827217427352, 8.030215511211464, 5.878278318292642, 47.152112961823406, -9.922687389533893, -10.554796090122949, 3.1702406568484314, -17.797956030581425, -15.604065107244708
3079.370000718198, -5.131876501635187, 1.715400609589258, -24.784829575650356, 5.295428366203243, 3.213673094101617, 49.533288020742546, -6.729145452058781, -7.995782503571347, -2.5450832140924087, -17.2506642972572, -14.944125696430765
3081.6727079706793, -3.2872127242650073, 1.458785211833281, -40.72124724997096, 6.19701481147274, 5.464559220835012, 51.575911886395765, -4.578395062433524, -7.728822815188389, -0.2189230159444313, -22.61867238916366, -11.758952149223031
3100.5084142500564, -4.7822912723109425, 3.174046215948167, -48.4463988093916, 6.524498081200537, 6.8956268885655225, 47.68804037824094, -7.465031715178685, -4.95086101961026, -0.9444266862838813, -23.55601409327474, -14.247370015730638
3086.0716127466712, -4.266333646434264, 2.9108110658071134, -46.85542852832165, 6.829583344179504, 6.888696844489375, 46.92563133095181, -7.888991333409911, -6.828408083298225, -0.22573821763479163, -23.820217324903048, -15.486849298053645
3075.439468718722, -3.900817870065693, 2.772591480311922, -45.86352953252236, 7.050201708903726, 6.900291052961852, 46.432058750418484, -8.172569864726556, -8.133357839316867, 0.2866955474259578, -23.96810889364606, -16.36868013961241
3066.0441536915196, -3.580218365020854, 1.905518087399917, -42.816689655123, 6.81038469233651, 6.5028427874476815, 47.01603071348958, -7.708956312075114, -8.121513147201496, 0.35191888044090336, -24.336300767153364, -17.476931106389607
3076.7942658791485, -3.9944372443193163, 1.203168899836871, -43.693533074303204, 7.046836478795926, 6.925610791602731, 46.47422315356045, -9.112299466489374, -9.291575137674116, 1.3828658839626127, -25.545464757418088, -19.754054491277657
3098.5061664217824, -4.120409201219323, 5.233125121651318, -21.88854412094254, 5.321639505789649, 1.6455419406899825, 40.65443087306163, -1.2550577340357156, -3.5673003675540613, -0.5575663360816563, -25.63658028613234, -18.17349554676609
3083.644320166879, -3.620041738133389, 6.506829274602694, -16.464437353199763, 3.8587553281814166, 1.1676713046461837, 43.07479634076473, -2.275512636169418, -7.0002860307231245, 2.524302029924657, -29.68186724227588, -16.4965294631963
3244.672855653226, -2.8337649893998282, 13.710741992200896, 1.3044067693043608, -1.888187868834577, -0.9119398457052705, 36.58249580806826, -17.04662114943008, -19.793069361154778, 6.7567700567497075, -27.312743879170498, -16.262337453131046
2917.746487334786, -8.061022951931838, -6.609964398233794, 216.8151169367727, 14.424076364525142, -0.4061740154502895, 34.81363162472649, -245.631559007009, -222.57909067895224, -5.493888974372356, -17.56719556954635, -25.27984840693994
2894.5183569896217, -24.261531583697504, 15.034595717336051, 178.48968012671259, 23.70426382401744, 5.441287376691758, 49.0007935946269, -259.1269445462735, -232.5596315889493, -5.329598328618745, -6.994636135055675, -13.244975077862328
2944.668512245323, -25.21731850468463, 14.854772185388837, 187.44524414324124, 19.682623746127724, 4.2729992145885, 44.46179377944801, -261.69593216723047, -232.53545342651304, -1.472468962139943, -8.644497633245395, -17.22580253861976
3009.069370626828, -24.285677007924463, 24.688977766025346, 176.62705146719483, 6.447126303312221, 2.783552147365609, 28.749676932008803, -235.2302515085287, -203.36431054211803, 4.213613222647522, -20.234164528178965, -33.25310964667955
1 change: 1 addition & 0 deletions src/weights/beta.csv
@@ -1,3 +1,4 @@
100, -5, 20, 20, 25, 10, 10, 20, 20, 30, 30, 30
1826.0705815169833, 12.365435899024902, 23.58295734807525, 57.36085478839676, -2.7837546960554285, -3.19771921999838, 47.08341161233382, -6.285784896148243, -5.208189183870571, 11.51522034240242, -3.2044344558613282, -8.223757596191781
2799.5131611566608, -1.944668143409672, -6.07582076732366, -15.020040153383759, -0.38978985234549934, 5.102487827528079, 45.55197753549653, -7.961856581907668, -11.80237286431448, 1.2111733368909983, -3.063047124671007, 1.986352732898951
3076.7942658791485, -3.9944372443193163, 1.203168899836871, -43.693533074303204, 7.046836478795926, 6.925610791602731, 46.47422315356045, -9.112299466489374, -9.291575137674116, 1.3828658839626127, -25.545464757418088, -19.754054491277657

0 comments on commit 58ed898

Please sign in to comment.