Skip to content

Commit

Permalink
final changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jorge-aparicio committed Dec 12, 2019
1 parent 6937451 commit 098cff3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Board extends JPanel implements ActionListener {
private final int ALL_DOTS = 900;
private final int RAND_POS = 29;
private final int DELAY = 120;
private int runtime = 10;
private int runtime = 15;

Random r = new Random();

Expand Down Expand Up @@ -57,7 +57,7 @@ public class Board extends JPanel implements ActionListener {
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private boolean newTurn = true;


//images and timer for the UI
private Timer timer;
Expand Down Expand Up @@ -214,7 +214,7 @@ private void gameOver(Graphics g) {
//alter the fitness depending on if the snake died or not
if (hasDied == true)
network.deathFitness();
network.increaseFitness(appleCount * 200);
network.applesEaten(appleCount);

//indicate that this snake has stopped running
numFinished++;
Expand Down
25 changes: 21 additions & 4 deletions Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
import java.util.Collections;

public class Game {

public static int iterations = 10;

//parameters for the genetic algorithm
private int numFeatures = 7;
private int numNetworks = 20;
private int numGenerations = 25;
private int numGenerations = 10;
public int genNum = 1;


//parameters for each neural network in the genetic algorithm
ArrayList<GeneticNN> networkList = new ArrayList<GeneticNN>();
private int total = numNetworks;
private int total;
public int numLayers = 1;
public int numHidden = 30;
public int numHidden = 25;

/**
* run the game
Expand All @@ -26,26 +30,31 @@ public class Game {
public static void main(String[] args) throws AWTException {
Game game = new Game();
game.start();


}


/**
* Initializes first gen of neural nets
*/
public Game() {
total = numNetworks;
for (int i = 0; i < numNetworks; i++) {
GeneticNN network = new GeneticNN(numHidden, numLayers, numFeatures, genNum);
network.train();
networkList.add(network);
}
}


/**
* run the generation of snakes and then create children based on the results
* @throws AWTException
*/
public void start() throws AWTException {
this.runGen(networkList);
for (int g = 1; g < numGenerations; g++) {
for (int g = 1; g <= numGenerations; g++) {
ArrayList<GeneticNN> newNetList = nextGen(networkList);
networkList = newNetList;
runGen(networkList);
Expand Down Expand Up @@ -91,6 +100,14 @@ public ArrayList<GeneticNN> nextGen(ArrayList<GeneticNN> networkList) {
ArrayList<GeneticNN> allTheChildren = new ArrayList<GeneticNN>(); //the new generation

Collections.sort(networkList, GeneticNN.byFitness());

// prints average performance per generation
double count = 0;
for (GeneticNN net : networkList) {
count+= net.appleCount();
}
System.out.println(count/numNetworks);


//Get the top 2 fittest networks and call the crossover function on them
GeneticNN net1 = networkList.get(numNetworks - 1);
Expand Down
24 changes: 20 additions & 4 deletions ml/classifiers/GeneticNN.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class GeneticNN implements Comparable<GeneticNN> {

private int numHidden;
private int numLayers = 1;
private int appleCount = 0;
private int numFeatures;

//weights for first and last layers in network
Expand Down Expand Up @@ -244,18 +245,33 @@ public void mutate(double mutationRate) {
* decrease the fitness of this model
* @param count the amount to decrease fitness by
*/
public void decreaseFitness(int count) {
fitness = fitness - count;
public void decreaseFitness(int amount) {
fitness = fitness - amount;
}

/**
* increase the fitness of this model
* @param count the amount to increase fitness by
*/
public void increaseFitness(int count) {
fitness = fitness + count;
public void increaseFitness(int amount) {
fitness = fitness + amount;
}

/**
* increase the fitness of this model by 200 times the amount of apples eaten
* @param count the amount to of apples eaten
*/
public void applesEaten(int count){
appleCount = count;
fitness = fitness+count*200;
}
/**
*
* @return amount of apples eaten
*/
public int appleCount(){
return appleCount;
}
/**
* alter the fitness by the appropriate amount if the network dies
*/
Expand Down

0 comments on commit 098cff3

Please sign in to comment.