Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
378 lines (296 loc) · 9.74 KB
/
Copy pathMain.java
File metadata and controls
378 lines (296 loc) · 9.74 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*************************************************************
* Main.java
* Secret Hitler
*
* MSc Computer Games Systems
* Nottingham Trent University
* Major Project
*
* Dario Hermann N0773470
* 2017/18
*************************************************************/
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
/**
* The starting class of the programm
*
*/
public class Main {
public static void main(String[] args) {
Random rnd = new Random(System.currentTimeMillis());
Game secretHitler = new GameRun();
ArrayList<String> roles = secretHitler.getRoles();
ArrayList<Integer> fascists = new ArrayList<Integer>();
ArrayList<Player> players = new ArrayList<Player>();
int victory;
float[] fitness = new float[20];
int[] positions = new int[20];
for(int i =0; i < 2; i++) {
fascists.add(-1);
}
ArrayList<NN> neuralNetworks = new ArrayList<NN>();
for(int i = 0; i < 20; i++) {
neuralNetworks.add(new NN(48, 30)); //creates 20 new NN with the intention of later reading all the 20 individuals saved on a txt file
}
String[] values = new String[20];
int weightNumb;
String filePath = "weightCounter.txt";
String content = null;
try{
content = new String ( Files.readAllBytes( Paths.get(filePath) ) ); //This the number of the file of the most recent population
}catch (IOException e){
e.printStackTrace();
}
weightNumb = Integer.parseInt(content);
filePath = "weights" + weightNumb + ".txt";
content = null;
try{
content = new String ( Files.readAllBytes( Paths.get(filePath) ) ); // this loads up the most recent population of weights
}catch (IOException e){
e.printStackTrace();
}
values = content.split("\\r?\\n");
for(int i = 0; i < 20; i++) {
neuralNetworks.get(i).initialWeights(values[i]); //adds the initial weights to the NN
}
filePath = "counter.txt";
content = null;
try{
content = new String ( Files.readAllBytes( Paths.get(filePath) ) ); //a counter to see what individual the next ML Bot needs to use
}catch (IOException e){
e.printStackTrace();
}
int counter = Integer.parseInt(content);
int chosen;
boolean[] isML = new boolean[5]; //isML is used to know how many players were ML Bots
for(int i=0; i < 5; i++) {
isML[i] = false;
}
int[] typeOfPlayers = new int[5];
Collections.shuffle(roles);
System.out.println(roles);
for(int i = 0; i < 5; i++) { //choosing what type of players will play
do {
chosen = rnd.nextInt(7); //The types of player commented out are only used if the user wants
// if(chosen == 0) {
// HonestBot n_player = new HonestBot(roles.get(i), i+1);
// players.add(n_player);
// } else if(chosen == 1) {
// RandomBot n_player = new RandomBot(roles.get(i), i+1);
// players.add(n_player);
/*} else*/ if(chosen == 2) {
Human n_player = new Human(roles.get(i), i+1);
players.add(n_player);
} else if(chosen < 5) {
InteligBot n_player = new InteligBot(roles.get(i), i+1);
players.add(n_player);
} else if(chosen < 7) {
if(counter < 300) {
MLBot n_player = new MLBot(roles.get(i), i+1, neuralNetworks.get(counter));
players.add(n_player);
isML[i] = true;
}
}
} while(chosen >= 5 && counter == 300);
typeOfPlayers[i] = chosen;
if(chosen >= 5) {
counter++;
}
if(roles.get(i).equals("Hitler")) {
fascists.set(1, i+1);
} else if(roles.get(i).equals("Fascist")) {
fascists.set(0, i+1);
}
}
secretHitler.makePlayersState(players);
for(int i = 0; i < 5; i++) {
players.get(i).receiveRole(fascists);
}
victory = secretHitler.start(); //start the game
if(victory == 1 ) { //LIBERALS WIN
for(int i = 0; i < 5; i++) {
if(roles.get(i).equals("Liberal")) {
players.get(i).didIWin(true);
} else {
players.get(i).didIWin(false);
}
}
} else {
for(int i = 0; i < 5; i++) {
if(roles.get(i).equals("Liberal")) {
players.get(i).didIWin(false);
} else {
players.get(i).didIWin(true);
}
}
}
int c = 0;
for(int i = 0; i < 5; i++) {
positions[i] = i;
positions[i+5] = i+5;
positions[i+10] = i+10;
positions[i+15] = i+15;
if(isML[i]) {
fitness[c++] = players.get(i).getTotalCost();
}
}
for(int i = 0; i < 5; i++) {
System.out.print(typeOfPlayers[i] + "\t"); //prints out what type of players were playing
}
/* FROM THIS POINT UNTIL THE END IT WAS ONLY USED WHILE TRAINING THE MLBOT, OUTSIDE OF TRAINING MODE THIS IS NOT NECESSARY
try(FileWriter fw = new FileWriter("fitnesses.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
for(int i = 0; i < c; i++) {
out.println(fitness[i]);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try(FileWriter fw = new FileWriter("counter.txt", false);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
out.print(counter);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//----------------------------------------------------
if(counter >= 300) {
String n_filePath = "fitnesses.txt";
String n_content = null;
try{
n_content = new String ( Files.readAllBytes( Paths.get(n_filePath) ) );
}catch (IOException e){
e.printStackTrace();
}
String[] _cont = n_content.split("\\r?\\n");
for(int i = 0; i < 20; i++) {
fitness[i] = Float.parseFloat(_cont[i]);
fitness[i] += Float.parseFloat(_cont[i+20]);
fitness[i] += Float.parseFloat(_cont[i+40]);
fitness[i] += Float.parseFloat(_cont[i+60]);
fitness[i] += Float.parseFloat(_cont[i+80]);
fitness[i] += Float.parseFloat(_cont[i+100]);
fitness[i] += Float.parseFloat(_cont[i+120]);
fitness[i] += Float.parseFloat(_cont[i+140]);
fitness[i] += Float.parseFloat(_cont[i+160]);
fitness[i] += Float.parseFloat(_cont[i+180]);
fitness[i] += Float.parseFloat(_cont[i+200]);
fitness[i] += Float.parseFloat(_cont[i+220]);
fitness[i] += Float.parseFloat(_cont[i+240]);
fitness[i] += Float.parseFloat(_cont[i+260]);
fitness[i] += Float.parseFloat(_cont[i+280]);
}
n_filePath = "final_counter.txt";
n_content = null;
try{
n_content = new String ( Files.readAllBytes( Paths.get(n_filePath) ) );
}catch (IOException e){
e.printStackTrace();
}
int page = Integer.parseInt(n_content);
filePath = "fitness_Final_" + page + ".txt";
try(FileWriter fw = new FileWriter(filePath, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
for(int i = 0; i < 20; i++) {
out.println(i+1 + "\t\t" + fitness[i]);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for(int i = 0; i < 20; i++)
{
positions[i] = i;
}
for(int i = 0; i < 19; i++) {
for(int j = 0; j < 19 - i; j++) {
if(fitness[j] > fitness[j+1]) {
float x = fitness[j];
fitness[j] = fitness[j+1];
fitness[j+1] = x;
int y = positions[j];
positions[j] = positions[j+1];
positions[j+1] = y;
}
}
}
filePath = "fitness_Final2_" + page + ".txt";
try(FileWriter fw = new FileWriter(filePath, false);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
for(int i = 0; i < 20; i++) {
out.println(positions[i]+1 + "\t\t" + fitness[i]);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
float[][] survivors = new float[4][];
for(int i = 0; i < 4; i++) {
survivors[i] = neuralNetworks.get(positions[i]).getBrain();
}
GeneticAlgorithms ga = new GeneticAlgorithms(survivors);
ga.generateChildren();
String[] newGeneration = ga.getNew_Gen_String();
filePath = "weights" + (weightNumb+1) + ".txt";
try(FileWriter fw = new FileWriter(filePath, false);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
for(int i = 0; i < newGeneration.length; i++) {
out.println(newGeneration[i]);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
filePath = "weightCounter.txt";
try(FileWriter fw = new FileWriter(filePath, false);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
out.print(weightNumb+1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
++page;
filePath = "final_counter.txt";
try(FileWriter fw = new FileWriter(filePath, false);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
out.print(page);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
filePath = "counter.txt";
try(FileWriter fw = new FileWriter(filePath, false);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
out.print(0);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
filePath = "fitnesses.txt";
try(FileWriter fw = new FileWriter(filePath, false);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
out.print("");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}*/
}
}
You can’t perform that action at this time.
