-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtournament.py
144 lines (121 loc) · 4.81 KB
/
tournament.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
import pickle
import numpy as np
from argparse import ArgumentParser
from engine.state.main import State
from game import Game
from util import constants, helpers
"""
Running this file in the command line will start the game.
Some additional arguments are provided for easier execution through the command line.
Arguments:
-r => repreat : int
-p => which bot to play (Entropy, Base, Random)
-url => to choose which dataset to use ( should be running on local server)
"""
class Tournament:
"""
The Class managing tournaments and the information gathered with these tournaments.
Attributes
----------
botName : str
The name of the questioner bot used.
repeat : int
The amount of times a game is played (default=10).
questionLimit : int
The amount of questions in which the bot has to guess the target entity.
Methods
-------
run()
Runs the amount of games specified by the repeat variable and reports the results of those games.
saveStats(toFile, short)
Saves the stats from the tournament to a file.
"""
def __init__(self, botName, repeat=10, questionLimit=constants.QUESTIONS_LIMIT):
self.botName = botName
self.repeat = repeat
self.questionLimit = questionLimit
self.stats = {
'bot': botName,
'games': {},
'questionLimit': questionLimit,
'repeat': repeat,
}
def run(self):
"""
Runs the amount of games specified by the repeat variable and reports the results of those games.
Parameters -> None
Returns -> None
"""
questionsAsked = np.array([])
winners = np.array([])
for i in range(self.repeat):
print(f'Playing game #{i+1}')
if self.botName == 'Entropy':
state = State(initializeState=False)
else:
state = State()
game = Game(state=state, nQuestions=self.questionLimit, questioner=self.botName, againstHuman=False)
winner = game.run()
winners = np.append(winners, [winner])
questionsAsked = np.append(questionsAsked, [state.questionsAsked])
self.stats['games'][str(i)] = {
"won": winner,
# "questioner": questioner.getStats(),
"questionsAsked": state.questionsAsked,
"yesAnswers": len(state.yesHints),
"noAnswers": len(state.noHints),
"yesHints": state.yesHints,
"noHints": state.noHints,
}
wonByBot = winners[np.where(winners == 1)].size
bestGame = np.min(questionsAsked)
self.stats['std'] = np.std(questionsAsked)
self.stats['nrQuestionsBestGame'] = round(bestGame)
print(f"\n \
The {self.botName} bot has won {wonByBot} games. \n \
Average number of asked questions {round(np.mean(questionsAsked))} out of {self.questionLimit}.\n \
Std: {np.std(questionsAsked)}. \n \
Number of asked questions in the best game {round(bestGame)} out of {self.questionLimit} \n ")
self.saveStats(toFile=True)
def saveStats(self, toFile=False, short=True):
"""
Saves the stats from the tournament to a file.
Parameters
----------
toFile : Boolean
Determines whether the user wants the results of the tournament saved to a file or not (default=False).
short : Boolean
Determines whether the stats should be printed or not (default=True).
Returns -> None
"""
if toFile:
oldData = helpers.readPickleBack('.\\20-questions\\tournament_output.pkl')
with open('.\\20-questions\\tournament_output.pkl', 'wb') as file:
pickle.dump(oldData, file)
pickle.dump(self.stats, file)
else:
if short:
return
else:
print(self.stats)
parser = ArgumentParser()
parser.add_argument("-r", "--repeat",
dest="repeat",
help="Number of games to play (default: 10)",
type=int,
default=10)
parser.add_argument("-p", "--player",
dest="player",
help="The bot to play against the answerer (default: Base)",
default='Base')
parser.add_argument("-url", "--repository-url",
dest="url",
help="URL of the repository to be used. (default: True.)",
default="http://127.0.0.1:7200/repositories/top2021")
options = parser.parse_args()
repeat = options.repeat
player = options.player
constants.URL = options.url
if __name__ == '__main__':
tournament = Tournament(player, repeat)
tournament.run()