cuju-engine 0.3.0__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. cuju/__init__.py +217 -0
  2. cuju/__main__.py +262 -0
  3. cuju/_batch/__init__.py +4 -0
  4. cuju/_batch/allocator.py +52 -0
  5. cuju/_batch/assets.py +68 -0
  6. cuju/_batch/environment.py +1890 -0
  7. cuju/_batch/frame_stack.py +114 -0
  8. cuju/_batch/library.py +190 -0
  9. cuju/_batch/log.py +103 -0
  10. cuju/_batch/memory.py +70 -0
  11. cuju/_batch/operators.py +55 -0
  12. cuju/_batch/pinned.py +39 -0
  13. cuju/_batch/pixels.py +207 -0
  14. cuju/_batch/state.py +77 -0
  15. cuju/_checksums.py +60 -0
  16. cuju/_cli_contract.py +244 -0
  17. cuju/_cli_parsers.py +191 -0
  18. cuju/_completion.py +277 -0
  19. cuju/_dump_to_txt.py +66 -0
  20. cuju/_grf_config.py +331 -0
  21. cuju/_library_path.py +141 -0
  22. cuju/_media.py +113 -0
  23. cuju/_pixels.py +276 -0
  24. cuju/_plugins.py +106 -0
  25. cuju/_python_players.py +224 -0
  26. cuju/_replay_command.py +109 -0
  27. cuju/_replay_player.py +42 -0
  28. cuju/_replays.py +414 -0
  29. cuju/_script_helpers.py +109 -0
  30. cuju/_version.py +6 -0
  31. cuju/action_log.py +371 -0
  32. cuju/check.py +596 -0
  33. cuju/cuju_cpu.dll +0 -0
  34. cuju/cuju_cuda.dll +0 -0
  35. cuju/cuju_render.dll +0 -0
  36. cuju/data/SHA256SUMS +20 -0
  37. cuju/data/animations.bin +0 -0
  38. cuju/data/assets/11_vs_11_competition_l0_r0.cjb +0 -0
  39. cuju/data/assets/11_vs_11_easy_stochastic_l0_r0.cjb +0 -0
  40. cuju/data/assets/11_vs_11_hard_stochastic_l0_r0.cjb +0 -0
  41. cuju/data/assets/11_vs_11_kaggle_l0_r0.cjb +0 -0
  42. cuju/data/assets/11_vs_11_stochastic_l0_r0.cjb +0 -0
  43. cuju/data/assets/1_vs_1_easy_l0_r0.cjb +0 -0
  44. cuju/data/assets/5_vs_5_l0_r0.cjb +0 -0
  45. cuju/data/assets/academy_3_vs_1_with_keeper_l0_r0.cjb +0 -0
  46. cuju/data/assets/academy_corner_l0_r0.cjb +0 -0
  47. cuju/data/assets/academy_counterattack_easy_l0_r0.cjb +0 -0
  48. cuju/data/assets/academy_counterattack_hard_l0_r0.cjb +0 -0
  49. cuju/data/assets/academy_empty_goal_close_l0_r0.cjb +0 -0
  50. cuju/data/assets/academy_empty_goal_l0_r0.cjb +0 -0
  51. cuju/data/assets/academy_pass_and_shoot_with_keeper_l0_r0.cjb +0 -0
  52. cuju/data/assets/academy_run_pass_and_shoot_with_keeper_l0_r0.cjb +0 -0
  53. cuju/data/assets/academy_run_to_score_l0_r0.cjb +0 -0
  54. cuju/data/assets/academy_run_to_score_with_keeper_l0_r0.cjb +0 -0
  55. cuju/data/assets/academy_single_goal_versus_lazy_l0_r0.cjb +0 -0
  56. cuju/data/assets/test_example_multiagent_l0_r0.cjb +0 -0
  57. cuju/data/digests.json +186 -0
  58. cuju/data/render_digests.json.z +0 -0
  59. cuju/data/scenarios/11_vs_11_competition.py +45 -0
  60. cuju/data/scenarios/11_vs_11_easy_stochastic.py +44 -0
  61. cuju/data/scenarios/11_vs_11_hard_stochastic.py +44 -0
  62. cuju/data/scenarios/11_vs_11_kaggle.py +46 -0
  63. cuju/data/scenarios/11_vs_11_stochastic.py +44 -0
  64. cuju/data/scenarios/1_vs_1_easy.py +25 -0
  65. cuju/data/scenarios/5_vs_5.py +33 -0
  66. cuju/data/scenarios/academy_3_vs_1_with_keeper.py +28 -0
  67. cuju/data/scenarios/academy_corner.py +45 -0
  68. cuju/data/scenarios/academy_counterattack_easy.py +45 -0
  69. cuju/data/scenarios/academy_counterattack_hard.py +45 -0
  70. cuju/data/scenarios/academy_empty_goal.py +25 -0
  71. cuju/data/scenarios/academy_empty_goal_close.py +25 -0
  72. cuju/data/scenarios/academy_pass_and_shoot_with_keeper.py +27 -0
  73. cuju/data/scenarios/academy_run_pass_and_shoot_with_keeper.py +27 -0
  74. cuju/data/scenarios/academy_run_to_score.py +30 -0
  75. cuju/data/scenarios/academy_run_to_score_with_keeper.py +30 -0
  76. cuju/data/scenarios/academy_single_goal_versus_lazy.py +49 -0
  77. cuju/data/scenarios/test_example_multiagent.py +28 -0
  78. cuju/dumps.py +942 -0
  79. cuju/engine_status.py +106 -0
  80. cuju/env.py +1168 -0
  81. cuju/errors.py +108 -0
  82. cuju/gfootball/GRF_SOURCES +86 -0
  83. cuju/gfootball/__init__.py +60 -0
  84. cuju/gfootball/_cv2.py +43 -0
  85. cuju/gfootball/_gym/__init__.py +88 -0
  86. cuju/gfootball/_gym/_vendored/GYM_SOURCES +22 -0
  87. cuju/gfootball/_gym/_vendored/__init__.py +43 -0
  88. cuju/gfootball/_gym/_vendored/core.py +361 -0
  89. cuju/gfootball/_gym/_vendored/envs/__init__.py +19 -0
  90. cuju/gfootball/_gym/_vendored/envs/registration.py +284 -0
  91. cuju/gfootball/_gym/_vendored/error.py +230 -0
  92. cuju/gfootball/_gym/_vendored/logger.py +56 -0
  93. cuju/gfootball/_gym/_vendored/spaces/__init__.py +30 -0
  94. cuju/gfootball/_gym/_vendored/spaces/box.py +171 -0
  95. cuju/gfootball/_gym/_vendored/spaces/dict.py +162 -0
  96. cuju/gfootball/_gym/_vendored/spaces/discrete.py +41 -0
  97. cuju/gfootball/_gym/_vendored/spaces/multi_binary.py +61 -0
  98. cuju/gfootball/_gym/_vendored/spaces/multi_discrete.py +77 -0
  99. cuju/gfootball/_gym/_vendored/spaces/space.py +94 -0
  100. cuju/gfootball/_gym/_vendored/spaces/tuple.py +96 -0
  101. cuju/gfootball/_gym/_vendored/spaces/utils.py +228 -0
  102. cuju/gfootball/_gym/_vendored/utils/__init__.py +13 -0
  103. cuju/gfootball/_gym/_vendored/utils/closer.py +72 -0
  104. cuju/gfootball/_gym/_vendored/utils/colorize.py +36 -0
  105. cuju/gfootball/_gym/_vendored/utils/ezpickle.py +37 -0
  106. cuju/gfootball/_gym/_vendored/utils/json_utils.py +27 -0
  107. cuju/gfootball/_gym/_vendored/utils/seeding.py +101 -0
  108. cuju/gfootball/_gym/_vendored/version.py +5 -0
  109. cuju/gfootball/_gym/_vendored/wrappers/__init__.py +8 -0
  110. cuju/gfootball/_gym/_vendored/wrappers/order_enforcing.py +20 -0
  111. cuju/gfootball/_gym/_vendored/wrappers/time_limit.py +31 -0
  112. cuju/gfootball/dump_to_txt.py +49 -0
  113. cuju/gfootball/dump_to_video.py +33 -0
  114. cuju/gfootball/env/__init__.py +271 -0
  115. cuju/gfootball/env/config.py +163 -0
  116. cuju/gfootball/env/constants.py +33 -0
  117. cuju/gfootball/env/controller_base.py +114 -0
  118. cuju/gfootball/env/event_queue.py +54 -0
  119. cuju/gfootball/env/football_action_set.py +220 -0
  120. cuju/gfootball/env/football_action_set_test.py +76 -0
  121. cuju/gfootball/env/football_env.py +225 -0
  122. cuju/gfootball/env/football_env_core.py +447 -0
  123. cuju/gfootball/env/football_env_test.py +692 -0
  124. cuju/gfootball/env/gym_test.py +47 -0
  125. cuju/gfootball/env/observation_preprocessing.py +94 -0
  126. cuju/gfootball/env/observation_processor.py +526 -0
  127. cuju/gfootball/env/observation_rotation.py +163 -0
  128. cuju/gfootball/env/observation_rotation_test.py +84 -0
  129. cuju/gfootball/env/player_base.py +46 -0
  130. cuju/gfootball/env/players/__init__.py +16 -0
  131. cuju/gfootball/env/players/agent.py +39 -0
  132. cuju/gfootball/env/players/bot.py +217 -0
  133. cuju/gfootball/env/players/gamepad.py +81 -0
  134. cuju/gfootball/env/players/keyboard.py +72 -0
  135. cuju/gfootball/env/players/lazy.py +31 -0
  136. cuju/gfootball/env/players/ppo2_cnn.py +119 -0
  137. cuju/gfootball/env/players/replay.py +40 -0
  138. cuju/gfootball/env/remote_football_env.py +189 -0
  139. cuju/gfootball/env/scenario_builder.py +125 -0
  140. cuju/gfootball/env/script_helpers.py +130 -0
  141. cuju/gfootball/env/script_helpers_test.py +107 -0
  142. cuju/gfootball/env/wrappers.py +440 -0
  143. cuju/gfootball/env/wrappers_test.py +150 -0
  144. cuju/gfootball/eval_server/__init__.py +17 -0
  145. cuju/gfootball/eval_server/config.py +30 -0
  146. cuju/gfootball/eval_server/example_client.py +107 -0
  147. cuju/gfootball/eval_server/proto/game_server.proto +62 -0
  148. cuju/gfootball/eval_server/proto/game_server_pb2.py +501 -0
  149. cuju/gfootball/eval_server/proto/game_server_pb2_grpc.py +113 -0
  150. cuju/gfootball/eval_server/proto/master.proto +25 -0
  151. cuju/gfootball/eval_server/proto/master_pb2.py +184 -0
  152. cuju/gfootball/eval_server/proto/master_pb2_grpc.py +62 -0
  153. cuju/gfootball/eval_server/utils.py +52 -0
  154. cuju/gfootball/examples/__init__.py +16 -0
  155. cuju/gfootball/examples/models.py +66 -0
  156. cuju/gfootball/examples/run_multiagent_rllib.py +140 -0
  157. cuju/gfootball/examples/run_ppo2.py +123 -0
  158. cuju/gfootball/play_game.py +73 -0
  159. cuju/gfootball/replay.py +43 -0
  160. cuju/gfootball/scenarios/11_vs_11_competition.py +59 -0
  161. cuju/gfootball/scenarios/11_vs_11_easy_stochastic.py +57 -0
  162. cuju/gfootball/scenarios/11_vs_11_hard_stochastic.py +57 -0
  163. cuju/gfootball/scenarios/11_vs_11_kaggle.py +60 -0
  164. cuju/gfootball/scenarios/11_vs_11_stochastic.py +57 -0
  165. cuju/gfootball/scenarios/1_vs_1_easy.py +39 -0
  166. cuju/gfootball/scenarios/5_vs_5.py +46 -0
  167. cuju/gfootball/scenarios/__init__.py +31 -0
  168. cuju/gfootball/scenarios/academy_3_vs_1_with_keeper.py +42 -0
  169. cuju/gfootball/scenarios/academy_corner.py +59 -0
  170. cuju/gfootball/scenarios/academy_counterattack_easy.py +59 -0
  171. cuju/gfootball/scenarios/academy_counterattack_hard.py +59 -0
  172. cuju/gfootball/scenarios/academy_empty_goal.py +39 -0
  173. cuju/gfootball/scenarios/academy_empty_goal_close.py +39 -0
  174. cuju/gfootball/scenarios/academy_pass_and_shoot_with_keeper.py +41 -0
  175. cuju/gfootball/scenarios/academy_run_pass_and_shoot_with_keeper.py +41 -0
  176. cuju/gfootball/scenarios/academy_run_to_score.py +44 -0
  177. cuju/gfootball/scenarios/academy_run_to_score_with_keeper.py +44 -0
  178. cuju/gfootball/scenarios/academy_single_goal_versus_lazy.py +63 -0
  179. cuju/gfootball/scenarios/test_example_multiagent.py +42 -0
  180. cuju/gfootball/scenarios/tests/11_vs_11_deterministic.py +58 -0
  181. cuju/gfootball/scenarios/tests/11_vs_11_easy_deterministic.py +58 -0
  182. cuju/gfootball/scenarios/tests/11_vs_11_hard_deterministic.py +58 -0
  183. cuju/gfootball/scenarios/tests/11_vs_11_single_goal_deterministic.py +58 -0
  184. cuju/gfootball/scenarios/tests/11_vs_11_single_goal_stochastic.py +58 -0
  185. cuju/gfootball/scenarios/tests/11_vs_4_offence_deterministic.py +47 -0
  186. cuju/gfootball/scenarios/tests/11_vs_4_offence_stochastic.py +47 -0
  187. cuju/gfootball/scenarios/tests/__init__.py +31 -0
  188. cuju/gfootball/scenarios/tests/corner_test.py +38 -0
  189. cuju/gfootball/scenarios/tests/goal_test.py +35 -0
  190. cuju/gfootball/scenarios/tests/keeper_test.py +38 -0
  191. cuju/gfootball/scenarios/tests/kickoff_for_goal_loosing_team_test.py +34 -0
  192. cuju/gfootball/scenarios/tests/multiagent_wrapper.py +60 -0
  193. cuju/gfootball/scenarios/tests/offside_test.py +39 -0
  194. cuju/gfootball/scenarios/tests/penalty.py +34 -0
  195. cuju/gfootball/scenarios/tests/second_half.py +33 -0
  196. cuju/gfootball/scenarios/tests/symmetric.py +53 -0
  197. cuju/gfootball_engine/__init__.py +31 -0
  198. cuju/gfootball_engine/_gameplayfootball.py +1624 -0
  199. cuju/gfootball_engine/_native.py +193 -0
  200. cuju/gfootball_engine/_pickle_names.py +114 -0
  201. cuju/gfootball_engine/_render_hooks.py +156 -0
  202. cuju/kaggle.py +425 -0
  203. cuju/multi_device.py +401 -0
  204. cuju/players.py +260 -0
  205. cuju/raw.py +93 -0
  206. cuju/scenarios.py +661 -0
  207. cuju/state.py +211 -0
  208. cuju/wrappers.py +325 -0
  209. cuju_engine-0.3.0.dist-info/METADATA +325 -0
  210. cuju_engine-0.3.0.dist-info/RECORD +222 -0
  211. cuju_engine-0.3.0.dist-info/WHEEL +5 -0
  212. cuju_engine-0.3.0.dist-info/entry_points.txt +4 -0
  213. cuju_engine-0.3.0.dist-info/licenses/LICENSE +202 -0
  214. cuju_engine-0.3.0.dist-info/licenses/LICENSES/BSD-3-Clause.txt +31 -0
  215. cuju_engine-0.3.0.dist-info/licenses/LICENSES/BSL-1.0.txt +23 -0
  216. cuju_engine-0.3.0.dist-info/licenses/LICENSES/LGPL-2.1-or-later.txt +502 -0
  217. cuju_engine-0.3.0.dist-info/licenses/LICENSES/MIT-Mesa.txt +26 -0
  218. cuju_engine-0.3.0.dist-info/licenses/LICENSES/MIT-gym.txt +23 -0
  219. cuju_engine-0.3.0.dist-info/licenses/LICENSES/MIT.txt +23 -0
  220. cuju_engine-0.3.0.dist-info/licenses/LICENSES/SunPro.txt +8 -0
  221. cuju_engine-0.3.0.dist-info/licenses/LICENSES/Zlib.txt +20 -0
  222. cuju_engine-0.3.0.dist-info/licenses/NOTICE +56 -0
