Skip to content

Commit 91c92cd

Browse files
committed
bugfix
1 parent 8071f46 commit 91c92cd

File tree

5 files changed

+71
-34
lines changed

5 files changed

+71
-34
lines changed

JavaRacer/GameWindow.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ public void update(){
7272
public void paintComponent(Graphics graphics){
7373
super.paintComponent(graphics);
7474
Graphics2D graphic = (Graphics2D)graphics;
75-
if(genAlg.eliteIndexes.size()>0&&!CAMERA){
75+
if(CAMERA){
76+
tileManager.draw(graphic,camera);
77+
}
78+
else if(genAlg.eliteIndexes.size()>0){
7679
tileManager.draw(graphic, camera, agents[genAlg.eliteIndexes.get(0)]);
7780
}
7881
else{
79-
tileManager.draw(graphic,camera);
82+
tileManager.draw(graphic, camera, agents[0]);
8083
}
8184
for (Agent agent : agents) {
8285
agent.draw(graphic);

JavaRacer/GenerationAlgorithm.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,33 @@
55

66
public class GenerationAlgorithm {
77
public final int POPULATION;
8-
public final int NUMBER_OF_GENERATIONS = 500;
9-
public final double MUTATION_CHANCE = 0.3;
8+
public final int NUMBER_OF_GENERATIONS = 1000;
9+
public final double MUTATION_CHANCE = 0.4;
1010
public final int MAX_MUTATION_COUNT = 8;
1111
public final int ELITISM_NUMBER = 2;
1212
public final int NUMBER_OF_GENERATIONS_PER_INCREASE = 10;
1313
public final int INSTRUCTION_SIZE_INCREASE = 10;
14-
public final boolean LOAD_FILE = false;
14+
public final boolean LOAD_FILE = true;
1515
static Random rand = new Random();
1616
SaveLoader saveLoader = new SaveLoader();
1717
ArrayList<Integer> eliteIndexes = new ArrayList<Integer>();
1818
int generationNumber = 0;
1919
int instructionSize = 20;
2020
double improvement = 0;
21+
double lastIncreasePoints = 0;
22+
int failedIncreaseCounter = 0;
2123
double bestFit = 0;
2224
Agent[] agents;
2325

2426
public GenerationAlgorithm(Agent[] agents){
2527
this.agents = agents;
2628
this.POPULATION = agents.length;
27-
if(LOAD_FILE){
29+
if(LOAD_FILE&&saveLoader.hasSaveFile()){
2830
saveLoader.loadSave(this);
2931
}
3032
else{
3133
createPopulation();
3234
}
33-
createPopulation();
34-
3535
}
3636
public void checkGeneration(){
3737
if(agents[agents.length-1].isFinished){
@@ -47,12 +47,17 @@ public void checkGeneration(){
4747
agent.reset();
4848
}
4949
generationNumber++;
50-
if(generationNumber>=instructionSize && generationNumber%NUMBER_OF_GENERATIONS_PER_INCREASE == 0){
50+
if(generationNumber>=instructionSize && generationNumber%NUMBER_OF_GENERATIONS_PER_INCREASE == 0 && fitnesses.get(eliteIndexes.get(0))-lastIncreasePoints>10){
5151
for(Agent agent : agents){
5252
assignRandomInstructions(agent, INSTRUCTION_SIZE_INCREASE);
5353
}
5454
instructionSize += INSTRUCTION_SIZE_INCREASE;
5555
saveLoader.save(this);
56+
lastIncreasePoints = fitnesses.get(eliteIndexes.get(0));
57+
failedIncreaseCounter = 0;
58+
}
59+
else{
60+
failedIncreaseCounter += 1;
5661
}
5762
}
5863
}
@@ -115,9 +120,9 @@ public void crossOver(ArrayList<Integer[]> parents){
115120
ArrayList<ArrayList<Integer>> instructionsTemp = new ArrayList<ArrayList<Integer>>();
116121
double mutationAgression = 0;
117122
if(improvement<0.5){
118-
mutationAgression = 0.2;
119-
System.out.println("Mutation Agression Activated");
120-
}
123+
mutationAgression = 0.2;
124+
System.out.println("Mutation Agression Activated");
125+
}
121126
for(int i = 0; i<POPULATION; i++){
122127
instructionsTemp.add(new ArrayList<Integer>(agents[i].instructions));
123128
}
@@ -136,19 +141,17 @@ public void crossOver(ArrayList<Integer[]> parents){
136141
agents[i].instructions.clear();
137142
agents[i].instructions.addAll(instruction1);
138143
if(Math.random()<MUTATION_CHANCE+mutationAgression){
139-
if(Math.random()<0.4){ //randomize anywhere in the instructions %40 chance to mutate anywhere, %60 chance to mutate last 20
140-
int numberOfMutations = rand.nextInt(MAX_MUTATION_COUNT) + (int)mutationAgression*10;
141-
for(int m = 0; m<numberOfMutations;m++){
142-
int randomInstructionIndex = rand.nextInt(agents[i].instructions.size());
143-
agents[i].instructions.set(randomInstructionIndex,rand.nextInt(5)); //randomize instructions
144-
}
144+
int numberOfMutations = rand.nextInt(MAX_MUTATION_COUNT) + (int)mutationAgression*10;
145+
int startOfMutations;
146+
if(failedIncreaseCounter>20){
147+
startOfMutations = 40;
148+
}
149+
else{
150+
startOfMutations = 20;
145151
}
146-
else{ //only randomize the last 20 (%60)
147-
int numberOfMutations = rand.nextInt(MAX_MUTATION_COUNT) + (int)mutationAgression*10;
148-
for(int m = 0; m<numberOfMutations;m++){
149-
int randomInstructionIndex = rand.nextInt(agents[i].instructions.size()-20,agents[i].instructions.size());
150-
agents[i].instructions.set(randomInstructionIndex,rand.nextInt(5)); //randomize instructions
151-
}
152+
for(int m = 0; m<numberOfMutations;m++){
153+
int randomInstructionIndex = rand.nextInt(agents[i].instructions.size()-startOfMutations,agents[i].instructions.size());
154+
agents[i].instructions.set(randomInstructionIndex,rand.nextInt(5)); //randomize instructions
152155
}
153156
}
154157
}

JavaRacer/SaveLoader.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,31 @@
44
import java.io.FileNotFoundException;
55
import java.io.IOException;
66
import java.io.PrintStream;
7-
import java.util.ArrayList;
7+
import java.util.Locale;
88
import java.util.Scanner;
99

1010
public class SaveLoader {
1111
public void loadSave(GenerationAlgorithm genAlg){
1212
try {
1313
File saveFile = new File("source/save.txt");
14-
Scanner scanner = new Scanner(saveFile);
15-
genAlg.generationNumber = scanner.nextInt();
16-
genAlg.instructionSize = scanner.nextInt();
17-
ArrayList<Integer> tempInstructions = new ArrayList<Integer>();
18-
scanner.nextLine().trim().split(null);
19-
System.out.println(tempInstructions.toString());
14+
Scanner scanner = new Scanner(saveFile).useLocale(Locale.US);
15+
if(scanner.hasNext()){
16+
genAlg.generationNumber = scanner.nextInt();
17+
genAlg.instructionSize = scanner.nextInt();
18+
genAlg.lastIncreasePoints = scanner.nextDouble();
19+
scanner.nextLine();
20+
String[] tempInstructions = scanner.nextLine().replaceAll("[\\[\\]]", "").split(", ");
21+
System.out.println(tempInstructions.length);
22+
for (String string : tempInstructions) {
23+
int instruct = Integer.parseInt(string);
24+
for(Agent agent : genAlg.agents){
25+
agent.instructions.add(instruct);
26+
}
27+
}
28+
}
29+
else{
30+
31+
}
2032
scanner.close();
2133
} catch (FileNotFoundException e) {
2234
e.printStackTrace();
@@ -27,9 +39,25 @@ public void save(GenerationAlgorithm genAlg){
2739
PrintStream saveFile = new PrintStream(new File("source/save.txt"));
2840
saveFile.println(genAlg.generationNumber);
2941
saveFile.println(genAlg.instructionSize);
42+
saveFile.println(genAlg.lastIncreasePoints);
3043
saveFile.print(genAlg.agents[genAlg.eliteIndexes.get(0)].instructions.toString());
3144
} catch (IOException e) {
3245
e.printStackTrace();
3346
}
3447
}
48+
public boolean hasSaveFile(){
49+
try {
50+
File saveFile = new File("source/save.txt");
51+
Scanner scanner = new Scanner(saveFile);
52+
if(scanner.hasNext()){
53+
scanner.close();
54+
return true;
55+
}
56+
scanner.close();
57+
return false;
58+
} catch (FileNotFoundException e) {
59+
e.printStackTrace();
60+
}
61+
return false;
62+
}
3563
}

JavaRacer/TileManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public void draw(Graphics2D graphics2d, Camera camera){ //Draws the map around t
9191
graphics2d.drawString("Gen. No: " + gameWindow.genAlg.generationNumber, 10, 40);
9292
graphics2d.drawString("Population Size: " + gameWindow.genAlg.POPULATION, 10, 60);
9393
graphics2d.drawString("Instruction Size: " + gameWindow.genAlg.instructionSize, 10, 80);
94+
graphics2d.drawString("Last Increase Points: " + gameWindow.genAlg.lastIncreasePoints, 10, 100);
9495
}
9596
public void draw(Graphics2D graphics2d,Camera camera, Agent agent){ //Draws the map around the agent
9697
camera.setWorldX(agent.agentX);
@@ -120,5 +121,6 @@ public void draw(Graphics2D graphics2d,Camera camera, Agent agent){ //Draws the
120121
graphics2d.drawString("Gen. No: " + gameWindow.genAlg.generationNumber, 10, 40);
121122
graphics2d.drawString("Population Size: " + gameWindow.genAlg.POPULATION, 10, 60);
122123
graphics2d.drawString("Instruction Size: " + gameWindow.genAlg.instructionSize, 10, 80);
124+
graphics2d.drawString("Last Increase Points: " + gameWindow.genAlg.lastIncreasePoints, 10, 100);
123125
}
124126
}

source/save.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
20
2-
30
3-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 4, 4, 3, 0, 1, 4, 4, 4, 2, 0]
1+
690
2+
400
3+
993.2700000000002
4+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 4, 3, 1, 2, 2, 4, 0, 2, 4, 4, 2, 0, 1, 2, 2, 2, 2, 0, 2, 0, 0, 2, 2, 0, 0, 0, 0, 2, 0, 0, 0, 2, 2, 2, 0, 2, 0, 2, 0, 4, 4, 3, 4, 0, 3, 2, 2, 4, 0, 3, 4, 3, 4, 2, 0, 4, 3, 0, 4, 2, 4, 4, 0, 2, 4, 2, 0, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 1, 1, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 2, 0, 0, 0, 2, 0, 0, 4, 2, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 0, 3, 0, 3, 4, 3, 3, 3, 3, 3, 1, 3, 1, 3, 3, 3, 2, 1, 1, 3, 2, 1, 3, 3, 0, 3, 3, 1, 3, 0, 2, 4, 0, 2, 3, 4, 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