/
heuristic.py
228 lines (207 loc) · 6.56 KB
/
heuristic.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
219
220
221
222
223
224
225
226
227
228
import numpy as np
import regex as re
UTILITY = {'Q': [20000000, ['xxxxx']],
'Q2': [400000, ['exxxxe']],
'Cap_ok':[500000, ['xnnxe', 'exnnx', 'nxnnx', 'xnnxn']],
'Cap':[200000, ['xnnee', 'eennx', 'eennx', 'xnnee', 'xnnen', 'nennx']],
'Q1': [50000, ['nxxxxe', 'exxxxn']],
'T2': [30000, ['exxxe']],
'T1': [15000, ['nxxxee', 'eexxxn']],
'PQ2': [7000, ['exexxe', 'exxexe']],
'PQ1': [3000, ['nxexxe','nxxexe', 'exxexn', 'exexxn']],
'D2': [500, ['eexxe', 'exxee']],
'D1': [400, ['nxxeee', 'eeexxn']],
'PT2': [100, ['exexe']],
'PT1': [40, ['nxexee', 'eexexn']]}
FINISHED = {'Q': [200000, ['xxxxx']]}
HEURISTIC = [[0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0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, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0, 0]]
def not2T(move, mapping, player=1):
map2 = mapping.copy()
count = 0
map2[move[0]][move[1]] = player
count += search(makeDig(map2, player), 'exxxe')
count += search(makeCol(map2, player), 'exxxe')
count += search(makeLin(map2, player), 'exxxe')
count += search(makeDig(map2, player), 'exxexe')
count += search(makeCol(map2, player), 'exxexe')
count += search(makeLin(map2, player), 'exxexe')
count += search(makeDig(map2, player), 'exexxe')
count += search(makeCol(map2, player), 'exexxe')
count += search(makeLin(map2, player), 'exexxe')
if count > 1:
return False
else:
return True
def winner(mapping, player, forwin='xxxxx'):
map2 = mapping.copy()
count = 0
count += search(makeDig(map2, player), forwin)
count += search(makeCol(map2, player), forwin)
count += search(makeLin(map2, player), forwin)
if count > 0:
return True
else:
return False
def calculH(mapping,player, val_H = UTILITY, pos_val_H = HEURISTIC):
seq_H = 0
pos_H = 0
map2 = mapping.copy()
for values in val_H.keys():
val_seq = val_H[values][0]
count = 0
sequence = val_H[values][1]
for seq in sequence:
count += search(makeDig(map2, player), seq)
count += search(makeCol(map2, player), seq)
count += search(makeLin(map2, player), seq)
seq_H += count* val_seq
# print(seq_H)
map2 = mapping.copy()
if(player == 1):
np.place(map2, map2 == -1, 0)
pos_H = np.sum(np.multiply(map2, pos_val_H))
else:
np.place(map2, map2 == 1, 0)
pos_H = -np.sum(np.multiply(map2, pos_val_H))
#print("pos_h: ", pos_H)
heuristic_val = pos_H + seq_H
# print(heuristic_val)
total = heuristic_val
player = -1*player
seq_H = 0
pos_H = 0
map2 = mapping.copy()
for values in val_H.keys():
val_seq = val_H[values][0]
count = 0
sequence = val_H[values][1]
for seq in sequence:
count += search(makeDig(map2, player), seq)
count += search(makeCol(map2, player), seq)
count += search(makeLin(map2, player), seq)
seq_H += count * val_seq
# print(seq_H)
map2 = mapping.copy()
if(player == 1):
np.place(map2, map2 == -1, 0)
pos_H = np.sum(np.multiply(map2, pos_val_H))
else:
# print("ok")
np.place(map2, map2 == 1, 0)
pos_H = -np.sum(np.multiply(map2, pos_val_H))
# print("pos_h: ", pos_H)
heuristic_val = pos_H + seq_H
# print(heuristic_val)
# print(total -1.05 * heuristic_val)
return total - 1.05 * heuristic_val
def makeDig(mapping, player):
dig = [mapping[::-1, :].diagonal(i) for i in range(-mapping.shape[1] + 5, mapping.shape[1] - 4)]
diagonal = []
for i in dig:
str1 = ''
for e in i:
if player == 1:
if e == 0:
str1 += 'e'
elif (e == -1 or e == 2):
str1 += 'n'
elif(e == 1):
str1 += 'x'
else:
if e == 0:
str1 += 'e'
elif (e == 1 or e == 2):
str1 += 'n'
elif(e == -1):
str1 += 'x'
diagonal.append(str1)
dig = [mapping.diagonal(i) for i in range(mapping.shape[1]-5, -mapping.shape[1] + 4, -1)]
for i in dig:
str1 = ''
for e in i:
if player == 1:
if e == 0:
str1 += 'e'
elif (e == -1 or e == 2):
str1 += 'n'
elif(e == 1):
str1 += 'x'
else:
if e == 0:
str1 += 'e'
elif (e == 1 or e == 2):
str1 += 'n'
elif(e == -1):
str1 += 'x'
diagonal.append(str1)
return diagonal
def makeLin(mapping, player):
line = []
for i in mapping:
str1 = ''
for e in i:
if player == 1:
if e == 0:
str1 += 'e'
elif (e == -1 or e == 2):
str1 += 'n'
elif(e == 1):
str1 += 'x'
else:
if e == 0:
str1 += 'e'
elif (e == 1 or e == 2):
str1 += 'n'
elif(e == -1):
str1 += 'x'
line.append(str1)
return line
def makeCol(mapping, player):
col = []
matrix = mapping.copy().T
for i in matrix:
str1 = ''
for e in i:
if player == 1:
if e == 0:
str1 += 'e'
elif (e == -1 or e == 2):
str1 += 'n'
elif(e == 1):
str1 += 'x'
else:
if e == 0:
str1 += 'e'
elif (e == 1 or e == 2):
str1 += 'n'
elif(e == -1):
str1 += 'x'
col.append(str1)
return col
def search(forcheck, pattern):
count = 0
for check in forcheck:
count += counteur(check, pattern)
return count
def counteur(check, pattern):
# print("counteur: ", check)
# print("pattern: ", pattern)
return len(re.findall(pattern, check, overlapped=True))