-
Notifications
You must be signed in to change notification settings - Fork 13
/
sim.c
703 lines (567 loc) · 17.1 KB
/
sim.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
/*
Ordo is program for calculating ratings of engine or chess players
Copyright 2013 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/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include "sim.h"
#include "encount.h"
#include "groups.h"
#include "plyrs.h"
#include "ra.h"
#include "relprior.h"
#include "randfast.h"
#include "pgnget.h"
#include "xpect.h"
#if 0
#define SAVE_SIMULATION
#define SAVE_SIMULATION_N 2
#endif
// Prototypes
static void
simulate_scores ( const double *ratingof_results
, double deq
, double wadv
, double beta
, struct GAMES *g // output
);
static void
ratings_set_to ( double general_average
, const struct PLAYERS *pPlayers
, struct RATINGS *pRA /*@out@*/
);
//----------------------------------------------------------------
void
get_a_simulated_run ( int limit
, bool_t quiet_mode
, double beta
, double drawrate_evenmatch_result
, double white_advantage_result
, const struct RATINGS *pRA
, const struct prior *PP_ori
, const struct rel_prior_set *pRPset_ori
, struct ENCOUNTERS *pEncounters // output
, struct PLAYERS *pPlayers // output
, struct GAMES *pGames // output
, struct prior *PP // output
, struct rel_prior_set *pRPset // output
)
{
int failed_sim = 0;
relpriors_copy (pRPset_ori, pRPset); // reload original
priors_copy (PP_ori, pPlayers->n, PP); // reload original
do {
if (!quiet_mode && failed_sim > 0)
printf("--> Simulation: [Rejected]\n\n");
players_flags_reset (pPlayers);
simulate_scores ( pRA->ratingof_results
, drawrate_evenmatch_result
, white_advantage_result
, beta
, pGames /*out*/);
relpriors_copy (pRPset_ori, pRPset); // reload original
relpriors_shuffle (pRPset); // simulate new
priors_copy (PP_ori, pPlayers->n, PP);// reload original
priors_shuffle (PP, pPlayers->n); // simulate new
assert(players_have_clear_flags(pPlayers));
encounters_calculate(ENCOUNTERS_FULL, pGames, pPlayers->flagged, pEncounters);
players_set_priored_info (PP, pRPset, pPlayers);
if (0 < players_set_super (quiet_mode, pEncounters, pPlayers)) {
players_purge (quiet_mode, pPlayers);
encounters_calculate(ENCOUNTERS_NOFLAGGED, pGames, pPlayers->flagged, pEncounters);
}
} while (failed_sim++ < limit && !well_connected (pEncounters, pPlayers));
if (!quiet_mode) printf("--> Simulation: [Accepted]\n");
}
/*=== simulation routines ==========================================*/
static int
rand_threeway_wscore(double pwin, double pdraw)
{
long z,x,y;
z = (long)((unsigned)(pwin * (0xffff+1)));
x = (long)((unsigned)((pwin+pdraw) * (0xffff+1)));
y = randfast32() & 0xffff;
if (y < z) {
return WHITE_WIN;
} else if (y < x) {
return RESULT_DRAW;
} else {
return BLACK_WIN;
}
}
// no globals
static void
simulate_scores ( const double *ratingof_results
, double deq
, double wadv
, double beta
, struct GAMES *g // output
)
{
gamesnum_t n_games = g->n;
struct gamei *gam = g->ga;
gamesnum_t i;
player_t w, b;
const double *rating = ratingof_results;
double pwin, pdraw, plos;
assert(deq <= 1 && deq >= 0);
for (i = 0; i < n_games; i++) {
if (gam[i].score != DISCARD) {
w = gam[i].whiteplayer;
b = gam[i].blackplayer;
get_pWDL(rating[w] + wadv - rating[b], &pwin, &pdraw, &plos, deq, beta);
gam[i].score = rand_threeway_wscore(pwin,pdraw);
}
}
}
/*==================================================================*/
// This section is to save simulated results for debugging purposes
static const char *Result_string[4] = {"1-0","1/2-1/2","0-1","*"};
void
save_simulated(struct PLAYERS *pPlayers, struct GAMES *pGames, int num)
{
gamesnum_t i;
const char *name_w;
const char *name_b;
const char *result;
char filename[256] = "";
FILE *fout;
sprintf (filename, "simulated_%04d.pgn", num);
printf ("\n--> filename=%s\n\n",filename);
if (NULL != (fout = fopen (filename, "w"))) {
for (i = 0; i < pGames->n; i++) {
int32_t score_i = pGames->ga[i].score;
player_t wp_i = pGames->ga[i].whiteplayer;
player_t bp_i = pGames->ga[i].blackplayer;
if (score_i == DISCARD) continue;
name_w = pPlayers->name [wp_i];
name_b = pPlayers->name [bp_i];
result = Result_string[score_i];
fprintf(fout,"[White \"%s\"]\n",name_w);
fprintf(fout,"[Black \"%s\"]\n",name_b);
fprintf(fout,"[Result \"%s\"]\n",result);
fprintf(fout,"%s\n\n",result);
}
fclose(fout);
}
}
//========================================================================
#include "sysport.h"
mythread_mutex_t Smpcount;
mythread_mutex_t Groupmtx;
mythread_mutex_t Summamtx;
mythread_mutex_t Printmtx;
static long Sim_N = 0;
static bool_t
smpcount_get (long *x)
{
bool_t ok;
mythread_mutex_lock (&Smpcount);
if (Sim_N > 0) {
*x = Sim_N--;
ok = TRUE;
} else {
*x = 0;
ok = FALSE;
}
mythread_mutex_unlock (&Smpcount);
return ok;
}
static void
smpcount_set (long x)
{
mythread_mutex_lock (&Smpcount);
Sim_N = x;
mythread_mutex_unlock (&Smpcount);
}
//========================================================================
struct SIMSMP {
long simulate
; bool_t sim_updates
; bool_t quiet_mode
; bool_t prior_mode
; bool_t adjust_white_advantage
; bool_t adjust_draw_rate
; bool_t anchor_use
; bool_t anchor_err_rel2avg
; double general_average
; player_t anchor
; player_t priored_n
; double beta
; double drawrate_evenmatch_result
; double white_advantage_result
; const struct rel_prior_set * rps
; const struct prior * pPrior
; struct prior wa_prior
; struct prior dr_prior
; struct ENCOUNTERS * encount // io, modified
; struct PLAYERS * plyrs // io, modified
; struct RATINGS * rat // io, modified
; struct GAMES * pGames // io, modified
; struct rel_prior_set RPset_work // mem provided
; struct prior * PP_work // mem provided
; struct summations * p_sfe_io // output
;
};
//========================================================================
#include "summations.h"
#include "rtngcalc.h"
static void
ratings_set_to ( double general_average
, const struct PLAYERS *pPlayers
, struct RATINGS *pRA /*@out@*/
)
{
// may improve convergence in pathological cases, it should not be needed.
ratings_set (pPlayers->n, general_average, pPlayers->prefed, pPlayers->flagged, pRA->ratingof);
ratings_set (pPlayers->n, general_average, pPlayers->prefed, pPlayers->flagged, pRA->ratingbk);
assert(ratings_sanity (pPlayers->n, pRA->ratingof));
assert(ratings_sanity (pPlayers->n, pRA->ratingbk));
}
static void
updates_print_scale (bool_t sim_updates)
{
if (sim_updates) {
printf ("0 10 20 30 40 50 60 70 80 90 100 (%s)\n","%");
printf ("|----|----|----|----|----|----|----|----|----|----|\n");
}
}
static void
updates_print_head (bool_t quiet, long z, long simulate)
{
mythread_mutex_lock (&Printmtx);
if (!quiet)
printf ("\n==> Simulation:%ld/%ld\n", z+1, simulate);
mythread_mutex_unlock (&Printmtx);
}
static double Fraction = 0.0;
static double Asterisk = 1;
static int Astcount = 0;
static void
updates_print_progress (bool_t sim_updates)
{
double fraction;
int astcount;
mythread_mutex_lock (&Printmtx);
fraction = Fraction;
astcount = Astcount;
if (sim_updates) {
fraction += 1.0;
while (fraction > Asterisk) {
fraction -= Asterisk;
astcount++;
printf ("*");
}
fflush(stdout);
}
Fraction = fraction;
Astcount = astcount;
mythread_mutex_unlock (&Printmtx);
return;
}
static void
updates_print_reachedgoal (bool_t sim_updates)
{
int astcount;
mythread_mutex_lock (&Printmtx);
astcount = Astcount;
if (sim_updates) {
int x = 51-astcount;
while (x-->0) {printf ("*"); fflush(stdout);}
printf ("\n");
}
mythread_mutex_unlock (&Printmtx);
}
void
simul
( long simulate
, bool_t sim_updates
, bool_t quiet_mode
, bool_t prior_mode
, bool_t adjust_white_advantage
, bool_t adjust_draw_rate
, bool_t anchor_use
, bool_t anchor_err_rel2avg
, double general_average
, player_t anchor
, player_t priored_n
, double beta
, double drawrate_evenmatch_result
, double white_advantage_result
, const struct rel_prior_set * rps
, const struct prior * pPrior
, struct prior wa_prior
, struct prior dr_prior
, struct ENCOUNTERS * encount // io, modified
, struct PLAYERS * plyrs // io, modified
, struct RATINGS * rat // io, modified
, struct GAMES * pGames // io, modified
, struct rel_prior_set RPset_work // mem provided
, struct prior * PP_work // mem provided
, struct summations * p_sfe_io // output
)
{
double white_advantage = white_advantage_result;
double drawrate_evenmatch = drawrate_evenmatch_result;
struct summations *sfe = p_sfe_io; // summations for errors
struct ENCOUNTERS Encounters = *encount;
struct rel_prior_set RPset = *rps;
struct PLAYERS Players = *plyrs;
struct RATINGS RA = *rat;
struct GAMES Games = *pGames;
const struct prior * PP = pPrior;
long zz;
ptrdiff_t topn = (ptrdiff_t)Players.n;
assert (simulate > 1);
if (simulate <= 1) return;
/* Simulation block, begin */
while (smpcount_get(&zz)) {
long z = simulate-zz;
if (z == 0) {
// original run
// should be done only once by only one thread, that is why z == 0
mythread_mutex_lock (&Summamtx);
sfe->wa_sum1 += white_advantage_result;
sfe->wa_sum2 += white_advantage_result * white_advantage_result;
sfe->dr_sum1 += drawrate_evenmatch_result;
sfe->dr_sum2 += drawrate_evenmatch_result * drawrate_evenmatch_result;
mythread_mutex_unlock (&Summamtx);
}
updates_print_head (quiet_mode, z, simulate);
// store originals
relpriors_copy (&RPset, &RPset_work);
priors_copy (PP, Players.n, PP_work);
mythread_mutex_lock (&Groupmtx);
get_a_simulated_run ( 100
, quiet_mode
, beta
, drawrate_evenmatch_result
, white_advantage_result
, &RA
, PP
, &RPset
, &Encounters // output
, &Players // output
, &Games // output
, PP_work // output
, &RPset_work // output
);
mythread_mutex_unlock (&Groupmtx);
#if defined(SAVE_SIMULATION)
if (z+1 == SAVE_SIMULATION_N) {
save_simulated(&Players, &Games, (int)(z+1));
}
#endif
// may improve convergence in pathological cases, it should not be needed.
ratings_set_to (general_average, &Players, &RA);
Encounters.n = calc_rating
( quiet_mode
, prior_mode
, adjust_white_advantage
, adjust_draw_rate
, anchor_use
, anchor_err_rel2avg
, general_average
, anchor
, priored_n
, beta
, &Encounters
, &RPset_work
, &Players
, &RA
, &Games
, PP_work
, wa_prior
, dr_prior
, &white_advantage
, &drawrate_evenmatch
);
ratings_cleared_for_purged (&Players, &RA);
if (anchor_err_rel2avg) {
ratings_copy (Players.n, RA.ratingof, RA.ratingbk); // ** save
ratings_center_to_zero (Players.n, Players.flagged, RA.ratingof);
}
// update summations for errors
mythread_mutex_lock (&Summamtx);
summations_update (sfe, topn, RA.ratingof, white_advantage, drawrate_evenmatch);
mythread_mutex_unlock (&Summamtx);
if (anchor_err_rel2avg) {
ratings_copy (Players.n, RA.ratingbk, RA.ratingof); // ** restore
}
updates_print_progress (sim_updates);
} // for loop end
} /* Simulation function, end */
static /*@null@*/
thread_return_t THREAD_CALL
simul_smp_process (void *p);
#include "inidone.h"
void
simul_smp
( int cpus
, long simulate
, bool_t sim_updates
, bool_t quiet_mode
, bool_t prior_mode
, bool_t adjust_white_advantage
, bool_t adjust_draw_rate
, bool_t anchor_use
, bool_t anchor_err_rel2avg
, double general_average
, player_t anchor
, player_t priored_n
, double beta
, double drawrate_evenmatch_result
, double white_advantage_result
, const struct rel_prior_set * rps
, const struct prior * pPrior
, struct prior wa_prior
, struct prior dr_prior
, struct ENCOUNTERS * encount // io, modified
, struct PLAYERS * plyrs // io, modified
, struct RATINGS * rat // io, modified
, struct GAMES * pGames // io, modified
, struct rel_prior_set RPset_work // mem provided
, struct prior * PP_work // mem provided
, struct summations * p_sfe_io // output
)
{
struct SIMSMP s;
void *pdata;
if (cpus < 1) return;
if (cpus > 1 && !quiet_mode) {quiet_mode = TRUE; sim_updates = TRUE;}
s.simulate = simulate ;
s.sim_updates = sim_updates ;
s.quiet_mode = quiet_mode ;
s.prior_mode = prior_mode ;
s.adjust_white_advantage = adjust_white_advantage ;
s.adjust_draw_rate = adjust_draw_rate ;
s.anchor_use = anchor_use ;
s.anchor_err_rel2avg = anchor_err_rel2avg ;
s.general_average = general_average ;
s.anchor = anchor ;
s.priored_n = priored_n ;
s.beta = beta ;
s.drawrate_evenmatch_result = drawrate_evenmatch_result ;
s.white_advantage_result = white_advantage_result ;
s.rps = rps ;
s.pPrior = pPrior ;
s.wa_prior = wa_prior ;
s.dr_prior = dr_prior ;
s.encount = encount ;
s.plyrs = plyrs ;
s.rat = rat ;
s.pGames = pGames ;
s.RPset_work = RPset_work ;
s.PP_work = PP_work ;
s.p_sfe_io = p_sfe_io ;
pdata = &s; // convert to a void pointer, needed for the SMP call
if(!summations_calloc(s.p_sfe_io, s.plyrs->n)) {
fprintf(stderr, "Memory for simulations could not be allocated\n");
exit(EXIT_FAILURE);
}
{
#define MAX_CPUS 64
int CPUS = cpus > MAX_CPUS? MAX_CPUS: cpus;
int t;
static bool_t iret [MAX_CPUS];
static mythread_t threadid [MAX_CPUS];
static int err [MAX_CPUS];
smpcount_set(simulate);
updates_print_scale (sim_updates);
Asterisk = (double)simulate/50.0;
/* Create independent threads each of which will execute function */
for (t = 0; t < CPUS; t++) {
iret[t] = mythread_create( &threadid[t], simul_smp_process, pdata, &err[t]);
}
/* reporting error */
for (t = 0; t < CPUS; t++) {
if (!iret[(short int)t]) { //cast to short int silences a warning
fprintf (stderr, "thread %d, fatal error at creating: %s\n", t, mythread_create_error(err[t]) );
exit(EXIT_FAILURE);
}
}
/* Wait for threads to be done */
for (t = 0; t < CPUS; t++) {
if (0==mythread_join( threadid[t] )) {
fprintf (stderr, "thread %d: fatal problems at joining\n", t);
exit(EXIT_FAILURE);
};
}
}
summations_calc_sdev (s.p_sfe_io, s.plyrs->n, (double)simulate);
updates_print_reachedgoal (sim_updates);
return;
}
static /*@null@*/
thread_return_t THREAD_CALL
simul_smp_process (void *p)
{
bool_t ok;
struct SIMSMP *s = p;
struct PLAYERS _plyrs ;
struct ENCOUNTERS _encount ;
struct GAMES _games ;
struct RATINGS _rat ;
struct prior * _PP_work = NULL;
struct rel_prior_set _RPset_work ;
// save locally
ok = TRUE;
ok = ok && players_replicate (s->plyrs, &_plyrs);
ok = ok && encounters_replicate (s->encount, &_encount);
ok = ok && games_replicate (s->pGames, &_games);
ok = ok && ratings_replicate (s->rat, &_rat);
ok = ok && priorlist_replicate (s->plyrs->n, s->PP_work, &_PP_work);
ok = ok && relpriors_replicate (&s->RPset_work, &_RPset_work);
if (!ok) {
printf ("Not enough memory to run in parallel\n");
exit(EXIT_FAILURE);
}
simul ( s->simulate
, s->sim_updates
, s->quiet_mode
, s->prior_mode
, s->adjust_white_advantage
, s->adjust_draw_rate
, s->anchor_use
, s->anchor_err_rel2avg
, s->general_average
, s->anchor
, s->priored_n
, s->beta
, s->drawrate_evenmatch_result
, s->white_advantage_result
, s->rps
, s->pPrior
, s->wa_prior
, s->dr_prior
, &_encount // io, modified
, &_plyrs // io, modified
, &_rat // io, modified
, &_games // io, modified
, _RPset_work // mem provided
, _PP_work // mem provided
, s->p_sfe_io // output
);
// done
players_done (&_plyrs);
encounters_done (&_encount);
games_done (&_games);
ratings_done (&_rat);
priorlist_done (&_PP_work);
relpriors_done1 (&_RPset_work);
mythread_exit ();
return (thread_return_t) 0;
}