Skip to content

Commit

Permalink
All working, some errors still being handled so need fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
georgehtaylor1 committed Nov 9, 2016
1 parent 019831f commit 0451893
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/Colors.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Colors {
public static Color LEFT_VIEW = new Color(255, 255, 0); // yellow
public static Color DIRECT_VIEW = new Color(51, 204, 51);
public static Color RIGHT_VIEW = new Color(255, 0, 255); // pink
public static Color IN_RANGE = new Color(255, 255, 255);
public static Color IN_RANGE = new Color(150, 150, 150);

public static Color FOOD = new Color(255, 255, 255);

Expand Down
71 changes: 56 additions & 15 deletions src/Creature.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.util.ArrayList;

public class Creature extends Entity {
public class Creature extends Entity implements Cloneable {

private double angle;

Expand All @@ -21,6 +19,8 @@ public class Creature extends Entity {

private int score = 0;

public static double STRAIGHT_ANGLE = 5;

/**
* Creatre a new creature with the set of parameters for the simulation
*
Expand All @@ -30,12 +30,12 @@ public class Creature extends Entity {
public Creature(ParameterSet params) {
super(params);
maxGene = (params.getState_count() * ACTION_COUNT) - 1;
genes = new int[params.getState_count()][PERCEPT_COUNT];
setGenes(new int[params.getState_count()][PERCEPT_COUNT]);

setPos(Utils.getRandomPoint(params.getWindow_width(), params.getWindow_height()));
for (int i = 0; i < params.getState_count(); i++) {
for (int j = 0; j < PERCEPT_COUNT; j++) {
genes[i][j] = Utils.rand.nextInt(maxGene);
getGenes()[i][j] = Utils.rand.nextInt(maxGene);
}
}
}
Expand All @@ -53,16 +53,15 @@ public void draw(Graphics g) {

for (Food f : Simulator.food) {
if (Utils.getDistance(getPos(), f.getPos()) < getParams().getView_range()) {
g.setColor(Colors.IN_RANGE);
double foodAngle = getFoodAngle(f);
if (foodAngle > -getParams().getView_angle() && foodAngle < 0) {
if (foodAngle > -getParams().getView_angle() && foodAngle < -STRAIGHT_ANGLE) {
g.setColor(Colors.LEFT_VIEW);
}
if (foodAngle < getParams().getView_angle() && foodAngle > 0) {
} else if (foodAngle < getParams().getView_angle() && foodAngle > STRAIGHT_ANGLE) {
g.setColor(Colors.RIGHT_VIEW);
}
if (foodAngle == 0) {
} else if (foodAngle <= STRAIGHT_ANGLE && foodAngle >= -STRAIGHT_ANGLE) {
g.setColor(Colors.DIRECT_VIEW);
} else {
g.setColor(Colors.IN_RANGE);
}
g.drawLine((int) getPos().getX(), (int) getPos().getY(), (int) f.getPos().getX(),
(int) f.getPos().getY());
Expand All @@ -85,13 +84,13 @@ private int getPercept() {

double foodAngle = getFoodAngle(closest);

if (foodAngle > -getParams().getView_range() && foodAngle < 0) {
if (foodAngle > -getParams().getView_angle() && foodAngle < -STRAIGHT_ANGLE) {
return FOOD_LEFT;
}
if (foodAngle < getParams().getView_range() && foodAngle > 0) {
if (foodAngle < getParams().getView_angle() && foodAngle > STRAIGHT_ANGLE) {
return FOOD_RIGHT;
}
if (foodAngle == 0) {
if (foodAngle <= STRAIGHT_ANGLE && foodAngle >= -STRAIGHT_ANGLE) {
return FOOD_STRAIGHT;
}
return FOOD_NONE;
Expand Down Expand Up @@ -160,10 +159,23 @@ private void turn(boolean dir) {
}
}

/**
* Perform an action for the creature from the current state and environment
*/
public void act() {
int percept = getPercept();
int gene = genes[state][percept];
assert (state <= getParams().getState_count() && state >= 0);
assert (percept <= PERCEPT_COUNT && percept >= 0);
int gene = 0;
try {
gene = getGenes()[state][percept];
} catch (Exception e) {
System.out.println(e);
}
state = Math.floorDiv(gene, ACTION_COUNT);
if (state < 0) {
System.out.println("ERR");
}
int action = Math.floorMod(gene, ACTION_COUNT);
switch (action) {
case 0:
Expand All @@ -178,6 +190,23 @@ public void act() {
}
}

/**
* Randomly mutate the genes
*
* @param currRound
* The current round of the simulation
*/
public void mutate(int currRound) {
for (int i = 0; i < getParams().getState_count(); i++) {
for (int j = 0; j < PERCEPT_COUNT; j++) {
int mod = (int) Math.round(Utils.learnFunction(currRound, getParams().getLearning_stretch())
* Utils.modificationFunction(maxGene, getParams().getLearning_exponent()));
getGenes()[i][j] += mod;
getGenes()[i][j] = (getGenes()[i][j] + maxGene) % maxGene;
}
}
}

public int getScore() {
return score;
}
Expand All @@ -190,4 +219,16 @@ public void eat() {
setScore(getScore() + 1);
}

public Creature clone() throws CloneNotSupportedException {
return (Creature) super.clone();
}

public int[][] getGenes() {
return genes;
}

public void setGenes(int[][] genes) {
this.genes = genes;
}

}
46 changes: 46 additions & 0 deletions src/Generation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.ArrayList;

public class Generation {

private ArrayList<Creature> creatures;
private double meanScore = -1;

public Generation(ArrayList<Creature> creatures) {
this.creatures = creatures;
}

/**
* Get the minimum score
*
* @return The minimum score
*/
public int getMinScore() {
return creatures.get(creatures.size() - 1).getScore();
}

/**
* Get the max store across the creatures
*
* @return The max score
*/
public int getmaxScore() {
return creatures.get(0).getScore();
}

/**
* Lazy implementation for the mean
*
* @return The mean score
*/
public double getMeanScore() {
if (meanScore == -1) {
int s = 0;
for (Creature c : creatures) {
s += c.getScore();
}
meanScore = s / creatures.size();
}
return meanScore;
}

}
22 changes: 11 additions & 11 deletions src/GeneticAlgorithm2.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ public class GeneticAlgorithm2 {

public static void main(String[] args) {

ParameterSet pset = new ParameterSet(1200, 800,
20, 10,
10, 20,
2, 2,
32,
45, 200,
5000);

Simulator sim = new Simulator(pset);
// Not too many creatures and quite large
ParameterSet pset1 = new ParameterSet(1400, 1000, 20, 10, 20, 70, 2, 2, 32, 45, 200, 800, 200.0, 0.8, 19);

// 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);

ParameterSet active_set = pset2;
Simulator sim = new Simulator(active_set);
JFrame frame = new JFrame("GeneticAlgorithms2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = (JPanel) frame.getContentPane();
panel.setPreferredSize(new Dimension(pset.getWindow_width(), pset.getWindow_height()));
panel.setPreferredSize(new Dimension(active_set.getWindow_width(), active_set.getWindow_height()));
panel.setLayout(null);

sim.setBounds(0, 0, pset.getWindow_width(), pset.getWindow_height());
sim.setBounds(0, 0, active_set.getWindow_width(), active_set.getWindow_height());
sim.setIgnoreRepaint(true);

panel.add(sim);

frame.pack();
frame.setResizable(false);
frame.setVisible(true);

sim.createBufferStrategy(2);
new Thread(sim).start();

Expand Down
34 changes: 33 additions & 1 deletion src/ParameterSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ public class ParameterSet {

private int generation_length;

private double learning_stretch;
private double selection_probability;
private int learning_exponent;

public ParameterSet(int window_width, int window_height, int creature_size, int food_size, int creature_count,
int food_count, int creature_speed, int creature_rotationSpeed, int state_count, int view_angle,
int view_range, int generation_length) {
int view_range, int generation_length, double learning_stretch, double selection_probability,
int learning_exponent) {
this.window_width = window_width;
this.window_height = window_height;
this.creature_size = creature_size;
Expand All @@ -34,6 +39,9 @@ public ParameterSet(int window_width, int window_height, int creature_size, int
this.view_angle = view_angle;
this.view_range = view_range;
this.generation_length = generation_length;
this.learning_stretch = learning_stretch;
this.selection_probability = selection_probability;
this.learning_exponent = learning_exponent;
}

public int getWindow_width() {
Expand Down Expand Up @@ -132,4 +140,28 @@ public void setState_count(int state_count) {
this.state_count = state_count;
}

public double getLearning_stretch() {
return learning_stretch;
}

public void setLearning_stretch(double learning_stretch) {
this.learning_stretch = learning_stretch;
}

public double getSelection_probability() {
return selection_probability;
}

public void setSelection_probability(double selection_probability) {
this.selection_probability = selection_probability;
}

public int getLearning_exponent() {
return learning_exponent;
}

public void setLearning_exponent(int learning_exponent) {
this.learning_exponent = learning_exponent;
}

}
Loading

0 comments on commit 0451893

Please sign in to comment.