-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
270 lines (216 loc) · 10.9 KB
/
Copy pathMain.java
File metadata and controls
270 lines (216 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
/**
*
* @author Kevin
*/
public class Main {
public static int[] nShape = {6, 64, 64, 4}; //Shape of neural network per-layer
public static final int GENERATIONS = 100000; //Amount of generations to simulate
public static final int TURNS = 35; //Maximum turns per episode
public static final int SAMPLES = 200; //Amount of agents to simulate per generation
public static final float MUTATION = 0.03f; //Rate of mutation of the agents
public static final int MAP_SIZE = 10; //Size of map
public static final String INPUT = "rnn-6-64-64-4-b-hard.txt"; //Optional input for weight initialization, leave as "" for random initial values
public static final String MAPNAME = "map_hard"; //Name of .png map to be loaded
public static final boolean TRAIN = true; //Train new generations or just display result
public static boolean[][] map;
public static void main(String[] args) {
NeuralNetwork t = new NeuralNetwork(nShape);
System.out.println("Network shape:");
System.out.println(t.toString());
System.out.println("CONTROLS\n"
+ "\tP - Display current best generation\n"
+ "\tX - End training early and go to viewing mode\n"
+ "\tN - Display Neural Network evolving as it trains\n"
+ "\tSPACE - Save learnt weights to text file 'out.txt'\n\n");
//StdDraw.setCanvasSize(800, 800); //Optional increase canvas size
StdDraw.enableDoubleBuffering();
StdDraw.setXscale(0, 1);
StdDraw.setYscale(0, 1);
StdDraw.setPenRadius(0.003);
loadMap("maps/" + MAPNAME);
Agent2[] agents = new Agent2[SAMPLES];
for (int i = 0; i < SAMPLES; i++)
agents[i] = new Agent2(nShape);
if (!INPUT.isEmpty()) for (int i = 0; i < agents.length/4; i++)
agents[i] = new Agent2(nShape, "weights/" + INPUT);
if (TRAIN) System.out.println("[TRAINING]");
for (int generation = 0; generation < GENERATIONS && TRAIN; generation++) {
//Initialize agents for the generation
for (Agent2 agent: agents)
agent.score = 0;
for (int j = 0; j < 4; j++) {
int[] map_stuff = new int[4]; //Food x,y; Player x,y;
for (int i = 0; i < 4; i++)
map_stuff[i] = (int)(Math.random()*MAP_SIZE);
//make sure not invalid placement of target
while (map[map_stuff[1]][map_stuff[0]]) map_stuff[(int)(Math.random()*2)] = (int)(Math.random()*MAP_SIZE);
while (map[map_stuff[3]][map_stuff[2]]) map_stuff[(int)(Math.random()*2)+2] = (int)(Math.random()*MAP_SIZE);
for (Agent2 agent: agents) {
agent.x = map_stuff[2];
agent.y = map_stuff[3];
agent.lx = 0;
agent.ly = 0;
agent.moves = 0;
agent.reset();
}
for (int i = 0; i < TURNS; i++) for (Agent2 agent: agents)
agent.move(map_stuff[0], map_stuff[1]);
for(Agent2 agent: agents)
agent.score += agent.getScore(map_stuff[0], map_stuff[1]);
}
//Sort best score
boolean sorted = false;
while (!sorted) {
sorted = true;
for (int i = 0; i < agents.length-1; i++) {
if (agents[i].score < agents[i+1].score) {
sorted = false;
Agent2 temp = agents[i];
agents[i] = agents[i+1];
agents[i+1] = temp;
}
}
}
for (int i = 0; i < SAMPLES/4; i++) {
agents[i+SAMPLES/4*3] = agents[i].duplicate();
agents[i+SAMPLES/4*3].mutate(MUTATION);
agents[i+SAMPLES/4*2] = agents[i].duplicate();
agents[i+SAMPLES/4*2].mutate(MUTATION*4);
}
if ((float)(generation)/5 == generation/5)
System.out.println("[Generation " + generation + "]: score = " + agents[0].score);
if (StdDraw.isKeyPressed(KeyEvent.VK_N)) {
StdDraw.clear();
agents[0].drawNetwork(0.1f, 0.1f, 0.8f);
StdDraw.show();
}
while (StdDraw.isKeyPressed(KeyEvent.VK_P)) {
int[] map_stuff = new int[4]; //Food x,y; Player x,y;
for (int i = 0; i < 4; i++)
map_stuff[i] = (int)(Math.random()*MAP_SIZE);
while (map[map_stuff[1]][map_stuff[0]]) map_stuff[(int)(Math.random()*2)] = (int)(Math.random()*MAP_SIZE);
while (map[map_stuff[3]][map_stuff[2]]) map_stuff[(int)(Math.random()*2)+2] = (int)(Math.random()*MAP_SIZE);
agents[0].x = map_stuff[2];
agents[0].y = map_stuff[3];
agents[0].lx = 0;
agents[0].ly = 0;
agents[0].reset();
for (int i = 0; i < TURNS; i++) {
StdDraw.clear();
drawMap();
StdDraw.setPenColor(Color.BLACK);
agents[0].move(map_stuff[0], map_stuff[1]);
agents[0].draw();
StdDraw.setPenColor(Color.RED);
StdDraw.filledSquare((map_stuff[0]+0.5)/(double)MAP_SIZE, (map_stuff[1]+0.5)/(double)MAP_SIZE, 0.4/(double)MAP_SIZE);
if (StdDraw.isKeyPressed(KeyEvent.VK_SPACE))
agents[0].save("out.txt");
StdDraw.show();
StdDraw.pause(20);
}
}
if (StdDraw.isKeyPressed(KeyEvent.VK_X))
break;
}
//Display Mode
while (true) {
int[] map_stuff = new int[4]; //Food x,y; Player x,y;
for (int i = 0; i < 4; i++)
map_stuff[i] = (int)(Math.random()*MAP_SIZE);
while (map[map_stuff[1]][map_stuff[0]]) map_stuff[(int)(Math.random()*2)] = (int)(Math.random()*MAP_SIZE);
agents[0].lx = 0;
agents[0].ly = 0;
agents[0].reset();
for (int i = 0; i < TURNS; i++) {
StdDraw.clear();
drawMap();
StdDraw.setPenColor(Color.BLACK);
agents[0].move(map_stuff[0], map_stuff[1]);
agents[0].draw();
StdDraw.setPenColor(Color.RED);
StdDraw.filledSquare((map_stuff[0]+0.5)/(double)MAP_SIZE, (map_stuff[1]+0.5)/(double)MAP_SIZE, 0.4/(double)MAP_SIZE);
if (StdDraw.isKeyPressed(KeyEvent.VK_R)) {
agents[0].x = (int)(Math.random()*4 + 6*(int)(Math.random()*2));
agents[0].y = (int)(Math.random()*4 + 6*(int)(Math.random()*2));
agents[0].reset();
}
if (StdDraw.isKeyPressed(KeyEvent.VK_SPACE))
agents[0].save("out.txt");
StdDraw.show();
StdDraw.pause(20);
}
}
}
public static void loadMap(String file) {
if (!file.contains(".png")) file += ".png";
Picture pc = new Picture(file);
System.out.println("[Loading]: " + file + " " + pc.width() + "x" + pc.height());
map = new boolean[pc.height()][pc.width()];
for (int i = 0; i < pc.height(); i++) {
for (int j = 0; j < pc.width(); j++) {
map[pc.width()-j-1][i] = (pc.get(i, j).getBlue()+pc.get(i, j).getRed()+pc.get(i, j).getGreen())/3 < 127;
}
}
}
public static void drawMap() {
for (int i = 0; i < MAP_SIZE; i++) {
for (int j = 0; j < MAP_SIZE; j++) {
if (map[i][j]) {
StdDraw.setPenColor(Color.BLUE);
StdDraw.filledSquare((j+0.5)/(double)Main.MAP_SIZE, (i+0.5)/(double)Main.MAP_SIZE, 0.5/(double)Main.MAP_SIZE);
} else if(i%2 == 0 && j%2 == 1) {
StdDraw.setPenColor(new Color(240, 240, 240));
StdDraw.filledSquare((j+0.5)/(double)Main.MAP_SIZE, (i+0.5)/(double)Main.MAP_SIZE, 0.5/(double)Main.MAP_SIZE);
} else if(i%2 == 1 && j%2 == 0) {
StdDraw.setPenColor(new Color(240, 240, 240));
StdDraw.filledSquare((j+0.5)/(double)Main.MAP_SIZE, (i+0.5)/(double)Main.MAP_SIZE, 0.5/(double)Main.MAP_SIZE);
}
}
}
}
public static int getDist(boolean[][] maze_, int startx, int starty, int endx, int endy) {
int r = maze_[0].length, c = maze_.length;
boolean[][] maze = new boolean[r][c];
boolean[][] solve = new boolean[c][r];
for (int y = 0; y<r; y++)
for (int x = 0; x<c; x++)
maze[x][y] = maze_[x][y];
for (int y = 0; y<r; y++)
for (int x = 0; x<c; x++)
solve[x][y] = false;
ArrayList<Path> paths = new ArrayList<>();
Path last;
paths.add(new Path(startx, starty, null));
//Generate A*
while(!paths.isEmpty()) {
Path cur = paths.get(0);
maze[cur.x][cur.y] = false;
if (cur.x == endx && cur.y == endy) break;
if (cur.x < maze_.length-1 && maze[cur.x+1][cur.y]) paths.add(new Path(cur.x+1, cur.y, cur));
if (cur.x > 0 && maze[cur.x-1][cur.y]) paths.add(new Path(cur.x-1, cur.y, cur));
if (cur.y < maze_.length-1 && maze[cur.x][cur.y+1]) paths.add(new Path(cur.x, cur.y+1, cur));
if (cur.y > 0 && maze[cur.x][cur.y-1]) paths.add(new Path(cur.x, cur.y-1, cur));
paths.remove(0);
}
if (paths.isEmpty()) return 0;
//Mark maze
Path best = paths.get(0);
while (best!=null) {
solve[best.x][best.y] = true;
best = best.prev;
}
return solve.length;
}
}
class Path {
int x, y;
Path prev;
public Path(int x, int y, Path prev) {
this.x = x;
this.y = y;
this.prev = prev;
}
}