-
Notifications
You must be signed in to change notification settings - Fork 2
/
mini_source_agent_add_map_bn.py
396 lines (307 loc) · 14.5 KB
/
mini_source_agent_add_map_bn.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import random
import tensorflow as tf
from pysc2.agents import base_agent
from pysc2.lib import actions as sc2_actions
from lib import utils as U
from lib import config as C
from lib import transform_pos as T
from lib import option as M
from lib import environment
from lib import my_sc2_env as sc2_env
from lib.replay_buffer import Buffer
from mini_agent import ProtossAction
class MiniSourceAgent(base_agent.BaseAgent):
"""Agent for source game of starcraft."""
def __init__(self, index=0, rl_training=False, restore_model=False, global_buffer=None, net=None, strategy_agent=None, greedy_action=False,
extract_save_dir=None, ob_space_add=0, image_debug=False, action_prob_debug=False, act_space_add=0):
super(MiniSourceAgent, self).__init__()
self.net = net
self.index = index
self.global_buffer = global_buffer
self.restore_model = restore_model
# model in brain
self.strategy_agent = strategy_agent
self.strategy_act = None
# count num
self.step = 0
self.strategy_wait_secs = 2
self.strategy_flag = False
self.policy_wait_secs = 2
self.policy_flag = True
# every 30 seconds to show the image
self.image_wait_secs = 1
# every 30 seconds to show the image
self.prob_show_wait_seconds = (np.array([0, 0.5, 1, 3, 5, 10]) * 60).astype(int).tolist()
self.env = None
self.obs = None
# buffer
self.local_buffer = Buffer()
self.num_players = 2
self.on_select = None
self._result = None
self._gases = None
self.is_end = False
self.greedy_action = greedy_action
self.rl_training = rl_training
self.extract_save_dir = extract_save_dir
self.ob_space_add = ob_space_add
self.act_space_add = act_space_add
self.image_debug = image_debug
self.action_prob_debug = action_prob_debug
self.action_num = 10 + act_space_add
def reset(self):
super(MiniSourceAgent, self).reset()
self.step = 0
self.obs = None
self._result = None
self._gases = None
self.is_end = False
self.strategy_flag = False
self.policy_flag = True
self.local_buffer.reset()
if self.strategy_agent is not None:
self.strategy_agent.reset()
def set_env(self, env):
self.env = env
def init_network(self):
self.net.initialize()
if self.restore_model:
self.net.restore_policy()
def reset_old_network(self):
self.net.reset_old_network()
def save_model(self):
self.net.save_policy()
def update_policy(self):
self.net.Update_policy(self.global_buffer)
def update_result_list(self, result_list):
self.net.Update_result(result_list)
def update_network(self, result_list):
self.net.Update_policy(self.global_buffer)
self.net.Update_result(result_list)
def update_summary(self, counter):
return self.net.Update_summary(counter)
def mini_step(self, action):
if action == ProtossAction.Build_probe.value:
M.mineral_worker(self)
elif action == ProtossAction.Build_zealot.value:
M.train_army(self, C._TRAIN_ZEALOT)
elif action == ProtossAction.Build_Stalker.value:
M.train_army(self, C._TRAIN_STALKER)
elif action == ProtossAction.Build_pylon.value:
no_unit_index = U.get_unit_mask_screen(self.obs, size=2)
pos = U.get_pos(no_unit_index)
M.build_by_idle_worker(self, C._BUILD_PYLON_S, pos)
elif action == ProtossAction.Build_gateway.value:
power_index = U.get_power_mask_screen(self.obs, size=5)
pos = U.get_pos(power_index)
M.build_by_idle_worker(self, C._BUILD_GATEWAY_S, pos)
elif action == ProtossAction.Build_Assimilator.value:
if self._gases is not None:
#U.find_gas_pos(self.obs, 1)
gas_1 = self._gases[0]
gas_2 = self._gases[1]
if gas_1 is not None and not U.is_assimilator_on_gas(self.obs, gas_1):
gas_1_pos = T.world_to_screen_pos(self.env.game_info, gas_1.pos, self.obs)
M.build_by_idle_worker(self, C._BUILD_ASSIMILATOR_S, gas_1_pos)
elif gas_2 is not None and not U.is_assimilator_on_gas(self.obs, gas_2):
gas_2_pos = T.world_to_screen_pos(self.env.game_info, gas_2.pos, self.obs)
M.build_by_idle_worker(self, C._BUILD_ASSIMILATOR_S, gas_2_pos)
elif action == ProtossAction.Build_CyberneticsCore.value:
power_index = U.get_power_mask_screen(self.obs, size=3)
pos = U.get_pos(power_index)
M.build_by_idle_worker(self, C._BUILD_CYBER_S, pos)
elif action == ProtossAction.Attack.value:
M.attack_step(self)
elif action == ProtossAction.Retreat.value:
M.retreat_step(self)
elif action == ProtossAction.Do_nothing.value:
self.safe_action(C._NO_OP, 0, [])
# added action
elif action == ProtossAction.All.value + 0:
M.attack_queued(self)
elif action == ProtossAction.All.value + 1:
M.retreat_queued(self)
elif action == ProtossAction.All.value + 2:
M.gas_worker_only(self)
elif action == ProtossAction.All.value + 3:
M.attack_main_base(self)
elif action == ProtossAction.All.value + 4:
M.attack_sub_base(self)
def get_the_input(self):
high_input, tech_cost, pop_num = U.get_input(self.obs)
controller_input = np.concatenate([high_input, tech_cost, pop_num], axis=0)
return controller_input
def get_the_input_right(self, obs):
high_input, tech_cost, pop_num = U.get_input(obs)
controller_input = np.concatenate([high_input, tech_cost, pop_num], axis=0)
return controller_input
def mapping_source_to_mini_by_rule(self, source_state):
simple_input = np.zeros([20], dtype=np.int16)
simple_input[0] = 0 # self.time_seconds
simple_input[1] = source_state[28] # self.mineral_worker_nums
simple_input[2] = source_state[30] + source_state[32] # self.gas_worker_nums
simple_input[3] = source_state[2] # self.mineral
simple_input[4] = source_state[3] # self.gas
simple_input[5] = source_state[6] # self.food_cup
simple_input[6] = source_state[7] # self.food_used
simple_input[7] = source_state[10] # self.army_nums
simple_input[8] = source_state[16] # self.gateway_num
simple_input[9] = source_state[14] # self.pylon_num
simple_input[10] = source_state[15] # self.Assimilator_num
simple_input[11] = source_state[17] # self.CyberneticsCore_num
simple_input[12] = source_state[12] # self.zealot_num
simple_input[13] = source_state[13] # self.Stalker_num
simple_input[14] = source_state[11] # self.probe_num
simple_input[15] = source_state[4] + source_state[2] # self.collected_mineral
simple_input[16] = source_state[4] # self.spent_mineral
simple_input[17] = source_state[5] + source_state[3] # self.collected_gas
simple_input[18] = source_state[5] # self.spent_gas
simple_input[19] = 1 # self.Nexus_num
return simple_input
def get_add_state(self, source_state):
add_input = np.zeros([4], dtype=np.int16)
add_input[0] = source_state[0] # self.difficulty
add_input[1] = source_state[1] # self.game_loop
add_input[2] = source_state[8] # self.food_army
add_input[3] = source_state[9] # self.food_workers
return add_input
def play_right_add(self, verbose=False):
# note this is a right version of game play, which also add input and action
prev_state = None
prev_action = None
prev_value = None
prev_add_state = None
prev_map_state = None
show_image = False
self.safe_action(C._NO_OP, 0, [])
self.safe_action(C._MOVE_CAMERA, 0, [C.base_camera_pos])
self._gases = U.find_initial_gases(self.obs)
self.dummy_add_state = np.zeros(1)
self.dummy_map_state = np.zeros([1, 1, 1])
simulate_seconds = 0
feature_dict = U.edge_state()
previous_match = -1
while True:
self.safe_action(C._MOVE_CAMERA, 0, [C.base_camera_pos])
if self.policy_flag and (not self.is_end):
# get the state
state = self.mapping_source_to_mini_by_rule(self.get_the_input_right(self.obs))
if self.image_debug and verbose and self.step % C.time_wait(self.image_wait_secs) == 0:
show_image = True
else:
show_image = False
if verbose:
print('show_image:', show_image)
map_state = U.get_small_simple_map_data(self.obs, show_image, show_image)
if verbose:
print('map_state.shape:', map_state.shape)
add_state = self.get_add_state(self.get_the_input_right(self.obs))
# get the action and value accoding to state
#print("add_state:", add_state)
if self.ob_space_add == 0:
add_state = self.dummy_add_state
map_state = self.dummy_map_state = np.zeros([1, 1, 1])
action, action_probs, value = self.net.policy.get_act_action_probs(state, add_state, map_state, verbose=verbose)
# if this is not the fisrt state, store things to buffer
if prev_state is not None:
# try reward = self.obs.reward
reward = self.obs.reward
if verbose:
print(prev_state, prev_add_state, prev_action, state, reward, prev_value, value)
self.local_buffer.append_more_more(prev_state, prev_add_state, prev_map_state, prev_action, state, reward, prev_value, value)
self.mini_step(action)
simulate_seconds += self.policy_wait_secs
# the evn step to new states
prev_state = state
prev_action = action
prev_value = value
prev_add_state = add_state
prev_map_state = map_state
self.policy_flag = False
if self.is_end:
# get the last state and reward
# get the state
state = self.mapping_source_to_mini_by_rule(self.get_the_input_right(self.obs))
map_state = U.get_small_simple_map_data(self.obs)
add_state = self.get_add_state(self.get_the_input_right(self.obs))
if self.ob_space_add == 0:
add_state = self.dummy_add_state
map_state = self.dummy_map_state = np.zeros([1, 1, 1])
value = self.net.policy.get_values(state, add_state, map_state)
# the value of the last state is defined somewhat different
value = self.get_values_right(value)
# if this is not the fisrt state, store things to buffer
if prev_state is not None:
reward = self.obs.reward
if verbose:
print(prev_state, prev_add_state, prev_action, state, reward, prev_value, value)
self.local_buffer.append_more_more(prev_state, prev_add_state, prev_map_state, prev_action, state, reward, prev_value, value)
break
if self.rl_training:
#print(self.local_buffer.values)
#print(self.local_buffer.values_next)
#print(self.local_buffer.rewards)
self.global_buffer.add(self.local_buffer)
print("add map bn:")
print("add %d buffer!" % (len(self.local_buffer.rewards)))
#print("returns:", self.global_buffer.returns)
#print("gaes:", self.global_buffer.gaes)
def set_flag(self):
if self.step % C.time_wait(self.strategy_wait_secs) == 1:
self.strategy_flag = True
if self.step % C.time_wait(self.policy_wait_secs) == 1:
self.policy_flag = True
def safe_action(self, action, unit_type, args):
if M.check_params(self, action, unit_type, args, 1):
obs = self.env.step([sc2_actions.FunctionCall(action, args)])[0]
self.obs = obs
self.step += 1
self.update_result()
self.set_flag()
def select(self, action, unit_type, args):
# safe select
if M.check_params(self, action, unit_type, args, 0):
self.obs = self.env.step([sc2_actions.FunctionCall(action, args)])[0]
self.on_select = unit_type
self.update_result()
self.step += 1
self.set_flag()
@property
def result(self):
return self._result
def update_result(self):
if self.obs is None:
return
if self.obs.last() or self.env.state == environment.StepType.LAST:
self.is_end = True
outcome = 0
o = self.obs.raw_observation
player_id = o.observation.player_common.player_id
for r in o.player_result:
if r.player_id == player_id:
outcome = sc2_env._possible_results.get(r.result, 0)
frames = o.observation.game_loop
result = {}
result['outcome'] = outcome
result['reward'] = self.obs.reward
result['frames'] = frames
self._result = result
print('play end, total return', self.obs.reward)
self.step = 0
def get_values(self, values):
# check if the game is end
if self.is_end and self.result['reward'] != 0:
return 0
else:
return values
def get_values_right(self, values):
# if the game ends with a win or loss (the result reward is 1 or -1), the value is set to 0
# else if the game ends without a result (the result reward is 1 or -1), the value is set to asbefore
if self.is_end and self.result['reward'] != 0:
return 0
else:
return values