-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntrain-cli.cpp
456 lines (364 loc) · 9.81 KB
/
ntrain-cli.cpp
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
#include "dirent.h"
#include "bpn.h"
BPN *bpn0;
void print_arguments() {
std::cout << "Usage: ntrain-cli [create|train|test|convert] log_directory/bpn_file [bpn_file] [threads] [epochs]" << std::endl
<< "--------------------------------------------------------------------------------------------------" << std::endl
<< "ntrain-cli create bpn_file" << std::endl
<< " Create and save a new network to `bpn_file'." << std::endl << std::endl
<< "ntrain-cli train log_directory bpn_file [threads=1] [epochs=1]" << std::endl
<< " Train network `bpn_file' (if doesn't exist create a new one) over `log_directory'" << std::endl
<< " `epoch' number of times with `threads' threads and save to `bpn_file'." << std::endl << std::endl
<< "ntrain-cli test log_directory bpn_file [threads=1]" << std::endl
<< " Test network `bpn_file' (if doesn't exist create a new one) over `log_directory'" << std::endl
<< " with `threads' threads." << std::endl << std::endl
<< "ntrain-cli convert log_directory" << std::endl
<< " Convert log files in directory `log_directory' to bpnl format" << std::endl
<< " keeping log files as well." << std::endl;
}
bool ends_with (const char *str, const char *end) {
unsigned len_str = strlen(str), len_end = strlen(end);
if (len_str < len_end) return false;
return strcmp((const char*) str + len_str - len_end, end) ==0;
}
int train (const char *bpn_file, const std::string folder, unsigned threads, unsigned epochs) {
try {
bpn0 = new BPN(bpn_file, threads -1);
DIR *dir;
struct dirent *ent;
dir = opendir(folder.c_str());
if (dir != NULL) {
unsigned logs = 0, bpnls = 0;
while ((ent = readdir(dir)) != NULL) {
if (ends_with(ent->d_name, ".bpnl")) {
++bpnls;
}
else if (ends_with(ent->d_name, ".log"))
++logs;
}
rewinddir(dir);
time_t rawtime;
time(&rawtime);
std::cout << "Start training over ";
if (bpnls >0) { // .bpnl
std::cout << bpnls << " bpnl files " << ctime(&rawtime);
while ((ent = readdir(dir)) != NULL) {
if (ends_with(ent->d_name, ".bpnl")) {
std::cout << "Training over " << ent->d_name << ": ";
for (unsigned i=0; i < epochs; ++i) {
if(bpn0->TrainOverFile(folder + ent->d_name)) {
bpn0->SaveToFile((char*)"./bpn~");
}
else {
delete bpn0;
bpn0 = new BPN((char*)"./bpn~", threads -1);
std::cout << "error ";
}
time(&rawtime);
std::cout << ctime(&rawtime);
}
}
}
}
else { // .log
std::cout << logs << " log files " << ctime(&rawtime);
while ((ent = readdir(dir)) != NULL) {
if (ends_with(ent->d_name, ".log")) {
std::cout << "Training over " << ent->d_name << ": ";
for (unsigned i=0; i < epochs; ++i) {
if(bpn0->TrainOverRawFile(folder + ent->d_name)) {
bpn0->SaveToFile((char*)"./bpn~");
}
else {
delete bpn0;
bpn0 = new BPN((char*)"./bpn~", threads -1);
std::cout << "error ";
}
time(&rawtime);
std::cout << ctime(&rawtime);
}
}
}
}
closedir (dir);
}
else {
std::cout << "Not a valid directory: " << folder << std::endl;
return 2;
}
}
catch (std::exception ex) {
std::cout << ex.what() << std::endl;
return 1;
}
return 0;
}
int test (const char *bpn_file, const std::string folder, unsigned threads) {
try {
bpn0 = new BPN(bpn_file, threads -1);
DIR *dir;
struct dirent *ent;
dir = opendir (folder.c_str());
if (dir != NULL) {
unsigned logs = 0, bpnls = 0;
while ((ent = readdir(dir)) != NULL) {
if (ends_with(ent->d_name, ".bpnl")) {
++bpnls;
}
else {
if (ends_with(ent->d_name, "log")) ++logs;
}
}
rewinddir(dir);
double total_error = 0, current_error;
int total_count=0, i=0;
time_t rawtime;
time(&rawtime);
std::cout << "Start testing over ";
if (bpnls >0) { // .bpnl
std::cout << bpnls << " bpnl files " << ctime(&rawtime);
while ((ent = readdir(dir)) != NULL) {
if (ends_with(ent->d_name, "bpnl")) {
i = 0;
current_error = bpn0->TestOverFile(folder + ent->d_name, i);
if(i > 0) {
total_error += current_error;
total_count += i;
current_error /= (float)i;
}
std::cout << "Mean error over " << ent->d_name << ": " << current_error << std::endl;
}
}
}
else { // .log
std::cout << logs << " log files " << ctime(&rawtime);
while ((ent = readdir(dir)) != NULL) {
if (ends_with(ent->d_name, ".log")) {
i = 0;
current_error = bpn0->TestOverRawFile(folder + ent->d_name, i);
if(i > 0) {
total_error += current_error;
total_count += i;
current_error /= (float)i;
}
std::cout << "Mean error over " << ent->d_name << ": " << current_error << std::endl;
}
}
}
if(total_count > 0) total_error /= (float)total_count;
std::cout << "Total mean error: " << total_error << std::endl;
time(&rawtime);
std::cout << ctime(&rawtime);
closedir (dir);
}
else {
std::cout << "Not a valid directory: " << folder << std::endl;
return 2;
}
}
catch (std::exception ex) {
std::cout << ex.what() << std::endl;
return 1;
}
return 0;
}
void FENtoInput(const char *line, std::ofstream &out) {
unsigned i=0, j=0, len = strlen(line);
while(i<256 && j < len) { // this is for 64 squares
switch(line[j]) {
case 'P': // white pawn - code 0001
out<<"0001";
i +=4;
break;
case 'p': // black pawn - code 1001
out<<"1001";
i +=4;
break;
case 'N': // white kNight - code 0010
out<<"0010";
i +=4;
break;
case 'n': // black kNight - code 1010
out<<"1010";
i +=4;
break;
case 'B': // white bishop - code 0011
out<<"0011";
i +=4;
break;
case 'b': // black bishop - code 1011
out<<"1011";
i +=4;
break;
case 'R': // white rook - code 0100
out<<"0100";
i +=4;
break;
case 'r': // black rook - code 1100
out<<"1100";
i +=4;
break;
case 'Q': // white queen - code 0101
out<<"0101";
i +=4;
break;
case 'q': // black queen - code 1101
out<<"1101";
i +=4;
break;
case 'K': // white king - code 0110
out<<"0110";
i +=4;
break;
case 'k': // black king - code 1110
out<<"1110";
i +=4;
break;
}
if(line[j] <= '8' && line[j] >= '1') {
int emptySquares = line[j] - '0';
emptySquares *= 4;
for(int k=0; k< emptySquares; ++k) {
//out<<true;
out<<'1';
}
i += emptySquares;
}
++j;
}
while(line[j] == ' ')
++j;
if(line[j] == 'w' || line[j] == 'W') out<<'1'; // set active colour bit - i == 256
else out<<'0';
do {
++j;
}while(line[j] == ' ');
char flags[4] = {'0','0','0','0'};
while(line[j] != '-' && line[j] != ' ') {
switch(line[j]) {
case 'K': // White can castle kingside
flags[0] = '1';
break;
case 'Q': // White can castle queenside
flags[1] = '1';
break;
case 'k': // Black can castle kingside
flags[2] = '1';
break;
case 'q': // Black can castle queenside
flags[3] = '1';
break;
}
++j;
}
for(i=0; i<4; ++i) {
out<<flags[i];
}
while(line[j] == ' ')
++j;
if(line[j] =='-')
out<<'0';
else // en passant possible
out<<'1';
while(line[j] != '[')
++j;
++j;
out << " ";
while (line[j] != ']')
out << line[j++];
}
void convert_file(const std::string file) {
std::string new_file = file.substr(0, file.length() - 3) + "bpnl";
std::ifstream fin;
std::ofstream fout;
fin.open(file.c_str(), std::ifstream::in);
if(!fin.is_open()) return;
fout.open(new_file.c_str(), std::ofstream::out);
if(!fout.is_open()) return;
std::string str;
while(getline(fin, str)) {
FENtoInput(str.c_str(), fout);
fout << std::endl;
}
fout.close();
fin.close();
}
int convert(std::string folder) {
DIR *dir;
struct dirent *ent;
dir = opendir (folder.c_str());
if (dir != NULL) {
unsigned logs = 0;
while ((ent = readdir(dir)) != NULL) {
if (ends_with(ent->d_name, ".log")) ++logs;
}
rewinddir(dir);
time_t rawtime;
time(&rawtime);
std::cout << "Start converting " << logs << " files: " << ctime(&rawtime);
while ((ent = readdir(dir)) != NULL) {
if (ends_with(ent->d_name, ".log")) {
std::cout << folder + ent->d_name << " ..." << std::endl;
convert_file(folder + ent->d_name);
}
}
time(&rawtime);
std::cout << "End of converting " << ctime(&rawtime);
closedir (dir);
}
else {
std::cout << "Not a valid directory: " << folder << std::endl;
return 2;
}
return 0;
}
int main(int argc, char * argv[]) {
if (argc < 3) {
print_arguments();
return 0;
}
if (strcmp(argv[1], "create") ==0) {
bpn0 = new BPN();
bpn0->SaveToFile(argv[2]);
delete bpn0;
return 0;
}
unsigned threads = 1, epochs = 1;
if (argc > 4) {
try {
threads = atof(argv[4]);
if (argc > 5) epochs = atof(argv[5]);
}
catch (std::exception ex) {
print_arguments();
std::cout << ex.what() << std::endl;
return 1;
}
}
std::string dir = std::string(argv[2]);
if (!ends_with(dir.c_str(), "/"))
dir += "/";
if (strcmp(argv[1], "convert") ==0)
return convert(dir);
else if (strcmp(argv[1], "train") ==0) {
if (train(argv[3], dir, threads, epochs) ==0) {
bpn0->SaveToFile(argv[3]);
delete bpn0;
return 0;
}
else
return 1;
}
else if (strcmp(argv[1], "test") ==0) {
int ret = test(argv[3], dir, threads);
delete bpn0;
return ret;
}
else
print_arguments();
return 0;
}