Skip to content

Commit

Permalink
Lots of changes, drawing and simulation code complete but needs to im…
Browse files Browse the repository at this point in the history
…plement GA
  • Loading branch information
georgehtaylor1 committed Nov 8, 2016
1 parent fe468d9 commit 6112c25
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 60 deletions.
18 changes: 18 additions & 0 deletions src/Colors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.awt.Color;

public class Colors {

public static Color BACKGROUND = new Color(0, 0, 0);
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);

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 FOOD = new Color(255, 255, 255);

public static Color PARENT_LINK = new Color(89, 89, 89);
}
157 changes: 152 additions & 5 deletions src/Creature.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,163 @@
import java.awt.Graphics;
import java.awt.geom.Point2D;

public class Creature extends Entity{
public class Creature extends Entity {

public Creature(int size) {
super(size);
private double angle;

public static int PERCEPT_COUNT = 4;
public static int ACTION_COUNT = 3;

private int[][] genes;
private int maxGene = 0;

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;

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
}

@Override
public void draw(Graphics g) {
// TODO Auto-generated method stub


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.drawLine((int) getPos().getX(), (int) getPos().getY(), dx, dy);

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) {
g.setColor(Colors.LEFT_VIEW);
}
if (foodAngle < getParams().getView_angle() && foodAngle > 0) {
g.setColor(Colors.RIGHT_VIEW);
}
if (foodAngle == 0) {
g.setColor(Colors.DIRECT_VIEW);
}
g.drawLine((int) getPos().getX(), (int) getPos().getY(), (int) f.getPos().getX(),
(int) f.getPos().getY());
}
}

}

/**
* Get the percept for the creature
*
* @return The percept for the creature
*/
private int getPercept() {
Food closest = getClosest();
double distance = Utils.getDistance(getPos(), closest.getPos());
if (distance > getParams().getView_range()) {
return FOOD_NONE;
}

double foodAngle = getFoodAngle(closest);

if (foodAngle > -getParams().getView_range() && foodAngle < 0) {
return FOOD_LEFT;
}
if (foodAngle < getParams().getView_range() && foodAngle > 0) {
return FOOD_RIGHT;
}
if (foodAngle == 0) {
return FOOD_STRAIGHT;
}
return FOOD_NONE;
}

/**
* Get the closest item of food to the creature
*
* @return The closest item of food
*/
private Food getClosest() {
Food closest = Simulator.food.get(0);
double distance = Utils.getDistance(closest.getPos(), getPos());
for (Food f : Simulator.food) {
double c = Utils.getDistance(f.getPos(), getPos());
if (c < distance) {
distance = c;
closest = f;
}
}
return closest;
}

/**
* Get the angle between the creature and the item of food
*
* @param closest
* The closest item of food
* @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 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));
return foodAngle;
}

/**
* Move the creature
*/
private void move() {

double x = (getPos().getX() + (getParams().getCreature_speed() * Math.cos(Math.toRadians(angle)))
+ getParams().getWindow_width()) % getParams().getWindow_width();
double y = (getPos().getY() + (getParams().getCreature_speed() * Math.sin(Math.toRadians(angle)))
+ getParams().getWindow_height()) % getParams().getWindow_height();
getPos().setLocation(x, y);
}

/**
* Turn the creature
*
* @param dir
* True = Left, False = Right
*/
private void turn(boolean dir) {
angle += dir ? getParams().getCreature_rotationSpeed() : -getParams().getCreature_rotationSpeed();

angle = angle % 360;
if (angle > 180) {
angle = angle - 360;
} else if (angle < -180) {
angle = 360 + angle;
}
}

public void act() {

}

public int getScore() {
return score;
}

public void setScore(int score) {
this.score = score;
}

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

}

}
33 changes: 25 additions & 8 deletions src/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@

public abstract class Entity {

private ParameterSet params;
private Point2D pos;
private int size;

public Entity(int size){
this.size = size;
public Entity(ParameterSet params) {
this.params = params;
}

/**
* Get the entities position
*
* @return The position of the entity
*/
public Point2D getPos() {
Expand All @@ -20,14 +22,17 @@ public Point2D getPos() {

/**
* Set the position of the entity
* @param pos The new position of the entity
*
* @param pos
* The new position of the entity
*/
public void setPos(Point2D pos) {
this.pos = pos;
}

/**
* Get the size of the entity
*
* @return The entities size
*/
public int getSize() {
Expand All @@ -36,16 +41,28 @@ public int getSize() {

/**
* Set the size of the entity
* @param size THe new size of the entity
*
* @param size
* THe new size of the entity
*/
public void setSize(int size) {
this.size = size;
}

/**
* Draw the entity
* @param g The graphics component
*
* @param g
* The graphics component
*/
public abstract void draw(Graphics g);


public ParameterSet getParams() {
return params;
}

public void setParams(ParameterSet params) {
this.params = params;
}

}
19 changes: 14 additions & 5 deletions src/Food.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import java.awt.Graphics;

public class Food extends Entity{
public class Food extends Entity {

public Food(int size) {
super(size);
public Food(ParameterSet params) {
super(params);
// TODO Auto-generated constructor stub
}

@Override
public void draw(Graphics g) {
// TODO Auto-generated method stub

g.setColor(Colors.FOOD);
g.drawOval((int) getPos().getX() - getParams().getFood_size() / 2,
(int) getPos().getY() - getParams().getFood_size() / 2, (int) getParams().getFood_size(),
(int) getParams().getFood_size());
}

/**
* Eat the food (move the food to a random position)
*/
public void eat() {
setPos(Utils.getRandomPoint(getParams().getWindow_width(), getParams().getWindow_height()));
}

}
18 changes: 9 additions & 9 deletions src/GeneticAlgorithm2.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
public class GeneticAlgorithm2 {

public static void main(String[] args) {
ParameterSet pset = null; //TODO: Create the parameter set
Simulator sim = new Simulator(pset);

ParameterSet pset = new ParameterSet(600, 600, 10, 5, 10, 10, 2, 2, 32, 45, 50);

Simulator sim = new Simulator(pset);
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.setLayout(null);

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

panel.add(sim);

frame.pack();
frame.setResizable(false);
frame.setVisible(true);
sim.createBufferStrategy(2);
new Thread(sim).start();

}

}
Loading

0 comments on commit 6112c25

Please sign in to comment.