-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtesting.py
150 lines (102 loc) · 3.31 KB
/
testing.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
import minimax
import GameState
import math
import time
from importlib import reload
import numpy as np
from matplotlib import pyplot as plt
from ete3 import Tree
#%%
reload(plt)
#%%
def countExpandedNodes(root: Tree):
if root.is_leaf():
return 0
else:
totalExpandedChildren = 0
for child in root.children:
totalExpandedChildren += countExpandedNodes(child)
return 1 + totalExpandedChildren
# %%
initial_state_string = '000000000000000000000000000000000000000000'
initial_state = GameState.GameState(initial_state_string, GameState.AI_PLAYER, None)
action_sequence = [3]
repeats = 1
k_minimax = [1, 2, 3, 4, 5, 6]
k_alphabeta = [1, 2, 3, 4, 5, 6, 7]
#%%
data_points_minimax = []
tree_points_noab = []
state = initial_state
for k in k_minimax:
avgDuration = 0
for i in range(repeats):
duration = 0
for action in action_sequence:
state = initial_state.makeMove(action)
startTime = time.time()
action, tree = minimax.decisionMinimax(state, k)
endTime = time.time()
duration += (endTime - startTime)
avgDuration += (duration / len(action_sequence))
tree_points_noab.append(countExpandedNodes(tree))
data_points_minimax.append(avgDuration/repeats)
#%%
duration = 0
for action in [2,3]:
state = initial_state.makeMove(action)
startTime = time.time()
action = minimax.decisionMinimax(state, 7)
endTime = time.time()
duration += (endTime - startTime)
avg = duration/2
print(avg)
#%%
data_points_ab = []
state = initial_state
tree_points_ab = []
for k in k_alphabeta:
avgDuration = 0
for i in range(repeats):
duration = 0
for action in action_sequence:
state = initial_state.makeMove(action)
startTime = time.time()
action, tree = minimax.decisionAlphaBeta(state, -math.inf, +math.inf, k)
endTime = time.time()
duration += (endTime - startTime)
avgDuration += (duration / len(action_sequence))
print(f'Depth {k} done')
tree_points_ab.append(countExpandedNodes(tree))
data_points_ab.append(avgDuration/repeats)
print('Done')
#%%
SMALL_SIZE = 26
MEDIUM_SIZE = 30
BIGGER_SIZE = 40
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=BIGGER_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
plt.style.use('fivethirtyeight')
#%%
figure = plt.figure(figsize=(16,10))
plt.plot(k_minimax, data_points_minimax, label='No Pruning')
plt.plot(k_alphabeta, data_points_ab, label='With Pruning')
plt.xlabel("Maximum Depth")
plt.ylabel("Time (in seconds)")
plt.xticks(k_alphabeta)
plt.legend()
plt.show()
#%%
figure2 = plt.figure(figsize=(16,10))
plt.plot(k_minimax, tree_points_noab, label='No Pruning')
plt.plot(k_alphabeta, tree_points_ab, label='With Pruning')
plt.xlabel("Maximum Depth")
plt.ylabel("Number of expanded nodes")
plt.xticks(k_alphabeta)
plt.legend()
plt.show()