diff --git a/best.nn b/best.nn index 1544120..5367429 100644 Binary files a/best.nn and b/best.nn differ diff --git a/bin/Activation.class b/bin/Activation.class index be807e6..12c1dc1 100644 Binary files a/bin/Activation.class and b/bin/Activation.class differ diff --git a/bin/Game$1.class b/bin/Game$1.class index f1d2bf4..1b8d0e8 100644 Binary files a/bin/Game$1.class and b/bin/Game$1.class differ diff --git a/bin/Game$2.class b/bin/Game$2.class index b8cad7c..2b2f898 100644 Binary files a/bin/Game$2.class and b/bin/Game$2.class differ diff --git a/bin/Game.class b/bin/Game.class index b1a7faa..cee0c96 100644 Binary files a/bin/Game.class and b/bin/Game.class differ diff --git a/bin/GeneticAlgorithm.class b/bin/GeneticAlgorithm.class index 3ba1c49..e5f34d4 100644 Binary files a/bin/GeneticAlgorithm.class and b/bin/GeneticAlgorithm.class differ diff --git a/bin/Individual.class b/bin/Individual.class index 978674b..2713087 100644 Binary files a/bin/Individual.class and b/bin/Individual.class differ diff --git a/bin/Mario.class b/bin/Mario.class index cdacd8c..8570c08 100644 Binary files a/bin/Mario.class and b/bin/Mario.class differ diff --git a/bin/Neuron.class b/bin/Neuron.class index 4760a10..ca10f3f 100644 Binary files a/bin/Neuron.class and b/bin/Neuron.class differ diff --git a/bin/Trainer.class b/bin/Trainer.class index 356dd6d..9c51d3a 100644 Binary files a/bin/Trainer.class and b/bin/Trainer.class differ diff --git a/data b/data index d15196b..fd70488 100644 --- a/data +++ b/data @@ -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 +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-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 \ No newline at end of file +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 \ No newline at end of file diff --git a/src/Activation.java b/src/Activation.java index afb6d94..7287264 100644 --- a/src/Activation.java +++ b/src/Activation.java @@ -1,4 +1,4 @@ public enum Activation { - Sigmoid, ReLu, Tanh, None + Sigmoid, ReLu, Tanh, DoubleSigmoid, None } diff --git a/src/Game.java b/src/Game.java index 67dcb57..6706b5c 100644 --- a/src/Game.java +++ b/src/Game.java @@ -31,11 +31,14 @@ 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; @@ -43,6 +46,7 @@ public void actionPerformed(ActionEvent e) { Game.me++; fitness += m.x; + fitness -= 0.5*m.numJumps; //System.out.println("ME"+Game.me); if (indiv != null) indiv.setDone(true); @@ -71,29 +75,67 @@ void start() { repaint.start(); } +// public ArrayList 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 answer = new ArrayList(); +// for (int x = 0; x < tilelayout.length * (tilelayout[0].length - 1); x++) { +// answer.add(doubles[x]); +// } +// +// return answer; +// } + public ArrayList 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 answer = new ArrayList(); + 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; rrow+buff) continue; + if (r<0) { + answer.add(0.0); + continue; + } + for (int c=col-buff; ccol+buff) continue; + if(c<0) { + answer.add(0.0); + continue; + } + answer.add((double)tilelayout[r][c]); + index++; } - } - ArrayList answer = new ArrayList(); - for (int x = 0; x < tilelayout.length * (tilelayout[0].length - 1); x++) { - answer.add(doubles[x]); } + + while (answer.size()=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; diff --git a/src/Individual.java b/src/Individual.java index d5d8d26..74f245f 100644 --- a/src/Individual.java +++ b/src/Individual.java @@ -7,8 +7,7 @@ public class Individual implements Callable { 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); } /** @@ -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 newState = game.getState(); ArrayList 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(); } } diff --git a/src/Mario.java b/src/Mario.java index ab4a8bd..bd4124b 100644 --- a/src/Mario.java +++ b/src/Mario.java @@ -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) { @@ -45,6 +46,7 @@ public void jump() { y_vel += 7; } upButton = true; + numJumps++; } } diff --git a/src/Neuron.java b/src/Neuron.java index 176bbd4..f171f12 100644 --- a/src/Neuron.java +++ b/src/Neuron.java @@ -8,11 +8,11 @@ public class Neuron implements Serializable { Activation activation; public ArrayList getWeights() { - return new ArrayList(weights); + return (ArrayList) weights.clone(); } public void setWeights(ArrayList w) { - weights = new ArrayList(w); + weights = (ArrayList) w.clone(); } public double getBias() { @@ -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(); for (int i = 0; i < numInputs; i++) { - weights.add((Math.random() * 2) - 1); + weights.add((Math.random() * 9) - 4); } } @@ -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; } @@ -96,7 +101,7 @@ public static Neuron reproduce(Neuron n1, Neuron n2, double mutationRate) { for (int i=lastRandom; i arr = new ArrayList(); + 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()); }