-
Notifications
You must be signed in to change notification settings - Fork 59
/
test_env.py
172 lines (126 loc) · 4.1 KB
/
test_env.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
import pytest
import numpy as np
import lbforaging
from lbforaging.foraging.environment import Action
import gym
def manhattan_distance(x,y):
return sum(abs(a-b) for a,b in zip(x,y))
@pytest.fixture
def simple2p1f():
env = gym.make("Foraging-8x8-2p-1f-v2")
_ = env.reset()
import time
env.field[:] = 0
env.field[4,4] = 2
env._food_spawned = env.field.sum()
env.players[0].position = (4,3)
env.players[1].position = (4,5)
env.players[0].level = 2
env.players[1].level = 2
env._gen_valid_moves()
return env
@pytest.fixture
def simple2p1f_sight1():
env = gym.make("Foraging-8x8-2p-1f-v2", sight=1)
_ = env.reset()
import time
env.field[:] = 0
env.field[4,4] = 2
env._food_spawned = env.field.sum()
env.players[0].position = (4,3)
env.players[1].position = (4,5)
env.players[0].level = 2
env.players[1].level = 2
env._gen_valid_moves()
return env
@pytest.fixture
def simple2p1f_sight2():
env = gym.make("Foraging-8x8-2p-1f-v2", sight=2)
_ = env.reset()
import time
env.field[:] = 0
env.field[4,4] = 2
env._food_spawned = env.field.sum()
env.players[0].position = (4,3)
env.players[1].position = (4,5)
env.players[0].level = 2
env.players[1].level = 2
env._gen_valid_moves()
return env
def test_make():
env = gym.make("Foraging-8x8-2p-1f-v2")
env = gym.make("Foraging-5x5-2p-1f-v2")
env = gym.make("Foraging-8x8-3p-1f-v2")
env = gym.make("Foraging-8x8-3p-1f-coop-v2")
def test_spaces():
pass
def test_seed():
env = gym.make("Foraging-8x8-2p-2f-v2")
for seed in range(10):
obs1 = []
obs2 = []
env.seed(seed)
for r in range(10):
obs1.append(env.reset())
env.seed(seed)
for r in range(10):
obs2.append(env.reset())
for o1, o2 in zip(obs1, obs2):
assert np.array_equal(o1, o2)
def test_food_spawning_0():
env = gym.make("Foraging-6x6-2p-2f-v2")
for i in range(1000):
_ = env.reset()
foods = [np.array(f) for f in zip(*env.field.nonzero())]
# we should have 2 foods
assert len(foods) == 2
#foods must not be within 2 steps of each other
assert manhattan_distance(foods[0], foods[1]) > 2
# food cannot be placed in first or last col/row
assert foods[0][0] not in [0, 7]
assert foods[0][1] not in [0, 7]
assert foods[1][0] not in [0, 7]
assert foods[1][1] not in [0, 7]
def test_food_spawning_1():
env = gym.make("Foraging-8x8-2p-3f-v2")
for i in range(1000):
_ = env.reset()
foods = [np.array(f) for f in zip(*env.field.nonzero())]
# we should have 3 foods
assert len(foods) == 3
#foods must not be within 2 steps of each other
assert manhattan_distance(foods[0], foods[1]) > 2
assert manhattan_distance(foods[0], foods[2]) > 2
assert manhattan_distance(foods[1], foods[2]) > 2
def test_reward_0(simple2p1f):
_, rewards, _, _ = simple2p1f.step([5, 5])
assert rewards[0] == 0.5
assert rewards[1] == 0.5
def test_reward_1(simple2p1f):
_, rewards, _, _ = simple2p1f.step([0, 5])
assert rewards[0] == 0
assert rewards[1] == 1
def test_partial_obs_1(simple2p1f_sight1):
env = simple2p1f_sight1
obs = [env._make_obs(player) for player in env.players]
obs, _, _, _ = env._make_gym_obs(obs)
assert obs[0][-2] == -1
assert obs[1][-2] == -1
def test_partial_obs_2(simple2p1f_sight2):
env = simple2p1f_sight2
obs = [env._make_obs(player) for player in env.players]
obs, _, _, _ = env._make_gym_obs(obs)
assert obs[0][-2] > -1
assert obs[1][-2] > -1
obs, _, _, _ = env.step([Action.WEST, Action.NONE])
assert obs[0][-2] == -1
assert obs[1][-2] == -1
def test_partial_obs_3(simple2p1f):
env = simple2p1f
obs = [env._make_obs(player) for player in env.players]
obs, _, _, _ = env._make_gym_obs(obs)
assert obs[0][-2] > -1
assert obs[1][-2] > -1
obs, _, _, _ = env.step([Action.WEST, Action.NONE])
assert obs[0][-2] > -1
assert obs[1][-2] > -1