-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.c
1372 lines (1160 loc) · 38.6 KB
/
main.c
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Ordo is program for calculating ratings of engine or chess players
Copyright 2015 Miguel A. Ballicora
This file is part of Ordo.
Ordo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Ordo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Ordo. If not, see <http://www.gnu.org/licenses/>.
*/
/*
|
| ORDO
|
\-------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <stddef.h>
#include "mystr.h"
#include "proginfo.h"
#include "boolean.h"
#include "pgnget.h"
#include "randfast.h"
#include "gauss.h"
#include "groups.h"
#include "mytypes.h"
#include "cegt.h"
#include "indiv.h"
#include "encount.h"
#include "rating.h"
#include "ratingb.h"
#include "xpect.h"
#include "csv.h"
#include "mymem.h"
#include "report.h"
#include "plyrs.h"
#include "namehash.h"
#include "relprior.h"
#include "inidone.h"
#include "rtngcalc.h"
#include "ra.h"
#include "sim.h"
#include "summations.h"
#include "myopt.h"
#include "sysport/sysport.h"
#include "mytimer.h"
/*
|
| GENERAL OPTIONS
|
\*--------------------------------------------------------------*/
const char *license_str = "\n"
" Copyright (c) 2015 Miguel A. Ballicora\n"
" Ordo is program for calculating ratings of engine or chess players\n"
"\n"
" Ordo is free software: you can redistribute it and/or modify\n"
" it under the terms of the GNU General Public License as published by\n"
" the Free Software Foundation, either version 3 of the License, or\n"
" (at your option) any later version.\n"
"\n"
" Ordo is distributed in the hope that it will be useful,\n"
" but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
" GNU General Public License for more details.\n"
"\n"
" You should have received a copy of the GNU General Public License\n"
" along with Ordo. If not, see <http://www.gnu.org/licenses/>.\n"
;
static void parameter_error(void);
static void example (void);
static void usage (FILE *outf);
/* VARIABLES */
static const char *copyright_str =
"Copyright (c) 2015 Miguel A. Ballicora\n"
"There is NO WARRANTY of any kind\n"
;
static const char *intro_str =
"Program to calculate individual ratings\n"
;
static const char *example_options =
"-a 2500 -p input.pgn -o output.txt";
static const char *example_str =
" - Processes input.pgn (PGN file) to calculate ratings to output.txt.\n"
" - The general pool will have an average of 2500\n"
;
/* ....5....|....5....|....5....|....5....|....5....|....5....|....5....|....5....|*/
//-------------------
#include "myhelp.h"
char OPTION_LIST[1024];
static struct option *long_options;
struct helpline SH[] = {
{'h', "help", no_argument, NULL, 0, "print this help and exit"},
{'H', "show-switches",no_argument, NULL, 0, "print switch information and exit"},
{'v', "version", no_argument, NULL, 0, "print version number and exit"},
{'L', "license", no_argument, NULL, 0, "print license information and exit"},
{'q', "quiet", no_argument, NULL, 0, "quiet mode (no progress updates on screen)"},
{'\0', "silent", no_argument, NULL, 0, "same as --quiet"},
{'Q', "terse", no_argument, NULL, 0, "same as --quiet, but shows simulation counter"},
{'\0', "timelog", no_argument, NULL, 0, "outputs elapsed time after each step"},
{'a', "average", required_argument, "NUM", 0, "set rating for the pool average"},
{'A', "anchor", required_argument, "<player>", 0, "anchor: rating given by '-a' is fixed for <player>"},
{'V', "pool-relative",no_argument, NULL, 0, "errors relative to pool average, not to the anchor"},
{'m', "multi-anchors",required_argument, "FILE", 0, "multiple anchors: file contains rows of \"AnchorName\",AnchorRating"},
{'y', "loose-anchors",required_argument, "FILE", 0, "loose anchors: file contains rows of \"Player\",Rating,Uncertainty"},
{'r', "relations", required_argument, "FILE", 0, "relations: rows of \"PlayerA\",\"PlayerB\",delta_rating,uncertainty"},
{'R', "remove-older", no_argument, NULL, 0, "no output of older 'related' versions (given by -r)"},
{'w', "white", required_argument, "NUM", 0, "white advantage value (default=0.0)"},
{'u', "white-error", required_argument, "NUM", 0, "white advantage uncertainty value (default=0.0)"},
{'W', "white-auto", no_argument, NULL, 0, "white advantage will be automatically adjusted"},
{'d', "draw", required_argument, "NUM", 0, "draw rate value % (default=50.0)"},
{'k', "draw-error", required_argument, "NUM", 0, "draw rate uncertainty value % (default=0.0 %)"},
{'D', "draw-auto", no_argument, NULL, 0, "draw rate value will be automatically adjusted"},
{'z', "scale", required_argument, "NUM", 0, "set rating for winning expectancy of 76% (default=202)"},
{'T', "table", no_argument, NULL, 0, "display winning expectancy table"},
{'p', "pgn", required_argument, "FILE", 0, "input file, PGN format"},
{'P', "pgn-list", required_argument, "FILE", 0, "multiple input: file with a list of PGN files"},
{'o', "output", required_argument, "FILE", 0, "output file, text format"},
{'c', "csv", required_argument, "FILE", 0, "output file, comma separated value format"},
{'E', "elostat", no_argument, NULL, 0, "output files in elostat format (rating.dat, programs.dat & general.dat)"},
{'j', "head2head", required_argument, "FILE", 0, "output file with head to head information"},
{'g', "groups", required_argument, "FILE", 0, "outputs group connection info (no rating output)"},
{'G', "force", no_argument, NULL, 0, "force program to run ignoring isolated-groups warning"},
{'s', "simulations", required_argument, "NUM", 0, "perform NUM simulations to calculate errors"},
{'e', "error-matrix", required_argument, "FILE", 0, "save an error matrix (use of -s required)"},
{'C', "cfs-matrix", required_argument, "FILE", 0, "save a matrix (comma separated value .csv) with confidence for superiority (-s was used)"},
{'J', "cfs-show", no_argument, NULL, 0, "output an extra column with confidence for superiority (relative to the player in the next row)"},
{'F', "confidence", required_argument, "NUM", 0, "confidence to estimate error margins (default=95.0)"},
{'X', "ignore-draws", no_argument, NULL, 0, "do not take into account draws in the calculation"},
{'t', "threshold", required_argument, "NUM", 0, "threshold of games for a participant to be included"},
{'N', "decimals", required_argument, "<a,b>", 0, "a=rating decimals, b=score decimals (optional)"},
{'M', "ML", no_argument, NULL, 0, "force maximum-likelihood estimation to obtain ratings"},
{'n', "cpus", required_argument, "NUM", 0, "number of processors used in simulations"},
{'U', "columns", required_argument, "<a,..,z>", 0, "info in output (default columns are \"0,1,2,3,4,5\")"},
{'Y', "synonyms", required_argument, "FILE", 0, "name synonyms (comma separated value format). Each line: main,syn1,syn2 or \"main\",\"syn1\",\"syn2\""},
{'\0', "aliases", required_argument, "FILE", 0, "same as --synonyms FILE"},
{'i', "include", required_argument, "FILE", 0, "include only games of participants present in FILE"},
{'x', "exclude", required_argument, "FILE", 0, "names in FILE will not have their games included"},
{'\0', "no-warnings", no_argument, NULL, 0, "supress warnings of names from -x or -i that do not match names in input file"},
{'b', "column-format",required_argument, "FILE", 0, "format column output, each line form FILE being <column>,<width>,\"Header\""},
{0, NULL, 0, NULL, 0, NULL},
};
/*
|
| ORDO DEFINITIONS
|
\*--------------------------------------------------------------*/
enum AnchorSZ {MAX_ANCHORSIZE=1024};
static bool_t Anchor_use = FALSE;
static player_t Anchor = 0;
static char Anchor_name[MAX_ANCHORSIZE] = "";
static bool_t Anchor_err_rel2avg = FALSE;
static bool_t General_average_set = FALSE;
static double Confidence = 95;
static double General_average = 2300.0;
static long Simulate = 0;
#define INVBETA 175.25
static double White_advantage = 0;
static double White_advantage_SD = 0;
static double Rtng_76 = 202;
static double BETA = 1.0/INVBETA;
static double Confidence_factor = 1.0;
static int OUTDECIMALS = 1; //FIXME Replace by decimals array
static bool_t Decimals_set = FALSE;
static struct GAMESTATS Game_stats;
static double Drawrate_evenmatch = STANDARD_DRAWRATE; //default
static double Drawrate_evenmatch_percent = 100*STANDARD_DRAWRATE; //default
static double Drawrate_evenmatch_percent_SD = 0;
/*------------------------------------------------------------------------*/
static struct prior Wa_prior = {40.0,20.0,FALSE};
static struct prior Dr_prior = { 0.5, 0.1,FALSE};
static struct prior *PP; // to be dynamically assigned
static struct prior *PP_store; // to be dynamically assigned
static struct rel_prior_set RPset = {0, NULL};
static struct rel_prior_set RPset_store = {0, NULL};;
static bool_t Hide_old_ver = FALSE;
static bool_t Prior_mode;
/*---- static functions --------------------------------------------------*/
static void table_output(double Rtng_76);
static int compare_GAME (const void * a, const void * b);
static char *skipblanks(char *p) {while (isspace(*p)) p++; return p;}
static bool_t
strlist_multipush (strlist_t *sl, const char *finp_name)
{
csv_line_t csvln;
FILE *finp;
char myline[MAXSIZE_CSVLINE];
char *p;
bool_t line_success = TRUE;
if (NULL == finp_name || NULL == (finp = fopen (finp_name, "r"))) {
return FALSE;
}
while (line_success && NULL != fgets(myline, MAXSIZE_CSVLINE, finp)) {
p = skipblanks(myline);
if (*p == '\0') continue;
if (csv_line_init(&csvln, myline)) {
line_success = csvln.n == 1 && strlist_push (sl, csvln.s[0]);
csv_line_done(&csvln);
} else {
line_success = FALSE;
}
}
fclose(finp);
return line_success;
}
/*
|
| MAIN
|
\*--------------------------------------------------------------*/
#include "strlist.h"
static bool_t
validate_dec_array (int max, int decimals_array_n, int *decimals_array)
{
int i;
bool_t ok = decimals_array_n <= max;
for (i = 0; ok && i < decimals_array_n; i++) {
ok = decimals_array[i] >= 0;
}
return ok;
}
int main (int argc, char *argv[])
{
enum mainlimits {INPUTMAX=1024, COLSMAX=256, DECMAX=2};
struct summations sfe; // summations for errors
struct DATA *pdaba;
struct GAMES Games;
struct PLAYERS Players;
struct RATINGS RA;
struct ENCOUNTERS Encounters;
double white_advantage_result;
double drawrate_evenmatch_result;
struct output_qualifiers outqual = {FALSE, 0};
long int mingames = 0;
bool_t csvf_opened;
bool_t textf_opened;
bool_t groupf_opened;
FILE *csvf;
FILE *textf;
FILE *groupf;
bool_t quiet_mode;
bool_t sim_updates;
bool_t adjust_white_advantage;
bool_t adjust_draw_rate;
bool_t dowarning;
int columns_n;
int columns[COLSMAX+1];
int decimals_array_n;
int decimals_array[DECMAX+1];
int cpus = 1;
int op;
int longoidx=0;
const char *textstr, *csvstr, *ematstr, *groupstr, *pinsstr;
const char *priorsstr, *relstr;
const char *head2head_str;
const char *ctsmatstr, *synstr;
const char *output_columns;
const char *output_decimals;
const char *includes_str, *excludes_str, *columns_format_str, *multi_pgn, *single_pgn;
int version_mode, help_mode, switch_mode, license_mode, input_mode, table_mode;
bool_t group_is_output, Elostat_output, Ignore_draws, groupcheck, Forces_ML, cfs_column;
bool_t switch_w=FALSE, switch_W=FALSE, switch_u=FALSE, switch_d=FALSE, switch_k=FALSE, switch_D=FALSE;
strlist_t SL;
strlist_t *psl = &SL;
group_var_t *gv = NULL;
/* defaults */
adjust_white_advantage = FALSE;
adjust_draw_rate = FALSE;
quiet_mode = FALSE;
sim_updates = FALSE;
version_mode = FALSE;
license_mode = FALSE;
help_mode = FALSE;
switch_mode = FALSE;
input_mode = FALSE;
table_mode = FALSE;
textstr = NULL;
csvstr = NULL;
ematstr = NULL;
ctsmatstr = NULL;
pinsstr = NULL;
priorsstr = NULL;
relstr = NULL;
synstr = NULL;
includes_str = NULL;
excludes_str = NULL;
columns_format_str = NULL;
single_pgn = NULL;
multi_pgn = NULL;
output_columns = NULL;
output_decimals = NULL;
group_is_output = FALSE;
groupcheck = TRUE;
groupstr = NULL;
Elostat_output = FALSE;
head2head_str = NULL;
Ignore_draws = FALSE;
Forces_ML = FALSE;
cfs_column = FALSE;
dowarning = TRUE;
// global default
TIMELOG = FALSE;
strlist_init(psl);
if (NULL == (long_options = optionlist_new (SH))) {
fprintf(stderr, "Memory error to initialize options\n");
exit(EXIT_FAILURE);
}
optionshort_build(SH, OPTION_LIST);
while (END_OF_OPTIONS != (op = options_l (argc, argv, OPTION_LIST, long_options, &longoidx))) {
switch (op) {
case '\0':
if (!strcmp(long_options[longoidx].name, "silent")) {
quiet_mode = TRUE;
} else if (!strcmp(long_options[longoidx].name, "aliases")) {
synstr = opt_arg;
} else if (!strcmp(long_options[longoidx].name, "no-warnings")) {
dowarning = FALSE;
} else if (!strcmp(long_options[longoidx].name, "timelog")) {
TIMELOG = TRUE;
} else {
fprintf (stderr, "ERROR: %d\n", op);
exit(EXIT_FAILURE);
}
break;
case 'v': version_mode = TRUE; break;
case 'L': version_mode = TRUE;
license_mode = TRUE;
break;
case 'h': help_mode = TRUE; break;
case 'H': switch_mode = TRUE; break;
case 'p': input_mode = TRUE;
single_pgn = opt_arg;
break;
case 'P': input_mode = TRUE;
multi_pgn = opt_arg;
break;
case 'c': csvstr = opt_arg;
break;
case 'o': textstr = opt_arg;
break;
case 'g': group_is_output = TRUE;
groupstr = opt_arg;
break;
case 'G': groupcheck = FALSE;
break;
case 'j': head2head_str = opt_arg;
break;
case 'e': ematstr = opt_arg;
break;
case 'C': ctsmatstr = opt_arg;
break;
case 'm': pinsstr = opt_arg;
break;
case 'r': relstr = opt_arg;
break;
case 'y': priorsstr = opt_arg;
break;
case 'a': if (1 != sscanf(opt_arg,"%lf", &General_average)) {
fprintf(stderr, "wrong average parameter\n");
exit(EXIT_FAILURE);
} else {
General_average_set = TRUE;
}
break;
case 'F': if (1 != sscanf(opt_arg,"%lf", &Confidence) ||
(Confidence > 99.999 || Confidence < 50.001)
) {
fprintf(stderr, "wrong confidence parameter\n");
exit(EXIT_FAILURE);
}
break;
case 'A': if (strlen(opt_arg) < MAX_ANCHORSIZE-1) {
mystrncpy (Anchor_name, opt_arg, MAX_ANCHORSIZE-1);
Anchor_use = TRUE;
} else {
fprintf(stderr, "ERROR: anchor name is too long\n");
exit(EXIT_FAILURE);
}
break;
case 'V': Anchor_err_rel2avg = TRUE;
break;
case 'M': Forces_ML = TRUE;
break;
case 's': if (1 != sscanf(opt_arg,"%lu", &Simulate) || Simulate < 0) {
fprintf(stderr, "wrong simulation parameter\n");
exit(EXIT_FAILURE);
}
break;
case 'w': if (1 != sscanf(opt_arg,"%lf", &White_advantage)) {
fprintf(stderr, "wrong white advantage parameter\n");
exit(EXIT_FAILURE);
} else {
adjust_white_advantage = FALSE;
Wa_prior.isset = FALSE;
switch_w = TRUE;
}
break;
case 'u': if (1 != sscanf(opt_arg,"%lf", &White_advantage_SD)) {
fprintf(stderr, "wrong white advantage uncertainty parameter\n");
exit(EXIT_FAILURE);
} else {
switch_u = TRUE;
}
break;
case 'd': if (1 != sscanf(opt_arg,"%lf", &Drawrate_evenmatch_percent)) {
fprintf(stderr, "wrong white drawrate parameter\n");
exit(EXIT_FAILURE);
} else {
adjust_draw_rate = FALSE;
Dr_prior.isset = FALSE;
switch_d = TRUE;
}
if (Drawrate_evenmatch_percent >= 0.0 && Drawrate_evenmatch_percent <= 100.0) {
Drawrate_evenmatch = Drawrate_evenmatch_percent/100.0;
} else {
fprintf(stderr, "drawrate parameter is out of range\n");
exit(EXIT_FAILURE);
}
break;
case 'k': if (1 != sscanf(opt_arg,"%lf", &Drawrate_evenmatch_percent_SD)) { //drsd
fprintf(stderr, "wrong draw rate uncertainty parameter\n");
exit(EXIT_FAILURE);
} else {
switch_k = TRUE;
}
break;
case 'z': if (1 != sscanf(opt_arg,"%lf", &Rtng_76)) {
fprintf(stderr, "wrong scaling parameter\n");
exit(EXIT_FAILURE);
}
break;
case 'J': cfs_column = TRUE; break;
case 'T': table_mode = TRUE; break;
case 'q': quiet_mode = TRUE; sim_updates = FALSE; break;
case 'Q': quiet_mode = TRUE; sim_updates = TRUE; break;
case 'R': Hide_old_ver=TRUE; break;
case 'W': adjust_white_advantage = TRUE;
Wa_prior.isset = FALSE;
Wa_prior.value = 0;
Wa_prior.sigma = 200.0;
switch_W = TRUE;
break;
case 'D': adjust_draw_rate = TRUE;
Dr_prior.isset = FALSE;
Dr_prior.value = 0.5;
Dr_prior.sigma = 0.5;
switch_D = TRUE;
break;
case 'E': Elostat_output = TRUE; break;
case 'X': Ignore_draws = TRUE; break;
case 'N': output_decimals = opt_arg;
break;
case 't': if (1 != sscanf(opt_arg,"%ld", &mingames) || mingames < 0) {
fprintf(stderr, "wrong threshold parameter\n");
exit(EXIT_FAILURE);
} else {
outqual.mingames = (gamesnum_t)mingames;
outqual.mingames_set = TRUE;
}
break;
case 'n': if (1 != sscanf(opt_arg,"%d", &cpus) || cpus < 1) {
fprintf(stderr, "wrong nuber of processors selected\n");
exit(EXIT_FAILURE);
}
break;
case 'U': output_columns = opt_arg;
break;
case 'Y': synstr = opt_arg;
break;
case 'i': includes_str = opt_arg;
break;
case 'x': excludes_str = opt_arg;
break;
case 'b': columns_format_str = opt_arg;
break;
case '?': parameter_error();
exit(EXIT_FAILURE);
break;
default: fprintf (stderr, "ERROR: %d\n", op);
exit(EXIT_FAILURE);
break;
}
}
optionlist_done (long_options);
/*
| Deal with switches & input
\*----------------------------------*/
if (version_mode) {
printf ("%s %s\n",proginfo_name(),proginfo_version());
if (license_mode)
printf ("%s\n", license_str);
return EXIT_SUCCESS;
}
if (argc < 2) {
fprintf (stderr, "%s %s\n",proginfo_name(),proginfo_version());
fprintf (stderr, "%s", copyright_str);
fprintf (stderr, "for help type:\n%s -h\n\n", proginfo_name());
exit (EXIT_FAILURE);
}
if (help_mode) {
printf ("\n%s", intro_str);
example();
usage(stdout);
printf ("%s\n", copyright_str);
exit (EXIT_SUCCESS);
}
if (switch_mode && !help_mode) {
usage(stdout);
exit (EXIT_SUCCESS);
}
if (table_mode) {
table_output(Rtng_76);
exit (EXIT_SUCCESS);
}
if (!input_mode && argc == opt_index) {
fprintf (stderr, "Need file name to proceed\n\n");
exit(EXIT_FAILURE);
}
if (output_columns != NULL) {
if (!str2list (output_columns, COLSMAX, &columns_n, columns)) {
fprintf (stderr, "Columns number provided are wrong or exceeded limit (%d)\n\n", COLSMAX);
exit(EXIT_FAILURE);
}
} else {
if (!str2list ("0,1,2,3,4,5", COLSMAX, &columns_n, columns)) {
fprintf (stderr, "Default number of columns is wrong or exceeded limit (%d)\n\n", COLSMAX);
exit(EXIT_FAILURE);
}
}
if (output_decimals != NULL) {
if (!str2list (output_decimals, DECMAX, &decimals_array_n, decimals_array)) {
fprintf (stderr, "Decimals number provided are wrong or exceeded limit (%d)\n\n", DECMAX);
exit(EXIT_FAILURE);
}
if (!validate_dec_array (DECMAX, decimals_array_n, decimals_array) || decimals_array_n == 0) {
fprintf (stderr, "Decimals cannot be negative or exceeded limit\n\n");
exit(EXIT_FAILURE);
}
OUTDECIMALS = decimals_array[0];
Decimals_set = TRUE;
} else {
if (!str2list ("1,0", DECMAX, &decimals_array_n, decimals_array)) {
fprintf (stderr, "Default number of decimals is wrong or exceeded limit (%d)\n\n", DECMAX);
exit(EXIT_FAILURE);
}
OUTDECIMALS = decimals_array[0];
Decimals_set = TRUE;
}
/* -------- input files, remaining args --------- */
if (single_pgn) {
if (!strlist_push(psl,single_pgn)) {
fprintf (stderr, "Lack of memory\n\n");
exit(EXIT_FAILURE);
}
}
if (multi_pgn) {
if (!strlist_multipush (psl, multi_pgn)) {
fprintf (stderr, "Errors in file \"%s\", or lack of memory\n", multi_pgn);
exit(EXIT_FAILURE);
}
}
while (opt_index < argc) {
if (!strlist_push(psl,argv[opt_index++])) {
fprintf (stderr, "Too many input files\n\n");
exit(EXIT_FAILURE);
}
}
/*==== Incorrect combination of switches ====*/
if (NULL != pinsstr && (General_average_set || Anchor_use)) {
fprintf (stderr, "Setting a general average (-a) or a single anchor (-A) is incompatible with multiple anchors (-m)\n\n");
exit(EXIT_FAILURE);
}
if ((switch_w || switch_u) && switch_W) {
fprintf (stderr, "Switches -w/-u and -W are incompatible and will not work simultaneously\n\n");
exit(EXIT_FAILURE);
}
if ((switch_d || switch_k) && switch_D) { //drsd
fprintf (stderr, "Switches -d/-k and -D are incompatible and will not work simultaneously\n\n");
exit(EXIT_FAILURE);
}
if (NULL != priorsstr && General_average_set) {
fprintf (stderr, "Setting a general average (-a) is incompatible with having a file with rating seeds (-y)\n\n");
exit(EXIT_FAILURE);
}
if (group_is_output && !groupcheck) {
fprintf (stderr, "Switches -g and -G cannot be used at the same time\n\n");
exit(EXIT_FAILURE);
}
if (includes_str && excludes_str) {
fprintf (stderr, "Switches -x and -i cannot be used at the same time\n\n");
exit(EXIT_FAILURE);
}
Prior_mode = switch_k || switch_u || NULL != relstr || NULL != priorsstr;
/*==== mem init ====*/
if (!name_storage_init()) {
fprintf (stderr, "Problem initializing buffers for reading names, probably lack of memory.\n");
exit(EXIT_FAILURE);
}
/*==== report init ====*/
if (!report_columns_init()) {
fprintf (stderr, "Problem initializing report settings.\n");
exit(EXIT_FAILURE);
}
if (columns_format_str)
report_columns_load_settings (quiet_mode, columns_format_str);
/*==== set input ====*/
timer_reset();
timelog("start");
timelog("input...");
if (NULL != (pdaba = database_init_frompgn (psl, synstr, quiet_mode))) {
if (0 == pdaba->n_players || 0 == pdaba->n_games) {
fprintf (stderr, "ERROR: Input file contains no games\n");
return EXIT_FAILURE;
}
if (Ignore_draws) database_ignore_draws(pdaba);
if (NULL != includes_str) {
bitarray_t ba;
if (ba_init (&ba,pdaba->n_players)) {
namelist_to_bitarray (quiet_mode, dowarning, includes_str, pdaba, &ba);
database_include_only(pdaba, &ba);
} else {
fprintf (stderr, "ERROR\n");
exit(EXIT_FAILURE);
}
}
if (NULL != excludes_str) {
bitarray_t ba;
if (ba_init (&ba,pdaba->n_players)) {
namelist_to_bitarray (quiet_mode, dowarning, excludes_str, pdaba, &ba);
ba_setnot(&ba);
database_include_only(pdaba, &ba);
} else {
fprintf (stderr, "ERROR\n");
exit(EXIT_FAILURE);
}
}
} else {
fprintf (stderr, "Problems reading results\n");
return EXIT_FAILURE;
}
strlist_done(psl); // string list not used anymore from this point on
/*==== memory initialization ====*/
{
player_t mpr = pdaba->n_players;
player_t mpp = pdaba->n_players;
gamesnum_t mg = pdaba->n_games;
gamesnum_t me = pdaba->n_games;
if (!ratings_init (mpr, &RA)) {
fprintf (stderr, "Could not initialize rating memory\n"); exit(EXIT_FAILURE);
} else
if (!games_init (mg, &Games)) {
ratings_done (&RA);
fprintf (stderr, "Could not initialize Games memory\n"); exit(EXIT_FAILURE);
} else
if (!encounters_init (me, &Encounters)) {
ratings_done (&RA);
games_done (&Games);
fprintf (stderr, "Could not initialize Encounters memory\n"); exit(EXIT_FAILURE);
} else
if (!players_init (mpp, &Players)) {
ratings_done (&RA);
games_done (&Games);
encounters_done (&Encounters);
fprintf (stderr, "Could not initialize Players memory\n"); exit(EXIT_FAILURE);
}
}
assert(players_have_clear_flags(&Players));
/*==== data translation ====*/
database_transform (pdaba, &Games, &Players, &Game_stats); /* convert database to global variables */
if (0 == Games.n) {
fprintf (stderr, "ERROR: Input file contains no games\n");
return EXIT_FAILURE;
}
qsort (Games.ga, (size_t)Games.n, sizeof(struct gamei), compare_GAME);
/*==== more memory initialization ====*/
if (!supporting_auxmem_init (Players.n, &PP, &PP_store)) {
ratings_done (&RA);
games_done (&Games);
encounters_done (&Encounters);
players_done(&Players);
fprintf (stderr, "Could not initialize auxiliary Players memory\n"); exit(EXIT_FAILURE);
}
/*==== process anchor ====*/
if (Anchor_use) {
player_t anch_idx;
if (players_name2idx(&Players, Anchor_name, &anch_idx)) {
Anchor = anch_idx;
anchor_j (anch_idx, General_average, &RA, &Players);
} else {
fprintf (stderr, "ERROR: No games of anchor player, mispelled, wrong capital letters, or extra spaces = \"%s\"\n", Anchor_name);
fprintf (stderr, "Surround the name with \"quotes\" if it contains spaces\n\n");
return EXIT_FAILURE;
}
}
/*==== more wrong input ====*/
if (Drawrate_evenmatch < 0.0 || Drawrate_evenmatch > 1.0) {
fprintf (stderr, "ERROR: Invalide draw rate set\n");
return EXIT_FAILURE;
}
if (!(Drawrate_evenmatch > 0.0) && Game_stats.draws > 0 && Prior_mode) {
fprintf (stderr, "ERROR: Draws present in the database but -d switch specified an invalid number\n");
return EXIT_FAILURE;
}
if (Drawrate_evenmatch > 0.999) {
fprintf (stderr, "ERROR: Draw rate set with -d switch is too high, > 99.9%s\n", "%");
return EXIT_FAILURE;
}
#if 0
// to ignore a given player --> test_player
// here, set:
// Players.flagged[test_player] = TRUE;
// Players.present_in_games[test_player] = FALSE;
#endif
assert(players_have_clear_flags(&Players));
encounters_calculate(ENCOUNTERS_FULL, &Games, Players.flagged, &Encounters);
if (0 == Encounters.n) {
fprintf (stderr, "ERROR: Input file contains no games to process\n");
return EXIT_FAILURE;
}
/*==== report, input checked ====*/
if (!quiet_mode) {
printf ("Total games %8ld\n",(long)
(Game_stats.white_wins
+Game_stats.draws
+Game_stats.black_wins
+Game_stats.noresult));
printf (" - White wins %8ld\n", (long) Game_stats.white_wins);
printf (" - Draws %8ld\n", (long) Game_stats.draws);
printf (" - Black wins %8ld\n", (long) Game_stats.black_wins);
printf (" - Truncated/Discarded %8ld\n", (long) Game_stats.noresult);
printf ("Unique head to head %8.2f%s\n", 100.0*(double)Encounters.n/(double)Games.n, "%");
if (Anchor_use) {
printf ("Reference rating %8.1lf",General_average);
printf (" (set to \"%s\")\n", Anchor_name);
} else if (priorsstr != NULL || pinsstr != NULL) { // priors
printf ("Reference rating ");
printf (" --> not fixed, determined by anchors\n");
} else {
printf ("Reference rating %8.1lf",General_average);
printf (" (average of the pool)\n");
}
printf ("\n");
}
//===
Confidence_factor = confidence2x(Confidence/100.0);
ratings_starting_point (Players.n, General_average, &RA);
// priors
priors_reset (PP, Players.n);
if (priorsstr != NULL) {
priors_load (quiet_mode, priorsstr, &RA, &Players, PP);
}
// multiple anchors here
if (pinsstr != NULL) {
init_manchors (quiet_mode, pinsstr, &RA, &Players);
}
// relative priors
if (relstr != NULL) {
relpriors_init (quiet_mode, &Players, relstr, &RPset, &RPset_store);
}
// show priored information
if (!quiet_mode) {
priors_show(&Players, PP, Players.n);
relpriors_show(&Players, &RPset);
players_set_priored_info (PP, &RPset, &Players);
}
// process draw and white adv. switches
if (switch_w && switch_u) {
if (White_advantage_SD > PRIOR_SMALLEST_SIGMA) {
Wa_prior.isset = TRUE;
Wa_prior.value = White_advantage;
Wa_prior.sigma = White_advantage_SD;
adjust_white_advantage = TRUE;
} else {
Wa_prior.isset = FALSE;
Wa_prior.value = White_advantage;
Wa_prior.sigma = White_advantage_SD;
adjust_white_advantage = FALSE;
}
}
if (switch_d && switch_k) {
if (Drawrate_evenmatch_percent_SD > PRIOR_SMALLEST_SIGMA) {
Dr_prior.isset = TRUE;
Dr_prior.value = Drawrate_evenmatch_percent/100.0;
Dr_prior.sigma = Drawrate_evenmatch_percent_SD/100.0;
adjust_draw_rate = TRUE;
} else {
Dr_prior.isset = FALSE;
Dr_prior.value = Drawrate_evenmatch_percent/100.0;
Dr_prior.sigma = Drawrate_evenmatch_percent_SD/100.0;
adjust_draw_rate = FALSE;
}
}
// open files
textf = NULL;
textf_opened = FALSE;
if (textstr == NULL) {
textf = stdout;
} else {
textf = fopen (textstr, "w");
if (textf == NULL) {
fprintf(stderr, "Errors with file: %s\n",textstr);
} else {
textf_opened = TRUE;
}
}
csvf = NULL;
csvf_opened = FALSE;
if (csvstr != NULL) {
csvf = fopen (csvstr, "w");
if (csvf == NULL) {
fprintf(stderr, "Errors with file: %s\n",csvstr);
} else {
csvf_opened = TRUE;
}
}
groupf_opened = FALSE;
groupf = NULL;
if (group_is_output) {
if (groupstr != NULL) {
groupf = fopen (groupstr, "w");
if (groupf == NULL) {
fprintf(stderr, "Errors with file: %s\n",groupstr);
exit(EXIT_FAILURE);
} else {
groupf_opened = TRUE;
}
}
}
/*
| BEGIN...
\*----------------------------------*/
mythread_mutex_init (&Smpcount);
mythread_mutex_init (&Groupmtx);
mythread_mutex_init (&Summamtx);
mythread_mutex_init (&Printmtx);
randfast_init (1324561);
BETA = (-log(1.0/0.76-1.0)) / Rtng_76;
summations_init(&sfe);
timelog("done with input");
timelog("process groups if needed...");
/*===== groups ========*/
gv = NULL;
assert(players_have_clear_flags (&Players));
encounters_calculate (ENCOUNTERS_FULL, &Games, Players.flagged, &Encounters);
if (group_is_output || groupcheck) {
timelog("processing groups...");
if (NULL == (gv = GV_make (&Encounters, &Players))) {
fprintf (stderr, "not enough memory for encounters allocation\n");