diff --git a/Lframe_0_delay-0.1s.gif b/Lframe_0_delay-0.1s.gif new file mode 100644 index 0000000..77a5cc8 Binary files /dev/null and b/Lframe_0_delay-0.1s.gif differ diff --git a/Lframe_1_delay-0.1s.gif b/Lframe_1_delay-0.1s.gif new file mode 100644 index 0000000..95d6e63 Binary files /dev/null and b/Lframe_1_delay-0.1s.gif differ diff --git a/Lframe_2_delay-0.1s.gif b/Lframe_2_delay-0.1s.gif new file mode 100644 index 0000000..d0b6dcd Binary files /dev/null and b/Lframe_2_delay-0.1s.gif differ diff --git a/Lframe_3_delay-0.1s.gif b/Lframe_3_delay-0.1s.gif new file mode 100644 index 0000000..95d6e63 Binary files /dev/null and b/Lframe_3_delay-0.1s.gif differ diff --git a/_classpath.xml b/_classpath.xml new file mode 100644 index 0000000..d655412 --- /dev/null +++ b/_classpath.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/_project.xml b/_project.xml new file mode 100644 index 0000000..eab011b --- /dev/null +++ b/_project.xml @@ -0,0 +1,17 @@ + + + Full algorithm + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/best.nn b/best.nn index 1544120..07255c8 100644 Binary files a/best.nn and b/best.nn differ diff --git a/best2.nn b/best2.nn new file mode 100644 index 0000000..b775540 Binary files /dev/null and b/best2.nn differ diff --git a/bin/.gitignore b/bin/.gitignore index f5d4628..7ad6bd0 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -1,14 +1,6 @@ -/gui$1.class -/gui$2.class -/gui$3.class -/gui$4.class -/gui.class -/gui$5.class -/gui$6.class -/Gui$1.class +/Gui$1$1.class /Gui$2.class /Gui$3.class /Gui$4.class /Gui$5.class -/Gui.class /Gui$6.class diff --git a/bin/Game$2.class b/bin/Game$2.class index 57fef85..1b94b7e 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 1602dba..7f0faf2 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..3ab13eb 100644 Binary files a/bin/GeneticAlgorithm.class and b/bin/GeneticAlgorithm.class differ diff --git a/bin/Gui$1.class b/bin/Gui$1.class new file mode 100644 index 0000000..a595f09 Binary files /dev/null and b/bin/Gui$1.class differ diff --git a/bin/Gui.class b/bin/Gui.class new file mode 100644 index 0000000..c42129f Binary files /dev/null and b/bin/Gui.class differ diff --git a/bin/Mario.class b/bin/Mario.class index 5ba8ee3..654e92a 100644 Binary files a/bin/Mario.class and b/bin/Mario.class differ diff --git a/bin/NeuralNetworkTester.class b/bin/NeuralNetworkTester.class index e5f9e0d..d9e3c0c 100644 Binary files a/bin/NeuralNetworkTester.class and b/bin/NeuralNetworkTester.class differ diff --git a/bin/Trainer.class b/bin/Trainer.class index 0a8dc33..b241675 100644 Binary files a/bin/Trainer.class and b/bin/Trainer.class differ diff --git a/data b/data index d15196b..a2e47ca 100644 --- a/data +++ b/data @@ -6,10 +6,6 @@ 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 -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, 1, 1 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 @@ -37,4 +33,8 @@ 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, 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 \ No newline at end of file diff --git a/frame_0_delay-0.1s.gif b/frame_0_delay-0.1s.gif new file mode 100644 index 0000000..c57b906 Binary files /dev/null and b/frame_0_delay-0.1s.gif differ diff --git a/frame_1_delay-0.1s.gif b/frame_1_delay-0.1s.gif new file mode 100644 index 0000000..6cf6d7a Binary files /dev/null and b/frame_1_delay-0.1s.gif differ diff --git a/frame_2_delay-0.1s.gif b/frame_2_delay-0.1s.gif new file mode 100644 index 0000000..f1e016b Binary files /dev/null and b/frame_2_delay-0.1s.gif differ diff --git a/frame_3_delay-0.1s.gif b/frame_3_delay-0.1s.gif new file mode 100644 index 0000000..322e177 Binary files /dev/null and b/frame_3_delay-0.1s.gif differ diff --git a/src/Game.java b/src/Game.java index 67dcb57..86984da 100644 --- a/src/Game.java +++ b/src/Game.java @@ -5,8 +5,10 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; @@ -20,7 +22,7 @@ public class Game { private JPanel panel; private Mario m = new Mario(0, 624); private Keyboard keys = new Keyboard(m); - private int[][] tilelayout = new int[13][14]; + private static int[][] tilelayout = new int[13][14]; private String[] tileID = {"AIR", "ground"}; private int offset = 0; private double fitness = 0; @@ -29,9 +31,9 @@ public class Game { public Individual indiv; public static int me = 0; public static int maxFrames = 50; - public boolean play = false; + public static boolean play = false; public BufferedReader in; - private Timer repaint = new Timer(18, new ActionListener(){ + private Timer repaint = new Timer(15, new ActionListener(){ public void actionPerformed(ActionEvent e) { frame.repaint(); frames += 1; @@ -61,6 +63,23 @@ public void actionPerformed(ActionEvent e) { } }); + public static void setLevel(int[][] nl) { + try { + BufferedWriter out = new BufferedWriter(new FileWriter("data")); + for (int[] y : nl) { + String s = ""; + for (int x : y) { + s += x + ", "; + } + s += "\n"; + out.write(s); + } + out.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + public static void main(String[] args) { new Game().start(); } @@ -136,6 +155,10 @@ public void paintComponent(Graphics g) { String str; for (int x = 0; x < tilelayout[0].length; x++) { str = in.readLine(); + if (str == null) { + in = new BufferedReader(new FileReader("level.gg")); + str = in.readLine(); + } process(str, x); } } catch (IOException e) { @@ -240,7 +263,7 @@ private void draw(Graphics g) { if (tile != 0) { Image img; try { - img = ImageIO.read(new File(tileID[tile] + ".png")); + img = ImageIO.read(new File(System.getProperty("user.dir") + "/" + tileID[tile] + ".png")); g.drawImage(img, x * 48 - offset, y * 48, null); } catch (IOException e) { // Auto-generated catch block diff --git a/src/GameGrid.java b/src/GameGrid.java index e81f726..5ee9677 100644 --- a/src/GameGrid.java +++ b/src/GameGrid.java @@ -22,6 +22,13 @@ public GameGrid (int cols) { } } + public void addRandom(int num) { + for (int i=0; i"+text+""); + jtf.setFont(new Font(jtf.getName(), Font.PLAIN, 20)); + frame.add(jtf); + frame.pack(); + frame.setVisible(true); + frame.setTitle("Generation Update!"); + } + + public GenerationUpdate(String text, int height) { + frame.setPreferredSize(new Dimension(400, height)); + JLabel jtf = new JLabel(""+text+""); + jtf.setFont(new Font(jtf.getName(), Font.PLAIN, 20)); + frame.add(jtf); + frame.pack(); + frame.setVisible(true); + + } + + public static void main(String[] args) { + GenerationUpdate gu = new GenerationUpdate("lol
lol"); + } +} diff --git a/src/GeneticAlgorithm.java b/src/GeneticAlgorithm.java index d47a7f9..f841f75 100644 --- a/src/GeneticAlgorithm.java +++ b/src/GeneticAlgorithm.java @@ -1,10 +1,11 @@ -import java.io.EOFException; import java.util.ArrayList; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import javax.swing.JOptionPane; + public class GeneticAlgorithm { private ArrayList individuals = new ArrayList (); private double mutationRate; @@ -25,6 +26,9 @@ public GeneticAlgorithm(double mutationRate, int popSize, int numInputs) { this.mutationRate = mutationRate; this.popSize = popSize; this.numInputs = numInputs; + System.out.println(mutationRate); + System.out.println(popSize); + System.out.println(numInputs); } public void start(int times) throws InterruptedException, ExecutionException { @@ -46,13 +50,14 @@ private void main() throws InterruptedException, ExecutionException { if (firstTime) { for(int i = 0; i < popSize; i++) { Individual ind = new Individual(numInputs); - + /*/ try { - ind.getNN().getFromFile("best.nn"); - } catch (EOFException e) { + ind.getNN().setLayers(NeuralNetwork.getFromFile(System.getProperty("user.dir") + "/" + "best.nn").getLayers()); + } catch (Exception e) { e.printStackTrace(); } - +// /*/ + System.out.println(ind.getNN().getLayers().get(0).get(0).getBias()); futures.add(service.submit(ind)); } firstTime = false; @@ -85,11 +90,15 @@ private void main() throws InterruptedException, ExecutionException { break; } } -// individuals.get(0).getNN().save("best.nn"); - System.out.println("Generation Score: " + (mean / individuals.size())); - System.out.println("Best Fitness: "+ (best)); - System.out.println("Old best: " + (oldBest)); - System.out.println("Order: " + (individuals)); + + individuals.get(0).getNN().save(System.getProperty("user.dir") + "/" + "best.nn"); +// new GenerationUpdate("Generation Score: " + (mean / individuals.size())+"
" +// +"Best Fitness: "+ (best)+"
" +// +"Old best: " + (oldBest)); + JOptionPane.showMessageDialog(null, "Generation Score: " + (mean / individuals.size())+"\n" + +"Best Fitness: "+ (best)+"\n" + +"Old best: " + (oldBest)+"\n"+"click okay to start next Generation", "Generation Update", JOptionPane.INFORMATION_MESSAGE); + System.out.println(); // Game.maxFrames += 10; Game.maxFrames += (int) ((best - oldBest) / 10.0) + 1; mutationRate = mutationRate * (oldBest / (best + time) * (1.0 + (1.0 / time))); @@ -131,16 +140,16 @@ private void select() { // theBest.add(new Individual(m1)); // theBest.add(new Individual(m2)); // } - for (int i = 0; i < 3; i++) + for (int i = 0; i < Math.round(initSize / 3.0); i++) theBest.add(individuals.get(i)); - for (int i = 0; i < 5; i++) { + for (int i = 0; i < Math.round(initSize * 2 / 3.0); 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"); + System.out.println("WAITING HERE"); theBest.add(new Individual(numInputs)); } System.out.println(theBest); diff --git a/src/Gui.java b/src/Gui.java index 782cda0..4df3382 100644 --- a/src/Gui.java +++ b/src/Gui.java @@ -27,6 +27,7 @@ public Gui() { initComponents(); setTitle("Mario Bros Genetic Algorithm"); setSize(600,400); + } int[][] gameGrid; public Gui(int[][] gg) { @@ -35,10 +36,21 @@ public Gui(int[][] gg) { } public void train() { - GeneticAlgorithm ga = new GeneticAlgorithm(jSlider1.getValue()/100, jSlider2.getValue(), 169); + //System.out.println("LuL"); + //double mutationRate, int popSize, int numInputs, int numTimes +// GeneticAlgorithm ga = new GeneticAlgorithm(jSlider1.getValue()/100, jSlider2.getValue(), 169); + try { - ga.start(jSlider3.getValue()); - } catch (Exception e) { + double mutationRate = ((double) jSlider1.getValue())/100.0; + int popSize = jSlider3.getValue(); + int numTimes = jSlider2.getValue(); + Trainer t = new Trainer(mutationRate, popSize, 169, numTimes); + t.start(); + setVisible(false); + //new Trainer((double)jSlider1.getValue()/100, jSlider).start(); + } + + catch (Exception e) { e.printStackTrace(); } } @@ -50,7 +62,8 @@ public void buildLevel() { @SuppressWarnings("unchecked") private void initComponents() { - + + jSlider1 = new javax.swing.JSlider(); jLabel1 = new javax.swing.JLabel(); jSlider2 = new javax.swing.JSlider(); @@ -175,7 +188,7 @@ public void stateChanged(javax.swing.event.ChangeEvent evt) { jLabel2.getAccessibleContext().setAccessibleName("JLblVal"); - pack(); + pack(); }// private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) { @@ -222,6 +235,12 @@ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Gui().setVisible(true); + new GenerationUpdate("Multiple windows will appear once you hit train, " + + "every generation there will be an update given on how good all" + + " Mario's are doing. In the multiple windows you can see how " + + "Mario is doing and learning. You can also create your own level, " + + "after creating the level you can click the Genetic Algorithm " + + "button so you can use it.", 400); } }); } diff --git a/src/LevelMaker.java b/src/LevelMaker.java index c1b2077..6a505b8 100644 --- a/src/LevelMaker.java +++ b/src/LevelMaker.java @@ -16,6 +16,7 @@ public class LevelMaker { private JLabel label = new JLabel("Green Squares represent blocks, black squares are holes"); private Dimension DIM; int[] dims; + public static void main(String[] args) { new LevelMaker(); } @@ -26,6 +27,8 @@ public LevelMaker() { private void start() { dims = grid.recommendedDims(); + + DIM = new Dimension(dims[0], dims[1]+50); panel = new JPanel() { @Override @@ -68,59 +71,49 @@ public void actionPerformed(ActionEvent e) { } }); - JButton loadButton = new JButton("load"); - loadButton.addActionListener(new ActionListener() { + JButton gameButton = new JButton("Play Game"); + gameButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + playGame(); + } + }); + + JButton randomButton = new JButton("Random"); + randomButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - load(); + grid.addRandom(7); + frame.repaint(); } }); + JPanel subPanel = new JPanel(); + subPanel.add(gameButton); + subPanel.add(randomButton); + label.setForeground(Color.BLACK); frame.add(label, BorderLayout.CENTER); - frame.add(saveButton, BorderLayout.WEST); - frame.add(loadButton, BorderLayout.EAST); - + frame.add(saveButton, BorderLayout.WEST); + frame.add(subPanel, BorderLayout.EAST); } - public File showFileDialog(String prompt) { - FileDialog dialog = new FileDialog((Frame)null, prompt); - dialog.setMode(FileDialog.LOAD); - dialog.setVisible(true); - File file = new File(dialog.getFile()); - return file; - } - - public void sendBack() { Gui gui = new Gui(grid.getGrid()); gui.setVisible(true); frame.setVisible(false); - } - - public static void print(int[][] arr) { - for (int r=0; r> { public static void main(String args[]) throws InterruptedException, ExecutionException { - ExecutorService executor = Executors.newFixedThreadPool(10); - int times = 0; - while (true) { - times++; - System.out.println(times+" times"); - Callable> callable = new NeuralNetworkTester(); - executor.submit(callable).get(); - executor.submit(callable).get(); +// ExecutorService executor = Executors.newFixedThreadPool(10); +// int times = 0; +// while (true) { +// times++; +// System.out.println(times+" times"); +// Callable> callable = new NeuralNetworkTester(); +// executor.submit(callable).get(); +// executor.submit(callable).get(); +// } + + for (int i=0; i<100; i++) { + System.out.println(1 / (1 + Math.pow(Math.E, (-1 * i)))); } } @@ -38,7 +40,7 @@ public ArrayList testPredict() { in.add(1.0); in.add(2.0); in.add(5.0); - return nn.predict(in); + return nn.predict(in, 0.8); } public void testReproduction() { diff --git a/src/Trainer.java b/src/Trainer.java index 15b32d1..4a5b60c 100644 --- a/src/Trainer.java +++ b/src/Trainer.java @@ -1,27 +1,65 @@ import java.io.EOFException; +import java.util.ArrayList; import java.util.concurrent.ExecutionException; -public class Trainer { +public class Trainer extends Thread { public static void main(String args[]) throws InterruptedException, ExecutionException, EOFException { - Trainer t = new Trainer(); - t.start(); + Trainer t = new Trainer(0.9, 10, 169, 10); + try{ + t.start(); + } + catch(Exception e) { + e.printStackTrace(); + } +// +// t.testSave(); +// t.testGet(); } - public void start() throws InterruptedException, ExecutionException { - GeneticAlgorithm ga = new GeneticAlgorithm(0.9, 10, 169); - ga.start((int)Double.MAX_VALUE); + private GeneticAlgorithm ga; + private int times; + public Trainer(double mutationRate, int popSize, int numInputs, int numTimes) { + times = numTimes; + ga = new GeneticAlgorithm(mutationRate, popSize, numInputs); +// double mutationRate, int popSize, int numInputs } - static String path = "/home/computerscience/eclipse-workspace/Mario-Bros-Genetic-Algorithm/best.nn"; - public void testSave() throws EOFException { + + public void run() { + try { + ga.start(times); + } - NeuralNetwork network = new NeuralNetwork(100); + catch (Exception e) { + e.printStackTrace(); + } + } +// public void start() throws InterruptedException, ExecutionException { +// GeneticAlgorithm ga = new GeneticAlgorithm(0.9, 10, 169); +// ga.start((int)Double.MAX_VALUE); +// } + + static String path = "best.nn"; + public void testSave() throws EOFException { + ArrayList 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()); }