-
Notifications
You must be signed in to change notification settings - Fork 0
/
ABOpening.py
167 lines (136 loc) · 4.51 KB
/
ABOpening.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
# Author: Justine George - JXG210092 - CS 6364 - 0U2 - Su23
# Part II.a Alpha-Beta Opening
import sys
from Utils import (
closeMill,
drawBoard,
generateMovesOpening,
getBlackMovesOpening,
getBlackPieceCount,
getMaxDepth,
getMillCount,
getMobilityCount,
getPositionScore,
getScoreForMetrics,
getWhitePieceCount,
)
# global variables
countStaticEstimate = 0
maxDepth = getMaxDepth()
# Max min
def maxMin(b, alpha, beta, currentDepth):
# if leaf node
if currentDepth == maxDepth:
return (getStaticEstimationOpening(b), b)
else:
v = float("-inf")
# get white moves
L = generateMovesOpening(b)
# for each position after a possible white move
bestB = ""
for y in L:
(estimate, _) = minMax(y, alpha, beta, currentDepth + 1)
if estimate > v:
v = estimate
bestB = y
if v >= beta:
return (v, bestB)
else:
alpha = max(v, alpha)
return (v, bestB)
# Min max
def minMax(b, alpha, beta, currentDepth):
# if leaf node
if currentDepth == maxDepth:
return (getStaticEstimationOpening(b), b)
else:
v = float("inf")
# get black moves
L = getBlackMovesOpening(b)
# for each position after a possible black move
bestB = ""
for y in L:
(estimate, _) = maxMin(y, alpha, beta, currentDepth + 1)
if estimate < v:
v = estimate
bestB = y
if v <= alpha:
return (v, bestB)
else:
beta = min(v, beta)
return (v, bestB)
# Static estimation for Opening
def getStaticEstimationOpening(b):
global countStaticEstimate
countStaticEstimate += 1
# pieceCount
whitePieceCount = getWhitePieceCount(b)
blackPieceCount = getBlackPieceCount(b)
# millCount
whiteMillCount = getMillCount(b, "W")
blackMillCount = getMillCount(b, "B")
# mobilityCount
whiteMobilityCount = getMobilityCount(b, "W")
blackMobilityCount = getMobilityCount(b, "B")
# positionscore
whiteScore = getPositionScore(b, "W")
blackScore = getPositionScore(b, "B")
# # other metrics
# otherScoreW = getScoreForMetrics(b, "W")
# otherScoreB = getScoreForMetrics(b, "B")
# Calculate delta values
delta_piece_count = (whitePieceCount - blackPieceCount) / 3
delta_mill_count = (whiteMillCount - blackMillCount) / 6
delta_mobility_count = (whiteMobilityCount - blackMobilityCount) / 15
delta_position_score = (whiteScore - blackScore) / 12
# delta_other_score = (otherScoreW - otherScoreB) / 400
# Save delta values to files
with open("opening/delta_piece_count.txt", "a") as f:
f.write(str(delta_piece_count) + ",")
with open("opening/delta_mill_count.txt", "a") as f:
f.write(str(delta_mill_count) + ",")
with open("opening/delta_mobility_count.txt", "a") as f:
f.write(str(delta_mobility_count) + ",")
with open("opening/delta_position_score.txt", "a") as f:
f.write(str(delta_position_score) + ",")
# with open("opening/delta_other_score.txt", "a") as f:
# f.write(str(delta_other_score) + ",")
return (
delta_piece_count
+ delta_mill_count
+ delta_mobility_count
+ delta_position_score
# + delta_other_score
)
# helper methods
# alpha-beta
def getMaxminEstimate(inputB, depth):
global maxDepth
maxDepth = depth
return maxMin(inputB, float("-inf"), float("inf"), 0)
# ##################################################################################
# # sys.argv[1:] contains command line arguments
# inputFile = sys.argv[1]
# outputFile = sys.argv[2]
# maxDepth = int(sys.argv[3])
# # read contents from the input file
# inputB = ""
# with open(inputFile, "r") as f:
# inputB = inputB + f.read()
# print("\nInput:")
# drawBoard(inputB)
# # calculate Minimax estimate
# (estimate, bestB) = getMaxminEstimate(inputB)
# print("\nOutput:")
# drawBoard(bestB)
# # write into the output file
# with open(outputFile, "w") as opFile:
# opFile.write("Board Position: " + bestB)
# opFile.write(
# "\nPositions evaluated by static estimation: " + str(countStaticEstimate) + "."
# )
# opFile.write("\nMINIMAX estimate: " + str(estimate) + ".")
# print(
# "\nPositions evaluated by static estimation: " + str(countStaticEstimate) + "."
# )
# print("MINIMAX estimate: " + str(estimate) + ".\n")