-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
trajectory_generator.py
310 lines (257 loc) · 9.9 KB
/
trajectory_generator.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
#!/usr/bin/env python
from pysc2.lib import features, point, actions, units
from pysc2.env.environment import TimeStep, StepType
from pysc2.env import sc2_env, available_actions_printer
from pysc2 import run_configs
from s2clientprotocol import sc2api_pb2 as sc_pb
import importlib
import random
import sys
import glob
import os
import argparse
import hickle as hkl
import numpy as np
from absl import app, flags
FLAGS = flags.FLAGS
FLAGS(['trajectory.py'])
parser = argparse.ArgumentParser(description='Trajetory File Generation')
parser.add_argument('--replay_path', type=str, help='Path of replay file')
parser.add_argument('--player_1', type=str, default='Terran', help='Race of player 1')
parser.add_argument('--player_2', type=str, default='Terran', help='Race of player 2')
parser.add_argument('--mmr', type=int, default=2500, help='Threshold of mmr score ')
parser.add_argument('--saving_path', type=str, help='Path for saving proprocessed replay file')
arguments = parser.parse_args()
class Trajectory(object):
def __init__(self, source, saving_path, home_race_name, away_race_name, replay_filter, filter_repeated_camera_moves=False):
self.source = source
self.home_race_name = home_race_name
self.away_race_name = away_race_name
self.replay_filter = replay_filter
self.filter_repeated_camera_moves = filter_repeated_camera_moves
self.home_BO = None
self.home_observation = []
self.home_feature_screen = []
self.home_feature_minimap = []
self.home_player = []
self.home_feature_units = []
self.home_game_loop = []
self.home_available_actions = []
self.home_build_queue = []
self.home_production_queue = []
self.home_single_select = []
self.home_multi_select = []
self.home_action = []
self.home_score_cumulative = []
self.away_BU = None
self.away_trajectory = []
self.away_score_cumulative = None
#print("saving_path: ", saving_path)
saving_folder_existence = os.path.isdir(saving_path)
#print("saving_folder_existence: ", saving_folder_existence)
assert saving_folder_existence, "saving folder is not existed"
def get_BO(self, player):
if player == 0:
return self.home_BO
else:
return self.away_BU
def generate_trajectory(self):
function_dict = {}
for _FUNCTION in actions._FUNCTIONS:
function_dict[_FUNCTION.ability_id] = _FUNCTION.name
race_list = ['Terran', 'Zerg', 'Protoss']
"""How many agent steps the agent has been trained for."""
run_config = run_configs.get()
sc2_proc = run_config.start()
controller = sc2_proc.controller
root_path = self.source
replay_file_path_list = glob.glob(root_path + '*.*')
#print("len(replay_file_path_list) != 0: ", len(replay_file_path_list) != 0)
assert len(replay_file_path_list) != 0, "No replay file is found"
for replay_file_path in replay_file_path_list:
#print("replay_file_path: " + str(replay_file_path))
#replay_file_path = random.choice(file_list)
replay_file_name = replay_file_path.split('/')[-1].split('.')[0]
#print("replay_file_name: {}".format(replay_file_name))
#print("replay_file_path: {}".format(replay_file_path))
try:
replay_data = run_config.replay_data(replay_file_path)
ping = controller.ping()
info = controller.replay_info(replay_data)
player0_race = info.player_info[0].player_info.race_actual
player0_mmr = info.player_info[0].player_mmr
player0_apm = info.player_info[0].player_apm
player0_result = info.player_info[0].player_result.result
home_race = race_list.index(self.home_race_name) + 1
if (home_race == player0_race):
pass
#print("player0_race pass")
#continue
else:
print("player0_race fail")
continue
if (player0_mmr >= self.replay_filter):
pass
#print("player0_mmr pass")
#continue
else:
pass
#print("player0_mmr fail")
#break
player1_race = info.player_info[1].player_info.race_actual
player1_mmr = info.player_info[1].player_mmr
player1_apm = info.player_info[1].player_apm
player1_result = info.player_info[1].player_result.result
away_race = race_list.index(self.away_race_name) + 1
if (away_race == player1_race):
pass
#print("player1_race pass ")
#continue
else:
print("player1_race fail ")
continue
if (player1_mmr >= self.replay_filter):
pass
#print("player1_mmr pass ")
#continue
else:
pass
#print("player1_mmr fail")
#continue
screen_size_px = (32, 32)
minimap_size_px = (32, 32)
player_id = 1
discount = 1.
step_mul = 1
screen_size_px = point.Point(*screen_size_px)
minimap_size_px = point.Point(*minimap_size_px)
interface = sc_pb.InterfaceOptions(raw=True, score=True,
feature_layer=sc_pb.SpatialCameraSetup(width=24))
screen_size_px.assign_to(interface.feature_layer.resolution)
minimap_size_px.assign_to(interface.feature_layer.minimap_resolution)
map_data = None
if info.local_map_path:
map_data = run_config.map_data(info.local_map_path)
_episode_length = info.game_duration_loops
_episode_steps = 0
controller.start_replay(sc_pb.RequestStartReplay(replay_data=replay_data,
map_data=map_data, options=interface,
observed_player_id=player_id))
_state = StepType.FIRST
if (info.HasField("error") or
info.base_build != ping.base_build or # different game version
info.game_duration_loops < 1000 or
len(info.player_info) != 2):
# Probably corrupt, or just not interesting.
print("error")
continue
feature_screen_size = 32
feature_minimap_size = 32
rgb_screen_size = None
rgb_minimap_size = None
action_space = None
use_feature_units = True
aif = sc2_env.parse_agent_interface_format(
feature_screen=feature_screen_size,
feature_minimap=feature_minimap_size,
rgb_screen=rgb_screen_size,
rgb_minimap=rgb_minimap_size,
action_space=action_space,
use_feature_units=use_feature_units)
_features = features.features_from_game_info(controller.game_info(), agent_interface_format=aif)
build_info = []
build_name = []
replay_step = 0
#print("_episode_length: {}".format(_episode_length))
for replay_step in range(0, _episode_length):
controller.step(step_mul)
obs = controller.observe()
if obs.player_result: # Episode over.
_state = StepType.LAST
discount = 0
else:
discount = discount
_episode_steps += step_mul
agent_obs = _features.transform_obs(obs)
if len(obs.actions) != 0:
exec_actions = []
for ac in obs.actions:
exec_act = _features.reverse_action(ac)
a_0 = int(exec_act.function)
a_l = []
#print("exec_act.arguments: {}".format(exec_act.arguments))
for argument in exec_act.arguments:
#print("argument: {}".format(argument))
if str(type(argument[0])) != "<class 'int'>":
a_l.append(argument[0].value)
else:
a_l.append(argument)
exec_actions.append([a_0, a_l])
#print("a_0: {}".format(a_0))
#print("a_l: {}".format(a_l))
self.home_action.append(exec_actions)
else:
exec_actions = []
a_0 = 0
a_l = [0]
exec_actions.append([a_0, a_l])
if replay_step % 8 == 0 or _state == StepType.LAST:
self.home_action.append(exec_actions)
pass
else:
continue
#print("")
done = 0
if _state == StepType.LAST:
done = 1
#print("agent_obs['score_cumulative']: ", agent_obs['score_cumulative'])
#print("agent_obs['multi_select']: ", agent_obs['multi_select'])
self.home_feature_screen.append(agent_obs['feature_screen'])
self.home_feature_minimap.append(agent_obs['feature_minimap'])
self.home_player.append(agent_obs['player'])
self.home_feature_units.append(agent_obs['feature_units'])
self.home_game_loop.append(agent_obs['game_loop'])
self.home_available_actions.append(agent_obs['available_actions'])
self.home_build_queue.append(agent_obs['build_queue'])
self.home_production_queue.append(agent_obs['production_queue'])
self.home_single_select.append(agent_obs['single_select'])
self.home_multi_select.append(agent_obs['multi_select'])
self.home_score_cumulative.append(agent_obs['score_cumulative'])
step = TimeStep(step_type=_state, reward=0,
discount=discount, observation=agent_obs)
if _state == StepType.LAST:
file_path = arguments.saving_path + replay_file_name + '.hkl'
data = {'home_feature_screen': self.home_feature_screen,
'home_feature_minimap': self.home_feature_minimap,
'home_player': self.home_player,
'home_feature_units': self.home_feature_units,
'home_game_loop': self.home_game_loop,
'home_available_actions': self.home_available_actions,
'home_action': self.home_action,
'home_build_queue': self.home_build_queue,
'home_production_queue': self.home_production_queue,
'home_single_select': self.home_single_select,
'home_multi_select': self.home_multi_select,
'home_score_cumulative': self.home_score_cumulative
}
self.home_feature_screen = []
self.home_feature_minimap = []
self.home_player = []
self.home_feature_units = []
self.home_game_loop = []
self.home_available_actions = []
self.home_build_queue = []
self.home_production_queue = []
self.home_single_select = []
self.home_multi_select = []
self.home_action = []
self.home_score_cumulative = []
hkl.dump(data, file_path)
break
_state = StepType.MID
#self.home_BO = build_info
#self.away_BU = score_cumulative_dict
except:
continue
replay = Trajectory(arguments.replay_path, arguments.saving_path, arguments.player_1, arguments.player_2, arguments.mmr)
replay.generate_trajectory()