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
|
#include <assert.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <SDL2/SDL.h>
#include "../game.h"
#include "logging.h"
Game games[2] = {0};
// Tsoding is now a 3-Star Developer!
const char *tsoding_shift(int *argc, char ***argv) {
assert(*argc > 0);
const char* result = **argv;
*argc -= 1;
*argv += 1;
return result;
}
void usage(FILE *stream) {
fprintf(stream, "Usage: ./gp_trainer [OPTIONS]\n");
fprintf(stream, " --gen|g <NUMBER>\n");
fprintf(stream, " Amount of generations (default: 100)\n");
fprintf(stream, " --out|o <FILEPATH>\n");
fprintf(stream, " Output filepath (default: out.bin)\n");
fprintf(stream, " --plot|p\n");
fprintf(stream, " Enable Gnuplot live reporting\n");
}
FILE* gnulog;
int gnuplot;
int current;
const char* output_filepath;
void ctrlc(int signum) {
if (gnuplot) {
log_close_pipe();
fclose(gnulog);
}
dump_game(output_filepath, &games[current]);
exit(1);
}
int main(int argc, char *argv[]) {
signal(SIGINT, ctrlc);
tsoding_shift(&argc, &argv); //Skip Program Name
srand(time(NULL));
int gen_count = 100;
output_filepath = "out.bin";
gnuplot = 0;
while (argc > 0) {
const char *flag = tsoding_shift(&argc, &argv);
if (strcmp(flag, "--gen") == 0 || strcmp(flag, "-g") == 0) {
if (argc == 0) {
usage(stderr);
fprintf(stderr, "ERROR: no value provided for flag %s\n", flag);
exit(1);
}
const char *value = tsoding_shift(&argc, &argv);
gen_count = atoi(value);
} else if (strcmp(flag, "--out") == 0 || strcmp(flag, "-o") == 0) {
if (argc == 0) {
usage(stderr);
fprintf(stderr, "ERROR: no value provided for flag %s\n", flag);
exit(1);
}
const char *value = tsoding_shift(&argc, &argv);
output_filepath = value;
} else if (strcmp(flag, "--plot") == 0 || strcmp(flag, "-p") == 0) {
gnuplot = 1;
} else if (strcmp(flag, "--help") == 0 || strcmp(flag, "-h") == 0) {
usage(stdout);
exit(0);
} else {
usage(stderr);
fprintf(stderr, "ERROR: unknown flag: %s\n", flag);
exit(1);
}
}
if (gnuplot) {
gnulog = fopen("log.dat", "w");
log_header(gnulog);
}
current = 0;
init_game(&games[current]);
for (int i = 0; i < gen_count; ++i) {
fprintf(stderr, "Generation %d... ", i);
fflush(stderr);
clock_t begin = clock();
while (!is_everyone_dead(&games[current])) {
step_game(&games[current]);
}
printf("%fs\n", (float)(clock() - begin) / (float) CLOCKS_PER_SEC);
fflush(stdout);
if (gnuplot) {
log_generation(gnulog, i, &games[current]);
fflush(gnulog);
log_live_update();
}
int next = 1 - current;
make_next_generation(&games[current], &games[next]);
current = next;
}
if (gnuplot) {
log_close_pipe();
fclose(gnulog);
}
dump_game(output_filepath, &games[current]);
return 0;
}
|