-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetFromNetwork.py
259 lines (204 loc) · 8.21 KB
/
GetFromNetwork.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
import NetworkMove
import NetworkPiece
import copy
# import main2
import numpy as np
import math
from Savery import *
import random
count_of_bad_moves = 0
def zero_count_of_bad_moves():
global count_of_bad_moves
count_of_bad_moves = 0
return count_of_bad_moves
def get_count_of_bad_moves():
global count_of_bad_moves
return count_of_bad_moves
ile_p_bad = 0
ile_m_bad = 0
def write_to_file_bad_piece(board, piece):
global ile_p_bad
if ile_p_bad >= 100000:
data_from_file = np.loadtxt("p_faulty_piece.txt")
file = open("p_faulty_piece.txt", "w")
np.savetxt(file, data_from_file[30000:])
file.close()
data_from_file = np.loadtxt("p_faulty_board.txt")
file = open("p_faulty_board.txt", "w")
np.savetxt(file, data_from_file[(32 * 30000):])
file.close()
ile_p_bad -= 30000
save_board_to_file(board, "p_faulty_board.txt")
save_smth_to_file(piece, "p_faulty_piece.txt")
ile_p_bad += 1
def write_to_file_bad_move(board, piece, move):
global ile_m_bad
if ile_m_bad >= 100000:
data_from_file = np.loadtxt("m_faulty_piece.txt")
file = open("m_faulty_piece.txt", "w")
np.savetxt(file, data_from_file[30000:])
file.close()
data_from_file = np.loadtxt("m_faulty_move.txt")
file = open("m_faulty_move.txt", "w")
np.savetxt(file, data_from_file[30000:])
file.close()
data_from_file = np.loadtxt("m_faulty_board.txt")
file = open("m_faulty_board.txt", "w")
np.savetxt(file, data_from_file[(32 * 30000):])
file.close()
ile_m_bad -= 30000
save_board_to_file(board, "m_faulty_board.txt")
save_smth_to_file(piece, "m_faulty_piece.txt")
save_smth_to_file(move, "m_faulty_move.txt")
ile_m_bad += 1
def get_rezult_from_network(checkers, model_piece, model_move, is_net_and_net):
global count_of_bad_moves
board_2 = copy.deepcopy(checkers.board)
board_list = []
if checkers.get_current_player() == "a":
board_2.reverse()
for i in range(len(board_2)):
board_2[i].reverse()
for i in range(len(board_2)):
for j in range(len(board_2[i])):
if board_2[i][j] == 'A':
board_2[i][j] = 'R'
elif board_2[i][j] == 'a':
board_2[i][j] = 'r'
elif board_2[i][j] == 'R':
board_2[i][j] = 'A'
elif board_2[i][j] == 'r':
board_2[i][j] = 'a'
for i in range(len(board_2)):
for j in range(len(board_2[i])):
if (i + j) % 2 == 0:
continue
element = board_2[i][j]
if element == 'A':
board_list.append(0)
elif element == 'a':
board_list.append(1)
elif element == ' ':
board_list.append(2)
elif element == 'r':
board_list.append(3)
elif element == 'R':
board_list.append(4)
num_list = np.array(board_list)
train_input = num_list.astype('float32') / 5
train_input = np.reshape(train_input, (1, 32))
predictions_piece = model_piece.predict(train_input)
predictions_piece = predictions_piece[0]
possible_moves = checkers.get_possible_moves()
if checkers.get_current_player() == "a":
for i in range(len(possible_moves)):
for j in range(len(possible_moves[i])):
for k in range(len(possible_moves[i][j])):
possible_moves[i][j][k] = 7 - possible_moves[i][j][k]
good_piece = 0
for iter in range(1, 33):
piece = np.argmax(predictions_piece)
predictions_piece[piece] = -10
x1 = math.floor(piece / 4)
x2 = ((piece % 4) * 2 + 1) if x1 % 2 == 0 else ((piece % 4) * 2)
select_piece = [x1, x2]
it_found = False
for possible_move in possible_moves:
if possible_move[0][0] == select_piece[0] and possible_move[0][1] == select_piece[1]:
it_found = True
good_piece = piece
break
if it_found:
break
else:
write_to_file_bad_piece(num_list, piece)
if is_net_and_net == 1:
count_of_bad_moves += 1
piece_table = np.zeros((1, 32))
piece_table[0, good_piece] = 1
piece_table = piece_table.astype('float32')
predictions_move = model_move.predict([piece_table, train_input])
predictions_move = predictions_move[0]
good_move = 0
for iter in range(1, 33):
move = np.argmax(predictions_move)
predictions_move[move] = -10
y1 = math.floor(move / 4)
y2 = ((move % 4) * 2 + 1) if y1 % 2 == 0 else ((move % 4) * 2)
it_found = False
for possible_move in possible_moves:
if possible_move[0][0] == x1 and possible_move[0][1] == x2 and possible_move[1][
0] == y1 and possible_move[1][1] == y2:
it_found = True
good_move = move
break
if it_found:
break
else:
write_to_file_bad_move(num_list, good_piece, move)
if is_net_and_net == 1:
count_of_bad_moves += 1
return num_list, good_piece, good_move;
def get_rezult_from_rand(checkers):
board_2 = copy.deepcopy(checkers.board)
board_list = []
if checkers.get_current_player() == "a":
board_2.reverse()
for i in range(len(board_2)):
board_2[i].reverse()
for i in range(len(board_2)):
for j in range(len(board_2[i])):
if board_2[i][j] == 'A':
board_2[i][j] = 'R'
elif board_2[i][j] == 'a':
board_2[i][j] = 'r'
elif board_2[i][j] == 'R':
board_2[i][j] = 'A'
elif board_2[i][j] == 'r':
board_2[i][j] = 'a'
for i in range(len(board_2)):
for j in range(len(board_2[i])):
if (i + j) % 2 == 0:
continue
element = board_2[i][j]
if element == 'A':
board_list.append(0)
elif element == 'a':
board_list.append(1)
elif element == ' ':
board_list.append(2)
elif element == 'r':
board_list.append(3)
elif element == 'R':
board_list.append(4)
num_list = np.array(board_list)
possible_moves = checkers.get_possible_moves()
if checkers.get_current_player() == "a":
for i in range(len(possible_moves)):
for j in range(len(possible_moves[i])):
for k in range(len(possible_moves[i][j])):
possible_moves[i][j][k] = 7 - possible_moves[i][j][k]
rand_move = possible_moves[random.randint(0, len(possible_moves) - 1)]
good_piece = math.floor(rand_move[0][0] * 4 + rand_move[0][1] / 2)
good_move = math.floor(rand_move[1][0] * 4 + rand_move[1][1] / 2)
return num_list, good_piece, good_move;
def write_to_file(board_1, piece_1, move_1, board_2, piece_2, move_2):
global data_correct_board_1
global data_correct_piece_1
global data_correct_move_1
global data_correct_board_2
global data_correct_piece_2
global data_correct_move_2
if len(data_correct_piece_1) >= 10000:
data_correct_board_1 = np.delete(data_correct_board_1, range(3000), axis = 0)
data_correct_piece_1 = np.delete(data_correct_piece_1, range(3000), axis = 0)
data_correct_move_1 = np.delete(data_correct_move_1, range(3000), axis = 0)
data_correct_board_2 = np.delete(data_correct_board_2, range(3000), axis = 0)
data_correct_piece_2 = np.delete(data_correct_piece_2, range(3000), axis = 0)
data_correct_move_2 = np.delete(data_correct_move_2, range(3000), axis = 0)
data_correct_board_1 = np.append(data_correct_board_1, board_1)
data_correct_piece_1 = np.append(data_correct_piece_1, piece_1)
data_correct_move_1 = np.append(data_correct_move_1, move_1)
data_correct_board_2 = np.append(data_correct_board_2, board_2)
data_correct_piece_2 = np.append(data_correct_piece_2, piece_2)
data_correct_move_2 = np.append(data_correct_move_2, move_2)