-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprediction.py
More file actions
109 lines (85 loc) · 3.26 KB
/
Copy pathprediction.py
File metadata and controls
109 lines (85 loc) · 3.26 KB
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
from utils import generate_board
import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread
class Predictor:
"""
Generate clues for the blue team
"""
def __init__(self, board, ids_to_score_path, invalid_guesses, decay=0.8):
"""
Parameters
----------
board: json
The current board state
ids_to_score_path: str
The path to the ids_to_score nd-array
invalid_guesses: set
Guesses which can't be used
decay: float
The decay for the score function
"""
self.board = [p for p in board if not p['active']]
self.ids_to_score = np.load(ids_to_score_path, allow_pickle=True)
self.valid_guesses = list(set(range(self.ids_to_score.shape[0])).difference(invalid_guesses))
self.decay = decay
def guess_scores(self, guess):
"""
Generate the scores for a guess
"""
scores = [self.ids_to_score[guess][p['pic_id']] for p in self.board]
sorted_idx = np.argsort(-np.array(scores))
decay = 1
for i in sorted_idx:
scores[i] *= decay
if self.board[i]['type'] != 'blue':
scores[i] *= -1
decay *= self.decay
return scores
def get_best_guess_and_scores(self):
"""
Get the best guess and its scores
"""
best_guess = ''
best_scores = []
best_total_score = -float('inf')
for guess in self.valid_guesses:
scores = self.guess_scores(guess)
total_score = sum(scores)
if total_score > best_total_score:
best_guess = guess
best_scores = scores
best_total_score = total_score
return best_guess, best_scores
def display_board(self, best_guess, best_scores, shape=(5, 5)):
pic_ids = [picture['pic_id'] for picture in self.board] + [best_guess]
fig, ax = plt.subplots(shape[0] + 1, shape[1], figsize=(24, 24))
for i in range(shape[0] + 1):
for j in range(shape[1]):
if i != shape[0]:
idx = shape[0]*i + j
path = f'../static/pictures/{pic_ids[idx]}.jpg'
image = imread(path)
ax[i][j].set_title(best_scores[idx], size=8)
ax[i][j].imshow(image)
elif j == shape[1]//2:
path = f'../static/pictures/{best_guess}.jpg'
image = imread(path)
ax[i][j].imshow(image)
ax[i][j].axis('off')
fig.subplots_adjust(hspace=0.4)
plt.show()
def main():
n_ids = 3242
ids_to_score_path = '../static/numpy/ids_to_score.npy'
board = generate_board(n_ids)
invalid_guesses = set([picture['pic_id'] for picture in board])
predictor = Predictor(board, ids_to_score_path, invalid_guesses)
best_guess, best_scores = predictor.get_best_guess_and_scores()
round_scores = [round(s) for s in best_scores]
board_types = [b['type'] for b in board]
for t, s in zip(board_types, round_scores):
print(f'Type:{t} || Score:{s}')
predictor.display_board(best_guess, round_scores, shape=(5, 5))
if __name__ == "__main__":
main()