Skip to content

Commit

Permalink
Code compiles and runs successfully, just need to implement GA
Browse files Browse the repository at this point in the history
  • Loading branch information
georgehtaylor1 committed Nov 8, 2016
1 parent 6112c25 commit 019831f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/Colors.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class Colors {

public static Color BACKGROUND = new Color(0, 0, 0);
public static Color TEXT = new Color(255, 255, 255);
public static Color CREATURE = new Color(255, 255, 255);
public static Color CREATURE_HOVER = new Color(103, 204, 255);
public static Color CREATURE_ACTIVE = new Color(77, 166, 255);
Expand Down
46 changes: 38 additions & 8 deletions src/Creature.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.util.ArrayList;

public class Creature extends Entity {

Expand All @@ -11,27 +12,43 @@ public class Creature extends Entity {
private int[][] genes;
private int maxGene = 0;

private int state;

public static int FOOD_LEFT = 0;
public static int FOOD_RIGHT = 1;
public static int FOOD_NONE = 2;
public static int FOOD_STRAIGHT = 3;

private int score = 0;

/**
* Creatre a new creature with the set of parameters for the simulation
*
* @param params
* THe simulation parameters
*/
public Creature(ParameterSet params) {
super(params);
maxGene = (params.getState_count() * ACTION_COUNT) - 1;
genes = new int[params.getState_count()][PERCEPT_COUNT];
// TODO Auto-generated constructor stub

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

@Override
public void draw(Graphics g) {

g.setColor(Colors.CREATURE);
g.drawOval((int) getPos().getX() - getSize() / 2, (int) getPos().getY() - getSize() / 2, getSize(), getSize());
int dx = (int) ((getSize() / 2 * Math.cos(Math.toRadians(angle))) + getPos().getX());
int dy = (int) ((getSize() / 2 * Math.sin(Math.toRadians(angle))) + getPos().getY());
g.drawOval((int) getPos().getX() - getParams().getCreature_size() / 2,
(int) getPos().getY() - getParams().getCreature_size() / 2, getParams().getCreature_size(),
getParams().getCreature_size());
int dx = (int) ((getParams().getCreature_size() / 2 * Math.cos(Math.toRadians(angle))) + getPos().getX());
int dy = (int) ((getParams().getCreature_size() / 2 * Math.sin(Math.toRadians(angle))) + getPos().getY());
g.drawLine((int) getPos().getX(), (int) getPos().getY(), dx, dy);

for (Food f : Simulator.food) {
Expand Down Expand Up @@ -106,8 +123,8 @@ private Food getClosest() {
* @return The angle between the creature and the food
*/
private double getFoodAngle(Food closest) {
double dx1 = ((getSize() / 2) * Math.cos(Math.toRadians(angle)));
double dy1 = ((getSize() / 2) * Math.sin(Math.toRadians(angle)));
double dx1 = ((getParams().getCreature_size() / 2) * Math.cos(Math.toRadians(angle)));
double dy1 = ((getParams().getCreature_size() / 2) * Math.sin(Math.toRadians(angle)));
double dx2 = closest.getPos().getX() - getPos().getX();
double dy2 = closest.getPos().getY() - getPos().getY();
double foodAngle = Math.toDegrees(Math.atan2(dx1 * dy2 - dy1 * dx2, dx1 * dx2 + dy1 * dy2));
Expand Down Expand Up @@ -144,7 +161,21 @@ private void turn(boolean dir) {
}

public void act() {

int percept = getPercept();
int gene = genes[state][percept];
state = Math.floorDiv(gene, ACTION_COUNT);
int action = Math.floorMod(gene, ACTION_COUNT);
switch (action) {
case 0:
move();
break;
case 1:
turn(true);
break;
case 2:
turn(false);
break;
}
}

public int getScore() {
Expand All @@ -157,7 +188,6 @@ public void setScore(int score) {

public void eat() {
setScore(getScore() + 1);

}

}
4 changes: 2 additions & 2 deletions src/Food.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ public class Food extends Entity {

public Food(ParameterSet params) {
super(params);
// TODO Auto-generated constructor stub
setPos(Utils.getRandomPoint(params.getWindow_width(), params.getWindow_height()));
}

@Override
public void draw(Graphics g) {
g.setColor(Colors.FOOD);
g.drawOval((int) getPos().getX() - getParams().getFood_size() / 2,
g.fillOval((int) getPos().getX() - getParams().getFood_size() / 2,
(int) getPos().getY() - getParams().getFood_size() / 2, (int) getParams().getFood_size(),
(int) getParams().getFood_size());
}
Expand Down
8 changes: 7 additions & 1 deletion src/GeneticAlgorithm2.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ public class GeneticAlgorithm2 {

public static void main(String[] args) {

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

Simulator sim = new Simulator(pset);
JFrame frame = new JFrame("GeneticAlgorithms2");
Expand Down
3 changes: 2 additions & 1 deletion src/ParameterSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ParameterSet {

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 view_range, int generation_length) {
this.window_width = window_width;
this.window_height = window_height;
this.creature_size = creature_size;
Expand All @@ -33,6 +33,7 @@ public ParameterSet(int window_width, int window_height, int creature_size, int
this.state_count = state_count;
this.view_angle = view_angle;
this.view_range = view_range;
this.generation_length = generation_length;
}

public int getWindow_width() {
Expand Down
32 changes: 26 additions & 6 deletions src/Simulator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Random;

Expand Down Expand Up @@ -29,8 +30,21 @@ public Simulator(ParameterSet params) {
Utils.rand = new Random(1); // Use a seed to make it deterministic
setCreatures(new ArrayList<Creature>());
food = new ArrayList<Food>();
initEntities();
}


/**
* Initialize the set of food and creatures
*/
private void initEntities() {
for (int i = 0; i < getParams().getCreature_count(); i++) {
creatures.add(new Creature(getParams()));
}
for (int i = 0; i < getParams().getFood_count(); i++) {
food.add(new Food(getParams()));
}
}

public void update() {

if (currFrame < params.getGeneration_length()) {
Expand Down Expand Up @@ -108,17 +122,23 @@ public void run() {

public void paint(Graphics g) {

g.setColor(Colors.BACKGROUND);
g.clearRect(0, 0, params.getWindow_width(), params.getWindow_height());
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Colors.BACKGROUND);
g2.fillRect(0, 0, params.getWindow_width(), params.getWindow_height());

for (Creature c : getCreatures()) {
c.draw(g);
c.draw(g2);
}

for (Food f : food) {
f.draw(g);
f.draw(g2);
}


g2.setColor(Colors.TEXT);
g.drawString(Integer.toString(currRound) + " - " + Integer.toString(currFrame), 10, 10);
g.dispose();
getBufferStrategy().show();

}

public ParameterSet getParams() {
Expand Down

0 comments on commit 019831f

Please sign in to comment.