forked from chris-chris/pysc2-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enjoy_mineral_shards.py
151 lines (115 loc) · 3.88 KB
/
enjoy_mineral_shards.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
import sys
import baselines.common.tf_util as U
import gflags as flags
import numpy as np
from baselines import deepq
from pysc2.env import environment
from pysc2.env import sc2_env
from pysc2.lib import actions
from pysc2.lib import actions as sc2_actions
from pysc2.lib import features
import deepq_mineral_shards
_PLAYER_RELATIVE = features.SCREEN_FEATURES.player_relative.index
_PLAYER_FRIENDLY = 1
_PLAYER_NEUTRAL = 3 # beacon/minerals
_PLAYER_HOSTILE = 4
_NO_OP = actions.FUNCTIONS.no_op.id
_MOVE_SCREEN = actions.FUNCTIONS.Move_screen.id
_ATTACK_SCREEN = actions.FUNCTIONS.Attack_screen.id
_SELECT_ARMY = actions.FUNCTIONS.select_army.id
_NOT_QUEUED = [0]
_SELECT_ALL = [0]
step_mul = 16
steps = 400
FLAGS = flags.FLAGS
def main():
FLAGS(sys.argv)
with sc2_env.SC2Env(
map_name="CollectMineralShards",
step_mul=step_mul,
visualize=True,
game_steps_per_episode=steps * step_mul) as env:
model = deepq.models.cnn_to_mlp(
convs=[(32, 8, 4), (64, 4, 2), (64, 3, 1)],
hiddens=[256],
dueling=True
)
def make_obs_ph(name):
return U.BatchInput((64, 64), name=name)
act_params = {
'make_obs_ph': make_obs_ph,
'q_func': model,
'num_actions': 4,
}
act = deepq_mineral_shards.load("mineral_shards.pkl", act_params=act_params)
while True:
obs = env.reset()
episode_rew = 0
done = False
step_result = env.step(actions=[sc2_actions.FunctionCall(_SELECT_ARMY, [_SELECT_ALL])])
while not done:
player_relative = step_result[0].observation["screen"][_PLAYER_RELATIVE]
obs = player_relative
player_y, player_x = (player_relative == _PLAYER_FRIENDLY).nonzero()
player = [int(player_x.mean()), int(player_y.mean())]
if(player[0]>32):
obs = shift(LEFT, player[0]-32, obs)
elif(player[0]<32):
obs = shift(RIGHT, 32 - player[0], obs)
if(player[1]>32):
obs = shift(UP, player[1]-32, obs)
elif(player[1]<32):
obs = shift(DOWN, 32 - player[1], obs)
action = act(obs[None])[0]
coord = [player[0], player[1]]
if(action == 0): #UP
if(player[1] >= 16):
coord = [player[0], player[1] - 16]
elif(player[1] > 0):
coord = [player[0], 0]
elif(action == 1): #DOWN
if(player[1] <= 47):
coord = [player[0], player[1] + 16]
elif(player[1] > 47):
coord = [player[0], 63]
elif(action == 2): #LEFT
if(player[0] >= 16):
coord = [player[0] - 16, player[1]]
elif(player[0] < 16):
coord = [0, player[1]]
elif(action == 3): #RIGHT
if(player[0] <= 47):
coord = [player[0] + 16, player[1]]
elif(player[0] > 47):
coord = [63, player[1]]
new_action = [sc2_actions.FunctionCall(_MOVE_SCREEN, [_NOT_QUEUED, coord])]
step_result = env.step(actions=new_action)
rew = step_result[0].reward
done = step_result[0].step_type == environment.StepType.LAST
episode_rew += rew
print("Episode reward", episode_rew)
UP, DOWN, LEFT, RIGHT = 'up', 'down', 'left', 'right'
def shift(direction, number, matrix):
''' shift given 2D matrix in-place the given number of rows or columns
in the specified (UP, DOWN, LEFT, RIGHT) direction and return it
'''
if direction in (UP):
matrix = np.roll(matrix, -number, axis=0)
matrix[number:,:] = -2
return matrix
elif direction in (DOWN):
matrix = np.roll(matrix, number, axis=0)
matrix[:number,:] = -2
return matrix
elif direction in (LEFT):
matrix = np.roll(matrix, -number, axis=1)
matrix[:,number:] = -2
return matrix
elif direction in (RIGHT):
matrix = np.roll(matrix, number, axis=1)
matrix[:,:number] = -2
return matrix
else:
return matrix
if __name__ == '__main__':
main()