Skip to content

Commit 877382b

Browse files
committed
developed the gen alg. fixed issues
1 parent c2fb950 commit 877382b

File tree

4 files changed

+95
-27
lines changed

4 files changed

+95
-27
lines changed

JavaRacer/Agent.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public class Agent {
1515
int screenX, screenY; //the x,y coordinates of the car on screen
1616
Rectangle solidArea = new Rectangle(); // aka hitbox of the car
1717
public double frictionCoefficient; //road friction: 1 in asphalt, 0.1 in grass checked by CollisionControl class
18-
public int points, laps = 0;
18+
public int laps = 0;
19+
public double points = 0;
1920
public boolean onFinishLine, offFinishLine, isCollided = false; //flags for game logic
2021
public ArrayList<Integer> instructions;
2122
int nextActionTimer,instructionIndex;
@@ -126,11 +127,11 @@ public void calculatePoints(){
126127
points-=1000;
127128
isCollided = true;
128129
}
129-
points -= 2*velocity.netVelocity()/frictionCoefficient;
130+
points -= (velocity.netVelocity()/frictionCoefficient)/10;
130131
}
131132
else{
132133
isCollided = false;
133-
points += velocity.netVelocity();
134+
points += velocity.netVelocity()/10;
134135
}
135136
if(points<0){
136137
points = 0;
@@ -158,6 +159,7 @@ public void draw(Graphics2D graphics){
158159
}
159160
public void reset(){
160161
this.isFinished = false;
162+
this.instructionIndex = 0;
161163
this.agentX=MapLoader.spawnX*gameWindow.tileSize;
162164
this.agentY=MapLoader.spawnY*gameWindow.tileSize;
163165
velocity.X = 0;
@@ -189,7 +191,7 @@ public int getAgentX(){
189191
public int getAgentY(){
190192
return this.agentY;
191193
}
192-
public int getPoints(){
194+
public double getPoints(){
193195
return this.points;
194196
}
195197
public int getLaps(){

JavaRacer/GameWindow.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public void update(){
6161
for(int i = 0; i<agents.length;i++){
6262
agents[i].update();
6363
}
64-
genAlg.checkGeneration();
64+
if(genAlg.generationNumber<genAlg.NUMBER_OF_GENERATIONS){
65+
genAlg.checkGeneration();
66+
}
6567
camera.update();
6668
}
6769
public void paintComponent(Graphics graphics){

JavaRacer/GenerationAlgorithm.java

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,118 @@
55
import java.util.Random;
66

77
public class GenerationAlgorithm {
8-
public final int POPULATION;
9-
public final int NUMBER_OF_GENERATIONS = 10;
8+
public final int POPULATION;
9+
public final int NUMBER_OF_GENERATIONS = 10;
1010
public final double MUTATION_CHANCE = 0.1;
11-
public final int ELITISM_NUMBER = 5;
12-
public final int NUMBER_OF_GENERATIONS_PER_INCREASE = 10;
11+
public final int MAX_MUTATION_COUNT = 5;
12+
public final int ELITISM_NUMBER = 2;
13+
public final int NUMBER_OF_GENERATIONS_PER_INCREASE = 10;
1314
static Random rand = new Random(0);
14-
int[] eliteIndexes = new int[ELITISM_NUMBER];
15+
ArrayList<Integer> eliteIndexes = new ArrayList<Integer>();
1516
int generationNumber = 0;
16-
boolean sessionStarted = false;
17+
int instructionSize = 20;
1718
Agent[] agents;
19+
1820
public GenerationAlgorithm(Agent[] agents){
1921
this.agents = agents;
2022
this.POPULATION = agents.length;
2123
createPopulation();
2224
}
2325
public void checkGeneration(){
24-
if(agents[agents.length-1].isFinished && !sessionStarted){
25-
getFitness();
26+
if(agents[agents.length-1].isFinished){
27+
ArrayList<Double> fitnesses = getFitness();
28+
ArrayList<Double> weights = getWeights(fitnesses);
29+
ArrayList<Integer[]> parentsList = getParents(weights);
30+
for (Integer[] integers : parentsList) {
31+
System.out.println(Arrays.toString(integers));
32+
}
33+
System.out.printf("Generation completed number: %d\n",generationNumber);
34+
crossOver(parentsList);
2635
for(Agent agent : agents){
2736
agent.reset();
2837
}
29-
sessionStarted = true;
3038
generationNumber++;
3139
}
3240
}
3341
public void createPopulation(){
3442
for (Agent agent : agents) {
35-
assignRandomInstructions(agent,20);
43+
assignRandomInstructions(agent,instructionSize);
3644
}
3745
}
3846
public void assignRandomInstructions(Agent agent,int size){
3947
for(int i = 0; i<size;i++){
4048
agent.instructions.add(rand.nextInt(5));
4149
}
4250
}
43-
public ArrayList<Integer> getFitness(){
44-
ArrayList<Integer> fitness = new ArrayList<>();
51+
public ArrayList<Double> getFitness(){
52+
ArrayList<Double> fitness = new ArrayList<>();
4553
for (Agent agent : agents) {
4654
fitness.add(agent.getPoints());
4755
}
48-
ArrayList<Integer> fitnessCopy = new ArrayList<>(fitness);
56+
ArrayList<Double> fitnessCopy = new ArrayList<>(fitness);
4957
Collections.sort(fitnessCopy, Collections.reverseOrder(null));
5058
for(int i = 0; i<ELITISM_NUMBER;i++){
51-
eliteIndexes[i] = fitness.indexOf(fitnessCopy.get(i));
59+
eliteIndexes.add(fitness.indexOf(fitnessCopy.get(i)));
5260
}
53-
System.out.println(fitness.toString());
54-
System.out.println(Arrays.toString(eliteIndexes));
5561
return fitness;
5662
}
57-
63+
public ArrayList<Double> getWeights(ArrayList<Double> fitness){
64+
double sum = 0;
65+
ArrayList<Double> weights = new ArrayList<Double>();
66+
for (double d : fitness) {
67+
sum += Math.exp(d);
68+
}
69+
for (int i = 0; i<fitness.size();i++) {
70+
double softMaxed = Math.exp(fitness.get(i))/sum;
71+
weights.add(softMaxed);
72+
}
73+
return weights;
74+
}
75+
public ArrayList<Integer[]> getParents(ArrayList<Double> weights){
76+
ArrayList<Integer[]> parentsList = new ArrayList<Integer[]>();
77+
double totalWeight = 0.0; //in case the sum of weights is not 1
78+
for (double i : weights) {
79+
totalWeight += i;
80+
}
81+
for(int i = 0; i<weights.size(); i++){
82+
Integer[] parents = new Integer[2];
83+
for(int j = 0; j<2;j++){
84+
int idx = 0;
85+
for (double r = Math.random() * totalWeight; idx < weights.size() - 1; ++idx) {
86+
r -= weights.get(idx);
87+
if (r <= 0.0) break;
88+
}
89+
parents[j] = idx;
90+
}
91+
parentsList.add(parents);
92+
}
93+
return parentsList;
94+
}
95+
public void crossOver(ArrayList<Integer[]> parents){
96+
ArrayList<ArrayList<Integer>> instructionsTemp = new ArrayList<ArrayList<Integer>>();
97+
for(int i = 0; i<POPULATION; i++){
98+
instructionsTemp.add(agents[i].instructions);
99+
}
100+
for (int i = 0; i<parents.size();i++) {
101+
Integer[] parentPair = parents.get(i);
102+
if(eliteIndexes.contains(parentPair[0])){
103+
continue;
104+
}
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+
}
111+
}
112+
if(Math.random()<MUTATION_CHANCE){
113+
int numberOfMutations = rand.nextInt(MAX_MUTATION_COUNT);
114+
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+
}
118+
}
119+
120+
}
121+
}
58122
}

pipline.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
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. +
5-
5. calculate probability values of the array using softmax
6-
6. using the above weights, get randomly chosen pairs from the array and append to a parents_list where each element contains 2 agents
7-
7. iterate over population and create childs of these parents using crossover.
8-
8. use random to determine whether to mutate or not
5+
5. calculate probability values of the array using softmax +
6+
6. using the above weights, get randomly chosen pairs from the array and append to a parents_list where each element contains 2 agents +
7+
7. iterate over population and create childs of these parents using crossover. +
8+
8. use random to determine whether to mutate or not+
99
9. directly append elite agents
10-
10. set population to new population and continue
10+
10. set population to new population and continue+

0 commit comments

Comments
 (0)