Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified best.nn
Binary file not shown.
Binary file modified bin/Activation.class
Binary file not shown.
Binary file modified bin/Game$1.class
Binary file not shown.
Binary file modified bin/Game$2.class
Binary file not shown.
Binary file modified bin/Game.class
Binary file not shown.
Binary file modified bin/GeneticAlgorithm.class
Binary file not shown.
Binary file modified bin/Individual.class
Binary file not shown.
Binary file modified bin/Mario.class
Binary file not shown.
Binary file modified bin/Neuron.class
Binary file not shown.
Binary file modified bin/Trainer.class
Binary file not shown.
30 changes: 15 additions & 15 deletions data
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1
0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1
0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
Expand All @@ -23,18 +37,4 @@
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1
0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1
0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
2 changes: 1 addition & 1 deletion src/Activation.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

public enum Activation {
Sigmoid, ReLu, Tanh, None
Sigmoid, ReLu, Tanh, DoubleSigmoid, None
}
78 changes: 60 additions & 18 deletions src/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ public class Game {
public static int maxFrames = 50;
public boolean play = false;
public BufferedReader in;
private Timer repaint = new Timer(18, new ActionListener(){

private Timer repaint = new Timer(0, new ActionListener(){
public void actionPerformed(ActionEvent e) {
frame.repaint();
frames += 1;
getState();
if (m.y < 0 || (frames >= maxFrames && !play)) {

//System.out.println("done"+GeneticAlgorithm.numDone);
if (m.y < 0) {
fitness -= 200;
}

Game.me++;
fitness += m.x;
fitness -= 0.5*m.numJumps;
//System.out.println("ME"+Game.me);
if (indiv != null)
indiv.setDone(true);
Expand Down Expand Up @@ -71,29 +75,67 @@ void start() {
repaint.start();
}

// public ArrayList<Double> getState() {
// double[] doubles = new double[tilelayout.length * (tilelayout[0].length - 1)];
// /*/
// for (int i = 0; i < tilelayout.length; i++) {
// for (int j = 0; j < tilelayout.length - 1; j++) {
// doubles[i][j] = tilelayout[i][j] * 5.0;
// }
// }
// /*/
// for (int i = m.tiley - 6; i < m.tiley + 6; i++) {
// if (i < 0) continue;
// if (i > 12) continue;
// for (int j = m.tilex - 6; j < m.tilex + 6; j++) {
// if (j < 0) continue;
// if (j > 13) continue;
// doubles[i * tilelayout.length + j] = tilelayout[i][j];
// }
// }
// ArrayList<Double> answer = new ArrayList<Double>();
// for (int x = 0; x < tilelayout.length * (tilelayout[0].length - 1); x++) {
// answer.add(doubles[x]);
// }
//
// return answer;
// }

public ArrayList<Double> getState() {
double[] doubles = new double[tilelayout.length * (tilelayout[0].length - 1)];
/*/
for (int i = 0; i < tilelayout.length; i++) {
for (int j = 0; j < tilelayout.length - 1; j++) {
doubles[i][j] = tilelayout[i][j] * 5.0;
ArrayList<Double> answer = new ArrayList<Double>();
int row = m.tiley;
int col = m.tilex;
int index = 0;
int buff = 1;

if (m.tilex == 0 && m.tiley==0) {
for (int i = 0; i < Math.pow((double)((2*buff)+1), 2); i++) {
answer.add((double) 0);
}
return answer;
}
/*/
for (int i = m.tiley - 6; i < m.tiley + 6; i++) {
if (i < 0) continue;
if (i > 12) continue;
for (int j = m.tilex - 6; j < m.tilex + 6; j++) {
if (j < 0) continue;
if (j > 13) continue;
doubles[i * tilelayout.length + j] = tilelayout[i][j];

for (int r=row-buff; r<tilelayout.length; r++) {
if (r>row+buff) continue;
if (r<0) {
answer.add(0.0);
continue;
}
for (int c=col-buff; c<tilelayout[0].length; c++) {
if (c>col+buff) continue;
if(c<0) {
answer.add(0.0);
continue;
}
answer.add((double)tilelayout[r][c]);
index++;
}
}
ArrayList<Double> answer = new ArrayList<Double>();
for (int x = 0; x < tilelayout.length * (tilelayout[0].length - 1); x++) {
answer.add(doubles[x]);
}


while (answer.size()<Math.pow((double)((2*buff)+1), 2)) {
answer.add((double) 0);
}
return answer;
}

Expand Down
14 changes: 9 additions & 5 deletions src/GeneticAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,21 @@ private void select() {
for (int i=0; i<3; i++)
theBest.add(individuals.get(i));

for (int i=0; i<5; i++) {
for (int i=0; i<7; i++) {
NeuralNetwork m1 = NeuralNetwork.reproduce(individuals.get(i).getNN(), individuals.get(i+1).getNN(), mutationRate);
theBest.add(new Individual(m1));
}

while (theBest.size() < initSize) {
//System.out.println("WAITING HERE");
theBest.add(new Individual(numInputs));

while (theBest.size() < initSize-1) {
System.out.println("WAITING HERE");
if (Math.random()>=0.5)
theBest.add(new Individual(numInputs));
else
theBest.add(individuals.get((int) ((individuals.size()/2)*Math.random())+individuals.size()/2));
}

mutationRate -= mutationRate*0.06;
mutationRate -= mutationRate*0.0278;

Individual.predictionThreshold += Individual.predictionThreshold*0.03;
individuals = theBest;
Expand Down
12 changes: 6 additions & 6 deletions src/Individual.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ public class Individual implements Callable<Individual> {
private Game game;
public Individual(int numInputs) {
network = new NeuralNetwork(numInputs);
network.addLayer(40, Activation.Sigmoid);
network.addLayer(3, Activation.Sigmoid);
network.addLayer(3, Activation.DoubleSigmoid);
}

/**
Expand All @@ -23,23 +22,24 @@ public double getFitness() {
return network.getFitness();

}
public static double predictionThreshold = 0.9;
public static double predictionThreshold = 0.7;
public void play() {
// System.out.println("PLAYING");
ArrayList<Double> newState = game.getState();

ArrayList<Double> actions = network.rawPredict(newState);
if (actions.get(0) == -1) {
return;
}


boolean turnRight = false;
if (actions.get(0) >= predictionThreshold) {
game.moveRight();
turnRight = true;
}
if(actions.get(1) >= predictionThreshold) {
game.jump();
}
if(actions.get(2) >= predictionThreshold) {
if(actions.get(2) >= predictionThreshold && !turnRight) {
game.moveLeft();
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Mario.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public boolean collided(int x2, int y2) {
return ((Math.abs(x - x2) < 48) && (Math.abs((618 - y) - y2) < 48));
}

int numJumps = 0;
public void jump() {
if (!upButton) {
if (inAir == false) {
Expand All @@ -45,6 +46,7 @@ public void jump() {
y_vel += 7;
}
upButton = true;
numJumps++;
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/Neuron.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public class Neuron implements Serializable {
Activation activation;

public ArrayList<Double> getWeights() {
return new ArrayList<Double>(weights);
return (ArrayList<Double>) weights.clone();
}

public void setWeights(ArrayList<Double> w) {
weights = new ArrayList<Double>(w);
weights = (ArrayList<Double>) w.clone();
}

public double getBias() {
Expand All @@ -25,10 +25,10 @@ public void setBias(double newB) {

public Neuron(Activation activation, int numInputs) {
this.activation = activation;
this.bias = (Math.random() * 2) - 1;
this.bias = (Math.random() * 9) - 4;
weights = new ArrayList<Double>();
for (int i = 0; i < numInputs; i++) {
weights.add((Math.random() * 2) - 1);
weights.add((Math.random() * 9) - 4);
}
}

Expand Down Expand Up @@ -63,6 +63,11 @@ else if (activation == Activation.ReLu)

else if (activation == Activation.Tanh)
sum = 2 / (1 + Math.pow(Math.E, (-2 * sum)));

else if (activation == Activation.DoubleSigmoid) {
sum = (1 / (1 + Math.pow(Math.E, (-1 * sum))));
sum = (1 / (1 + Math.pow(Math.E, (-1 * sum))));
}

return sum;
}
Expand Down Expand Up @@ -96,7 +101,7 @@ public static Neuron reproduce(Neuron n1, Neuron n2, double mutationRate) {
for (int i=lastRandom; i<rand; i++) {
double num = Math.random();
if (num < mutationRate) {
newWeights.set(i, Math.random());
newWeights.set(i, (Math.random() * 9) - 4);
}
else if (num < 0.5) {
newWeights.set(i, n2.getWeights().get(i)); // need to clone
Expand Down
36 changes: 28 additions & 8 deletions src/Trainer.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@

import java.io.EOFException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

public class Trainer {
public static void main(String args[]) throws InterruptedException, ExecutionException, EOFException {
Trainer t = new Trainer();
t.testSave();
// try{
// t.start();
// }
//
// catch(Exception e) {
// e.printStackTrace();
// }
while (true) {
t.testSave();
t.testGet();
}
}

public void start() throws InterruptedException, ExecutionException {
GeneticAlgorithm ga = new GeneticAlgorithm(0.9, 10, 169);
GeneticAlgorithm ga = new GeneticAlgorithm(0.9, 10, 9);
ga.start((int)Double.MAX_VALUE);
}
static String path = "/home/computerscience/eclipse-workspace/Mario-Bros-Genetic-Algorithm/best.nn";
static String path = "/Users/arjunbemarkar/eclipse-workspace/Mario-Bros-Genetic-Algorithm/best.nn";
public void testSave() throws EOFException {

NeuralNetwork network = new NeuralNetwork(100);
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(123);
arr.add(9);
NeuralNetwork network = new NeuralNetwork(2);
network.addLayer(40, Activation.Sigmoid);
network.addLayer(3, Activation.Sigmoid);
network.save(path);
System.out.println("done");
NeuralNetwork nn2 = NeuralNetwork.getFromFile(path);
System.out.println("done2");
System.out.println(network.getLayers().get(0).get(0).getBias());
}

public void testGet() {
NeuralNetwork network = null;
try {
network = NeuralNetwork.getFromFile(path);
} catch (EOFException e) {}

System.out.println(network.getLayers().get(0).get(0).getBias());
}


Expand Down