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,72 @@
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
+ """Player with actions coming from the keyboard."""
19
+
20
+ import pygame
21
+
22
+ from cuju.gfootball.env import controller_base
23
+ from cuju.gfootball.env import football_action_set
24
+ from cuju.gfootball.env import event_queue
25
+
26
+
27
+ KEY_TO_ACTIONS = {
28
+ pygame.K_s: [football_action_set.action_short_pass,
29
+ football_action_set.action_pressure],
30
+ pygame.K_d: [football_action_set.action_shot,
31
+ football_action_set.action_team_pressure],
32
+ pygame.K_a: [football_action_set.action_high_pass,
33
+ football_action_set.action_sliding],
34
+ pygame.K_w: [football_action_set.action_long_pass,
35
+ football_action_set.action_keeper_rush],
36
+ pygame.K_q: [football_action_set.action_switch],
37
+ pygame.K_c: [football_action_set.action_dribble],
38
+ pygame.K_e: [football_action_set.action_sprint],
39
+ }
40
+
41
+
42
+ class Player(controller_base.Controller):
43
+ """Player with actions coming from the keyboard."""
44
+
45
+ def __init__(self, player_config, env_config):
46
+ controller_base.Controller.__init__(self, player_config, env_config)
47
+ self._can_play_right = True
48
+ self._init_done = False
49
+ pygame.init()
50
+ event_queue.add_controller('keyboard')
51
+
52
+ def take_action(self, observations):
53
+ assert len(observations) == 1, 'Keyboard does not support multiple player control'
54
+ if not self._init_done:
55
+ self._init_done = True
56
+ pygame.display.set_mode((1, 1), pygame.NOFRAME)
57
+ active_buttons = {}
58
+ for event in event_queue.get('keyboard'):
59
+ if event.type == pygame.KEYDOWN:
60
+ actions = KEY_TO_ACTIONS.get(event.key, [])
61
+ for a in actions:
62
+ active_buttons[a] = 1
63
+ keys = pygame.key.get_pressed()
64
+ left = keys[pygame.K_LEFT]
65
+ right = keys[pygame.K_RIGHT]
66
+ top = keys[pygame.K_UP]
67
+ bottom = keys[pygame.K_DOWN]
68
+ for key, actions in KEY_TO_ACTIONS.items():
69
+ if keys[key]:
70
+ for a in actions:
71
+ active_buttons[a] = 1
72
+ return self.get_env_action(left, right, top, bottom, active_buttons)
@@ -0,0 +1,31 @@
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
+ """Lazy player not moving at all."""
19
+
20
+
21
+ from cuju.gfootball.env import football_action_set
22
+ from cuju.gfootball.env import player_base
23
+
24
+ class Player(player_base.PlayerBase):
25
+ """Lazy player not moving at all."""
26
+
27
+ def __init__(self, player_config, env_config):
28
+ player_base.PlayerBase.__init__(self, player_config)
29
+
30
+ def take_action(self, observations):
31
+ return [football_action_set.action_idle] * len(observations)
@@ -0,0 +1,119 @@
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
+ r"""Player from PPO2 cnn checkpoint.
18
+
19
+ Example usage with play_game script:
20
+ python3 -m gfootball.play_game \
21
+ --players "ppo2_cnn:left_players=1,checkpoint=$YOUR_PATH,policy=$POLICY"
22
+
23
+ $POLICY should be one of: cnn, impala_cnn, gfootball_impala_cnn.
24
+ """
25
+
26
+ from baselines.common.policies import build_policy
27
+ from cuju.gfootball.env import football_action_set
28
+ from cuju.gfootball.env import observation_preprocessing
29
+ from cuju.gfootball.env import player_base
30
+ from cuju.gfootball.examples import models
31
+ from cuju.gfootball import _gym as gym
32
+ import joblib
33
+ import numpy as np
34
+ import tensorflow.compat.v1 as tf
35
+
36
+
37
+ class Player(player_base.PlayerBase):
38
+ """An agent loaded from PPO2 cnn model checkpoint."""
39
+
40
+ def __init__(self, player_config, env_config):
41
+ player_base.PlayerBase.__init__(self, player_config)
42
+
43
+ self._action_set = (env_config['action_set']
44
+ if 'action_set' in env_config else 'default')
45
+ config = tf.ConfigProto()
46
+ config.gpu_options.allow_growth = True
47
+ self._sess = tf.Session(config=config)
48
+ self._player_prefix = 'player_{}'.format(player_config['index'])
49
+ stacking = 4 if player_config.get('stacked', True) else 1
50
+ policy = player_config.get('policy', 'cnn')
51
+ self._stacker = ObservationStacker(stacking)
52
+ with tf.variable_scope(self._player_prefix):
53
+ with tf.variable_scope('ppo2_model'):
54
+ policy_fn = build_policy(DummyEnv(self._action_set, stacking), policy)
55
+ self._policy = policy_fn(nbatch=1, sess=self._sess)
56
+ _load_variables(player_config['checkpoint'], self._sess,
57
+ prefix=self._player_prefix + '/')
58
+
59
+ def __del__(self):
60
+ self._sess.close()
61
+
62
+ def take_action(self, observation):
63
+ assert len(observation) == 1, 'Multiple players control is not supported'
64
+
65
+ observation = observation_preprocessing.generate_smm(observation)
66
+ observation = self._stacker.get(observation)
67
+ action = self._policy.step(observation)[0][0]
68
+ actions = [football_action_set.action_set_dict[self._action_set][action]]
69
+ return actions
70
+
71
+ def reset(self):
72
+ self._stacker.reset()
73
+
74
+
75
+ def _load_variables(load_path, sess, prefix='', remove_prefix=True):
76
+ """Loads variables from checkpoint of policy trained by baselines."""
77
+
78
+ # Forked from address below since we needed loading from different var names:
79
+ # https://github.com/openai/baselines/blob/master/baselines/common/tf_util.py
80
+ variables = [v for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
81
+ if v.name.startswith(prefix)]
82
+
83
+ loaded_params = joblib.load(load_path)
84
+ restores = []
85
+ for v in variables:
86
+ v_name = v.name[len(prefix):] if remove_prefix else v.name
87
+ restores.append(v.assign(loaded_params[v_name]))
88
+
89
+ sess.run(restores)
90
+
91
+
92
+ class ObservationStacker(object):
93
+ """Utility class that produces stacked observations."""
94
+
95
+ def __init__(self, stacking):
96
+ self._stacking = stacking
97
+ self._data = []
98
+
99
+ def get(self, observation):
100
+ if self._data:
101
+ self._data.append(observation)
102
+ self._data = self._data[-self._stacking:]
103
+ else:
104
+ self._data = [observation] * self._stacking
105
+ return np.concatenate(self._data, axis=-1)
106
+
107
+ def reset(self):
108
+ self._data = []
109
+
110
+
111
+ class DummyEnv(object):
112
+ # We need env object to pass to build_policy, however real environment
113
+ # is not there yet.
114
+
115
+ def __init__(self, action_set, stacking):
116
+ self.action_space = gym.spaces.Discrete(
117
+ len(football_action_set.action_set_dict[action_set]))
118
+ self.observation_space = gym.spaces.Box(
119
+ 0, 255, shape=[72, 96, 4 * stacking], dtype=np.uint8)
@@ -0,0 +1,40 @@
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
+ """Player with actions coming from specific game replay."""
18
+
19
+ from cuju.gfootball.env import player_base
20
+ from cuju.gfootball.env import script_helpers
21
+
22
+
23
+ class Player(player_base.PlayerBase):
24
+ """Player with actions coming from specific game replay."""
25
+
26
+ def __init__(self, player_config, env_config):
27
+ player_base.PlayerBase.__init__(self, player_config)
28
+ self._can_play_right = True
29
+ self._replay = script_helpers.ScriptHelpers().load_dump(player_config['path'])
30
+ self._step = 0
31
+ self._player = player_config['index']
32
+
33
+ def take_action(self, observations):
34
+ if self._step == len(self._replay):
35
+ print("Replay finished.")
36
+ exit(0)
37
+ actions = self._replay[self._step]['debug']['action'][
38
+ self._player:self.num_controlled_players() + self._player]
39
+ self._step += 1
40
+ return actions
@@ -0,0 +1,189 @@
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
+ """Remote football environment."""
19
+ import pickle
20
+ import time
21
+ from absl import logging
22
+ from cuju.gfootball import _cv2 as cv2
23
+ from cuju.gfootball.env import football_action_set
24
+ from cuju.gfootball.eval_server import config
25
+ from cuju.gfootball.eval_server import utils
26
+ from cuju.gfootball.eval_server.proto import game_server_pb2
27
+ from cuju.gfootball.eval_server.proto import game_server_pb2_grpc
28
+ from cuju.gfootball.eval_server.proto import master_pb2
29
+ from cuju.gfootball.eval_server.proto import master_pb2_grpc
30
+ import grpc
31
+ from cuju.gfootball import _gym as gym
32
+ import numpy as np
33
+
34
+
35
+ CONNECTION_TRIALS = 20
36
+
37
+
38
+ # This config is needed in order to run various env wrappers.
39
+ class FakeConfig(object):
40
+ """An immitation of Config with the set of fields necessary to run wrappers.
41
+ """
42
+
43
+ def __init__(self, num_players):
44
+ self._num_players = num_players
45
+ self._values = {
46
+ }
47
+
48
+ def number_of_players_agent_controls(self):
49
+ return self._num_players
50
+
51
+ def __getitem__(self, key):
52
+ return self._values[key]
53
+
54
+
55
+ class RemoteFootballEnv(gym.Env):
56
+
57
+ def __init__(self, username, token, model_name='', track='default',
58
+ include_rendering=False):
59
+ self._config = FakeConfig(
60
+ config.track_to_num_controlled_players.get(track, 1))
61
+ self._num_actions = len(football_action_set.action_set_dict['default'])
62
+ self._track = track
63
+ self._include_rendering = include_rendering
64
+
65
+ self._username = username
66
+ self._token = token
67
+ self._model_name = model_name
68
+
69
+ self._game_id = None
70
+ self._channel = None
71
+
72
+ self._update_master()
73
+
74
+ def _update_master(self):
75
+ while True:
76
+ try:
77
+ master_address = utils.get_master_address(self._track)
78
+ logging.info('Connecting to %s', master_address)
79
+ self._master_channel = utils.get_grpc_channel(master_address)
80
+ grpc.channel_ready_future(self._master_channel).result(timeout=10)
81
+ break
82
+ except grpc.FutureTimeoutError:
83
+ logging.info('Failed to connect to master')
84
+ except BaseException as e:
85
+ logging.info('Error %s, sleeping 10 secs', e)
86
+ time.sleep(10)
87
+ logging.info('Connection successful')
88
+
89
+ @property
90
+ def action_space(self):
91
+ return gym.spaces.Discrete(self._num_actions)
92
+
93
+ def step(self, action):
94
+ if self._game_id is None:
95
+ raise RuntimeError('Environment should be reset!')
96
+ if np.isscalar(action):
97
+ action = [int(action)]
98
+ request = game_server_pb2.StepRequest(
99
+ game_version=config.game_version, game_id=self._game_id,
100
+ username=self._username, token=self._token, action=-1,
101
+ model_name=self._model_name, action_list=action)
102
+ return self._get_env_result(request, 'Step')
103
+
104
+ def reset(self):
105
+ if self._channel is not None:
106
+ # Client surrenders in the current game and starts next one.
107
+
108
+ self._channel.close()
109
+ self._channel = None
110
+
111
+ # Get game server address and side id from master.
112
+ start_game_request = master_pb2.StartGameRequest(
113
+ game_version=config.game_version, username=self._username,
114
+ token=self._token, model_name=self._model_name,
115
+ include_rendering=self._include_rendering)
116
+ response = self._reset_with_retries(start_game_request)
117
+ self._game_id = response.game_id
118
+ self._channel = utils.get_grpc_channel(response.game_server_address)
119
+ grpc.channel_ready_future(self._channel).result()
120
+ get_env_result_request = game_server_pb2.GetEnvResultRequest(
121
+ game_version=config.game_version, game_id=self._game_id,
122
+ username=self._username, token=self._token, model_name=self._model_name)
123
+ return self._get_env_result(get_env_result_request, 'GetEnvResult')[0]
124
+
125
+ def _reset_with_retries(self, request):
126
+ time_to_sleep = 1
127
+ while True:
128
+ try:
129
+ stub = master_pb2_grpc.MasterStub(self._master_channel)
130
+ return stub.StartGame(request, timeout=10*60)
131
+ except grpc.RpcError as e:
132
+ if e.code() == grpc.StatusCode.DEADLINE_EXCEEDED:
133
+ time_to_sleep = 1
134
+ continue
135
+ logging.warning('Exception during request: %s', e)
136
+ logging.warning('Sleeping for %d seconds', time_to_sleep)
137
+ time.sleep(time_to_sleep)
138
+ if time_to_sleep < 30:
139
+ time_to_sleep *= 2
140
+ self._update_master()
141
+ except BaseException as e:
142
+ logging.warning('Exception during request: %s', e)
143
+ logging.warning('Sleeping for %d seconds', time_to_sleep)
144
+ time.sleep(time_to_sleep)
145
+ if time_to_sleep < 30:
146
+ time_to_sleep *= 2
147
+ self._update_master()
148
+ raise RuntimeError('Connection problems!')
149
+
150
+ def _get_env_result(self, request, rpc_name):
151
+ assert rpc_name in ['GetEnvResult', 'Step']
152
+
153
+ response = None
154
+ for _ in range(CONNECTION_TRIALS):
155
+ time_to_sleep = 1
156
+ try:
157
+ stub = game_server_pb2_grpc.GameServerStub(self._channel)
158
+ response = getattr(stub, rpc_name)(request)
159
+ break
160
+ except grpc.RpcError as e:
161
+ if e.code() == grpc.StatusCode.INVALID_ARGUMENT:
162
+ raise e
163
+ if e.code() == grpc.StatusCode.FAILED_PRECONDITION:
164
+ raise e
165
+ logging.warning('Exception during request: %s', e)
166
+ time.sleep(time_to_sleep)
167
+ if time_to_sleep < 30:
168
+ time_to_sleep *= 2
169
+ except BaseException as e:
170
+ logging.warning('Exception during request: %s', e)
171
+ time.sleep(time_to_sleep)
172
+ if time_to_sleep < 30:
173
+ time_to_sleep *= 2
174
+ if response is None:
175
+ raise RuntimeError('Connection problems!')
176
+
177
+ env_result = pickle.loads(response.env_result)
178
+ self._process_env_result(env_result)
179
+ return env_result[0], env_result[1], env_result[2], env_result[3]
180
+
181
+ def _process_env_result(self, env_result):
182
+ ob, rew, done, info = env_result
183
+ if self._include_rendering and 'frame' in info:
184
+ cv2.imshow('GRF League', info['frame'])
185
+ cv2.waitKey(1)
186
+ if done:
187
+ self._game_id = None
188
+ self._channel.close()
189
+ self._channel = None
@@ -0,0 +1,125 @@
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
+ """Class responsible for generating scenarios."""
19
+
20
+ import importlib
21
+ import os
22
+ import pkgutil
23
+ import random
24
+ import sys
25
+
26
+ from absl import flags
27
+ from absl import logging
28
+
29
+ from cuju import gfootball_engine as libgame
30
+
31
+ Player = libgame.FormationEntry
32
+ Role = libgame.e_PlayerRole
33
+ Team = libgame.e_Team
34
+
35
+ FLAGS = flags.FLAGS
36
+
37
+
38
+ def all_scenarios():
39
+ path = os.path.abspath(__file__)
40
+ path = os.path.join(os.path.dirname(os.path.dirname(path)), 'scenarios')
41
+ scenarios = []
42
+ for m in pkgutil.iter_modules([path]):
43
+ # There was API change in pkgutil between Python 3.5 and 3.6...
44
+ if m.__class__ == tuple:
45
+ scenarios.append(m[1])
46
+ else:
47
+ scenarios.append(m.name)
48
+ return scenarios
49
+
50
+
51
+ class Scenario(object):
52
+
53
+ def __init__(self, config):
54
+ # Game config controls C++ engine and is derived from the main config.
55
+ self._scenario_cfg = libgame.ScenarioConfig.make()
56
+ self._config = config
57
+ self._active_team = Team.e_Left
58
+ scenario = None
59
+ try:
60
+ scenario = importlib.import_module('cuju.gfootball.scenarios.{}'.format(config['level']))
61
+ except ImportError as e:
62
+ logging.error('Loading scenario "%s" failed' % config['level'])
63
+ logging.error(e)
64
+ sys.exit(1)
65
+ scenario.build_scenario(self)
66
+ self.SetTeam(libgame.e_Team.e_Left)
67
+ self._FakePlayersForEmptyTeam(self._scenario_cfg.left_team)
68
+ self.SetTeam(libgame.e_Team.e_Right)
69
+ self._FakePlayersForEmptyTeam(self._scenario_cfg.right_team)
70
+ self._BuildScenarioConfig()
71
+
72
+ def _FakePlayersForEmptyTeam(self, team):
73
+ if len(team) == 0:
74
+ self.AddPlayer(-1.000000, 0.420000, libgame.e_PlayerRole.e_PlayerRole_GK, True)
75
+
76
+ def _BuildScenarioConfig(self):
77
+ """Builds scenario config from gfootball.environment config."""
78
+ self._scenario_cfg.real_time = self._config['real_time']
79
+ self._scenario_cfg.left_agents = self._config.number_of_left_players()
80
+ self._scenario_cfg.right_agents = self._config.number_of_right_players()
81
+ # This is needed to record 'game_engine_random_seed' in the dump.
82
+ if 'game_engine_random_seed' not in self._config._values:
83
+ self._config.set_scenario_value('game_engine_random_seed',
84
+ random.randint(0, 2000000000))
85
+ if not self._scenario_cfg.deterministic:
86
+ self._scenario_cfg.game_engine_random_seed = (
87
+ self._config['game_engine_random_seed'])
88
+ if 'reverse_team_processing' not in self._config:
89
+ self._config['reverse_team_processing'] = (
90
+ bool(self._config['game_engine_random_seed'] % 2))
91
+ if 'reverse_team_processing' in self._config:
92
+ self._scenario_cfg.reverse_team_processing = (
93
+ self._config['reverse_team_processing'])
94
+
95
+ def config(self):
96
+ return self._scenario_cfg
97
+
98
+ def SetTeam(self, team):
99
+ self._active_team = team
100
+
101
+ def AddPlayer(self, x, y, role, lazy=False, controllable=True):
102
+ """Build player for the current scenario.
103
+
104
+ Args:
105
+ x: x coordinate of the player in the range [-1, 1].
106
+ y: y coordinate of the player in the range [-0.42, 0.42].
107
+ role: Player's role in the game (goal keeper etc.).
108
+ lazy: Computer doesn't perform any automatic actions for lazy player.
109
+ controllable: Whether player can be controlled.
110
+ """
111
+ player = Player(x, y, role, lazy, controllable)
112
+ if self._active_team == Team.e_Left:
113
+ self._scenario_cfg.left_team.append(player)
114
+ else:
115
+ self._scenario_cfg.right_team.append(player)
116
+
117
+ def SetBallPosition(self, ball_x, ball_y):
118
+ self._scenario_cfg.ball_position[0] = ball_x
119
+ self._scenario_cfg.ball_position[1] = ball_y
120
+
121
+ def EpisodeNumber(self):
122
+ return self._config['episode_number']
123
+
124
+ def ScenarioConfig(self):
125
+ return self._scenario_cfg