forked from aferron/AIChessPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
219 lines (201 loc) · 8.41 KB
/
main.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
from aichess import AIChess, Results
from chessplayer import ChessPlayer
from dataclasses import dataclass
from enum import Enum
from heuristics import Heuristic
import matplotlib.pyplot as plt
import numpy as np
from minimaxchessplayer import MinimaxPlayer
from randomchessplayer import RandomChessPlayer
import time
from typing import Any, List
@dataclass
class ResultsPerMatchup:
results_per_matchup: List[Any]
label: str
class Main:
def run(self) -> None:
# self.__run_heuristics_by_depth_experiments()
# self.__run_heuristics_ablation_study()
# self.__run_minimax_with_heuristics_vs_random()
self.__compare_runtimes_of_basic_configs()
def __run_and_plot_one_experiment(self, iterations: int, baselines: List[ChessPlayer], testplayers: List[ChessPlayer], title_addition: str) -> None:
test_results: List[List[Results]] = AIChess(iterations=iterations, baselines=baselines, testplayers=testplayers).run()
print("test_results: ", test_results)
for i, results_per_baseline in enumerate(test_results):
baseline = baselines[i]
print ("results_per_baseline:", results_per_baseline)
test_player_wins, baseline_wins, draws, process_time_player1, process_time_player2, moves_per_game = [], [], [], [], [], []
for results in results_per_baseline:
print("results: ", results)
self.__append_as_percent(results_by_type=test_player_wins, percent=results.percent_wins_player1)
self.__append_as_percent(results_by_type=baseline_wins, percent=results.percent_wins_player2)
self.__append_as_percent(results_by_type=draws, percent=results.percent_draws)
process_time_player1.append(results.average_decision_time_player1)
process_time_player2.append(results.average_decision_time_player2)
win_results = [
ResultsPerMatchup(test_player_wins, "Test Player Wins"),
ResultsPerMatchup(baseline_wins, "Baseline Wins"),
ResultsPerMatchup(draws, "Draws")
]
time_results = [
ResultsPerMatchup([round(time, 4) for time in process_time_player1], "Test Player"),
ResultsPerMatchup([round(time, 4) for time in process_time_player2], "Baseline Player")
]
self.__plot_results(
graph_type = 'wins',
baseline=baseline,
testplayers=testplayers,
iterations=iterations,
matchupresults=win_results,
ylabel='Percent Wins or Draws',
title_addition=title_addition
)
self.__plot_results(
graph_type = 'processing-time',
baseline=baseline,
testplayers=testplayers,
iterations=iterations,
matchupresults=time_results,
ylabel='Average Processing Time Per Move in Seconds',
title_addition=title_addition
)
def __append_as_percent(self, results_by_type: np.array(float), percent: float) -> None:
results_by_type.append(np.round(percent, 2) * 100)
def __plot_results(
self,
graph_type: str,
baseline: ChessPlayer,
testplayers: List[ChessPlayer],
iterations: int,
matchupresults: List[ResultsPerMatchup],
ylabel: str,
title_addition: str
) -> None:
title_of_saved_file = title_addition + 'Baseline-' + baseline.get_name()+ ' ' + str(iterations) + \
' iterations per run'
title_of_graph = title_addition + '\n' + ' Baseline: ' + baseline.get_name()+ '\n' + str(iterations) + \
' iterations per run'
labels = [player.get_name() for player in testplayers]
x = np.arange(len(labels))
width = .15
r1 = np.arange(len(labels))
r2 = [x + width for x in r1]
r3 = [x + width for x in r2]
x_locations = [r1, r2, r3]
fig, ax = plt.subplots(figsize=((len(labels)*1.85),6))
rects = [ax.bar(
x_locations[i],
matchupresults[i].results_per_matchup,
width,
label=matchupresults[i].label
) for i in range(len(matchupresults))]
ax.set_ylabel(ylabel)
ax.set_xlabel('Player Type')
ax.set_title(title_of_graph)
ax.set_xticks(x, labels)
ax.legend(bbox_to_anchor=(.2,1.2,0,0))
for rect in rects:
ax.bar_label(rect, padding=3)
for rect in rects:
ax.bar_label(rect, padding=3)
fig.tight_layout()
plt.savefig('charts/' + title_of_saved_file + "-" + graph_type)
plt.show()
def __run_heuristics_by_depth_experiments(self) -> None:
title = "Heuristics by Depth"
num_iterations = 50
depth_iterations = [1, 2, 3]
baselines = [MinimaxPlayer(
time=time,
depth=depth,
heuristics=[Heuristic.Distance_From_Starting_Location, Heuristic.Maximize_Number_Of_Pieces],
run_alpha_beta=False) \
for depth in depth_iterations]
testplayers = [
MinimaxPlayer(
time=time,
depth=depth,
heuristics=[
Heuristic.Piece_Could_Be_Captured,
Heuristic.Distance_From_Starting_Location,
Heuristic.Keep_Pawns_Diagonally_Supported,
Heuristic.Stacked_Pawns,
Heuristic.Maximize_Number_Of_Pieces
],
run_alpha_beta=False
) for depth in depth_iterations]
self.__run_and_plot_one_experiment(iterations=num_iterations, baselines=baselines, testplayers=testplayers, title_addition=title)
def __run_heuristics_ablation_study(self) -> None:
title = 'Heuristic Ablation Study'
num_iterations = 100
depth = 4
all_heuristics = list(Heuristic)
baselines = [MinimaxPlayer(
time=time,
depth=depth,
heuristics=[],
run_alpha_beta=True
), MinimaxPlayer(
time=time,
depth=depth,
heuristics=all_heuristics,
run_alpha_beta=True
)]
testplayers = [MinimaxPlayer(
time=time,
depth=depth,
heuristics=[heuristic],
run_alpha_beta=True
) for heuristic in all_heuristics] + \
[MinimaxPlayer(
time=time,
depth=depth,
heuristics=[heuristic for heuristic in all_heuristics if all_heuristics.index(heuristic) != index],
run_alpha_beta=True
) for index in range(len(all_heuristics))]
self.__run_and_plot_one_experiment(iterations=num_iterations, baselines=baselines, testplayers=testplayers, title_addition=title)
def __run_minimax_with_heuristics_vs_random(self) -> None:
title = 'Minimax with Heuristics vs Random Player'
num_iterations = 100
baselines = [RandomChessPlayer(time=time)]
testplayers = [MinimaxPlayer(
time=time,
depth=depth,
heuristics=list(Heuristic),
run_alpha_beta=True
) for depth in range(4)]
self.__run_and_plot_one_experiment(iterations=num_iterations, baselines=baselines, testplayers=testplayers, title_addition=title)
def __compare_runtimes_of_basic_configs(self) -> None:
title = 'Runtime of Basic Configurations'
depth = 3
baselines = [RandomChessPlayer(time=time)]
testplayers = [
RandomChessPlayer(time=time),
MinimaxPlayer(
time=time,
depth=depth,
heuristics=[],
run_alpha_beta=False
),
MinimaxPlayer(
time=time,
depth=depth,
heuristics=[],
run_alpha_beta=True
),
MinimaxPlayer(
time=time,
depth=depth,
heuristics=list(Heuristic),
run_alpha_beta=False
),
MinimaxPlayer(
time=time,
depth=depth,
heuristics=list(Heuristic),
run_alpha_beta=True
)
]
self.__run_and_plot_one_experiment(iterations=100, baselines=baselines, testplayers=testplayers, title_addition=title)
Main().run()