Skip to content

Commit e328774

Browse files
committed
finished gen alg. added some ui features
1 parent 877382b commit e328774

File tree

5 files changed

+30
-20
lines changed

5 files changed

+30
-20
lines changed

JavaRacer/Agent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ public void draw(Graphics2D graphics){
160160
public void reset(){
161161
this.isFinished = false;
162162
this.instructionIndex = 0;
163+
this.points = 0;
164+
this.laps = 0;
163165
this.agentX=MapLoader.spawnX*gameWindow.tileSize;
164166
this.agentY=MapLoader.spawnY*gameWindow.tileSize;
165167
velocity.X = 0;

JavaRacer/GenerationAlgorithm.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package JavaRacer;
22
import java.util.ArrayList;
3-
import java.util.Arrays;
43
import java.util.Collections;
54
import java.util.Random;
65

@@ -11,10 +10,11 @@ public class GenerationAlgorithm {
1110
public final int MAX_MUTATION_COUNT = 5;
1211
public final int ELITISM_NUMBER = 2;
1312
public final int NUMBER_OF_GENERATIONS_PER_INCREASE = 10;
14-
static Random rand = new Random(0);
13+
static Random rand = new Random();
1514
ArrayList<Integer> eliteIndexes = new ArrayList<Integer>();
1615
int generationNumber = 0;
1716
int instructionSize = 20;
17+
double bestFit = 0;
1818
Agent[] agents;
1919

2020
public GenerationAlgorithm(Agent[] agents){
@@ -27,9 +27,8 @@ public void checkGeneration(){
2727
ArrayList<Double> fitnesses = getFitness();
2828
ArrayList<Double> weights = getWeights(fitnesses);
2929
ArrayList<Integer[]> parentsList = getParents(weights);
30-
for (Integer[] integers : parentsList) {
31-
System.out.println(Arrays.toString(integers));
32-
}
30+
this.bestFit = fitnesses.get(eliteIndexes.get(0));
31+
System.out.println("Best fit: " + fitnesses.get(eliteIndexes.get(0)) + " "+ fitnesses.get(eliteIndexes.get(1)));
3332
System.out.printf("Generation completed number: %d\n",generationNumber);
3433
crossOver(parentsList);
3534
for(Agent agent : agents){
@@ -55,6 +54,7 @@ public ArrayList<Double> getFitness(){
5554
}
5655
ArrayList<Double> fitnessCopy = new ArrayList<>(fitness);
5756
Collections.sort(fitnessCopy, Collections.reverseOrder(null));
57+
eliteIndexes.clear();
5858
for(int i = 0; i<ELITISM_NUMBER;i++){
5959
eliteIndexes.add(fitness.indexOf(fitnessCopy.get(i)));
6060
}
@@ -95,28 +95,29 @@ public ArrayList<Integer[]> getParents(ArrayList<Double> weights){
9595
public void crossOver(ArrayList<Integer[]> parents){
9696
ArrayList<ArrayList<Integer>> instructionsTemp = new ArrayList<ArrayList<Integer>>();
9797
for(int i = 0; i<POPULATION; i++){
98-
instructionsTemp.add(agents[i].instructions);
98+
instructionsTemp.add(new ArrayList<Integer>(agents[i].instructions));
9999
}
100100
for (int i = 0; i<parents.size();i++) {
101-
Integer[] parentPair = parents.get(i);
102-
if(eliteIndexes.contains(parentPair[0])){
101+
if(eliteIndexes.contains(i)){
103102
continue;
104103
}
105-
if(parentPair[0] != parentPair[1]){
106-
int crossOverLength = rand.nextInt(10);
107-
int indexOfCrossing = rand.nextInt(agents[parentPair[0]].instructions.size()-crossOverLength);
108-
for(int j = indexOfCrossing; j<indexOfCrossing+crossOverLength;j++){
109-
agents[parentPair[0]].instructions.set(j,instructionsTemp.get(parentPair[1]).get(j));
110-
}
104+
Integer[] parentPair = parents.get(i);
105+
int crossOverLength = rand.nextInt(10);
106+
int indexOfCrossing = rand.nextInt(agents[parentPair[0]].instructions.size()-crossOverLength);
107+
ArrayList<Integer> instruction1 = new ArrayList<Integer>(instructionsTemp.get(parentPair[0]));
108+
ArrayList<Integer> instruction2 = new ArrayList<Integer>(instructionsTemp.get(parentPair[1]));
109+
for(int j = indexOfCrossing; j<indexOfCrossing+crossOverLength;j++){
110+
instruction1.set(j, instruction2.get(j));
111111
}
112+
agents[i].instructions.clear();
113+
agents[i].instructions.addAll(instruction1);
112114
if(Math.random()<MUTATION_CHANCE){
113115
int numberOfMutations = rand.nextInt(MAX_MUTATION_COUNT);
114116
for(int m = 0; m<numberOfMutations;m++){
115-
int randomInstructionIndex = rand.nextInt(agents[parentPair[0]].instructions.size());
116-
agents[parentPair[0]].instructions.set(randomInstructionIndex,rand.nextInt(5)); //randomize instructions
117+
int randomInstructionIndex = rand.nextInt(agents[i].instructions.size());
118+
agents[i].instructions.set(randomInstructionIndex,rand.nextInt(5)); //randomize instructions
117119
}
118120
}
119-
120121
}
121122
}
122123
}

JavaRacer/MapLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class MapLoader {
1111
public static int[][] loadMap() {
1212
BufferedImage image;
1313
int[][] map = new int[100][100];
14-
Random rand = new Random(0);
14+
Random rand = new Random();
1515
try {
1616
image = ImageIO.read(new File("source/track.png"));
1717
if(image.getHeight()== 100&& image.getWidth() == 100){

JavaRacer/TileManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package JavaRacer;
22

3+
import java.awt.Color;
4+
import java.awt.Font;
35
import java.awt.Graphics2D;
46
import java.io.File;
57
import java.io.IOException;
@@ -83,5 +85,10 @@ public void draw(Graphics2D graphics2d, Camera camera){ //Draws the map around t
8385
}
8486
}
8587
}
88+
graphics2d.setFont(new Font("TimesRoman", Font.BOLD, 20));
89+
graphics2d.setColor(Color.BLACK);
90+
graphics2d.drawString("Best fit: " + gameWindow.genAlg.bestFit, 10, 20);
91+
graphics2d.drawString("Gen. No: " + gameWindow.genAlg.generationNumber, 10, 40);
92+
8693
}
8794
}

pipline.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
1. create a population with rand properties +
2-
2. iterate over number of generations
2+
2. iterate over number of generations +
33
3. calculate fitness of each agent (points) +
44
4. in the fitness array get the (elitism_number) of agents starting from the best. +
55
5. calculate probability values of the array using softmax +
66
6. using the above weights, get randomly chosen pairs from the array and append to a parents_list where each element contains 2 agents +
77
7. iterate over population and create childs of these parents using crossover. +
88
8. use random to determine whether to mutate or not+
9-
9. directly append elite agents
9+
9. directly append elite agents +
1010
10. set population to new population and continue+

0 commit comments

Comments
 (0)