Skip to content

Commit

Permalink
Everything working, added a graph (just need to fix the strange closi…
Browse files Browse the repository at this point in the history
…ng bug)
  • Loading branch information
georgehtaylor1 committed Nov 10, 2016
1 parent 2550e59 commit ca62291
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Generation.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public int getMinScore() {
*
* @return The max score
*/
public int getmaxScore() {
public int getMaxScore() {
return creatures.get(0).getScore();
}

Expand Down
37 changes: 37 additions & 0 deletions src/GenerationsGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.awt.Dimension;
import java.util.ArrayList;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;

@SuppressWarnings("serial")
public class GenerationsGraph extends ApplicationFrame {

public GenerationsGraph(String title, ArrayList<Generation> generations) {
super(title);
JFreeChart lineChart = ChartFactory.createLineChart("Creature Progression", "Generation No.", "Score",
createDataSet(generations), PlotOrientation.VERTICAL, true, true, false);

ChartPanel chartPanel = new ChartPanel(lineChart);
chartPanel.setPreferredSize(new Dimension(560, 367));
setContentPane(chartPanel);
}

private CategoryDataset createDataSet(ArrayList<Generation> generations) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
int i = 0;
for(Generation g : generations){
dataset.addValue(g.getMeanScore(), "Mean", ""+i);
dataset.addValue(g.getMinScore(), "Max", ""+i);
dataset.addValue(g.getMaxScore(), "Min", ""+i);
i++;
}
return dataset;
}

}
5 changes: 4 additions & 1 deletion src/GeneticAlgorithm2.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ public static void main(String[] args) {

// Lots of creatures, more breeding, longer running, everything smaller
ParameterSet pset2 = new ParameterSet(1600, 1000, 10, 5, 50, 150, 3, 3, 48, 30, 150, 1000, 500.0, 0.7, 25);

// More Food, longer burn down for alpha, slightly shorter view range!
ParameterSet pset3 = new ParameterSet(1600, 1000, 10, 5, 50, 400, 3, 3, 48, 30, 100, 1000, 100.0, 0.8, 25);

ParameterSet active_set = pset2;
ParameterSet active_set = pset3;
Simulator sim = new Simulator(active_set);
JFrame frame = new JFrame("GeneticAlgorithms2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Expand Down
16 changes: 16 additions & 0 deletions src/Simulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import java.util.Comparator;
import java.util.Random;

import javax.swing.JFrame;

import org.jfree.ui.RefineryUtilities;

@SuppressWarnings("serial")
public class Simulator extends Canvas implements Runnable, KeyListener {

Expand Down Expand Up @@ -215,9 +219,21 @@ public void keyPressed(KeyEvent arg0) {
case KeyEvent.VK_SPACE:
paused = !paused;
break;
case KeyEvent.VK_G:
graph();
break;
}
}

private void graph() {
GenerationsGraph g = new GenerationsGraph("Generation Scores", generations);
g.pack();
RefineryUtilities.centerFrameOnScreen(g);
g.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
g.setVisible(true);

}

@Override
public void keyReleased(KeyEvent arg0) {
}
Expand Down

0 comments on commit ca62291

Please sign in to comment.