/
collect_mineral_shards_1d.py
60 lines (46 loc) · 2.04 KB
/
collect_mineral_shards_1d.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
"""
Copyright 2017 Islam Elnabarawy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import numpy as np
from examples.base_example import BaseExample
__author__ = 'Islam Elnabarawy'
_PLAYER_FRIENDLY = 1
_PLAYER_NEUTRAL = 3 # beacon/minerals
_NO_OP = 0
_ENV_NAME = "SC2CollectMineralShards-v0"
_VISUALIZE = False
_STEP_MUL = None
_NUM_EPISODES = 10
class CollectMineralShards1d(BaseExample):
def __init__(self, visualize=False, step_mul=None) -> None:
super().__init__(_ENV_NAME, visualize, step_mul)
def get_action(self, env, obs):
neutral_y, neutral_x = (obs[0] == _PLAYER_NEUTRAL).nonzero()
player_y, player_x = (obs[0] == _PLAYER_FRIENDLY).nonzero()
if not neutral_y.any():
raise Exception('No minerals found!')
if not player_y.any():
raise Exception('No marines found!')
player = [np.ceil(player_x.mean()).astype(int), np.ceil(player_y.mean()).astype(int)]
shards = np.array(list(zip(neutral_x, neutral_y)))
closest_ix = np.argmin(np.linalg.norm(np.array(player) - shards, axis=1))
target = np.ravel_multi_index(shards[closest_ix], obs.shape[1:])
return target
def main():
example = CollectMineralShards1d(_VISUALIZE, _STEP_MUL)
rewards = example.run(_NUM_EPISODES)
print('Total reward: {}'.format(rewards.sum()))
print('Average reward: {} +/- {}'.format(rewards.mean(), rewards.std()))
print('Minimum reward: {}'.format(rewards.min()))
print('Maximum reward: {}'.format(rewards.max()))
if __name__ == "__main__":
main()