-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoup.py
More file actions
80 lines (66 loc) · 2.51 KB
/
coup.py
File metadata and controls
80 lines (66 loc) · 2.51 KB
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
from game import Game
from agents import QLearningAgent, MinimaxAgent, BogoAgent
import sys
import collections
import signal
from optparse import OptionParser
def signal_handler(signum, frame):
raise Exception("Timed out!")
def default(option_desc: str) -> str:
return f'{option_desc} [Default: %default]'
def readCommand(argv):
"""
Processes the command used to run Coup from the command line.
Currently supports the number of games to play.
TODO: Add more options to the command line, such as the number of
agents to play with, the agents to play with, etc.
"""
usage_str = ""
parser = OptionParser(usage_str)
parser.add_option('-n', '--numGames', dest='numGames', type='int',
help=default('the number of GAMES to play'), metavar='GAMES', default=1)
options, _ = parser.parse_args(argv)
return {'numGames': options.numGames}
def runMatchups(numGames: int):
"""
Runs the matchups between the different agents for a set number of games.
"""
signal.signal(signal.SIGALRM, signal_handler)
algorithms = ['qlearning', 'minimax', 'random']
algorithms_to_agents = {
'qlearning': QLearningAgent,
'minimax': MinimaxAgent,
'random': BogoAgent
}
agents_by_algorithm = {
idx: {algo: algorithms_to_agents[algo](idx) for algo in algorithms}
for idx in range(3)
}
for algorithm1 in algorithms:
for algorithm2 in algorithms:
for algorithm3 in algorithms:
agents = [agents_by_algorithm[0][algorithm1],
agents_by_algorithm[1][algorithm2],
agents_by_algorithm[2][algorithm3]]
signal.alarm(10)
scores_file_path = f'{algorithm1}-{algorithm2}-{algorithm3}.txt'
with open(scores_file_path, 'a') as scores:
runGames(numGames=numGames, agents=agents, scores=scores)
def runGames(numGames: int = 100, agents: list = [], scores=None):
"""
Runs the games for a set number of games with a set of agents.
Prints out the winning bot for each game and the final scores.
"""
numGames = 500
wins = collections.Counter()
for i in range(numGames):
game = Game(agents)
winner = game.run()
wins[winner] += 1
print('Winning Bot:', winner)
if scores:
scores.write(f'{winner}\n')
print('Final scores:', wins)
if __name__ == '__main__':
args = readCommand(sys.argv[1:])
runMatchups(args['numGames'])