Skip to content

Commit 8071f46

Browse files
committed
added the base of a new saving system, incomplete.
1 parent 6bf786c commit 8071f46

File tree

6 files changed

+103
-13
lines changed

6 files changed

+103
-13
lines changed

JavaRacer/GameWindow.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.awt.Dimension;
44
import java.awt.Graphics;
55
import java.awt.Graphics2D;
6+
67
import javax.swing.JPanel;
78

89
public class GameWindow extends JPanel implements Runnable{
@@ -12,6 +13,7 @@ public class GameWindow extends JPanel implements Runnable{
1213
public final int WIDTH = tileSize*30;
1314
public final int HEIGHT = tileSize*20;
1415
private final int FPS = 60;
16+
private final boolean CAMERA = false;
1517
Thread gameThread;
1618
KeyHandler keyHandle = new KeyHandler();
1719
TileManager tileManager = new TileManager(this);
@@ -20,6 +22,7 @@ public class GameWindow extends JPanel implements Runnable{
2022
Camera camera;
2123
GenerationAlgorithm genAlg;
2224

25+
2326
public GameWindow(){
2427
Color backgroundColor = new Color(34, 139, 34);
2528
for(int i = 0; i<agents.length;i++){
@@ -69,8 +72,12 @@ public void update(){
6972
public void paintComponent(Graphics graphics){
7073
super.paintComponent(graphics);
7174
Graphics2D graphic = (Graphics2D)graphics;
72-
73-
tileManager.draw(graphic,camera);
75+
if(genAlg.eliteIndexes.size()>0&&!CAMERA){
76+
tileManager.draw(graphic, camera, agents[genAlg.eliteIndexes.get(0)]);
77+
}
78+
else{
79+
tileManager.draw(graphic,camera);
80+
}
7481
for (Agent agent : agents) {
7582
agent.draw(graphic);
7683
}

JavaRacer/GenerationAlgorithm.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,40 @@
55

66
public class GenerationAlgorithm {
77
public final int POPULATION;
8-
public final int NUMBER_OF_GENERATIONS = 200;
9-
public final double MUTATION_CHANCE = 0.2;
10-
public final int MAX_MUTATION_COUNT = 5;
8+
public final int NUMBER_OF_GENERATIONS = 500;
9+
public final double MUTATION_CHANCE = 0.3;
10+
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;
1415
static Random rand = new Random();
16+
SaveLoader saveLoader = new SaveLoader();
1517
ArrayList<Integer> eliteIndexes = new ArrayList<Integer>();
1618
int generationNumber = 0;
1719
int instructionSize = 20;
20+
double improvement = 0;
1821
double bestFit = 0;
1922
Agent[] agents;
2023

2124
public GenerationAlgorithm(Agent[] agents){
2225
this.agents = agents;
2326
this.POPULATION = agents.length;
27+
if(LOAD_FILE){
28+
saveLoader.loadSave(this);
29+
}
30+
else{
31+
createPopulation();
32+
}
2433
createPopulation();
34+
2535
}
2636
public void checkGeneration(){
2737
if(agents[agents.length-1].isFinished){
2838
ArrayList<Double> fitnesses = getFitness();
2939
ArrayList<Double> weights = getWeights(fitnesses);
3040
ArrayList<Integer[]> parentsList = getParents(weights);
41+
this.improvement = fitnesses.get(eliteIndexes.get(0)) - this.bestFit;
3142
this.bestFit = fitnesses.get(eliteIndexes.get(0));
3243
System.out.println("Best fit: " + fitnesses.get(eliteIndexes.get(0)) + " "+ fitnesses.get(eliteIndexes.get(1)));
3344
System.out.printf("Generation completed number: %d\n",generationNumber);
@@ -41,6 +52,7 @@ public void checkGeneration(){
4152
assignRandomInstructions(agent, INSTRUCTION_SIZE_INCREASE);
4253
}
4354
instructionSize += INSTRUCTION_SIZE_INCREASE;
55+
saveLoader.save(this);
4456
}
4557
}
4658
}
@@ -101,6 +113,11 @@ public ArrayList<Integer[]> getParents(ArrayList<Double> weights){
101113
}
102114
public void crossOver(ArrayList<Integer[]> parents){
103115
ArrayList<ArrayList<Integer>> instructionsTemp = new ArrayList<ArrayList<Integer>>();
116+
double mutationAgression = 0;
117+
if(improvement<0.5){
118+
mutationAgression = 0.2;
119+
System.out.println("Mutation Agression Activated");
120+
}
104121
for(int i = 0; i<POPULATION; i++){
105122
instructionsTemp.add(new ArrayList<Integer>(agents[i].instructions));
106123
}
@@ -118,19 +135,18 @@ public void crossOver(ArrayList<Integer[]> parents){
118135
}
119136
agents[i].instructions.clear();
120137
agents[i].instructions.addAll(instruction1);
121-
122-
if(Math.random()<MUTATION_CHANCE){
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);
138+
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;
125141
for(int m = 0; m<numberOfMutations;m++){
126142
int randomInstructionIndex = rand.nextInt(agents[i].instructions.size());
127143
agents[i].instructions.set(randomInstructionIndex,rand.nextInt(5)); //randomize instructions
128144
}
129145
}
130-
else{ //only randomize the last 10 (%60)
131-
int numberOfMutations = rand.nextInt(MAX_MUTATION_COUNT);
146+
else{ //only randomize the last 20 (%60)
147+
int numberOfMutations = rand.nextInt(MAX_MUTATION_COUNT) + (int)mutationAgression*10;
132148
for(int m = 0; m<numberOfMutations;m++){
133-
int randomInstructionIndex = rand.nextInt(agents[i].instructions.size()-10,agents[i].instructions.size());
149+
int randomInstructionIndex = rand.nextInt(agents[i].instructions.size()-20,agents[i].instructions.size());
134150
agents[i].instructions.set(randomInstructionIndex,rand.nextInt(5)); //randomize instructions
135151
}
136152
}

JavaRacer/JavaRacer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ public static void main(String[] args) {
88
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
99
window.setTitle("Java Racer");
1010
window.setResizable(false);
11-
11+
1212
GameWindow gameWindow = new GameWindow();
13+
1314
window.add(gameWindow);
1415
window.pack();
1516
window.setVisible(true);

JavaRacer/SaveLoader.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package JavaRacer;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.io.IOException;
6+
import java.io.PrintStream;
7+
import java.util.ArrayList;
8+
import java.util.Scanner;
9+
10+
public class SaveLoader {
11+
public void loadSave(GenerationAlgorithm genAlg){
12+
try {
13+
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());
20+
scanner.close();
21+
} catch (FileNotFoundException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
public void save(GenerationAlgorithm genAlg){
26+
try {
27+
PrintStream saveFile = new PrintStream(new File("source/save.txt"));
28+
saveFile.println(genAlg.generationNumber);
29+
saveFile.println(genAlg.instructionSize);
30+
saveFile.print(genAlg.agents[genAlg.eliteIndexes.get(0)].instructions.toString());
31+
} catch (IOException e) {
32+
e.printStackTrace();
33+
}
34+
}
35+
}

JavaRacer/TileManager.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,34 @@ 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+
}
95+
public void draw(Graphics2D graphics2d,Camera camera, Agent agent){ //Draws the map around the agent
96+
camera.setWorldX(agent.agentX);
97+
camera.setWorldY(agent.agentY);
98+
int cameraX = camera.getWorldX();
99+
int cameraY = camera.getWorldY();
100+
int screenMidX = camera.getScreenMidX();
101+
int screenMidY = camera.getScreenMidY();
94102

103+
for(int i = 0; i<row;i++){
104+
int worldY = i*tileSize;
105+
int screenY = worldY - cameraY + screenMidY;
106+
for(int j = 0; j<collumn; j++){
107+
int worldX = j*tileSize;
108+
int screenX = worldX - cameraX + screenMidX;
109+
if(worldX>cameraX-screenMidX -1*tileSize&&
110+
worldX<cameraX+screenMidX+3*tileSize&&
111+
worldY>cameraY-screenMidY-1*tileSize&&
112+
worldY<cameraY+screenMidY+3*tileSize){
113+
graphics2d.drawImage(tiles[map[i][j]].image,screenX,screenY,tileSize,tileSize,null);
114+
}
115+
}
116+
}
117+
graphics2d.setFont(new Font("TimesRoman", Font.BOLD, 20));
118+
graphics2d.setColor(Color.BLACK);
119+
graphics2d.drawString("Best fit: " + gameWindow.genAlg.bestFit, 10, 20);
120+
graphics2d.drawString("Gen. No: " + gameWindow.genAlg.generationNumber, 10, 40);
121+
graphics2d.drawString("Population Size: " + gameWindow.genAlg.POPULATION, 10, 60);
122+
graphics2d.drawString("Instruction Size: " + gameWindow.genAlg.instructionSize, 10, 80);
95123
}
96124
}

source/save.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
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]

0 commit comments

Comments
 (0)