Skip to content

Commit c2fb950

Browse files
committed
added generation algorithm basics, added pipeline
1 parent 5bf5ac9 commit c2fb950

File tree

4 files changed

+106
-14
lines changed

4 files changed

+106
-14
lines changed

JavaRacer/Agent.java

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,23 @@
66
import java.awt.image.BufferedImage;
77
import java.io.File;
88
import java.io.IOException;
9-
import java.util.Random;
9+
import java.util.ArrayList;
10+
1011
import javax.imageio.ImageIO;
1112

1213
public class Agent {
13-
static Random rand = new Random(0);
14-
1514
int agentX,agentY; //x,y coordinates in the world and rotation of the car in degrees. 0 representing right as in cartesian system(stored in velocity)
1615
int screenX, screenY; //the x,y coordinates of the car on screen
1716
Rectangle solidArea = new Rectangle(); // aka hitbox of the car
1817
public double frictionCoefficient; //road friction: 1 in asphalt, 0.1 in grass checked by CollisionControl class
1918
public int points, laps = 0;
2019
public boolean onFinishLine, offFinishLine, isCollided = false; //flags for game logic
21-
public int[] instructions = new int[100];
20+
public ArrayList<Integer> instructions;
2221
int nextActionTimer,instructionIndex;
22+
public boolean isFinished = false;
2323
public BufferedImage playerModel;
2424
GameWindow gameWindow;
2525
Velocity velocity;
26-
int counter;
2726

2827
public Agent(GameWindow gw){
2928
this.gameWindow = gw;
@@ -32,12 +31,8 @@ public Agent(GameWindow gw){
3231
public void setDefault(){
3332
this.agentX=MapLoader.spawnX*gameWindow.tileSize;
3433
this.agentY=MapLoader.spawnY*gameWindow.tileSize;
35-
this.velocity = new Velocity();
36-
37-
for(int i = 0; i<50; i++){
38-
instructions[i] = rand.nextInt(5);
39-
}
40-
34+
this.velocity = new Velocity();
35+
this.instructions = new ArrayList<Integer>();
4136
switch (MapLoader.spawnDirection) {
4237
case 0:
4338
velocity.angle = 0;
@@ -58,7 +53,6 @@ public void setDefault(){
5853
this.solidArea.y = 5;
5954
this.solidArea.width = 25;
6055
this.solidArea.height = 25;
61-
6256
try {
6357
File tmp = new File("source/car.png");
6458
playerModel = ImageIO.read(tmp);
@@ -70,10 +64,10 @@ public void update(){
7064
frictionCoefficient = gameWindow.collisionControl.checkCollision(this);
7165
checkLaps();
7266
calculatePoints();
73-
if(nextActionTimer%5 == 0 && instructionIndex<instructions.length){
67+
if(nextActionTimer%5 == 0 && instructionIndex<instructions.size()){ //take action every 5 frames
7468
int rotationAngle;
7569
double netVelocity = velocity.netVelocity();
76-
switch (instructions[instructionIndex]) {
70+
switch (instructions.get(instructionIndex)) {
7771
case 0: //accelerate
7872
velocity.accelerate(0.4*frictionCoefficient);
7973
break;
@@ -107,6 +101,9 @@ public void update(){
107101
while(frictionCoefficient<1&&velocity.netVelocity()>10*frictionCoefficient){
108102
velocity.accelerate(-0.5);
109103
}
104+
if(instructionIndex == instructions.size()-1){
105+
this.isFinished = true;
106+
}
110107
this.agentX += velocity.X;
111108
this.agentY -= velocity.Y;
112109
nextActionTimer++;
@@ -159,6 +156,30 @@ public void draw(Graphics2D graphics){
159156
graphics.setTransform(oldTransform); // restore old graphics
160157
}
161158
}
159+
public void reset(){
160+
this.isFinished = false;
161+
this.agentX=MapLoader.spawnX*gameWindow.tileSize;
162+
this.agentY=MapLoader.spawnY*gameWindow.tileSize;
163+
velocity.X = 0;
164+
velocity.Y = 0;
165+
switch (MapLoader.spawnDirection) {
166+
case 0:
167+
velocity.angle = 0;
168+
break;
169+
case 1:
170+
velocity.angle = 90;
171+
break;
172+
case 2:
173+
velocity.angle = 180;
174+
break;
175+
case 3:
176+
velocity.angle = 270;
177+
break;
178+
default:
179+
break;
180+
}
181+
182+
}
162183
public String toString(){
163184
return this.agentX + " " + this.agentY;
164185
}

JavaRacer/GameWindow.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ public class GameWindow extends JPanel implements Runnable{
1818
Agent[] agents = new Agent[10];
1919
CollisionControl collisionControl = new CollisionControl(this);
2020
Camera camera;
21+
GenerationAlgorithm genAlg;
2122

2223
public GameWindow(){
2324
Color backgroundColor = new Color(34, 139, 34);
2425
for(int i = 0; i<agents.length;i++){
2526
agents[i] = new Agent(this);
2627
}
28+
this.genAlg = new GenerationAlgorithm(agents);
2729
this.camera = new Camera(this,keyHandle, agents[0].agentX, agents[0].agentY);
2830
this.setBackground(backgroundColor);
2931
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
@@ -59,6 +61,7 @@ public void update(){
5961
for(int i = 0; i<agents.length;i++){
6062
agents[i].update();
6163
}
64+
genAlg.checkGeneration();
6265
camera.update();
6366
}
6467
public void paintComponent(Graphics graphics){

JavaRacer/GenerationAlgorithm.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package JavaRacer;
2+
import java.util.ArrayList;
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.Random;
6+
7+
public class GenerationAlgorithm {
8+
public final int POPULATION;
9+
public final int NUMBER_OF_GENERATIONS = 10;
10+
public final double MUTATION_CHANCE = 0.1;
11+
public final int ELITISM_NUMBER = 5;
12+
public final int NUMBER_OF_GENERATIONS_PER_INCREASE = 10;
13+
static Random rand = new Random(0);
14+
int[] eliteIndexes = new int[ELITISM_NUMBER];
15+
int generationNumber = 0;
16+
boolean sessionStarted = false;
17+
Agent[] agents;
18+
public GenerationAlgorithm(Agent[] agents){
19+
this.agents = agents;
20+
this.POPULATION = agents.length;
21+
createPopulation();
22+
}
23+
public void checkGeneration(){
24+
if(agents[agents.length-1].isFinished && !sessionStarted){
25+
getFitness();
26+
for(Agent agent : agents){
27+
agent.reset();
28+
}
29+
sessionStarted = true;
30+
generationNumber++;
31+
}
32+
}
33+
public void createPopulation(){
34+
for (Agent agent : agents) {
35+
assignRandomInstructions(agent,20);
36+
}
37+
}
38+
public void assignRandomInstructions(Agent agent,int size){
39+
for(int i = 0; i<size;i++){
40+
agent.instructions.add(rand.nextInt(5));
41+
}
42+
}
43+
public ArrayList<Integer> getFitness(){
44+
ArrayList<Integer> fitness = new ArrayList<>();
45+
for (Agent agent : agents) {
46+
fitness.add(agent.getPoints());
47+
}
48+
ArrayList<Integer> fitnessCopy = new ArrayList<>(fitness);
49+
Collections.sort(fitnessCopy, Collections.reverseOrder(null));
50+
for(int i = 0; i<ELITISM_NUMBER;i++){
51+
eliteIndexes[i] = fitness.indexOf(fitnessCopy.get(i));
52+
}
53+
System.out.println(fitness.toString());
54+
System.out.println(Arrays.toString(eliteIndexes));
55+
return fitness;
56+
}
57+
58+
}

pipline.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1. create a population with rand properties +
2+
2. iterate over number of generations
3+
3. calculate fitness of each agent (points) +
4+
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
9+
9. directly append elite agents
10+
10. set population to new population and continue

0 commit comments

Comments
 (0)