-
Notifications
You must be signed in to change notification settings - Fork 3
/
dqn_player.py
235 lines (187 loc) · 7.69 KB
/
dqn_player.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
# Written by Michelle Blom, 2019
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
from advance_model import *
from utils import *
from keras.models import model_from_json
import json
import numpy as np
import tensorflow as tf
from tensorflow.python.keras.backend import set_session
from display_utils import *
class myPlayer(AdvancePlayer):
def __init__(self, _id):
super().__init__(_id)
self.tmp_idx = 9999
self.err_count = 0
self.total_count = 0
#self.base_state = np.zeros(85) #47 7 132 85 (1,85)
self.sess = tf.Session()
self.graph = tf.get_default_graph()
self.sess.run(tf.global_variables_initializer())
set_session(self.sess)
json_file = open('players/model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("players/model.h5")
loaded_model.compile(loss='mse', optimizer = 'rmsprop', metrics=['accuracy'])
loaded_model._make_predict_function()
self.loaded_model = loaded_model
json_file = open('players/generalized_moves.json')
json_str = json_file.read()
self.generalized_moves=json.loads(json_str)
def get_player_order(self, game_state):
player_order = []
for i in range(self.id + 1, len(game_state.players)):
player_order.append(i)
for i in range(0, self.id + 1):
player_order.append(i)
return player_order
def get_move(self, move):
move = str(TileToShortString(move[2].tile_type)) + '/' \
+ str(move[2].pattern_line_dest) + '/'+ str(move[2].num_to_pattern_line) + '/' + str(move[2].num_to_floor_line)
return move
def get_FactoryCentre(self, game_state):
factory_list = []
for i in range(5):
factory = game_state.factories[i].tiles
for j in range(len(factory)):
factory_list.append(factory[j])
for j in range(len(game_state.centre_pool.tiles)):
factory_list.append(game_state.centre_pool.tiles[j])
return factory_list
def get_fullfeatures(self, move, game_state, state_feature, round_count):
state_feature_copy = copy.deepcopy(state_feature)
move_idx = self.get_int(move)
move_idx.append(round_count)
factory_list = self.get_FactoryCentre(game_state)
state_feature_copy.extend(factory_list)
state_feature_copy.extend(move_idx)
base = state_feature_copy
return base, move_idx
def get_statefeatures(self, game_state, state_feature, round_count):
state_feature_copy = copy.deepcopy(state_feature)
#factory_list = self.get_FactoryCentre(game_state)
#state_feature_copy.extend(factory_list)
state_feature_copy.append(round_count)
return state_feature_copy
def SelectMove(self, moves, game_state):
best_move = None
self.total_count +=1
plr_state = game_state.players[self.id]
enemy_state = game_state.players[self.id*-1 + 1]
gs_copy = copy.deepcopy(game_state)
moves_copy = copy.deepcopy(moves)
state_feature = StateToArray(self.id, plr_state)
state_feature_enemy = StateToArray(self.id*-1 + 1, enemy_state)
state_feature.extend(state_feature_enemy)
round_num = (4 - len(game_state.bag) // 20) #from 0
state = []
s_features = self.get_statefeatures(gs_copy, state_feature, round_num)
state = s_features
available_actions_list = []
total_idx = 0
for available_idx, move in enumerate(moves_copy):
try:
total_idx = self.generalized_moves[self.get_move(move)]
except:
total_idx = 0
self.err_count +=1
pass
available_actions_list.append([total_idx, available_idx])
inp = np.array([state])
with self.graph.as_default():
set_session(self.sess)
preds = self.loaded_model.predict(inp)[0]
maxval = 0
maxtotalidx = 0
maxavailidx = 0
for total_idx, available_idx in available_actions_list:
val = preds[total_idx]
if val > maxval:
maxval = val
maxtotalidx = total_idx
maxavailidx = available_idx
best_move = moves_copy[maxavailidx]
return best_move
def B2N(binary, i, j):
arr = [[],[],[],[],[]]
#arr [0] = ['B', 'Y', 'R', 'K', 'W']
#arr [1] = ['W' ,'B','Y','R','K']
#arr [2] = ['K', 'W', 'B', 'Y', 'R']
#arr [3] = ['R', 'K', 'W', 'B', 'Y']
#arr [4] = ['Y', 'R', 'K', 'W', 'B']
arr [0] = [2, 5, 1, 4, 3]
arr [1] = [3 ,2, 5, 1, 4]
arr [2] = [4, 3, 2, 5, 1]
arr [3] = [1, 4, 3, 2, 5]
arr [4] = [5, 1, 4, 3, 2]
if binary == 0:
return 0
else:
return arr[i][j]
def StateToArray(player_id, ps):
pattern_line_arr = []
grid_arr = []
for i in range(ps.GRID_SIZE):
if ps.lines_tile[i] != -1:
tt = ps.lines_tile[i]
ts = TileToNum(tt)
num = ps.lines_number[i]
for j in range(num):
pattern_line_arr.append(ts)
for j in range(num, i+1):
pattern_line_arr.append(0)
else:
assert ps.lines_number[i] == 0
for j in range(i+1):
pattern_line_arr.append(0)
for i in ps.floor:
if i == 1:
pattern_line_arr.append(-1)
else:
pattern_line_arr.append(0)
grid_arr.append(B2N(ps.grid_state[0][0], 0, 0))
grid_arr.append(B2N(ps.grid_state[0][1], 0, 1))
grid_arr.append(B2N(ps.grid_state[0][2], 0, 2))
grid_arr.append(B2N(ps.grid_state[0][3], 0, 3))
grid_arr.append(B2N(ps.grid_state[0][4], 0, 4))
#i == 1:
grid_arr.append(B2N(ps.grid_state[1][0], 1, 0))
grid_arr.append(B2N(ps.grid_state[1][1], 1, 1))
grid_arr.append(B2N(ps.grid_state[1][2], 1, 2))
grid_arr.append(B2N(ps.grid_state[1][3], 1, 3))
grid_arr.append(B2N(ps.grid_state[1][4], 1, 4))
#i == 2:
grid_arr.append(B2N(ps.grid_state[2][0], 2, 0))
grid_arr.append(B2N(ps.grid_state[2][1], 2, 1))
grid_arr.append(B2N(ps.grid_state[2][2], 2, 2))
grid_arr.append(B2N(ps.grid_state[2][3], 2, 3))
grid_arr.append(B2N(ps.grid_state[2][4], 2, 4))
#i == 3:
grid_arr.append(B2N(ps.grid_state[3][0], 3, 0))
grid_arr.append(B2N(ps.grid_state[3][1], 3, 1))
grid_arr.append(B2N(ps.grid_state[3][2], 3, 2))
grid_arr.append(B2N(ps.grid_state[3][3], 3, 3))
grid_arr.append(B2N(ps.grid_state[3][4], 3, 4))
#i == 4:
grid_arr.append(B2N(ps.grid_state[4][0], 4, 0))
grid_arr.append(B2N(ps.grid_state[4][1], 4, 1))
grid_arr.append(B2N(ps.grid_state[4][2], 4, 2))
grid_arr.append(B2N(ps.grid_state[4][3], 4, 3))
grid_arr.append(B2N(ps.grid_state[4][4], 4, 4))
pattern_line_arr.extend(grid_arr)
return pattern_line_arr