forked from rhdemo/2021-ai
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prediction.py
416 lines (351 loc) · 11.3 KB
/
prediction.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import numpy as np
from numpy.random import random, randint
BOARD_SIZE = 5
SHIP = 0
MISS = 1
HIT = 2
SHIPS_SIZE = [5, 4, 3, 2]
SHIPS = {'carrier': 5, 'battleship': 4, 'destroyer': 3, 'submarine': 2}
HITS_SKEW_PROBS = True
BOARD_STATE = [[-1 for x in range(5)] for x in range(5)]
boardProbabilities = [[0 for x in range(5)] for x in range(5)]
SKEW = 2
TRAINING_INFO = [[0.88607595, 0.93670886, 0.83544304, 0.93670886, 0.89873418],
[0.98734177, 0.96202532, 1.0, 0.98734177, 0.96202532],
[0.98734177, 0.93670886, 0.94936709, 0.93670886, 0.89873418],
[0.7721519, 0.69620253, 0.78481013, 0.83544304, 0.89873418],
[0.88607595, 0.94936709, 0.88607595, 0.98734177, 0.88607595]]
def isValidPosition(x, y, ship_size, vertical, obstacles):
if ship_size not in SHIPS_SIZE:
return True
if not vertical and y + ship_size > BOARD_SIZE:
return True
if vertical and x + ship_size > BOARD_SIZE:
return True
for j in range(ship_size):
index = getNextCell(x, y, j, vertical)
if index in obstacles:
return True
def getNextCell(x, y, offset, vertical):
if vertical:
x += offset
else:
y += offset
return [x,y] #(y * 5) + x
def getSurroundingPos(pos):
x = pos[0]
y = pos[1]
adj = []
if y + 1 < BOARD_SIZE:
adj.append([x, y + 1])
if y - 1 >= 0:
adj.append([x, y - 1])
if x + 1 < BOARD_SIZE:
adj.append([x + 1, y])
if x - 1 >= 0:
adj.append([x - 1, y])
return adj
def updateProbs(pos, shipSize, vertical, bProbs):
x = pos[0]
y = pos[1]
if vertical:
z = y
else:
z = x
end = z + shipSize - 1
for i in range(z, end+1):
if vertical:
bProbs[x][i] = bProbs[x][i] + 1
else:
bProbs[i][y] = bProbs[i][y] + 1
return bProbs
def canPlaceShip(positionMISSed, pos, shipSize, vertical, bState):
x = pos[0]
y = pos[1]
if vertical:
z = y
else:
z = x
end = z + shipSize - 1
if end > BOARD_SIZE - 1:
return False
for i in range(z, end+1):
if vertical:
thisPos = bState[x][i]
else:
thisPos = bState[i][y]
if thisPos == positionMISSed:
return False
return True
def isPositionUnPlayed(bState, x, y):
# if -1 then not played.
if bState[x][y] == -1:
return True
else:
return False
def getAttackPos3(bState, bProbs):
bestProb = 0
bestPos = []
for x in range(BOARD_SIZE):
for y in range(BOARD_SIZE):
if isPositionUnPlayed(bState, x, y) and bProbs[x][y] > bestProb:
bestProb = bProbs[x][y]
bestPos = [x, y]
mat = np.array(bState)
max_value = np.max(mat)
if max_value == -1:
value = randint(0, 5)
if value == 1:
bestPos = [2, 0]
elif value == 2:
bestPos = [4, 2]
elif value == 3:
bestPos = [0, 2]
elif value == 4:
bestPos = [2, 4]
else:
bestPos = [x, y]
return bestPos
def getAttackPos4(bState, bProbs):
bestProb = 0
bestPos = []
mat = np.array(bState)
max_value = np.max(mat)
#model v6
if max_value == -1:
training_info = np.array(TRAINING_INFO)
max_value_index = np.argmax(training_info, axis=1)
max_value_index = max_value_index.tolist()
bestPos = [max_value_index[0], max_value_index[1]]
return bestPos
for x in range(BOARD_SIZE):
for y in range(BOARD_SIZE):
if isPositionUnPlayed(bState, x, y) and bProbs[x][y] > bestProb:
bestProb = bProbs[x][y]
bestPos = [x, y]
return bestPos
def getAttackPos2(bState, bProbs):
bestProb = 0
bestPos = []
for x in range(BOARD_SIZE):
for y in range(BOARD_SIZE):
if isPositionUnPlayed(bState, x, y) and bProbs[x][y] > bestProb:
bestProb = bProbs[x][y]
bestPos = [x, y]
mat = np.array(bState)
max_value = np.max(mat)
if max_value == -1:
value = randint(0, 5)
if value == 1:
bestPos = [2, 0]
elif value == 2:
bestPos = [4, 2]
elif value == 3:
bestPos = [0, 2]
elif value == 4:
bestPos = [2, 4]
else:
bestPos = [x, y]
return bestPos
def getAttackPos1(bState, bProbs):
bestProb = 0
bestPos = []
for x in range(BOARD_SIZE):
for y in range(BOARD_SIZE):
if isPositionUnPlayed(bState, x, y) and bProbs[x][y] > bestProb:
bestProb = bProbs[x][y]
bestPos = [x, y]
mat = np.array(bState)
return bestPos
def skewProbabilityAroundHits(toSkew, probs):
uniques = []
for i in range(len(toSkew)):
toSkew += getSurroundingPos(toSkew[i])
for i in range(len(toSkew)):
uniques.append(' '.join([str(c) for c in toSkew[i]]))
unique_numbers = list(set(uniques))
for i in range(len(unique_numbers)):
toSkew_p = unique_numbers[i].split(' ')
toSkew_p = [int(k1) for k1 in toSkew_p]
x = toSkew_p[0]
y = toSkew_p[1]
probs[x][y] *= SKEW
return probs
def getProbs2(bState, ship_locs):
bProbs = [[0 for x in range(5)] for x in range(5)]
remaining_ships_list = get_unsunkShips(ship_locs)
sunk_cells = get_sunkCells(ship_locs)
obstacle_cells = get_obstacles(bState, sunk_cells)
hit_cells = get_hitCells(bState, sunk_cells)
for i in range(0, len(remaining_ships_list)):
for y in range(BOARD_SIZE):
for x in range(BOARD_SIZE):
for direction in [True, False]:
ship_size = SHIPS[remaining_ships_list[i]]
if not isValidPosition(x, y, ship_size, direction, obstacle_cells):
hit_seen = 0
for j in range(ship_size):
pos = getNextCell(x, y, j, direction)
bProbs[pos[0]][pos[1]] += 1
if pos in hit_cells:
hit_seen += 1
if hit_seen:
for j in range(ship_size):
pos = getNextCell(x, y, j, direction)
bProbs[pos[0]][pos[1]] += 5 * hit_seen
for p in hit_cells:
bProbs[p[0]][p[1]] = 0
return bProbs
def getProbs1(bState):
hits = []
bProbs = [[0 for x in range(5)] for x in range(5)]
for y in range(BOARD_SIZE):
for x in range(BOARD_SIZE):
if HITS_SKEW_PROBS and bState[x][y] == HIT:
hits.append([x, y])
for i in range(0, len(SHIPS_SIZE)):
for y in range(BOARD_SIZE):
for x in range(BOARD_SIZE):
#H
if canPlaceShip(MISS, [x, y], SHIPS_SIZE[i], False, bState):
bProbs = updateProbs([x, y], SHIPS_SIZE[i], False, bProbs)
#V
if canPlaceShip(MISS, [x, y], SHIPS_SIZE[i], True, bState):
bProbs = updateProbs([x, y], SHIPS_SIZE[i], True, bProbs)
if HITS_SKEW_PROBS:
bProbs = skewProbabilityAroundHits(hits, bProbs)
return bProbs
def get_obstacles(bState, sunk_cells):
cells = []
for y in range(BOARD_SIZE):
for x in range(BOARD_SIZE):
if [x,y] in sunk_cells or bState[x][y] == MISS:
cells.append([x,y])
return cells
def get_hitCells(bState, sunk_cells):
cells = []
for y in range(BOARD_SIZE):
for x in range(BOARD_SIZE):
if [x,y] not in sunk_cells and bState[x][y] == HIT:
cells.append([x,y])
return cells
def get_sunkCells(ship_loc):
cells = []
for key in ship_loc:
for v in ship_loc[key]:
cells.append(v)
return cells
def get_unsunkShips(ship_loc):
unsunk_ships = []
if ship_loc:
for key in ship_loc:
for x in SHIPS.keys():
if x != key:
unsunk_ships.append(x)
else:
unsunk_ships = list(SHIPS.keys())
return unsunk_ships
def swapxy(l):
fl = []
for x in l:
fl.append([x[1], x[0]])
return fl
def get_sunkShips(bShips):
ship_loc = {}
for x in bShips:
stype = ''
loc = []
for key in x:
if key == "type":
stype = x[key]
if key == "cells":
loc = swapxy(x[key])
ship_loc[stype.lower()] = loc
return ship_loc
#doesn't use ship locations
def predict1(data):
#print(bState)
bState = data['board_state']
mat = np.array(bState)
mat = mat.transpose()
bState = mat.tolist()
print(bState)
newProbs = getProbs1(bState)
pos = getAttackPos1(bState, newProbs)
x = pos[0]
y = pos[1]
res = {"x": y, "y": x, "prob": newProbs}
print(res)
return res
#uses ship locations
#attacks center
def predict2(data):
#print(bState)
bState = data['board_state']
bShips = data['ship_types']
ship_loc = get_sunkShips(bShips)
mat = np.array(bState)
mat = mat.transpose()
bState = mat.tolist()
print(bState)
newProbs = getProbs2(bState, ship_loc)
pos = getAttackPos1(bState, newProbs)
x = pos[0]
y = pos[1]
res = {"x": y, "y": x, "prob": newProbs}
print(res)
return res
#uses ship locations
#attacks center + random
def predict3(data):
#print(bState)
bState = data['board_state']
bShips = data['ship_types']
ship_loc = get_sunkShips(bShips)
mat = np.array(bState)
mat = mat.transpose()
bState = mat.tolist()
print(bState)
newProbs = getProbs2(bState, ship_loc)
pos = getAttackPos2(bState, newProbs)
x = pos[0]
y = pos[1]
res = {"x": y, "y": x, "prob": newProbs}
print(res)
return res
#uses ship locations
#attacks based on training info then center
def predict(data):
#print(bState)
bState = data['board_state']
bShips = data['ship_types']
ship_loc = get_sunkShips(bShips)
mat = np.array(bState)
mat = mat.transpose()
bState = mat.tolist()
print(bState)
newProbs = getProbs2(bState, ship_loc)
pos = getAttackPos4(bState, newProbs)
x = pos[0]
y = pos[1]
res = {"x": y, "y": x, "prob": newProbs}
print(res)
return res
if __name__ == "__main__":
BOARD_STATE[2][2] = MISS
data1 = {
'board_state': BOARD_STATE
}
data = {'board_state': [[-1, 1, -1, -1, -1], [1, 2, -1, -1, -1], [-1, 2, 1, -1, -1], [-1, -1, -1, 1, -1],
[-1, -1, -1, -1, -1]], 'ship_types': [{'type': 'Destroyer', 'cells': [[1, 1], [2, 1]]}]}
#take from pod logs
data = {'board_state': [[-1, -1, 1, 2, -1], [-1, 1, -1, 2, 2], [-1, -1, 1, 2, -1], [-1, -1, 1, 2, 2], [-1, -1, -1, 2, -1]],
'ship_types': [{'type': 'Carrier', 'cells': [[0, 3], [1, 3], [2, 3], [3, 3], [4, 3]]}]}
data = {'board_state': [[-1, -1, 1, 2, -1], [-1, 1, -1, 2, 2], [-1, -1, 1, 2, 2], [-1, -1, 1, 2, 2], [-1, -1, -1, 2, -1]],
'ship_types': [{'type': 'Carrier', 'cells': [[0, 3], [1, 3], [2, 3], [3, 3], [4, 3]]}]}
##
data = {'board_state': [[-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1]], 'ship_types': []}
res = predict(data)
print(res)
print("done")