Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Get converging numbers
  • Loading branch information
sas12028 committed Apr 24, 2017
1 parent fccb11a commit 0aea87b
Show file tree
Hide file tree
Showing 6 changed files with 6,839 additions and 97 deletions.
2 changes: 1 addition & 1 deletion src/BaseEvaluator.java
Expand Up @@ -25,7 +25,7 @@ public class BaseEvaluator implements Evaluator{
public double evaluate(CheckersGameState s, int player){
if(s.isTerminal()){
if(s.winner() == player){
return 1000; // what should this be?
return 200; // what should this be?
}
else{
return 0; // assuming only positive evalutions
Expand Down
11 changes: 6 additions & 5 deletions src/Learn.java
Expand Up @@ -4,7 +4,7 @@ public class Learn{

public static void main(String[] args){

final int num_games = 15;
final int num_games = 30;
LearningEvaluator le = new LearningEvaluator("../src/weights/alpha.csv", .1);
BaseEvaluator be = new BaseEvaluator("../src/weights/beta.csv");
CheckersAI alpha = new CheckersAI(le, 1);
Expand All @@ -18,6 +18,7 @@ public class Learn{
for(int i = 0; i < num_games; i++){ // play num_games amount of games
s = new CheckersGameState3();
winner = play(s, alpha, beta, le); // alpha and beta play a game
System.out.println("winner " + winner);
le.updateWeights(); // get new weights using data from game
played++;
if(winner == alpha.getPlayer()){
Expand All @@ -42,13 +43,12 @@ public class Learn{


public static int play(CheckersGameState s, CheckersAI alpha, CheckersAI beta, LearningEvaluator le){
System.out.println("playing");
CheckersGameState current = s;
int moves = 0; // draw after 200 moves
Random rand = new Random();
int player = rand.nextInt(2) + 1; // choose which player alpha plays as
int other = 1 - (player - 1) + 1;
System.out.println("playing as " + player);
//System.out.println("playing as " + player);
alpha.setPlayer(player);
beta.setPlayer(other);
current.printState();
Expand All @@ -61,16 +61,17 @@ public class Learn{
Move next = alpha.minimax(current, 7); // get alpha's move
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
current.printState();
//current.printState();
moves++;
if(current.isTerminal()){ // if alpha won, then break
break;
}
current = current.result(beta.minimax(current, 7)); // beta's move
current.printState();
moves++;

}
current.printState();
System.out.println("playing as " + alpha.getPlayer());
return current.winner();
}
}
25 changes: 14 additions & 11 deletions src/LearningEvaluator.java
Expand Up @@ -39,23 +39,23 @@ public class LearningEvaluator extends BaseEvaluator{
// using least squares might be a bad idea
// get a lot of singular matrices
// we could do samuel's method or come up with another function to modify the coefficients
int curr_in = 0;
int data_sz = params.get(0).length + 1; // need to do regression with data sets of size 10, so each iteration of loop uses 10 lines of data
while(params.size() - curr_in > data_sz){
double[] vals = new double [data_sz]; //converting arraylist to array
// int curr_in = 0;
// int data_sz = params.get(0).length + 1; // need to do regression with data sets of size 10, so each iteration of loop uses 10 lines of data
//while(params.size() - curr_in > data_sz){
double[] vals = new double [values.size()]; //converting arraylist to array
System.out.println("printing values");
int j = 0;
for(int i = curr_in; i < curr_in + data_sz; i++){
vals[j] = values.get(i);
for(int i = 0; i < values.size(); i++){
vals[i] = values.get(i);
System.out.println(values.get(i));
j++;
}
System.out.println(vals);
System.out.println("printing params");
double[][] pars = new double[data_sz][]; //converting 2d arraylist to array
double[][] pars = new double[params.size()][]; //converting 2d arraylist to array
j=0;
for(int i=curr_in; i < curr_in + data_sz; i++){
pars[j] = params.get(i);
for(int i=0; i < params.size(); i++){
pars[i] = params.get(i);
System.out.println(Arrays.toString(params.get(i)));
j++;
}
Expand All @@ -67,13 +67,16 @@ public class LearningEvaluator extends BaseEvaluator{
for(int i = 0; i < this.weights.length; i++){
this.weights[i] = this.weights[i] + alpha * (new_weights[i] - this.weights[i]);
}
System.out.println("updated weights " + Arrays.toString(this.weights));
commitWeights(this.file);
} catch(SingularMatrixException e) {
System.out.println("Matrix was singular, not updating weights");
}
curr_in += data_sz;
}
//curr_in += data_sz;
//}

//values = new ArrayList<Double>(values.subList(curr_in, values.size()));
//params = new ArrayList<double[]>(params.subList(curr_in, params.size()));
values.clear();
params.clear();

Expand Down

0 comments on commit 0aea87b

Please sign in to comment.