-
Notifications
You must be signed in to change notification settings - Fork 355
/
demo.py
87 lines (66 loc) · 1.84 KB
/
demo.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
import sys
import os
import pdb
from absl import flags
from pysc2.env import sc2_env
from pysc2.lib import actions as sc2_actions
import os
import datetime
from pysc2.env import environment
import numpy as np
from common.vec_env.subproc_vec_env import SubprocVecEnv
import random
import time
FLAGS = flags.FLAGS
def construct_action(marine_num, x, y):
move_action = []
# Base action is choosing a control group.
# 4 == select_control_group
move_action.append(
sc2_actions.FunctionCall(4, [[0], [marine_num]]))
# Right click.
# 331 == Move
move_action.append(
sc2_actions.FunctionCall(331, [[0], [int(x), int(y)]]))
return move_action
def get_position(env, marine_num):
"""Get position by selecting a unit.
This function has a side effect, so we return rewards and dones.
"""
select_action = construct_action(marine_num, -1, -1)
_, rs, dones, _, _, _, selected, _ = env.step([select_action])
xys = []
for s in selected:
pos = s.nonzero()
x = pos[1][0]
y = pos[2][0]
xys.append((x, y))
return xys, rs, dones
def main():
FLAGS(sys.argv)
env = SubprocVecEnv(1, 'CollectMineralShards')
env.reset()
total_reward = 0
for _ in range(1000):
marine = random.randrange(2)
x = random.randrange(32)
y = random.randrange(32)
print('Move %d to (%d, %d)' % (marine, x, y))
move_action = construct_action(marine, x, y)
# This controls the APM.
for _ in range(7):
obs, rs, dones, _, _, _, selected, screens = env.step([move_action])
total_reward += rs
# Querying the position
m_pos = {}
m_pos['0'], rs, dones = get_position(env, 0)
total_reward += rs
m_pos['1'], rs, dones = get_position(env, 1)
total_reward += rs
print(rs)
print(dones)
print('Total reward: ', total_reward)
print(m_pos)
env.close()
if __name__ == '__main__':
main()