-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
58 lines (49 loc) · 2.28 KB
/
utils.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
import numpy as np
from pysc2.lib import features
_SCREEN_PLAYER_ID = features.SCREEN_FEATURES.player_id.index
_SCREEN_UNIT_TYPE = features.SCREEN_FEATURES.unit_type.index
_MINIMAP_PLAYER_ID = features.MINIMAP_FEATURES.player_id.index
def screen_channel():
screen_channel = 0
for i in range(len(features.SCREEN_FEATURES)):
if i == _SCREEN_PLAYER_ID or i == _SCREEN_UNIT_TYPE:
screen_channel += 1
elif features.SCREEN_FEATURES[i].type == features.FeatureType.SCALAR:
screen_channel += 1
else:
screen_channel += features.SCREEN_FEATURES[i].scale
return screen_channel
def minimap_channel():
minimap_channel = 0
for i in range(len(features.MINIMAP_FEATURES)):
if i == _MINIMAP_PLAYER_ID:
minimap_channel += 1
elif features.MINIMAP_FEATURES[i].type == features.FeatureType.SCALAR:
minimap_channel += 1
else:
minimap_channel += features.MINIMAP_FEATURES[i].scale
return minimap_channel
def preprocess_screen(screen):
layers = []
for i in range(len(features.SCREEN_FEATURES)):
if i == _SCREEN_PLAYER_ID or i == _SCREEN_UNIT_TYPE or features.SCREEN_FEATURES[i].type == features.FeatureType.SCALAR:
layers.append(screen[i:i+1] / features.SCREEN_FEATURES[i].scale)
else:
layer = np.zeros([features.SCREEN_FEATURES[i].scale, screen.shape[1], screen.shape[2]], dtype=np.float32)
for j in range(features.SCREEN_FEATURES[i].scale):
y, x = (screen[i] == j).nonzero()
layer[j, y, x] = 1
layers.append(layer)
return np.concatenate(layers, axis=0)
def preprocess_minimap(minimap):
layers = []
for i in range(len(features.MINIMAP_FEATURES)):
if i == _MINIMAP_PLAYER_ID or features.MINIMAP_FEATURES[i].type == features.FeatureType.SCALAR:
layers.append(minimap[i:i+1] / features.MINIMAP_FEATURES[i].scale)
else:
layer = np.zeros([features.MINIMAP_FEATURES[i].scale, minimap.shape[1], minimap.shape[2]], dtype=np.float32)
for j in range(features.MINIMAP_FEATURES[i].scale):
y, x = (minimap[i] == j).nonzero()
layer[j, y, x] = 1
layers.append(layer)
return np.concatenate(layers, axis=0)