-
Notifications
You must be signed in to change notification settings - Fork 0
/
beaufils.h
708 lines (599 loc) · 14.5 KB
/
beaufils.h
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
#ifndef REINFORCEMENT_IPD_BEAUFILS_H
#define REINFORCEMENT_IPD_BEAUFILS_H
// part of this file is adapted from http://www.lifl.fr/IPD/ipd.html.en#softwares
// PRISON, Copyright (C) 1992-1998 by LIFL (SMAC team)
#include "strukture.h"
enum B_akcija { COOPERATE, DEFECT };
typedef B_akcija card;
int turn = -1;
struct _emul_history
{
vector<interakcija> *izvor;
bool ja_sam_glavni = true;
card operator[](int n)
{
auto i = izvor->at(n - 1);
if (ja_sam_glavni)
return i.first == akcija::n ? DEFECT : COOPERATE;
else
return i.second == akcija::n ? DEFECT : COOPERATE;
}
} RH, MH;
int nbC(_emul_history& emh) { return count_if(emh.izvor->begin(), emh.izvor->end(), [&](const interakcija& i)
{
if (emh.ja_sam_glavni)
return i.first == akcija::s;
else
return i.second == akcija::s;
});
}
int nbD(_emul_history& emh) { return count_if(emh.izvor->begin(), emh.izvor->end(), [&](const interakcija& i)
{
if (emh.ja_sam_glavni)
return i.first == akcija::n;
else
return i.second == akcija::n;
});
}
B_akcija _pokreni_klasicnu_strategiju(function<card(int)> st_f, stanje s)
{
auto &povijest = s.povijest[s.s_kime_trebam_igrati->id];
turn = povijest.size() + 1;
MH.ja_sam_glavni = false; // MH je nasa strategija, a mi nismo klasicna
RH.ja_sam_glavni = true;
MH.izvor = RH.izvor = &povijest;
return st_f(s.s_kime_trebam_igrati->id /*irelevantan parametar */ );
}
struct klasicna_strategija : strategija
{
function<card(int)> ime;
klasicna_strategija(function<card(int)> _ime) : ime(_ime) {}
virtual akcija operator()(const stanje& st)
{
return _pokreni_klasicnu_strategiju(ime, st) == COOPERATE ? akcija::s : akcija::n;
}
};
populacija klasicna_populacija(const initializer_list<function<card(int)>>& strategije, int kopija)
{
populacija p;
int k = 1;
for (auto &s : strategije)
for (int i = 0; i < kopija; ++i, ++k)
p.obicni_igraci.push_back(new igrac{new klasicna_strategija(s), k});
return p;
}
namespace bfs
{
card
all_c(int X)
{
/* Always cooperates. [c]* */
return COOPERATE;
}
card
all_d(int X)
{
/* Always defects. [d]* */
return DEFECT;
}
card
tit_for_tat(int X)
{
/* The tit_for_tat strategy was introduced by Anatole Rapoport. It begins to
cooperate, and then play what its opponent played in the last move. */
if (turn == 1)
return COOPERATE;
else
return RH[turn - 1];
}
card
mistrust(int X)
{
/* Defects, then plays opponent's move. */
if (turn == 1)
return DEFECT;
else
return RH[turn - 1];
}
card
spiteful(int X)
{
/* It cooperates until the opponent has defected, after that move it always
defects. */
if (turn == 1)
return COOPERATE;
else if (RH[turn - 1] == DEFECT || MH[turn - 1] == DEFECT)
return DEFECT;
else
return COOPERATE;
}
card
per_ddc(int X)
{
/* Plays periodically : [d,d,c]* */
if (turn % 3 == 0)
return COOPERATE;
else
return DEFECT;
}
card
per_ccd(int X)
{
/* Plays periodically : [c,c,d]* */
if (turn % 3 == 0)
return DEFECT;
else
return COOPERATE;
}
card
soft_majo(int X)
{
/* Cooperates, then plays opponent's majority move, if equal then
cooperates. */
if (nbC(RH) >= nbD(RH))
return COOPERATE;
else
return DEFECT;
}
card
hard_majo(int X)
{
/* Cooperates, then plays opponent's majority move, if equal then
defects. */
if (nbC(RH) > nbD(RH))
return COOPERATE;
else
return DEFECT;
}
card
prober(int X)
{
/* Plays [d,c,c], then it defects in all other move if opponent has
cooperated in move 2 and 3, and plays as tit_for_tat in other cases. */
if (turn == 1) return DEFECT;
else if (turn == 2) return COOPERATE;
else if (turn == 3) return COOPERATE;
else if (RH[2] == COOPERATE && RH[3] == COOPERATE)
return DEFECT;
else
return RH[turn - 1]; /* tit for tat */
}
card
hard_tft(int X)
{
/* Cooperates except if opponent has defected at least one time in the two
previous move. */
if (turn == 1)
return COOPERATE;
// luka: del: else if (defect_lim(2,RH) >= 1)
else if (RH[turn - 1] == DEFECT || (turn >= 3) && (RH[turn - 2] == DEFECT))
return DEFECT;
else
return COOPERATE;
}
card
gradual (int X)
{
/* The gradual strategy is studied in beaufils-delahaye-mathieu:meeting. It
cooperates until the opponent defects. Then after the first defection it
defects one time, and cooperate two times, ... after the nth defections it
reacts with n consecutive defections and cooperates two times. */
static int n[1] = {0};
if (turn == 1)
{
n[X] = 0;
return COOPERATE;
}
else if (n[X] == 1 || n[X] == 2)
{
n[X]--;
return COOPERATE;
}
else if (n[X] > 2)
{
n[X]--;
return DEFECT;
}
else if (RH[turn - 1] == DEFECT)
{
n[X] = nbD(RH) + 2 - 1;
return DEFECT;
}
else
return COOPERATE;
}
card
gradual_killer(int X)
{
/* This strategy has been built on a late friday evening in order to be sure
that tit_for_tat can beat gradual in round-robin tournament as well as in
ecological competition. To achieve this goal it begins by defecting in
the first fifth moves, then cooperates two times. Then it defects all the
time if the opponent has defected in move 6 and 7, and else cooperates
all the times. It works with an environment of 3 strategies :
tit_for_tat, gradual and gradual_killer */
if (turn <= 5)
return DEFECT;
else if (turn <= 7)
return COOPERATE;
else if (RH[6] == DEFECT && RH[7] == DEFECT)
return DEFECT;
else
return COOPERATE;
}
card
hard_tf2t(int X)
{
/* Cooperates except if opponent has defected two consecutive times in the
last 3 moves. */
if (turn <= 2)
return COOPERATE;
else if (RH[turn - 1] == DEFECT && RH[turn - 2] == DEFECT)
return DEFECT;
else if (turn > 3 && RH[turn - 2] == DEFECT && RH[turn - 3] == DEFECT)
return DEFECT;
else
return COOPERATE;
}
card
soft_tf2t(int X)
{
/* Cooperates except if opponent has defected two consecutive times. */
if (turn <= 2)
return COOPERATE;
else if (RH[turn - 1] == DEFECT && RH[turn - 2] == DEFECT)
return DEFECT;
else
return COOPERATE;
}
#ifdef neimpl
card
c_then_per_dc (int X)
{
/* Cooperates until first opponent's defection, then plays [d,c]*. */
static counter n;
if (turn==1)
{
n[X]=0;
return COOPERATE;
}
else if (n[X] == 1)
{
if (MH[turn - 1]==DEFECT)
return COOPERATE;
else
return DEFECT;
}
else if (RH[turn - 1]==DEFECT)
{
n[X]=1;
return DEFECT;
}
else
return COOPERATE;
}
card
per_cd (int X)
{
/* Plays periodically [c,d]. */
if ((turn%2) != 0)
return COOPERATE;
else
return DEFECT;
}
card
per_cccdcd (int X)
{
/* Plays periodically : [c,c,c,d,c,d]*. */
if (turn % 6 == 1) return COOPERATE;
else if (turn % 6 == 2) return COOPERATE;
else if (turn % 6 == 3) return COOPERATE;
else if (turn % 6 == 4) return DEFECT;
else if (turn % 6 == 5) return COOPERATE;
else return DEFECT;
}
card
per_ccccd (int X)
{
/* Plays periodically : [c,c,c,c,d]*. */
if ((turn%5) == 0)
return DEFECT;
else
return COOPERATE;
}
card
prober2 (int X)
{
/* Plays [d,c,c], then if opponent plays [d,c] in move 2 and 3, always
cooperate, else plays as tit for tat. */
if (turn==1) return DEFECT;
else if (turn==2) return COOPERATE;
else if (turn==3) return COOPERATE;
else if (RH[2]==DEFECT && RH[3]==COOPERATE)
return COOPERATE;
else
return RH[turn-1]; /* tit for tat */
}
card
prober3 (int X)
{
/* Plays [d,c] then if opponent cooperates in move 2 always defect, else
plays as tit for tat. */
if (turn==1) return DEFECT;
else if (turn==2) return COOPERATE;
else if (RH[2]==COOPERATE)
return DEFECT;
else
return RH[turn-1]; /* tit for tat */
}
card
prober4 (int X)
{
/* Plays [c,c,d,c,d,d,d,c,c,d,c,d,c,c,d,c,d,d,c,d] then ... */
static counter nb_d_d, nb_c_d;
static char what[22]=" ccdcdddccdcdccdcddcd";
if (turn==1)
{
nb_c_d[X]=0;
nb_d_d[X]=0;
return COOPERATE;
}
else if (turn==2)
return COOPERATE;
else
{
if (turn <= 20)
{
if (what[turn-2]=='c' && RH[turn-1]==DEFECT) nb_c_d[X]++;
if (what[turn-2]=='d' && RH[turn-1]==DEFECT) nb_d_d[X]++;
if (what[turn]=='c')
return COOPERATE;
else
return DEFECT;
}
else
if (abs(nb_c_d[X] - nb_d_d[X]) <= 2)
return DEFECT;
else
if (turn<=25)
return COOPERATE;
else
return RH[turn-1];
}
}
card
hard_prober (int X)
{
/* Plays [d,d,c,c], then if opponent cooperated in move 2 and 3 always
defects, else plays as tit for tat. */
if (turn==1 || turn==2) return DEFECT;
else if (turn==3 || turn==4) return COOPERATE;
else if ((RH[2]==COOPERATE) && (RH[3]==COOPERATE))
return DEFECT;
else
return RH[turn-1]; /* tit for tat */
}
card
doubler (int X)
{
/* Cooperates except when opponent defected, and opponent's cooperations is
less than 2 times opponent's defections. */
static counter nb_d, nb_c;
if (turn==1)
{
nb_c[X]=0;
nb_d[X]=0;
return COOPERATE;
}
else
{
if (RH[turn - 1] == DEFECT)
nb_d[X]++;
else
nb_c[X]++;
if (RH[turn - 1]==DEFECT && nb_c[X]<=2*nb_d[X])
return DEFECT;
else
return COOPERATE;
}
}
card
soft_spiteful (int X)
{
/* Cooperates except when opponent defected, in such a case opponent is
punished with [d,d,d,d,c,c]. */
static counter n;
if (turn==1)
{
n[X]=0;
return COOPERATE;
}
else
{
if (n[X]==1 || n[X]==2)
{
n[X]--;
return COOPERATE;
}
else if (n[X] > 2)
{
n[X]--;
return DEFECT;
}
else if (RH[turn - 1]==DEFECT)
{
n[X] = 4+2-1;
return DEFECT;
}
else
return COOPERATE;
}
}
card
pavlov (int X)
{
/* The win-stay/lose-shift strategy is studied in nowak-sigmund:strategy.
It cooperates if and only if both players opted for the same choice in
the previous move. */
if (turn == 1)
return COOPERATE;
else if (MH[turn-1] == RH[turn-1])
return COOPERATE;
else
return DEFECT;
}
card
slow_tft (int X)
{
/* Plays [c,c], then if opponent plays two consecutive time the same move
plays my opposite move .*/
if (turn<=2)
return COOPERATE;
if (RH[turn-1] == RH[turn-2])
return RH[turn-1];
else
return MH[turn-1];
}
card
easy_go (int X)
{
/* It defects until the opponent has defected, after that move it always
cooperates. */
if (turn == 1)
return DEFECT;
else
if (RH[turn - 1] == DEFECT || MH[turn - 1] == COOPERATE)
return COOPERATE;
else
return DEFECT;
}
/*****************************************************************************
* Strategies with random intervention.
*****************************************************************************/
card
ipd_random (int X)
{
/* Cooperates with probability 1/2. */
if ((rand()%2) != 0)
return COOPERATE;
else
return DEFECT;
}
card
hard_joss (int X)
{
/* Plays as tit for tat, but cooperates with a probability of only 0.9. */
if (turn==1)
return COOPERATE;
else if (RH[turn - 1]==DEFECT)
return DEFECT;
else if ((rand()%10) != 0)
return COOPERATE;
else
return DEFECT;
}
card
soft_joss (int X)
{
/* Plays as tit for tat, but defects with a probability of only 0.9. */
if (turn == 1)
return COOPERATE;
else if (RH[turn - 1]==COOPERATE)
return COOPERATE;
else if ((rand()%10) != 0)
return DEFECT;
else
return COOPERATE;
}
card
prob_c_4_on_5 (int X)
{
/* Cooperates with probability p(c)=4/5. */
if ((rand()%5) != 0)
return COOPERATE;
else
return DEFECT;
}
card
calculator (int X)
{
/* Plays as hard_joss in the first 20 moves. Then if opponent played
periodically, always defects, else plays as tit for tat. */
static counter period, find;
int j, end;
if (turn<=20)
return hard_joss(X); /* no static var in hard_joss */
if (turn==21)
{
/* looking for a period. */
find[X]=0;
for (period[X]=1; !find[X] && period[X]<=(turn/2); period[X]++)
{
end=0;
for (j=1; j<=(turn - 1 - period[X]) && !end; j++)
{
if (RH[j] != RH[j+period[X]])
end=1;
}
if (!end)
find[X]=1;
}
}
if (find[X] == 1)
return DEFECT;
else
return RH[turn-1]; /* tit for tat */
}
card
better_and_better (int X)
{
/* Defects with probability (1000-turn)/1000, so less and less. */
if ((rand()%1000) < (1000-turn))
return DEFECT;
else
return COOPERATE;
}
card
worse_and_worse (int X)
{
/* Defects with probability turn/1000, so more and more. */
if ((rand()%1000) < turn)
return DEFECT;
else
return COOPERATE;
}
card
worse_and_worse2 (int X)
{
/* Plays as tit for tat during the first 20 moves, then defects with probability
(turn-20) / turn. */
if (turn==1) return COOPERATE;
if (turn<=20) return RH[turn-1];
else if (RH[turn-1]==DEFECT)
return DEFECT;
else if ((rand()%turn) < (turn-20))
return DEFECT;
else
return COOPERATE;
}
card
worse_and_worse3 (int X)
{
/* Cooperates, then the more the opponent defect, the more it defects,
i.e. in move turn+1 defects with probability nb(d)/turn. */
static counter n;
if (turn==1)
{
n[X]=0;
return COOPERATE;
}
else if (RH[turn - 1]==DEFECT)
n[X]++;
if ((rand()%turn) < n[X])
return DEFECT;
else
return COOPERATE;
}
#endif // neimplem.
}
#endif //REINFORCEMENT_IPD_BEAUFILS_H