-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_check.py
81 lines (60 loc) · 3.37 KB
/
time_check.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
#!/usr/bin/env python3
import time_info
test = time_info.TimeCheck()
hw02 = test.load('hw02')
from game import Agent
from ghostAgents import RandomGhost, DirectionalGhost
import sys
import pacman, time, layout, textDisplay
textDisplay.SLEEP_TIME = 0
textDisplay.DRAW_EVERY = 1000
thismodule = sys.modules[__name__]
TIME_LIMIT = 3000
def run(layname, pac, ghosts, nGames =300):
if test.fatalError:
return {'time': 65536, 'wins': 0, 'games': None, 'scores': [0]*nGames, 'timeouts': nGames}
lay = layout.getLayout(layname, 3)
disp = textDisplay.NullGraphics()
games = pacman.runGames(lay, pac, ghosts, disp, nGames, False, numTraining =0,catchExceptions=False)
stats = {'wins': [g.state.isWin() for g in games].count(True), 'games': games, 'scores': [g.state.getScore() for g in games], 'timeouts': [g.agentTimeout for g in games].count(True)}
return stats
def small_map(agentName):
stats = {}
if agentName == 'alphabeta':
stats = run('smallmap', hw02.AlphaBetaAgent(depth=2), [DirectionalGhost(i + 1) for i in range(2)])
elif agentName == 'minimax':
stats = run('smallmap', hw02.MinimaxAgent(depth=2), [DirectionalGhost(i + 1) for i in range(2)])
else:
stats = run('smallmap', hw02.ExpectimaxAgent(depth=2), [DirectionalGhost(i + 1) for i in range(2)])
if stats['timeouts'] > 0:
test.fail('Your ' + agentName + ' agent timed out on smallmap.')
return
def medium_map(agentName):
stats = {}
if agentName == 'alphabeta':
stats = run('mediummap', hw02.AlphaBetaAgent(depth=2), [DirectionalGhost(i + 1) for i in range(2)])
elif agentName == 'minimax':
stats = run('mediummap', hw02.MinimaxAgent(depth=2), [DirectionalGhost(i + 1) for i in range(2)])
else:
stats = run('mediummap', hw02.ExpectimaxAgent(depth=2), [DirectionalGhost(i + 1) for i in range(2)])
if stats['timeouts'] > 0:
test.fail('Your ' + agentName + ' agent timed out on mediummap.')
return
def minimax_map(agentName):
stats = {}
if agentName == 'alphabeta':
stats = run('minimaxmap', hw02.AlphaBetaAgent(depth=4), [DirectionalGhost(i + 1) for i in range(2)],1000)
elif agentName == 'minimax':
stats = run('minimaxmap', hw02.MinimaxAgent(depth=4), [DirectionalGhost(i + 1) for i in range(2)],1000)
else:
stats = run('minimaxmap', hw02.ExpectimaxAgent(depth=4), [DirectionalGhost(i + 1) for i in range(2)],1000)
if stats['timeouts'] > 0:
test.fail('Your ' + agentName + ' agent timed out on minimaxmap.')
return
test.addTest('MiniMax (depth=2) For Small Map', lambda : small_map('minimax'), maxSeconds=TIME_LIMIT, description='MiniMax for timeout on smallMap.')
test.addTest('AlphaBeta (depth=2) For Small Map', lambda : small_map('alphabeta'), maxSeconds=TIME_LIMIT, description='AlphaBeta for timeout on smallMap.')
test.addTest('MiniMax (depth=2) For Medium Map', lambda : medium_map('minimax'), maxSeconds=TIME_LIMIT, description='MiniMax for timeout on mediumMap.')
test.addTest('AlphaBeta (depth=2) For Medium Map', lambda : medium_map('alphabeta'), maxSeconds=TIME_LIMIT, description='AlphaBeta for timeout on mediumMap.')
test.addTest('MiniMax (depth=4) For Minimax Map', lambda : minimax_map('minimax'), maxSeconds=TIME_LIMIT, description='MiniMax for timeout on minimaxMap.')
test.addTest('AlphaBeta (depth=4) For Minimax Map', lambda : minimax_map('alphabeta'), maxSeconds=TIME_LIMIT, description='AlphaBeta for timeout on minimaxMap.')
test.start()