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 .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions Asteroid.pde
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public class Asteroid{
if (distance < size/2 + locals.player.getSize()){
locals.player.setHit();
}

if (distance < 100 + (size/2) && locals.player.accelerate){
locals.player.avoids = locals.player.avoids + 1;
}
}

public void explode(){
Expand Down
55 changes: 50 additions & 5 deletions GameScene.pde
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ public class GameScene{
boolean online;
Locals locals;
Simple_NEAT n;
Grid gameGrid;

public GameScene(Locals l){
locals = l;
locals.level = 4;
locals.level = 1;
resetAstroids(locals.level);
gameGrid = new Grid(50, l);
locals.player = new Ship(locals);
n = new Simple_NEAT(33,4);
Network temp = Network.loadFromFile("C:/Users/WALTR/Downloads/CODE/Processing/Asteroids_Train/best.net");
n = new Simple_NEAT(gameGrid.getWidth() * gameGrid.getWidth() + 1, 4);
Network temp = Network.loadFromFile("/home/sam/Documents/CIS_365/AiProject/newRepo/Asteroids_Train/best.net");
n.addAgent(temp);
n.setCurrentAgent(0);
}
Expand Down Expand Up @@ -73,9 +75,10 @@ public class GameScene{
showText();
// locals.player.showBullets();
locals.player.show();
runNetwork();
runNetwork2();
showAsteroids();
checkLevel();
gameGrid.show();
}

public void runNetwork(){
Expand Down Expand Up @@ -105,7 +108,49 @@ public class GameScene{
if (outputs[2] >= 0.5){
locals.player.turnRight();
}
if (outputs[3] >= 0.5){
if (outputs[3] <= 0.5){
locals.player.accelerate = true;
locals.player.accelerate();
}
else{
locals.player.accelerate = false;
}

String outs = "";
for(float f : outputs){
outs +=(f + ", ");
}
textSize(20);
text(outs, width/2, height-50);

}

public void runNetwork2(){
float[] inputs = new float[gameGrid.getWidth() * gameGrid.getWidth() + 1];
inputs[0] = (float) locals.player.getAngle() / (2*3.14159265359);
int c = 1;
for (int x = 0; x < gameGrid.getGrid().length; x++){
for (int y = 0; y < gameGrid.getGrid().length; y++){
inputs[c] = (float) gameGrid.getGrid()[x][y];
// System.out.println(c + " - " + (float) gameGrid.getGrid()[x][y]);
c++;
}
}

n.runCurrent(inputs);

float[] outputs = n.getCurOutput();

if (outputs[0] > 0.0){
locals.player.shoot();
}
if (outputs[1] > 0.0){
locals.player.turnLeft();
}
if (outputs[2] > 0.0){
locals.player.turnRight();
}
if (outputs[3] > 0.0){
locals.player.accelerate = true;
locals.player.accelerate();
}
Expand Down
96 changes: 96 additions & 0 deletions Grid.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
public class Grid{
double[][] grid;
Locals l;
int cellWidth;


//CellWidth should be an even multiple of the width and height of the screen.
public Grid(int cellWidth, Locals l){
this.l = l;
this.cellWidth = cellWidth;
int xSize = (int) l.width / cellWidth;
int ySize = (int) l.height / cellWidth;
grid = new double[xSize][ySize];
resetGrid();
}

private void resetGrid(){
for (int x = 0; x < grid.length; x++){
for (int y = 0; y < grid.length; y++){
grid[x][y] = 0.0;
}
}
}

private void setAsteroids(){
resetGrid();
for (Asteroid a : l.asteroids){
int xCell = (int) (a.getX() / cellWidth);
int yCell = (int) (a.getY() / cellWidth);

//Cap all coordinates
if (xCell >= grid.length)
xCell = grid.length-1;
if (xCell < 0)
xCell = 0;
if (yCell >= grid[0].length)
yCell = grid[0].length-1;
if (yCell < 0)
yCell = 0;

grid[xCell][yCell] = 1.0;
}

int xCell = (int) (l.player.getX() / cellWidth);
int yCell = (int) (l.player.getY() / cellWidth);

//Cap all coordinates
if (xCell >= grid.length)
xCell = grid.length-1;
if (xCell < 0)
xCell = 0;
if (yCell >= grid[0].length)
yCell = grid[0].length-1;
if (yCell < 0)
yCell = 0;

grid[xCell][yCell] = -1;
}

public void show(){
float squareSize = 200;

stroke(255);
fill(0, 0, 0);
rect(10, 10, squareSize, squareSize);

float subSize = squareSize / grid.length;

setAsteroids();
for (int x = 0; x < grid.length; x++){
for (int y = 0; y < grid.length; y++){
if (grid[x][y] > 0){
fill(255);
noStroke();
rectMode(CORNER);
rect(10 + subSize * x, 10 + subSize * y, subSize, subSize);
}
if (grid[x][y] < 0){
fill(255, 0, 0);
noStroke();
rectMode(CORNER);
rect(10 + subSize * x, 10 + subSize * y, subSize, subSize);
}
}
}
}

public int getWidth(){
return grid.length;
}

public double[][] getGrid(){
return grid;
}

}
9 changes: 9 additions & 0 deletions Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ public void mutateWeight(){
c.randomizeWeight();
}

public void mutateBiasWeight(){
ArrayList<Neuron> validConnections = new ArrayList<Neuron>();
validConnections.addAll(hidden);
validConnections.addAll(outputs);

Neuron n = validConnections.get(r.nextInt(validConnections.size()));
n.randomizeBiasWeight();
}

public void remvoveRandConnection(){
ArrayList<Neuron> validConnections = new ArrayList<Neuron>();
validConnections.addAll(hidden);
Expand Down
28 changes: 28 additions & 0 deletions NetworkView.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
public class NetworkView{
Network net;
ArrayList<Node> nodes;

public NetworkView(Network net){
this.net = net;
}

private void initNodes(){
// for (Neuron n : net.getInputs()){
// break;
// }
}

public void show(){
return;
}
}


class Node{
float x, y;

public Node(float x, float y){
this.x = x;
this.y = y;
}
}
10 changes: 8 additions & 2 deletions Neuron.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ public void setInput(float val){
}

public void activate(){
float d = (float) Math.pow((double) Math.exp(1.0),(double) sum());
value = (float) (1.0/(1+d));
float sum = sum();
float n = (float) Math.pow((double) Math.exp(1.0), (double) sum) - (float) Math.pow((double) Math.exp(1.0), (double) -sum);
float d = (float) Math.pow((double) Math.exp(1.0), (double) sum) + (float) Math.pow((double) Math.exp(1.0), (double) -sum);
value = (float) (n/d);
activated = true;
}

Expand Down Expand Up @@ -130,6 +132,10 @@ public void removeConnection(Neuron n){
}
}

public void randomizeBiasWeight(){
this.biasWeight = r.nextFloat();
}

public static void main(String[] args){
Neuron n0 = new Neuron(1, 0);
Neuron n1 = new Neuron(1, 1);
Expand Down
12 changes: 10 additions & 2 deletions Ship.pde
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Ship{
boolean turn, accelerate, dead, noHit;
long timeStamp;
int k;
int lives, maxLives, score;
int lives, maxLives, score, avoids;
Vector velocity;
final double PI = 3.14159265359;
Locals locals;
Expand All @@ -35,6 +35,7 @@ public class Ship{
this.maxLives = 1;
this.lives = maxLives;
this.score = 0;
this.avoids = 0;
//ArrayList for the current pressed characters.
//(Mainly used for making turning less janky.)
this.pressedChars = new ArrayList<Integer>();
Expand Down Expand Up @@ -105,6 +106,13 @@ public class Ship{
turnRight();
}
}

if (angle > 2*PI){
angle = angle - (2*PI);
}
if (angle < 0){
angle = (2*PI) + angle;
}
}

private void turnLeft(){
Expand Down Expand Up @@ -253,7 +261,7 @@ public class Ship{
pushMatrix();
//Display the bullets
showBullets();
showSensors();
//showSensors();

//Edit pos of the ship.
turn();
Expand Down
6 changes: 3 additions & 3 deletions Simple_NEAT.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Simple_NEAT(int nI, int nO){
public void addAgent(){
agents.add(new Network(numInputs, numOutputs));
}

public void addAgent(Network n){
agents.add(n);
}
Expand Down Expand Up @@ -119,10 +119,10 @@ public void mutate(){
//Should we mutate this agent?
if (mutationRate >= num){
num = ThreadLocalRandom.current().nextDouble(0,1);
if (num <= 1/3){
if (num <= .33){
a.addRandHiddenNode();
}
else if(num > 1/3 && num <= 2/3){
else if(num > .33 && num <= .66){
a.addRandConnection();
}
else{
Expand Down
6 changes: 6 additions & 0 deletions Train.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
javac ./*.java
javac ./Train_Files/*.java
cd ./Train_Files
java Asteroids_Train $1
cd ..
Binary file added Train_Files/.DS_Store
Binary file not shown.
Binary file modified Train_Files/Asteroid.class
Binary file not shown.
4 changes: 4 additions & 0 deletions Train_Files/Asteroid.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public void checkHit(){
//System.out.println(distance);
if (distance < size/2 + locals.player.getSize()){
locals.player.setHit();
}

if (distance < 100 + (size/2) && locals.player.accelerate){
locals.player.avoids = locals.player.avoids + 1;
}
}

Expand Down
Binary file modified Train_Files/Asteroids_Train.class
Binary file not shown.
Loading