-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprisonerDilemmaWithGUI.py
964 lines (764 loc) · 34.3 KB
/
prisonerDilemmaWithGUI.py
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
# COMP-3710 Final project Prisoner Dilemma simulator
# main file that holds the AI tournament
# import all the bots
from tkinter.ttk import Notebook, Panedwindow, Labelframe
from tkinter import Frame
from tkinter import *
from basicBot import basicBot
from titForTatBot import *
from grudgerBot import *
from randomBot import randomBot
from majority import *
from periodicBot import periodicBot
from pavlov import pavlovBot
from gradual import gradualBot
from prober import proberBot
from deterministic import *
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import numpy as np
# import random to add in noise
import random
# math for calculating mean and standard deviation
import math
class tournament:
cooperate = True
defect = False
# holds all strategies currently available in game
strategyList = []
# strategies array for text-based GUI only, not needed here
strategies = ["always cooperate", "always defect", "tit-for-tat", "grudger", "choose randomly",
"soft majority", "hard majority", "Cyclical DDC", "Cyclical CCD", "Cyclical CD",
"mean tit-for-tat", "pavlov", "cooperative tit-for-tat", "hard tit-for-tat", "slow tit-for-tat", "gradual", "prober", "sneaky tit-for-tat", "forgetful grudger", "forgiving tit-for-tat",
"generous tit-for-tat", "probability (set your own probability)"]
def __init__(self, rounds: int, noiseVal: int):
self.numRounds = rounds
# list of bots competing
self.botList = []
self.noise = noiseVal
# Sam
def insertBots(botNum: int, numOfBots: int):
for i in range(numOfBots):
bot = tournament.getBot(botNum) # get bot the using that strategy
self.botList.append(bot) # add to botlist
@staticmethod
def getBot(botNum: int):
if botNum == 1:
return basicBot("always cooperate", tournament.cooperate)
if botNum == 2:
return basicBot("always defect", tournament.defect)
if botNum == 3:
return titForTatBot("tit-for-tat", tournament.cooperate)
if botNum == 4:
return grudgerBot()
if botNum == 5:
return randomBot()
if botNum == 6:
return softMajorityBot()
if botNum == 7:
return hardMajorityBot()
if botNum == 8:
return periodicBot("Cyclical DDC", [tournament.defect, tournament.defect, tournament.cooperate])
if botNum == 9:
return periodicBot("Cyclical CCD", [tournament.cooperate, tournament.cooperate, tournament.defect])
if botNum == 10:
return periodicBot("Cyclical CD", [tournament.cooperate, tournament.defect])
if botNum == 11:
return titForTatBot("mean tit-for-tat", tournament.defect)
if botNum == 12:
return pavlovBot()
if botNum == 13:
return cooperativeTitForTatBot()
if botNum == 14:
return hardTitForTatBot()
if botNum == 15:
return slowTitForTatBot()
if botNum == 16:
return gradualBot()
if botNum == 17:
return proberBot()
if botNum == 18:
return sneakyTitForTatBot()
if botNum == 19:
# 10 rounds to forget by default
return forgetfulGrudgerBot(grudgerForgetMemoryEntry.get())
if botNum == 20:
return forgivingTitForTatBot(0.1) # set to 10% by default
if botNum == 21:
# set to random forgiveness to 5%
return generousTitForTatBot(0.05)
if (botNum != -1): # if not a valid choice and not -1 raise
raise Exception('Invalid choice ')
@staticmethod # Sam
# Get slider values and load bots into tournament
def getSliderValue(sliderNum: int) -> int:
if sliderNum == 1:
return allCooperateSlider.get()
if sliderNum == 2:
return allDefectSlider.get()
if sliderNum == 3:
return tftSlider.get()
if sliderNum == 4:
return grudgerSlider.get()
if sliderNum == 5:
return randomSlider.get()
if sliderNum == 6:
return majSoftSlider.get()
if sliderNum == 7:
return majHardSlider.get()
if sliderNum == 8:
return ddcSlider.get()
if sliderNum == 9:
return ccdSlider.get()
if sliderNum == 10:
return cdSlider.get()
if sliderNum == 11:
return tftMeanSlider.get()
if sliderNum == 12:
return pavlovSlider.get()
if sliderNum == 13:
return tftCoopSlider.get()
if sliderNum == 14:
return tftHardSlider.get()
if sliderNum == 15:
return tftSlowSlider.get()
if sliderNum == 16:
return gradualSlider.get()
if sliderNum == 17:
return proberSlider.get()
if sliderNum == 18:
return tftSneakySlider.get()
# if sliderNum == 19:
# return grudgerForgetSlider.get()
if sliderNum == 20:
return tftForgiveSlider.get()
# if sliderNum == 21:
# return tftGenerousSlider.get()
else:
return 0
# create round for the tournament
def setNoise(self, n: float):
self.noise = n
def displayResult(self): # moved here so could be called in runTournament
#self.sortBot()
# dynamically created lists for switch case
labels = []
minYears = []
maxYears = []
# where each bot will load their corresponding .getYears() into
alwaysCooperate = []
alwaysDefect = []
traditionalTFT = []
grudgerTraditional = []
chooseRandomly = []
softMajority = []
hardMajority = []
DDC = []
CCD = []
CD = []
meanTFT = []
pavlov = []
cooperativeTFT = []
hardTFT = []
slowTFT = []
gradual = []
prober = []
sneakyTFT = []
grudgerForgetful = []
forgivingTFT = []
generousTFT = []
for bot in self.botList:
print(bot.getName() + " spent " + bot.getYears() + " in prison\n")
if(bot.getName() == tournament.strategies[0]) :
alwaysCooperate.append(int(bot.getYears()))
if 'Always Cooperate' not in labels :
labels.append('Always Cooperate')
elif(bot.getName() == tournament.strategies[1]) :
alwaysDefect.append(int(bot.getYears()))
if 'Always Defect' not in labels :
labels.append('Always Defect')
elif(bot.getName() == tournament.strategies[2]) :
traditionalTFT.append(int(bot.getYears()))
if 'Tit-For-Tat' not in labels :
labels.append('Tit-For-Tat')
elif(bot.getName() == tournament.strategies[3]) :
grudgerTraditional.append(int(bot.getYears()))
if 'Traditional Grudger' not in labels :
labels.append('Traditional Grudger')
elif(bot.getName() == tournament.strategies[4]) :
chooseRandomly.append(int(bot.getYears()))
if 'Random' not in labels :
labels.append('Random')
elif(bot.getName() == tournament.strategies[5]) :
softMajority.append(int(bot.getYears()))
if 'Soft Majority' not in labels :
labels.append('Soft Majority')
elif(bot.getName() == tournament.strategies[6]) :
hardMajority.append(int(bot.getYears()))
if 'Hard Majority' not in labels :
labels.append('Hard Majority')
elif(bot.getName() == tournament.strategies[7]) :
DDC.append(int(bot.getYears()))
if 'DDC' not in labels :
labels.append('DDC')
elif(bot.getName() == tournament.strategies[8]) :
CCD.append(int(bot.getYears()))
if 'CCD' not in labels :
labels.append('CCD')
elif(bot.getName() == tournament.strategies[9]) :
CD.append(int(bot.getYears()))
if 'CD' not in labels :
labels.append('CD')
elif(bot.getName() == tournament.strategies[10]) :
meanTFT.append(int(bot.getYears()))
if 'Mean TFT' not in labels :
labels.append('Mean TFT')
elif(bot.getName() == tournament.strategies[11]) :
pavlov.append(int(bot.getYears()))
if 'Pavlov' not in labels :
labels.append('Pavlov')
elif(bot.getName() == tournament.strategies[12]) :
cooperativeTFT.append(int(bot.getYears()))
if 'Cooperative TFT' not in labels :
labels.append('Cooperative TFT')
elif(bot.getName() == tournament.strategies[13]) :
hardTFT.append(int(bot.getYears()))
if 'Hard TFT' not in labels :
labels.append('Hard TFT')
elif(bot.getName() == tournament.strategies[14]) :
slowTFT.append(int(bot.getYears()))
if 'Slow TFT' not in labels :
labels.append('Slow TFT')
elif(bot.getName() == tournament.strategies[15]) :
gradual.append(int(bot.getYears()))
if 'Gradual' not in labels :
labels.append('Gradual')
elif(bot.getName() == tournament.strategies[16]) :
prober.append(int(bot.getYears()))
if 'Prober' not in labels :
labels.append('Prober')
elif(bot.getName() == tournament.strategies[17]) :
sneakyTFT.append(int(bot.getYears()))
if 'Sneaky TFT' not in labels :
labels.append('Sneaky TFT')
elif(bot.getName() == tournament.strategies[18]) :
grudgerForgetful.append(int(bot.getYears()))
if 'Forgetful Grudger' not in labels :
labels.append('Forgetful Grudger')
elif(bot.getName() == tournament.strategies[19]) :
forgivingTFT.append(int(bot.getYears()))
if 'Forgiving TFT' not in labels :
labels.append('Forgiving TFT')
elif(bot.getName() == tournament.strategies[20]) :
generousTFT.append(int(bot.getYears()))
if 'Generous TFT' not in labels :
labels.append('Generous TFT')
else :
print("Not Working")
# Getting the minimum and maximum years spent in prison for every subset of strategies
if labels.__contains__("Always Cooperate") :
minYears.append(min(alwaysCooperate))
maxYears.append(max(alwaysCooperate))
if labels.__contains__("Always Defect") :
minYears.append(min(alwaysDefect))
maxYears.append(max(alwaysDefect))
if labels.__contains__("Tit-For-Tat") :
minYears.append(min(traditionalTFT))
maxYears.append(max(traditionalTFT))
if labels.__contains__("Traditional Grudger") :
minYears.append(min(grudgerTraditional))
maxYears.append(max(grudgerTraditional))
if labels.__contains__("Random") :
minYears.append(min(chooseRandomly))
maxYears.append(max(chooseRandomly))
if labels.__contains__("Soft Majority") :
minYears.append(min(softMajority))
maxYears.append(max(softMajority))
if labels.__contains__("Hard Majority") :
minYears.append(min(hardMajority))
maxYears.append(max(hardMajority))
if labels.__contains__("DDC") :
minYears.append(min(DDC))
maxYears.append(max(DDC))
if labels.__contains__("CCD") :
minYears.append(min(CCD))
maxYears.append(max(CCD))
if labels.__contains__("CD") :
minYears.append(min(CD))
maxYears.append(max(CD))
if labels.__contains__("Mean TFT") :
minYears.append(min(meanTFT))
maxYears.append(max(meanTFT))
if labels.__contains__("Pavlov") :
minYears.append(min(pavlov))
maxYears.append(max(pavlov))
if labels.__contains__("Cooperative TFT") :
minYears.append(min(cooperativeTFT))
maxYears.append(max(cooperativeTFT))
if labels.__contains__("Hard TFT") :
minYears.append(min(hardTFT))
maxYears.append(max(hardTFT))
if labels.__contains__("Slow TFT") :
minYears.append(min(slowTFT))
maxYears.append(max(slowTFT))
if labels.__contains__("Gradual") :
minYears.append(min(gradual))
maxYears.append(max(gradual))
if labels.__contains__("Prober") :
minYears.append(min(prober))
maxYears.append(max(prober))
if labels.__contains__("Sneaky TFT") :
minYears.append(min(sneakyTFT))
maxYears.append(max(sneakyTFT))
if labels.__contains__("Forgetful Grudger") :
minYears.append(min(grudgerForgetful))
maxYears.append(max(grudgerForgetful))
if labels.__contains__("Forgiving TFT") :
minYears.append(min(forgivingTFT))
maxYears.append(max(forgivingTFT))
if labels.__contains__("Generous TFT") :
minYears.append(min(generousTFT))
maxYears.append(max(generousTFT))
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, minYears, width, label='Min Years')
rects2 = ax.bar(x + width/2, maxYears, width, label='Max Years')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('# of Years')
ax.set_title('Prisoner Dilemma Observations')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = rect.get_height()
#print("This is the height = " + str(height))
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
fig.tight_layout()
plt.show()
def setUp(self): # Run Simulation button runs this function!
i = 1
for i in range(1, 21):
# print(self.getSliderValue(i)) # for testing
numOfBots = int(self.getSliderValue(i))
if numOfBots == 0:
pass
else:
j = 1
for j in range(1, numOfBots + 1):
bot = tournament.getBot(i) # get the using that strategy
self.botList.append(bot)
def humanPlay(self):
print("Welcome to the strategy choosing Menu.\n")
tournament.printStrategyMenu()
botNum = int(input("Please choose your opponent strategy\n"))
if (botNum <= -1 or botNum > len(tournament.strategies)):
print("Invalid strategy... Now exiting")
return
bot = tournament.getBot(botNum)
humanMoves = []
humanYears = 0
for r in range(self.numRounds):
humanMove = int(
input("Please pick your move 1.Cooperate 2.Betray"))
if (humanMove == 1):
move1 = tournament.cooperate
else:
move1 = tournament.defect
move2 = bot.getMove(humanMoves)
humanMoves.append(move1)
# both cooperate they both go to jail for 2 years
if (move1 == tournament.cooperate and move2 == tournament.cooperate):
print("You both cooperated you get to go to jail for 2 years!")
humanYears += 2
bot.addYears(2)
# both defect they both go to jail for 5 years
if (move1 == tournament.defect and move2 == tournament.defect):
print(
"You both betrayed each other you get to go to jail for 5 years...")
humanYears += 5
bot.addYears(5)
# if bot1 cooperates and bot 2 defects then bot1
# goes to jail for 10 years while bot 2 gets to walk
if (move1 == tournament.cooperate and move2 == tournament.defect):
print(
"You got betrayed... You got sent to jail for 10 years while the bot walked free :(")
humanYears += 10
bot.addYears(0)
# same as third scenario in reverse
if (move1 == tournament.defect and move2 == tournament.cooperate):
print(
"Your plan worked! You get to walk free and the bot got 10 years in prison O.O")
humanYears += 0
bot.addYears(10)
print("At the end of the you got " + str(
humanYears) + " years in jail and the bot got " + bot.getYears() + " years")
def faceOff(self):
for i in range(len(self.botList)):
for j in range(i + 1, len(self.botList)):
# list of bot moves for bots that need memory
bot1Moves = []
bot2Moves = []
strategy1 = self.botList[i]
strategy2 = self.botList[j]
# for strategies that change variables during the round reset them
strategy1.newRound()
strategy2.newRound()
for r in range(self.numRounds):
move1 = strategy1.getMove(bot2Moves)
move2 = strategy2.getMove(bot1Moves)
# create noise
if (random.uniform(0, 10) < self.noise):
if (random.randint(0, 1) == 0):
move1 = False
else:
move1 = True
if (random.uniform(0, 10) < self.noise):
if (random.randint(0, 1) == 0):
move2 = False
else:
move2 = True
bot1Moves.append(move1)
bot2Moves.append(move2)
# both cooperate they both go to jail for 1 years
if (move1 == tournament.cooperate and move2 == tournament.cooperate):
strategy1.addYears(1)
strategy2.addYears(1)
# both defect they both go to jail for 2 years
if (move1 == tournament.defect and move2 == tournament.defect):
strategy1.addYears(2)
strategy2.addYears(2)
# if bot1 cooperates and bot2 defects then bot1
# goes to jail for 10 years while bot2 gets to walk
if (move1 == tournament.cooperate and move2 == tournament.defect):
strategy1.addYears(3)
strategy2.addYears(0)
# same as third scenario in reverse
if (move1 == tournament.defect and move2 == tournament.cooperate):
strategy1.addYears(0)
strategy2.addYears(3)
# ** sort bot from best to worse performing
def sortBot(self):
self.botList.sort(key=lambda bot: bot.getYears())
def geneticAlgorithm(self):
sumYears = 0
standardDeviation = 0.00
variance = 0.00
mean = 0.00
if (self.numRounds < 1):
raise Exception(
"No bots faced off against each other cannot complete genetic algorithm")
return
# get mean
for bot in self.botList:
y = int(bot.getYears())
sumYears += y
mean = sumYears / len(self.botList)
# get variance
for bot in self.botList:
y = int(bot.getYears())
variance += pow(y - mean, 2)
variance /= len(self.botList)
standardDeviation = math.sqrt(variance)
self.numRounds = 0
if standardDeviation != 0 :
newBotList = []
for bot in self.botList:
y = int(bot.getYears())
numKid = -round((y - mean) / standardDeviation)
for i in range(numKid):
newBotList.append(bot.getChild())
self.botList = newBotList
@staticmethod
def runTournament():
print(tftSlider.get())
mainTournament = tournament(int(roundsEntry.get()), int(noiseSlider.get()))
mainTournament.setUp()
mainTournament.faceOff()
mainTournament.displayResult()
# Prisoner's Dilemma Simulator GUI
notebookWidth = 225
class strategySlider(Scale):
def __init__(self, **kwargs):
Scale.__init__(self, orient=HORIZONTAL, state=NORMAL, from_=0, to=10, length=170, **kwargs)
self.pack()
class infoButton(Button):
def __init__(self, **kwargs):
Button.__init__(self, text="Info", **kwargs)
self.pack()
root = Tk()
left = Frame(root)
left.pack(side=LEFT)
right = Frame(root)
right.pack(side=RIGHT) # TODO Pack other half of widgets here!
root.title("Prisoner\'s Dilemma Simulator")
def info_window(str):
window = Toplevel(root)
label = Label(window, text=str)
label.pack()
return
theLabel = Label(left, text="Tit-For-Tat")
theLabel.pack()
tft = Notebook(left, width=notebookWidth)
tft.pack()
tftTrad = Frame(tft)
tftMean = Frame(tft)
tftCoop = Frame(tft)
tftHard = Frame(tft)
tft.add(tftTrad, text='Traditional')
tft.add(tftMean, text='Mean')
tft.add(tftCoop, text='Cooperative')
tft.add(tftHard, text='Hard')
tftSlider = strategySlider(master=tftTrad, label="Traditional")
tftSlider.pack()
tftMeanSlider = strategySlider(master=tftMean, label="Mean")
tftMeanSlider.pack()
tftCoopSlider = strategySlider(master=tftCoop, label="Cooperative")
tftCoopSlider.pack()
tftHardSlider = strategySlider(master=tftHard, label="Hard")
tftHardSlider.pack()
tradInfoButton = infoButton(master=tftTrad, command=lambda: info_window(
"Traditional Tit-For-Tat\n\n This strategy will begin by cooperating, then mirror its opponents' strategies. \n"))
tradInfoButton.pack()
meanInfoButton = infoButton(master=tftMean, command=lambda: info_window(
"Mean Tit-For-Tat\n\n This strategy will begin by defecting, then mirror its opponents' strategies. \n"))
meanInfoButton.pack()
coopInfoButton = infoButton(master=tftCoop, command=lambda: info_window(
"Cooperative Tit-For-Tat\n\n This strategy cooperates unless its opponent's previous two moves were defections, then it plays Tit-For-Tat. \n"))
coopInfoButton.pack()
hardInfoButton = infoButton(master=tftHard, command=lambda: info_window(
"Hard Tit-For-Tat\n\n This strategy will cooperate until one of the opponent's last two moves was a defection, then it plays Tit-For-Tat. \n"))
hardInfoButton.pack()
# Periodic Interface
periodicLabel = Label(left, text="Periodic")
periodicLabel.pack()
periodic = Notebook(left, width=notebookWidth)
periodic.pack()
periodicDDC = Frame(periodic)
periodicCCD = Frame(periodic)
periodicCD = Frame(periodic)
periodic.add(periodicDDC, text='DDC')
periodic.add(periodicCCD, text='CCD')
periodic.add(periodicCD, text='CD')
ddcSlider = strategySlider(master=periodicDDC, label='Defect Defect Cooperate')
ddcSlider.pack()
ccdSlider = strategySlider(
master=periodicCCD, label='Cooperate Cooperate Defect')
ccdSlider.pack()
cdSlider = strategySlider(master=periodicCD, label='Cooperate Defect')
cdSlider.pack()
DDCInfoButton = infoButton(master=periodicDDC, command=lambda: info_window(
"DDC\n\n This strategy plays these moves in the following order on repeat: defect, defect, cooperate. \n"))
DDCInfoButton.pack()
CCDInfoButton = infoButton(master=periodicCCD, command=lambda: info_window(
"CCD\n\n This strategy plays these moves in the following order on repeat: cooperate, cooperate, defect. \n"))
CCDInfoButton.pack()
CDInfoButton = infoButton(master=periodicCD, command=lambda: info_window(
"CD\n\n This strategy alternates between cooperating and defecting. \n"))
CDInfoButton.pack()
# Majority Bots
majorityLabel = Label(left, text="Majority")
majorityLabel.pack()
majority = Notebook(left, width=notebookWidth)
majority.pack()
majForget = Frame(majority)
majHard = Frame(majority)
majSoft = Frame(majority)
majority.add(majHard, text='Hard')
majority.add(majSoft, text='Soft')
majHardSlider = strategySlider(master=majHard, label='Hard')
majHardSlider.pack()
majSoftSlider = strategySlider(master=majSoft, label='Soft')
majSoftSlider.pack()
majHardInfoButton = infoButton(master=majHard, command=lambda: info_window(
"Hard Majority \n\n This strategy defects so long as the opponent defects more often than it cooperates. \n"))
majHardInfoButton.pack()
majSoftInfoButton = infoButton(master=majSoft, command=lambda: info_window(
"Soft Majority \n\n This strategy cooperates so long as the opponent cooperates more often than it defects. \n"))
majSoftInfoButton.pack()
# Pavlov Interface
pavlovLabel = Label(left, text="Pavlov")
pavlovLabel.pack()
pavlov = Notebook(left, width=notebookWidth)
pavlov.pack()
Pavlov = Frame(pavlov)
pavlov.add(Pavlov, text = "Pavlov")
pavlovSlider = strategySlider(master=Pavlov, label='Pavlov')
pavlovSlider.pack()
pavInfoButton = infoButton(master=Pavlov, command=lambda: info_window(
"Pavlov \n\n This strategy plays DCC, then switches to a Tit-For-Tat style \n"))
pavInfoButton.pack()
# Grudger Interface
grudgerLabel = Label(left, text="Grudger")
grudgerLabel.pack()
grudger = Notebook(left, width=notebookWidth)
grudger.pack()
grudgerTrad = Frame(grudger)
#grudgerForget = Frame(grudger)
grudger.add(grudgerTrad, text="Traditional")
#grudger.add(grudgerForget, text="Forgetful")
grudgerSlider = strategySlider(master=grudgerTrad, label='Grudger')
grudgerSlider.pack()
grudgeInfoButton = infoButton(master=grudgerTrad, command=lambda: info_window(
"Grudger \n\n This strategy begins by cooperating but will defect forever if it is defected against. \n"))
grudgeInfoButton.pack()
# Forgetful Grudger
#grudgerForgetSlider = strategySlider(
# master=grudgerForget, label='Forgetful Grudger')
#grudgerForgetSlider.pack()
# Forgetful memory
#forgetMemoryLabel = Label(grudgerForget, text="Previous Rounds Remembered")
#forgetMemoryLabel.pack()
#grudgerForgetMemoryEntry = Spinbox(grudgerForget, from_=0, to=100, width=4)
# TODO Connect to memory value
#grudgerForgetMemoryEntry.pack()
# TFT PART DEUX
theLabel2 = Label(right, text="Tit-For-Tat")
theLabel2.pack()
tft2 = Notebook(right, width=notebookWidth)
tft2.pack()
tftSlow = Frame(tft2)
tftSneaky = Frame(tft2)
tftForgive = Frame(tft2)
#tftGenerous = Frame(tft2)
tft2.add(tftSlow, text='Slow')
tft2.add(tftSneaky, text='Sneaky')
tft2.add(tftForgive, text='Forgiving')
#tft2.add(tftGenerous, text='Generous')
tftSlowSlider = strategySlider(master=tftSlow, label="Slow")
tftSlowSlider.pack()
tftSneakySlider = strategySlider(master=tftSneaky, label="Sneaky")
tftSneakySlider.pack()
tftForgiveSlider = strategySlider(master=tftForgive, label="Forgiving")
tftForgiveSlider.pack()
#tftGenerousSlider = strategySlider(master=tftGenerous, label="Generous")
#tftGenerousSlider.pack()
slowInfoButton = infoButton(master=tftSlow, command=lambda: info_window(
"Slow Tit-For-Tat \n\n This strategy cooperates and only defects when the opponent's last two moves were defections. It then enters a cool-down mode where it cooperates for one round, then begins the cycle again. \n"))
slowInfoButton.pack()
sneakyInfoButton = infoButton(master=tftSneaky, command=lambda: info_window(
"Sneaky Tit-For-Tat \n\n This strategy plays traditional Tit-For-Tat but will defect at random intervals. If the defection was retaliated against, this bot will cooperate for a round to make amends. \n"))
sneakyInfoButton.pack()
forgiveInfoButton = infoButton(master=tftForgive, command=lambda: info_window(
"Forgiving Tit-For-Tat t\n\n This strategy will play Tit-For-Tat against any opponent that has defected more than 10% of the time, and will otherwise cooperate. \n"))
forgiveInfoButton.pack()
# Always Bots
alwaysLabel = Label(right, text="Always")
alwaysLabel.pack()
always = Notebook(right, width=notebookWidth)
always.pack()
allDefect = Frame(always)
allCooperate = Frame(always)
always.add(allDefect, text='Defect')
always.add(allCooperate, text='Cooperate')
allDefectSlider = strategySlider(master=allDefect, label='Defect')
allDefectSlider.pack()
allCooperateSlider = strategySlider(master=allCooperate, label='Cooperate')
allCooperateSlider.pack()
defectInfoButton = infoButton(master=allDefect, command=lambda: info_window(
"Defect \n\n This strategy only defects. \n"))
defectInfoButton.pack()
cooperateInfoButton = infoButton(master=allCooperate, command=lambda: info_window(
"Cooperate \n\n This strategy only cooperates. \n"))
cooperateInfoButton.pack()
# randomBot
randomLabel = Label(right, text="Random")
randomLabel.pack()
rand = Notebook(right, width=notebookWidth)
rand.pack()
Rand = Frame(rand)
rand.add(Rand, text = "Random")
randomSlider = strategySlider(master=Rand, label='Random')
randomSlider.pack()
ranInfoButton = infoButton(master=Rand, command=lambda: info_window(
"Random \n\n This strategy defects and cooperates randomly. \n"))
ranInfoButton.pack()
# gradualBot
gradualLabel = Label(right, text="Gradual")
gradualLabel.pack()
gradual = Notebook(right, width=notebookWidth)
gradual.pack()
Gradual = Frame(gradual)
gradual.add(Gradual, text = "Gradual")
gradualSlider = strategySlider(master=Gradual, label='Gradual')
gradualSlider.pack()
gradInfoButton = infoButton(master=Gradual, command=lambda: info_window(
"Gradual \n\n This strategy tracks the number of times its opponent has defected against them, and defects that number of times when defected against. Following a defection, it cooperates twice in a row. \n"))
gradInfoButton.pack()
# proberBot
proberLabel = Label(right, text="Prober")
proberLabel.pack()
prober = Notebook(right, width=notebookWidth)
prober.pack()
Prober = Frame(prober)
prober.add(Prober, text = "Prober")
proberSlider = strategySlider(master=Prober, label='Prober')
proberSlider.pack()
proInfoButton = infoButton(master=Prober, command=lambda: info_window(
"Prober\n\n This strategy plays DCC, then defects continuously if it was not defected against in the first three rounds. If it was defected against in the first three rounds, it plays Tit-For-Tat \n"))
proInfoButton.pack()
# Limiting Sliders (Only 5 strategies at a time)
sliderList = ['Always Cooperate', 'Always Defect', 'Tit-For-Tat',
'Traditional Grudger', 'Random', 'Soft Majority',
'Hard Majority', 'DDC', 'CCD',
'CD', 'Mean TFT', 'Pavlov',
'Cooperative TFT', 'Hard TFT', 'Slow TFT',
'Gradual', 'Prober', 'Sneaky TFT',
'Forgetful Grudger', 'Forgiving TFT', 'Generous TFT']
activeSliders = []
valueArray = []
sliderCount = 0
def limitStrategies(val):
valueArray.append(val)
for v in valueArray :
if (v != 0) :
sliderCount += 1
if (sliderCount >= 5) :
if (v == 0) :
print ("This is what happens when v == 0:")
# Title
theLabel = Label(
root, text="\n\nWelcome to the Prisoner's Dilemma! \n",
font="Arial 24 bold")
theLabel.pack(anchor=CENTER)
theLabel = Label(
root, text= "Select the number of bots for each strategy you would like to simulate. \n"
+ "Once you have selected your strategies, \n"
+ "click the 'Run Simulation' button to view the results.",
font="Arial 18")
theLabel.pack(anchor=CENTER)
# Logo
canvas = Canvas(root, width = 300, height = 400)
canvas.pack()
img = PhotoImage(file="prisoner.png")
canvas.create_image(150,70, anchor=N, image=img)
# Run Simulation, Select Rounds, Noise Sliders/Buttons
rounds = Notebook(root, width=notebookWidth)
rounds.pack()
roundFrame = Frame(rounds)
noiseFrame = Frame(rounds)
roundsEntry = Spinbox(master=roundFrame, from_=1, to=10, width=4)
roundsEntry.pack()
roundsLabel = Label(master=roundFrame, text="Rounds to Play (Max 10):")
roundsLabel.pack()
runButton = Button(master=roundFrame, text="Run Simulation", highlightbackground="blue", highlightcolor="blue", font="Arial 20 bold",
command=lambda: tournament.runTournament()) # add command
runButton.pack()
rounds.add(roundFrame, text='Run Simulation')
rounds.add(noiseFrame, text='Noise')
noiseSlider = strategySlider(master=noiseFrame, label="Noise")
noiseSlider.pack()
noiseInfoButton = infoButton(master=noiseFrame, command=lambda: info_window(
"Noise: Noise represents miscommunications that happen in everyday life. For the set noise level, the simulator will disregard the bot's input and randomly defect or cooperate.\n"))
noiseInfoButton.pack()
root.mainloop()
# to keep GUI window open