Skip to content

Commit 323509e

Browse files
Introduce DoublesEnv (#687)
* initial commit * black * unused imports * fix this * don't need this * fix * put import back * import * fix * fix * simplify * name fix * simplify * fix * introduce SinglesEnv * fix tests * unused import * fix integration tests * fix examples * simplify * update docs * rename files * format * polish * unused import * condense code * polish * add doubles_env file * add into tests * format * raise time limit * add unit test * fix * raise time limit * cleanup * bugfix * bugfix * add strictness * fix test * format and parameterize strict * add parameter * add strict for doubles * format; fix test * fix * format * bring this back to the way it was before * fix test * fix assertion * invalid causes default * invalid causes default * fix test * fix test * fix test * experiment * debug * debug * fix bug * cleanup * put tests back to normal * avoid returning DefaultBattleOrder if at all possible during conversions * add "fake" parameter which will allow conversions to be fabricated if they are invalid, if at all possible * add fake as parameter to env * bugfix * fix test * implement fake parameter for doubles * bugfix * fix test * fix * fix forfeit behavior * add pass logic * fix * make pass logic "iff" style * fix trying_again * even more true * simplify * simplify and remove maybe_default_order logic * this doesn't work great * bugfix * bugfix * bugfix * simplify and format * bugfix * go back to how it used to be * better name and bugfix * bugfix * cover zoroark nonsense * better coverage of nonsense * fix * cover trapped * refine assertions * assert fixing and improving * fix * no more start_challenging * fix tests * fix * fix * fix * split up test file * improve docstring
1 parent d2b9cc4 commit 323509e

File tree

5 files changed

+555
-8
lines changed

5 files changed

+555
-8
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import numpy as np
2+
import pytest
3+
from gymnasium.spaces import Box
4+
from gymnasium.utils.env_checker import check_env
5+
from pettingzoo.test.parallel_test import parallel_api_test
6+
7+
from poke_env.environment import DoublesEnv, SingleAgentWrapper
8+
from poke_env.player import RandomPlayer
9+
10+
11+
class DoublesTestEnv(DoublesEnv):
12+
def __init__(self, **kwargs):
13+
super().__init__(**kwargs)
14+
self.observation_spaces = {
15+
agent: Box(np.array([0]), np.array([1]), dtype=np.int64)
16+
for agent in self.possible_agents
17+
}
18+
19+
def calc_reward(self, battle) -> float:
20+
return 0.0
21+
22+
def embed_battle(self, battle):
23+
return np.array([0])
24+
25+
26+
def play_function(env, n_battles):
27+
for _ in range(n_battles):
28+
done = False
29+
env.reset()
30+
while not done:
31+
actions = {name: env.action_space(name).sample() for name in env.agents}
32+
_, _, terminated, truncated, _ = env.step(actions)
33+
done = any(terminated.values()) or any(truncated.values())
34+
35+
36+
@pytest.mark.timeout(120)
37+
def test_env_run():
38+
for gen in range(8, 10):
39+
env = DoublesTestEnv(
40+
battle_format=f"gen{gen}randomdoublesbattle",
41+
log_level=25,
42+
strict=False,
43+
)
44+
play_function(env, 10)
45+
env.close()
46+
47+
48+
def single_agent_play_function(env: SingleAgentWrapper, n_battles: int):
49+
for _ in range(n_battles):
50+
done = False
51+
env.reset()
52+
while not done:
53+
action = env.action_space.sample()
54+
_, _, terminated, truncated, _ = env.step(action)
55+
done = terminated or truncated
56+
57+
58+
@pytest.mark.timeout(120)
59+
def test_single_agent_env_run():
60+
for gen in range(8, 10):
61+
env = DoublesTestEnv(
62+
battle_format=f"gen{gen}randomdoublesbattle",
63+
log_level=25,
64+
strict=False,
65+
)
66+
env = SingleAgentWrapper(env, RandomPlayer())
67+
single_agent_play_function(env, 10)
68+
env.close()
69+
70+
71+
@pytest.mark.timeout(60)
72+
def test_repeated_runs():
73+
env = DoublesTestEnv(
74+
battle_format="gen8randomdoublesbattle",
75+
log_level=25,
76+
strict=False,
77+
)
78+
play_function(env, 2)
79+
play_function(env, 2)
80+
env.close()
81+
env = DoublesTestEnv(
82+
battle_format="gen9randomdoublesbattle",
83+
log_level=25,
84+
strict=False,
85+
)
86+
play_function(env, 2)
87+
play_function(env, 2)
88+
env.close()
89+
90+
91+
@pytest.mark.timeout(60)
92+
def test_env_api():
93+
for gen in range(8, 10):
94+
env = DoublesTestEnv(
95+
battle_format=f"gen{gen}randomdoublesbattle",
96+
log_level=25,
97+
strict=False,
98+
)
99+
parallel_api_test(env)
100+
env.close()
101+
102+
103+
@pytest.mark.timeout(60)
104+
def test_single_agent_env_api():
105+
for gen in range(8, 10):
106+
env = DoublesTestEnv(
107+
battle_format=f"gen{gen}randomdoublesbattle",
108+
log_level=25,
109+
strict=False,
110+
)
111+
env = SingleAgentWrapper(env, RandomPlayer())
112+
check_env(env)
113+
env.close()

integration_tests/test_env.py renamed to integration_tests/test_singles_env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def single_agent_play_function(env: SingleAgentWrapper, n_battles: int):
5555
done = terminated or truncated
5656

5757

58-
@pytest.mark.timeout(60)
58+
@pytest.mark.timeout(120)
5959
def test_single_agent_env_run():
6060
for gen in range(4, 10):
6161
env = SinglesTestEnv(

src/poke_env/environment/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pettingzoo.utils.env import ActionType, ObsType # type: ignore[import-untyped]
22

3+
from poke_env.environment.doubles_env import DoublesEnv
34
from poke_env.environment.env import PokeEnv
45
from poke_env.environment.single_agent_wrapper import SingleAgentWrapper
56
from poke_env.environment.singles_env import SinglesEnv
@@ -10,4 +11,5 @@
1011
"PokeEnv",
1112
"SingleAgentWrapper",
1213
"SinglesEnv",
14+
"DoublesEnv",
1315
]

0 commit comments

Comments
 (0)