Skip to content

Commit

Permalink
change to doubles, WORKS WELL
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbohman committed Apr 18, 2015
1 parent 1a5f54f commit b60eb0d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
8 changes: 4 additions & 4 deletions Genetic.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Individual {
public int fitness, cols;
public int[] chromosomes;
public double[] chromosomes;
private static int MUCHANCE = 20;
java.util.Random r;

Expand Down Expand Up @@ -28,8 +28,8 @@ public void mutate() {
double flipChance = 0.10;
// double scale = 1;
for (int i = 0; i < chromosomes.length; i++) {
int chr = chromosomes[i];
int mutated = (int) ((double) Math.round(chr * (stdDev*r.nextGaussian()+1.0) * scale) );
double chr = chromosomes[i];
double mutated = chr * (stdDev*r.nextGaussian()+1.0) * scale;
// if(r.nextDouble() < flipChance) {
// mutated = -mutated;
// }
Expand All @@ -40,7 +40,7 @@ public void mutate() {
public void print(int num) {
System.out.format("%d Fitness: %d - Chromosomes: ", num, fitness);
for (int i = 0; i < chromosomes.length; i++)
System.out.format("[%d]", chromosomes[i]);
System.out.format("[%f]", chromosomes[i]);
System.out.format("\n");
}
}
Expand Down
54 changes: 27 additions & 27 deletions PlayerSkeleton.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ public static int[][] getLegalMoves(int piece) {
}

class Weights {
public int numHoles;
public int maxHeight;
public int rowsCleared;
public int colHeights;
public int adjColHeightDiffs;
public int rowTrans;
public int colTrans;
public int wellSums;
public double numHoles;
public double maxHeight;
public double rowsCleared;
public double colHeights;
public double adjColHeightDiffs;
public double rowTrans;
public double colTrans;
public double wellSums;

public Weights() {}

public int[] toArray() {
int[] arr = new int[8];
public double[] toArray() {
double[] arr = new double[8];
int wi = 0;

arr[wi++] = numHoles;
Expand All @@ -35,7 +35,7 @@ public int[] toArray() {
return arr;
}

public static Weights fromArray(int[] arr) {
public static Weights fromArray(double[] arr) {
Weights w = new Weights();
int wi = 0;

Expand Down Expand Up @@ -96,14 +96,14 @@ public static Weights someWeights() {

public static Weights fiftyKWeights() {
Weights w = new Weights(); // [10978][4024][-432][2][1680][11][925][5396]
w.numHoles = 10978/10;
w.maxHeight = 4024/10;
w.rowsCleared = -432/10;
w.colHeights = 2/10;
w.adjColHeightDiffs = 1680/10;
w.rowTrans = 11/10;
w.colTrans = 925/10;
w.wellSums = 5396/10;
w.numHoles = 10.978;
w.maxHeight = 4.024;
w.rowsCleared = -0.432;
w.colHeights = 0.002;
w.adjColHeightDiffs = 1.680;
w.rowTrans = 0.011;
w.colTrans = 0.925;
w.wellSums = 5.396;
return w;
}

Expand All @@ -120,7 +120,7 @@ public static Weights randomWeights() {
return w;
}

public static int getRandom() {
public static double getRandom() {
java.util.Random r = new java.util.Random();
return r.nextInt(5001)-2500;
}
Expand All @@ -146,7 +146,7 @@ class Simulator
// - Column Heights
// - Holes
// - Cleared
public int heuristic;
public double heuristic;

public Simulator(Simulator sim) {
this(sim.rows, sim.cols, sim.weights);
Expand All @@ -171,8 +171,8 @@ public void revertTo(Simulator sim) {
maxHeight = sim.maxHeight;
}

public int getHeuristic() {
int sum = heuristic;
public double getHeuristic() {
double sum = heuristic;

for(int i = 0; i < top.length - 1; i++)
sum += Math.abs(top[i] - top[i+1]) * weights.adjColHeightDiffs;
Expand Down Expand Up @@ -347,22 +347,22 @@ public int playAndReturnScore() {
return gameSim.rowsCleared;
}

private int forwardLookAvg(Simulator s, int maxdepth) {
int average = 0;
private double forwardLookAvg(Simulator s, int maxdepth) {
double average = 0;
Simulator sim = new Simulator(s);

// For all possible pieces
for (int piece = 0; piece < State.N_PIECES; piece++) {
int numMoves = Moves.getNumMoves(piece);
int pieceBestHeu = Integer.MAX_VALUE;
double pieceBestHeu = Double.MAX_VALUE;

// Try all possible moves for piece
for (int move = 0; move < numMoves; move++) {
sim.revertTo(s);
if (!sim.simMove(move, piece))
continue;

int heu;
double heu;
if (maxdepth != 1)
heu = forwardLookAvg(sim, maxdepth - 1);
else
Expand Down

0 comments on commit b60eb0d

Please sign in to comment.