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 added Lframe_0_delay-0.1s.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lframe_1_delay-0.1s.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lframe_2_delay-0.1s.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lframe_3_delay-0.1s.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions _classpath.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-10">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions _project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Full algorithm</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Binary file modified best.nn
Binary file not shown.
Binary file added best2.nn
Binary file not shown.
14 changes: 0 additions & 14 deletions bin/.gitignore

This file was deleted.

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/GameGrid.class
Binary file not shown.
Binary file added bin/GenerationUpdate.class
Binary file not shown.
Binary file modified bin/GeneticAlgorithm.class
Binary file not shown.
Binary file added bin/Gui$1.class
Binary file not shown.
Binary file added bin/Gui$2.class
Binary file not shown.
Binary file added bin/Gui$3.class
Binary file not shown.
Binary file added bin/Gui$4.class
Binary file not shown.
Binary file added bin/Gui$5.class
Binary file not shown.
Binary file added bin/Gui$6.class
Binary file not shown.
Binary file added bin/Gui.class
Binary file not shown.
Binary file modified bin/LevelMaker$1.class
Binary file not shown.
Binary file modified bin/LevelMaker$2.class
Binary file not shown.
Binary file modified bin/LevelMaker$3.class
Binary file not shown.
Binary file modified bin/LevelMaker$4.class
Binary file not shown.
Binary file added bin/LevelMaker$5.class
Binary file not shown.
Binary file modified bin/LevelMaker.class
Binary file not shown.
Binary file modified bin/Mario.class
Binary file not shown.
Binary file modified bin/NeuralNetworkTester.class
Binary file not shown.
Binary file modified bin/Trainer.class
Binary file not shown.
10 changes: 5 additions & 5 deletions data
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
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
Binary file added frame_0_delay-0.1s.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frame_1_delay-0.1s.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frame_2_delay-0.1s.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frame_3_delay-0.1s.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 28 additions & 5 deletions src/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,18 +22,18 @@ 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;
boolean isDone = false;
private int frames = 0;
public Individual indiv;
public static int me = 0;
public static int maxFrames = 50;
public boolean play = false;
public static int maxFrames = 10;
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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/GameGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public GameGrid (int cols) {
}
}

public void addRandom(int num) {
for (int i=0; i<num; i++) {
int col = (int) (Math.random()*grid[0].length);
int row = (int) (Math.random()*grid.length);
grid[row][col] = 1;
}
}

public void updateClick(int x, int y) {
int row = (y/size);
Expand Down
30 changes: 30 additions & 0 deletions src/GenerationUpdate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import javax.swing.*;

import java.awt.Dimension;
import java.awt.Font;
public class GenerationUpdate {
private JFrame frame = new JFrame();

public GenerationUpdate(String text) {
JLabel jtf = new JLabel("<html>"+text+"</html>");
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(500, height));
JLabel jtf = new JLabel("<html>"+text+"</html>");
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<br>lol");
}
}
35 changes: 22 additions & 13 deletions src/GeneticAlgorithm.java
Original file line number Diff line number Diff line change
@@ -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 <Individual> individuals = new ArrayList <Individual>();
private double mutationRate;
Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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())+"<br>"
// +"Best Fitness: "+ (best)+"<br>"
// +"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)));
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 28 additions & 5 deletions src/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public Gui() {
initComponents();
setTitle("Mario Bros Genetic Algorithm");
setSize(600,400);

}
int[][] gameGrid;
public Gui(int[][] gg) {
Expand All @@ -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();
}
}
Expand All @@ -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();
Expand Down Expand Up @@ -175,7 +188,7 @@ public void stateChanged(javax.swing.event.ChangeEvent evt) {
jLabel2.getAccessibleContext().setAccessibleName("JLblVal");


pack();
pack();
}// </editor-fold>

private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {
Expand Down Expand Up @@ -222,6 +235,16 @@ public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Gui().setVisible(true);
new GenerationUpdate("Adjust the sliders to your preferred extent "
+ "for each of the 3 parameters of the Genetic Algorithm. "
+ "They are the Mutation Rate, the Number of Generations and the "
+ "Population Size respectively. Select Level Builder to create "
+ "your own custom level clicking on an area in a grid to "
+ "designate it as a solid block and clicking it again to "
+ "deselect it. Multiple windows will appear once you hit train, "
+ "in every generation there will be an update given on how "
+ "good all the Mario's are doing. In the multiple windows you "
+ "can see how Mario is doing and learning. ", 400);
}
});
}
Expand Down
Loading