Skip to content

Commit d97205a

Browse files
committed
completed generation alg.
1 parent e328774 commit d97205a

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

JavaRacer/Agent.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Agent {
1717
public double frictionCoefficient; //road friction: 1 in asphalt, 0.1 in grass checked by CollisionControl class
1818
public int laps = 0;
1919
public double points = 0;
20-
public boolean onFinishLine, offFinishLine, isCollided = false; //flags for game logic
20+
public boolean onFinishLine, offFinishLine= false; //flags for game logic
2121
public ArrayList<Integer> instructions;
2222
int nextActionTimer,instructionIndex;
2323
public boolean isFinished = false;
@@ -123,14 +123,9 @@ public void checkLaps(){
123123
}
124124
public void calculatePoints(){
125125
if(frictionCoefficient<1){
126-
if(!isCollided){
127-
points-=1000;
128-
isCollided = true;
129-
}
130126
points -= (velocity.netVelocity()/frictionCoefficient)/10;
131127
}
132128
else{
133-
isCollided = false;
134129
points += velocity.netVelocity()/10;
135130
}
136131
if(points<0){
@@ -202,7 +197,4 @@ public int getLaps(){
202197
public Velocity getVelocity(){
203198
return this.velocity;
204199
}
205-
public boolean getIsCollided(){
206-
return this.isCollided;
207-
}
208200
}

JavaRacer/Camera.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public class Camera {
55
int screenMidX, screenMidY;
66
KeyHandler keyHandle;
77
GameWindow gameWindow;
8+
int speed = 10;
89
public Camera(GameWindow gw, KeyHandler kh, int worldX, int worldY){
910
this.gameWindow = gw;
1011
this.keyHandle = kh;
@@ -15,16 +16,16 @@ public Camera(GameWindow gw, KeyHandler kh, int worldX, int worldY){
1516
}
1617
public void update(){
1718
if (keyHandle.aPressed){
18-
this.worldX -=8;
19+
this.worldX -=speed;
1920
}
2021
else if(keyHandle.dPressed){
21-
this.worldX += 8;
22+
this.worldX += speed;
2223
}
2324
else if(keyHandle.wPressed){
24-
this.worldY -=8;
25+
this.worldY -=speed;
2526
}
2627
else if(keyHandle.sPressed){
27-
this.worldY += 8;
28+
this.worldY += speed;
2829
}
2930
}
3031
public int getWorldX(){

JavaRacer/GameWindow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class GameWindow extends JPanel implements Runnable{
1515
Thread gameThread;
1616
KeyHandler keyHandle = new KeyHandler();
1717
TileManager tileManager = new TileManager(this);
18-
Agent[] agents = new Agent[10];
18+
Agent[] agents = new Agent[100];
1919
CollisionControl collisionControl = new CollisionControl(this);
2020
Camera camera;
2121
GenerationAlgorithm genAlg;

JavaRacer/GenerationAlgorithm.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
public class GenerationAlgorithm {
77
public final int POPULATION;
8-
public final int NUMBER_OF_GENERATIONS = 10;
9-
public final double MUTATION_CHANCE = 0.1;
8+
public final int NUMBER_OF_GENERATIONS = 200;
9+
public final double MUTATION_CHANCE = 0.2;
1010
public final int MAX_MUTATION_COUNT = 5;
1111
public final int ELITISM_NUMBER = 2;
1212
public final int NUMBER_OF_GENERATIONS_PER_INCREASE = 10;
13+
public final int INSTRUCTION_SIZE_INCREASE = 10;
1314
static Random rand = new Random();
1415
ArrayList<Integer> eliteIndexes = new ArrayList<Integer>();
1516
int generationNumber = 0;
@@ -35,6 +36,12 @@ public void checkGeneration(){
3536
agent.reset();
3637
}
3738
generationNumber++;
39+
if(generationNumber>=instructionSize && generationNumber%NUMBER_OF_GENERATIONS_PER_INCREASE == 0){
40+
for(Agent agent : agents){
41+
assignRandomInstructions(agent, INSTRUCTION_SIZE_INCREASE);
42+
}
43+
instructionSize += INSTRUCTION_SIZE_INCREASE;
44+
}
3845
}
3946
}
4047
public void createPopulation(){
@@ -111,11 +118,21 @@ public void crossOver(ArrayList<Integer[]> parents){
111118
}
112119
agents[i].instructions.clear();
113120
agents[i].instructions.addAll(instruction1);
121+
114122
if(Math.random()<MUTATION_CHANCE){
115-
int numberOfMutations = rand.nextInt(MAX_MUTATION_COUNT);
116-
for(int m = 0; m<numberOfMutations;m++){
117-
int randomInstructionIndex = rand.nextInt(agents[i].instructions.size());
118-
agents[i].instructions.set(randomInstructionIndex,rand.nextInt(5)); //randomize instructions
123+
if(Math.random()<0.4){ //randomize anywhere in the instructions %40 chance to mutate anywhere, %60 chance to mutate last 10
124+
int numberOfMutations = rand.nextInt(MAX_MUTATION_COUNT);
125+
for(int m = 0; m<numberOfMutations;m++){
126+
int randomInstructionIndex = rand.nextInt(agents[i].instructions.size());
127+
agents[i].instructions.set(randomInstructionIndex,rand.nextInt(5)); //randomize instructions
128+
}
129+
}
130+
else{ //only randomize the last 10 (%60)
131+
int numberOfMutations = rand.nextInt(MAX_MUTATION_COUNT);
132+
for(int m = 0; m<numberOfMutations;m++){
133+
int randomInstructionIndex = rand.nextInt(agents[i].instructions.size()-10,agents[i].instructions.size());
134+
agents[i].instructions.set(randomInstructionIndex,rand.nextInt(5)); //randomize instructions
135+
}
119136
}
120137
}
121138
}

JavaRacer/TileManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void loadTileImages(){
6666
e.printStackTrace();
6767
}
6868
}
69-
public void draw(Graphics2D graphics2d, Camera camera){ //Draws the map around the agent
69+
public void draw(Graphics2D graphics2d, Camera camera){ //Draws the map around the camera
7070
int cameraX = camera.getWorldX();
7171
int cameraY = camera.getWorldY();
7272
int screenMidX = camera.getScreenMidX();
@@ -89,6 +89,8 @@ public void draw(Graphics2D graphics2d, Camera camera){ //Draws the map around t
8989
graphics2d.setColor(Color.BLACK);
9090
graphics2d.drawString("Best fit: " + gameWindow.genAlg.bestFit, 10, 20);
9191
graphics2d.drawString("Gen. No: " + gameWindow.genAlg.generationNumber, 10, 40);
92+
graphics2d.drawString("Population Size: " + gameWindow.genAlg.POPULATION, 10, 60);
93+
graphics2d.drawString("Instruction Size: " + gameWindow.genAlg.instructionSize, 10, 80);
9294

9395
}
9496
}

0 commit comments

Comments
 (0)