@@ -0,0 +1,692 @@
1
+ # coding=utf-8
2
+ # Copyright 2019 Google LLC
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # Modified by the cuju Authors in 2026, in the import lines that ci/port_gfootball.py lists.
15
+ # SPDX-License-Identifier: Apache-2.0
16
+
17
+
18
+ """Football environment E2E test."""
19
+
20
+ from __future__ import absolute_import
21
+ from __future__ import division
22
+ from __future__ import print_function
23
+ from absl.testing import parameterized
24
+ from collections.abc import Iterable
25
+ from multiprocessing import pool
26
+ from multiprocessing import Queue
27
+ from cuju import gfootball
28
+ import os
29
+ import platform
30
+ import sys
31
+ import random
32
+ import threading
33
+ import atexit
34
+ import zlib
35
+ from absl.testing import absltest
36
+
37
+ from cuju.gfootball.env import config
38
+ from cuju.gfootball.env import football_action_set
39
+ from cuju.gfootball.env import football_env
40
+ from cuju.gfootball.env import wrappers
41
+ from cuju.gfootball.env import scenario_builder
42
+ import numpy as np
43
+ import psutil
44
+ from six.moves import range
45
+
46
+ fast_run = False
47
+
48
+
49
+ def observation_hash(observation, hash_value = 0):
50
+ for obs in observation:
51
+ for key, value in sorted(obs.items()):
52
+ hash_value = zlib.adler32(key.encode(), hash_value)
53
+ hash_value = zlib.adler32(np.ascontiguousarray(value), hash_value)
54
+ return hash_value
55
+
56
+
57
+ def compute_hash(env, actions):
58
+ """Computes hash of observations returned by environment for a given scenario.
59
+
60
+ Args:
61
+ env: environment
62
+ actions: number of actions
63
+
64
+ Returns:
65
+ hash
66
+ """
67
+ o = env.reset()
68
+ hash_value = observation_hash(o)
69
+ done = False
70
+ step = 0
71
+ while not done:
72
+ o, _, done, _ = env.step(step % actions)
73
+ hash_value = observation_hash(o, hash_value)
74
+ step += 1
75
+ if step >= 200:
76
+ break
77
+ return hash_value
78
+
79
+
80
+ def run_scenario(cfg, queue, actions, render=False, validation=True):
81
+ env = football_env.FootballEnv(cfg)
82
+ if render:
83
+ env.render()
84
+ obs = env.reset()
85
+ queue.put(obs)
86
+ if validation:
87
+ # Otherwise the following error is generated on Windows:
88
+ # OverflowError: Python int too large to convert to C long
89
+ # The number can be also derived by
90
+ # import struct
91
+ # end = 2 ** (struct.Struct('l').size * 8 - 1) - 1
92
+ end = 999999999999999 if platform.system() != 'Windows' else 2147483647
93
+ env.tracker_setup(0, end)
94
+ done = False
95
+ step = 0
96
+ while not done:
97
+ if isinstance(actions, Iterable):
98
+ if step >= len(actions):
99
+ break
100
+ action = actions[step]
101
+ else:
102
+ action = actions.get()
103
+ if action is None:
104
+ break
105
+ step += 1
106
+ if isinstance(action, Iterable):
107
+ obs, _, done, _ = env.step(action)
108
+ else:
109
+ obs, _, done, _ = env.step([action, action])
110
+ queue.put(obs)
111
+ queue.put(None)
112
+ env.close()
113
+
114
+
115
+ def normalize_observation(o):
116
+ if o['ball'][0] == -0:
117
+ o['ball'][0] = 0
118
+ if o['ball'][1] == -0:
119
+ o['ball'][1] = 0
120
+ if o['ball_direction'][0] == -0:
121
+ o['ball_direction'][0] = 0
122
+ if o['ball_direction'][1] == -0:
123
+ o['ball_direction'][1] = 0
124
+
125
+
126
+ class FootballEnvTest(parameterized.TestCase):
127
+
128
+ def compare_observations(self, l1, l2):
129
+ for o1, o2 in zip(l1, l2):
130
+ if 'frame' in o1 and 'frame' not in o2:
131
+ del o1['frame']
132
+ elif 'frame' in o2 and 'frame' not in o1:
133
+ del o2['frame']
134
+ normalize_observation(o1)
135
+ normalize_observation(o2)
136
+ o1 = str(tuple(sorted(o1.items())))
137
+ o2 = str(tuple(sorted(o2.items())))
138
+ self.assertEqual(o1, o2)
139
+
140
+ def test___control_all_players(self):
141
+ """Validate MultiAgentToSingleAgent wrapper and control_all_players flag."""
142
+ try:
143
+ gfootball.env.create_environment(
144
+ env_name='tests.multiagent_wrapper',
145
+ rewards='checkpoints,scoring',
146
+ number_of_left_players_agent_controls=2)
147
+ except AssertionError:
148
+ pass
149
+ else:
150
+ self.assertTrue(False)
151
+
152
+ env = gfootball.env.create_environment(
153
+ env_name='tests.multiagent_wrapper',
154
+ rewards='checkpoints,scoring',
155
+ representation='simple115v2',
156
+ number_of_left_players_agent_controls=11,
157
+ number_of_right_players_agent_controls=11)
158
+ obs = env.reset()
159
+ self.assertLen(obs, 22)
160
+ self.assertIn(obs, env.observation_space)
161
+
162
+ env = gfootball.env.create_environment(
163
+ env_name='tests.multiagent_wrapper',
164
+ rewards='checkpoints,scoring',
165
+ number_of_left_players_agent_controls=11,
166
+ number_of_right_players_agent_controls=0)
167
+ obs = env.reset()
168
+ self.assertLen(obs, 11)
169
+ self.assertIn(obs, env.observation_space)
170
+
171
+ env = gfootball.env.create_environment(
172
+ env_name='tests.multiagent_wrapper',
173
+ rewards='checkpoints,scoring',
174
+ representation='simple115v2',
175
+ number_of_left_players_agent_controls=0,
176
+ number_of_right_players_agent_controls=11)
177
+ obs = env.reset()
178
+ self.assertLen(obs, 11)
179
+ self.assertIn(obs, env.observation_space)
180
+
181
+ env = gfootball.env.create_environment(
182
+ env_name='tests.multiagent_wrapper',
183
+ rewards='checkpoints,scoring',
184
+ number_of_left_players_agent_controls=1,
185
+ number_of_right_players_agent_controls=1)
186
+ obs = env.reset()
187
+ self.assertLen(obs, 2)
188
+ self.assertIn(obs, env.observation_space)
189
+
190
+ env = gfootball.env.create_environment(
191
+ env_name='tests.multiagent_wrapper',
192
+ rewards='checkpoints,scoring',
193
+ number_of_left_players_agent_controls=1)
194
+ obs = env.reset()
195
+ self.assertEqual(np.shape(obs), (72, 96, 4))
196
+ self.assertIn(obs, env.observation_space)
197
+ obs, _, _, _ = env.step([football_action_set.action_left])
198
+ self.assertEqual(np.shape(obs), (72, 96, 4))
199
+ env = gfootball.env.create_environment(
200
+ env_name='tests.multiagent_wrapper',
201
+ rewards='checkpoints,scoring',
202
+ representation='raw',
203
+ number_of_left_players_agent_controls=1,
204
+ number_of_right_players_agent_controls=1)
205
+ obs = env.reset()
206
+ self.assertLen(obs, 2)
207
+ self.assertEqual(obs[0]['sticky_actions'][0], 0)
208
+ self.assertEqual(obs[1]['sticky_actions'][4], 0)
209
+ obs, _, _, _ = env.step(
210
+ [football_action_set.action_idle, football_action_set.action_idle])
211
+ obs, _, _, _ = env.step(
212
+ [football_action_set.action_left, football_action_set.action_right])
213
+ self.assertLen(obs, 2)
214
+ self.assertEqual(obs[0]['sticky_actions'][0], 1)
215
+ self.assertEqual(obs[1]['sticky_actions'][4], 1)
216
+
217
+ def test_score_empty_goal(self):
218
+ """Score on an empty goal."""
219
+ cfg = config.Config()
220
+
221
+ env = football_env.FootballEnv(cfg)
222
+ cfg['level'] = 'academy_empty_goal'
223
+ last_o = env.reset()[0]
224
+ for _ in range(120):
225
+ o, reward, done, _ = env.step(football_action_set.action_right)
226
+ o = o[0]
227
+ if done:
228
+ self.assertEqual(reward, 1)
229
+ break
230
+ self.assertFalse(done)
231
+ self.assertGreaterEqual(o['ball'][0], last_o['ball'][0] - 0.01)
232
+ self.assertGreaterEqual(
233
+ o['left_team'][o['active']][0],
234
+ last_o['left_team'][last_o['active']][0] - 0.01)
235
+ last_o = o
236
+ self.assertTrue(done)
237
+ env.close()
238
+
239
+ def test_second_half(self):
240
+ """Test second half feature."""
241
+ cfg = config.Config()
242
+ cfg['level'] = 'tests.second_half'
243
+ env = football_env.FootballEnv(cfg)
244
+ for _ in range(5):
245
+ o, _, done, _ = env.step(football_action_set.action_idle)
246
+ self.assertFalse(done)
247
+ self.assertAlmostEqual(o[0]['left_team'][o[0]['active']][0], 0, delta=0.1)
248
+ for _ in range(6):
249
+ self.assertFalse(done)
250
+ o, _, done, _ = env.step(football_action_set.action_idle)
251
+ self.assertAlmostEqual(
252
+ o[0]['left_team'][o[0]['active']][0], -0.5, delta=0.1)
253
+ self.assertTrue(done)
254
+ env.close()
255
+
256
+ def test___render(self):
257
+ """Make sure rendering is not broken."""
258
+ if 'UNITTEST_IN_DOCKER' in os.environ:
259
+ # Rendering is not supported.
260
+ return
261
+ cfg = config.Config({
262
+ 'level': 'tests.11_vs_11_hard_deterministic',
263
+ })
264
+ env = football_env.FootballEnv(cfg)
265
+ env.render()
266
+ o = env.reset()
267
+ hash_value = observation_hash(o)
268
+ for _ in range(10):
269
+ o, _, _, _ = env.step(football_action_set.action_right)
270
+ hash_value = observation_hash(o, hash_value)
271
+ # Linux
272
+ expected_hash_value = 4000732293
273
+ if platform.system() == 'Windows':
274
+ # On Windows we may have to check four possible values:
275
+ # for each of architectures (32 or 64 bits)
276
+ # what SDK was used (latest or from Visual Studio 2017)
277
+ # We will check only two values once we start using windows-latest VM again
278
+ if sys.maxsize > 2 ** 32: # x64
279
+ expected_hash_value = 683941870 if hash_value != 3676773624 else 3676773624
280
+ else: # x86
281
+ expected_hash_value = 1490754124 if hash_value != 2808421794 else 2808421794
282
+ elif platform.system() == 'Darwin':
283
+ expected_hash_value = 1865563121
284
+ self.assertEqual(hash_value, expected_hash_value)
285
+ env.close()
286
+
287
+ def test_dynamic_render(self):
288
+ """Verifies dynamic render support."""
289
+ if 'UNITTEST_IN_DOCKER' in os.environ:
290
+ # Rendering is not supported.
291
+ return
292
+ cfg = config.Config({
293
+ 'level': 'tests.11_vs_11_hard_deterministic',
294
+ })
295
+ env = football_env.FootballEnv(cfg)
296
+ o = env.reset()
297
+ for _ in range(10):
298
+ o, _, _, _ = env.step(football_action_set.action_right)
299
+ self.assertNotIn('frame', o[0])
300
+ env.render()
301
+ self.assertIn('frame', env.observation()[0])
302
+ self.compare_observations(o, env.observation())
303
+ o, _, _, _ = env.step(football_action_set.action_right)
304
+ self.assertIn('frame', env.observation()[0])
305
+ env.disable_render()
306
+ self.compare_observations(o, env.observation())
307
+ env.close()
308
+
309
+ def test_different_action_formats(self):
310
+ """Verify different action formats are accepted."""
311
+ cfg = config.Config()
312
+ env = football_env.FootballEnv(cfg)
313
+ env.reset()
314
+ env.step(football_action_set.action_right)
315
+ env.step([football_action_set.action_right])
316
+ env.step(np.array([football_action_set.action_right]))
317
+ env.step(np.array(football_action_set.action_right))
318
+ env.close()
319
+
320
+ def test_multi_instance(self):
321
+ """Validates that two instances of the env can run in the same thread."""
322
+
323
+ def run_episode():
324
+ if 'UNITTEST_IN_DOCKER' in os.environ:
325
+ return 0
326
+ cfg = config.Config({
327
+ 'level': 'tests.11_vs_11_hard_deterministic'
328
+ })
329
+ env = football_env.FootballEnv(cfg)
330
+ actions = len(football_action_set.get_action_set(cfg))
331
+ hash_value = compute_hash(env, actions)
332
+ env.close()
333
+ return hash_value
334
+
335
+ tpool = pool.ThreadPool(processes=2)
336
+ atexit.register(tpool.close)
337
+ run1 = tpool.apply_async(run_episode)
338
+ run2 = tpool.apply_async(run_episode)
339
+ self.assertEqual(run1.get(), run2.get())
340
+
341
+ def test_multi_render(self):
342
+ """Only one rendering instance allowed at a time."""
343
+ if 'UNITTEST_IN_DOCKER' in os.environ:
344
+ # Rendering is not supported.
345
+ return
346
+ cfg = config.Config({})
347
+ env1 = football_env.FootballEnv(cfg)
348
+ env1.render()
349
+ env1.reset()
350
+
351
+ env2 = football_env.FootballEnv(cfg)
352
+ try:
353
+ env2.render()
354
+ except AssertionError:
355
+ env1.close()
356
+ env2.close()
357
+ # It is still possible to render.
358
+ env3 = football_env.FootballEnv(cfg)
359
+ env3.reset()
360
+ env3.close()
361
+ return
362
+ assert False, 'Exception expected'
363
+
364
+ def test_scenarios_are_at_least_loading(self):
365
+ cfg = config.Config()
366
+ for l in scenario_builder.all_scenarios():
367
+ cfg['level'] = l
368
+ unused_game_cfg = cfg.ScenarioConfig()
369
+
370
+ def memory_usage(self):
371
+ process = psutil.Process(os.getpid())
372
+ return process.memory_info().rss
373
+
374
+ def test__memory_usage(self):
375
+ """Make sure memory usage is low when not recording videos."""
376
+ # This test has to go first, so that memory usage is not affected.
377
+ if 'UNITTEST_IN_DOCKER' in os.environ:
378
+ # Forge doesn't support rendering.
379
+ return
380
+ cfg = config.Config({'write_video': False})
381
+ env = football_env.FootballEnv(cfg)
382
+ env.render()
383
+ env.reset()
384
+ initial_memory = self.memory_usage()
385
+ for _ in range(100):
386
+ _, _, _, _ = env.step(football_action_set.action_right)
387
+ memory_usage = self.memory_usage() - initial_memory
388
+ env.close()
389
+ self.assertGreaterEqual(10000000, memory_usage)
390
+
391
+ def test_player_order_invariant(self):
392
+ """Checks that environment behaves the same regardless of players order."""
393
+ players = ['agent:right_players=1', 'lazy:left_players=11']
394
+ cfg = config.Config({
395
+ 'level': 'tests.11_vs_11_hard_deterministic',
396
+ 'players': players
397
+ })
398
+ env = football_env.FootballEnv(cfg)
399
+ actions = len(football_action_set.get_action_set(cfg))
400
+ hash_value1 = compute_hash(env, actions)
401
+ players = [players[1], players[0]]
402
+ cfg = config.Config({
403
+ 'level': 'tests.11_vs_11_hard_deterministic',
404
+ 'players': players
405
+ })
406
+ env = football_env.FootballEnv(cfg)
407
+ hash_value2 = compute_hash(env, actions)
408
+ self.assertEqual(hash_value1, hash_value2)
409
+ env.close()
410
+
411
+ @parameterized.parameters(range(1))
412
+ def test_setstate(self, seed):
413
+ """Checks setState functionality."""
414
+ cfg1 = config.Config({
415
+ 'level': 'tests.symmetric',
416
+ 'game_engine_random_seed': seed,
417
+ 'reverse_team_processing': False
418
+ })
419
+ cfg2 = config.Config({
420
+ 'level': 'tests.symmetric',
421
+ 'game_engine_random_seed': seed + 10,
422
+ 'reverse_team_processing': False
423
+ })
424
+ env1 = football_env.FootballEnv(cfg1)
425
+ env2 = football_env.FootballEnv(cfg2)
426
+ initial_obs = env1.reset()
427
+ env2.reset()
428
+ initial_state = env1.get_state()
429
+ env2.set_state(initial_state)
430
+ random.seed(seed)
431
+ actions = len(football_action_set.get_action_set(cfg1))
432
+ first_action = random.randint(0, actions - 1)
433
+ first_obs, _, _, _ = env1.step(first_action)
434
+ _, _, _, _ = env2.step(first_action)
435
+ step = 0
436
+ limit = 10 if fast_run else 3000
437
+ while step < limit:
438
+ step += 1
439
+ action = random.randint(0, actions - 1)
440
+ if step % 10 == 0:
441
+ env2.set_state(initial_state)
442
+ self.compare_observations(initial_obs, env2.observation())
443
+ env2.step(first_action)
444
+ self.compare_observations(first_obs, env2.observation())
445
+ env2.set_state(env1.get_state())
446
+ self.compare_observations(env1.observation(), env2.observation())
447
+ _, _, done1, _ = env1.step(action)
448
+ _, _, done2, _ = env2.step(action)
449
+ self.assertEqual(done1, done2)
450
+ if done1:
451
+ break
452
+ env1.close()
453
+ env2.close()
454
+
455
+ @parameterized.parameters(range(1))
456
+ def test_symmetry(self, seed):
457
+ """Checks game symmetry."""
458
+ processes = []
459
+ cfg1 = config.Config({
460
+ 'level': 'tests.symmetric',
461
+ 'game_engine_random_seed': seed,
462
+ 'players': ['agent:left_players=1,right_players=1'],
463
+ 'reverse_team_processing': False,
464
+ })
465
+ cfg2 = config.Config({
466
+ 'level': 'tests.symmetric',
467
+ 'game_engine_random_seed': seed,
468
+ 'players': ['agent:left_players=1,right_players=1'],
469
+ 'reverse_team_processing': True,
470
+ })
471
+ random.seed(seed)
472
+ action_cnt = len(football_action_set.get_action_set(cfg1))
473
+ actions = [random.randint(0, action_cnt - 1) for _ in range(10 if fast_run else 3000)]
474
+ queue1 = Queue()
475
+ thread1 = threading.Thread(
476
+ target=run_scenario, args=(cfg1, queue1, actions))
477
+ thread1.start()
478
+ queue2 = Queue()
479
+ thread2 = threading.Thread(
480
+ target=run_scenario, args=(cfg2, queue2, actions))
481
+ thread2.start()
482
+ while True:
483
+ o1 = queue1.get()
484
+ o2 = queue2.get()
485
+ if not o1 or not o2:
486
+ self.assertEqual(o1, o2)
487
+ break
488
+ self.compare_observations(o1[:1], o2[1:])
489
+ self.compare_observations(o2[:1], o1[1:])
490
+ thread1.join()
491
+ thread2.join()
492
+
493
+ @parameterized.parameters((1, 'left', True), (0, 'right', True),
494
+ (1, 'left', False), (0, 'right', False))
495
+ def test_offside(self, episode, team2, reverse):
496
+ cfg = config.Config({
497
+ 'level': 'tests.offside_test',
498
+ 'players': ['agent:{}_players=1'.format(team2)],
499
+ 'episode_number': episode,
500
+ 'reverse_team_processing': reverse,
501
+ })
502
+ env = football_env.FootballEnv(cfg)
503
+ env.reset()
504
+ o, _, done, _ = env.step(football_action_set.action_long_pass)
505
+ done = False
506
+ while not done and o[0]['right_team'][1][0] == 0:
507
+ o, _, done, _ = env.step(football_action_set.action_idle)
508
+ self.assertAlmostEqual(o[0]['ball'][0], 0.6, delta=0.4)
509
+ self.assertAlmostEqual(o[0]['right_team'][0][0], 0.6, delta=0.4)
510
+ self.assertAlmostEqual(o[0]['right_team'][1][0], 0.6, delta=0.4)
511
+ self.assertAlmostEqual(o[0]['left_team'][0][0], -0.6, delta=0.4)
512
+ self.assertAlmostEqual(o[0]['left_team'][1][0], -0.6, delta=0.4)
513
+ env.close()
514
+
515
+ @parameterized.parameters((0, 1, True), (1, -1, True), (0, 1, False),
516
+ (1, -1, False))
517
+ def test_corner(self, episode, factor, reverse):
518
+ cfg = config.Config({
519
+ 'level': 'tests.corner_test',
520
+ 'players': ['agent:left_players=1,right_players=1'],
521
+ 'episode_number': episode,
522
+ 'reverse_team_processing': reverse,
523
+ })
524
+ env = football_env.FootballEnv(cfg)
525
+ o = env.reset()
526
+ done = False
527
+ while not done:
528
+ o, _, done, _ = env.step([football_action_set.action_left,
529
+ football_action_set.action_left])
530
+ self.assertAlmostEqual(o[0]['ball'][0], -0.95 * factor, delta=0.1)
531
+ self.assertAlmostEqual(o[0]['ball'][1], 0.4 * factor, delta=0.1)
532
+ self.assertAlmostEqual(o[0]['right_team'][0][0], 1, delta=0.1)
533
+ self.assertAlmostEqual(o[0]['right_team'][1][0], -0.95 * factor, delta=0.1)
534
+ self.assertAlmostEqual(o[0]['left_team'][0][0], -0.95, delta=0.1)
535
+ self.assertAlmostEqual(o[0]['left_team'][1][0], -0.9 * factor, delta=0.2)
536
+ env.close()
537
+
538
+ def test_penalty(self):
539
+ cfg = config.Config({
540
+ 'level': 'tests.penalty',
541
+ 'players': ['agent:left_players=1'],
542
+ })
543
+ env = football_env.FootballEnv(cfg)
544
+ o = env.reset()
545
+ done = False
546
+ while not done:
547
+ o, _, done, _ = env.step([football_action_set.action_sliding])
548
+ self.assertAlmostEqual(o[0]['ball'][0], -0.809, delta=0.01)
549
+ self.assertAlmostEqual(o[0]['ball'][1], 0.0, delta=0.01)
550
+ self.assertAlmostEqual(o[0]['right_team'][0][0], 1, delta=0.1)
551
+ self.assertAlmostEqual(o[0]['right_team'][1][0], -0.75, delta=0.1)
552
+ self.assertAlmostEqual(o[0]['left_team'][0][0], -0.95, delta=0.1)
553
+ self.assertAlmostEqual(o[0]['left_team'][1][0], -0.70, delta=0.1)
554
+ env.close()
555
+
556
+ @parameterized.parameters((0, -1, True), (1, 1, True), (0, -1, False),
557
+ (1, 1, False))
558
+ def test_keeper_ball(self, episode, factor, reverse):
559
+ cfg = config.Config({
560
+ 'level': 'tests.keeper_test',
561
+ 'players': ['agent:left_players=1,right_players=1'],
562
+ 'episode_number': episode,
563
+ 'reverse_team_processing': reverse,
564
+ })
565
+ env = football_env.FootballEnv(cfg)
566
+ o = env.reset()
567
+ done = False
568
+ while not done:
569
+ o, _, done, _ = env.step([football_action_set.action_right,
570
+ football_action_set.action_right])
571
+ self.assertAlmostEqual(o[0]['ball'][0], -1.0 * factor, delta=0.1)
572
+ self.assertAlmostEqual(o[0]['ball'][1], 0.0, delta=0.1)
573
+ self.assertAlmostEqual(o[0]['right_team'][0][0], 1, delta=0.1)
574
+ self.assertAlmostEqual(o[0]['right_team'][1][0], 0.4, delta=0.1)
575
+ self.assertAlmostEqual(o[0]['left_team'][0][0], -0.9, delta=0.1)
576
+ self.assertAlmostEqual(o[0]['left_team'][1][0], -0.33, delta=0.1)
577
+ env.close()
578
+
579
+ @parameterized.parameters((0, True), (1, True), (0, False), (1, False))
580
+ def test_goal(self, episode, reverse):
581
+ cfg = config.Config({
582
+ 'level': 'tests.goal_test',
583
+ 'players': ['agent:left_players=1,right_players=1'],
584
+ 'episode_number': episode,
585
+ 'reverse_team_processing': reverse,
586
+ })
587
+ env = football_env.FootballEnv(cfg)
588
+ o = env.reset()
589
+ done = False
590
+ while not done:
591
+ o, _, done, _ = env.step(
592
+ [football_action_set.action_right, football_action_set.action_right])
593
+ self.assertAlmostEqual(o[0]['ball'][0], 0.0, delta=0.1)
594
+ self.assertEqual(o[0]['score'][episode], 1)
595
+ self.assertEqual(o[0]['score'][1 - episode], 0)
596
+ env.close()
597
+
598
+ @parameterized.parameters(range(1))
599
+ def test_render_state_equals_norender(self, seed):
600
+ """Checks that rendering game state is the same as non-rendering."""
601
+ if 'UNITTEST_IN_DOCKER' in os.environ:
602
+ # Forge doesn't support rendering.
603
+ return
604
+ cfg1 = config.Config({
605
+ 'level': 'tests.symmetric',
606
+ 'game_engine_random_seed': seed,
607
+ 'players': ['agent:left_players=1,right_players=1'],
608
+ 'reverse_team_processing': False,
609
+ })
610
+ cfg2 = config.Config({
611
+ 'level': 'tests.symmetric',
612
+ 'game_engine_random_seed': seed,
613
+ 'players': ['agent:left_players=1,right_players=1'],
614
+ 'reverse_team_processing': False,
615
+ })
616
+ random.seed(seed)
617
+ action_cnt = len(football_action_set.get_action_set(cfg1))
618
+ actions = [random.randint(0, action_cnt - 1) for _ in range(50)]
619
+ queue1 = Queue()
620
+ thread1 = threading.Thread(
621
+ target=run_scenario, args=(cfg1, queue1, actions, False, False))
622
+ thread1.start()
623
+ queue2 = Queue()
624
+ thread2 = threading.Thread(
625
+ target=run_scenario, args=(cfg2, queue2, actions, True, False))
626
+ thread2.start()
627
+ while True:
628
+ o1 = queue1.get()
629
+ o2 = queue2.get()
630
+ if not o1 or not o2:
631
+ self.assertEqual(o1, o2)
632
+ break
633
+ self.compare_observations(o1, o2)
634
+ thread1.join()
635
+ thread2.join()
636
+
637
+ def test_get_state_wrapper(self):
638
+ env = gfootball.env.create_environment(
639
+ stacked=True,
640
+ env_name='academy_empty_goal',
641
+ rewards='checkpoints,scoring')
642
+ o = env.reset()
643
+ state = env.get_state()
644
+ reward1 = 0
645
+ hash1 = 0
646
+ while reward1 < 0.9:
647
+ o, r, _, _ = env.step(football_action_set.action_right)
648
+ reward1 += r
649
+ hash1 = zlib.adler32(o, hash1)
650
+ self.assertAlmostEqual(reward1, 0.9, delta=0.01)
651
+ env.set_state(state)
652
+ hash2 = 0
653
+ reward2 = 0
654
+ while reward2 < 0.9:
655
+ o, r, _, _ = env.step(football_action_set.action_right)
656
+ reward2 += r
657
+ hash2 = zlib.adler32(o, hash2)
658
+ self.assertAlmostEqual(reward2, 0.9, delta=0.01)
659
+ self.assertEqual(hash1, hash2)
660
+
661
+ def test_restore_after_reset(self):
662
+ cfg = config.Config({
663
+ 'level': '11_vs_11_competition',
664
+ })
665
+ env = football_env.FootballEnv(cfg)
666
+ obs = env.reset()
667
+ state = env.get_state()
668
+ env.reset()
669
+ env.set_state(state)
670
+ obs_ = env.observation()
671
+ state_ = env.get_state()
672
+ env.step(0) # Test if can take step
673
+ self.compare_observations(obs, obs_)
674
+ self.assertEqual(state, state_)
675
+
676
+ def test_restore_after_done(self):
677
+ cfg = config.Config({
678
+ 'level': 'academy_empty_goal_close',
679
+ })
680
+ env = football_env.FootballEnv(cfg)
681
+ env.reset()
682
+ state = env.get_state()
683
+ # Go right until reaching the goal.
684
+ done = False
685
+ while not done:
686
+ _, _, done, _ = env.step(5)
687
+ env.set_state(state)
688
+ env.step(0) # Test if can take step
689
+
690
+
691
+ if __name__ == '__main__':
692
+ absltest.main(failfast=True)