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.
- cuju/__init__.py +217 -0
- cuju/__main__.py +262 -0
- cuju/_batch/__init__.py +4 -0
- cuju/_batch/allocator.py +52 -0
- cuju/_batch/assets.py +68 -0
- cuju/_batch/environment.py +1890 -0
- cuju/_batch/frame_stack.py +114 -0
- cuju/_batch/library.py +190 -0
- cuju/_batch/log.py +103 -0
- cuju/_batch/memory.py +70 -0
- cuju/_batch/operators.py +55 -0
- cuju/_batch/pinned.py +39 -0
- cuju/_batch/pixels.py +207 -0
- cuju/_batch/state.py +77 -0
- cuju/_checksums.py +60 -0
- cuju/_cli_contract.py +244 -0
- cuju/_cli_parsers.py +191 -0
- cuju/_completion.py +277 -0
- cuju/_dump_to_txt.py +66 -0
- cuju/_grf_config.py +331 -0
- cuju/_library_path.py +141 -0
- cuju/_media.py +113 -0
- cuju/_pixels.py +276 -0
- cuju/_plugins.py +106 -0
- cuju/_python_players.py +224 -0
- cuju/_replay_command.py +109 -0
- cuju/_replay_player.py +42 -0
- cuju/_replays.py +414 -0
- cuju/_script_helpers.py +109 -0
- cuju/_version.py +6 -0
- cuju/action_log.py +371 -0
- cuju/check.py +596 -0
- cuju/cuju_cpu.dll +0 -0
- cuju/cuju_cuda.dll +0 -0
- cuju/cuju_render.dll +0 -0
- cuju/data/SHA256SUMS +20 -0
- cuju/data/animations.bin +0 -0
- cuju/data/assets/11_vs_11_competition_l0_r0.cjb +0 -0
- cuju/data/assets/11_vs_11_easy_stochastic_l0_r0.cjb +0 -0
- cuju/data/assets/11_vs_11_hard_stochastic_l0_r0.cjb +0 -0
- cuju/data/assets/11_vs_11_kaggle_l0_r0.cjb +0 -0
- cuju/data/assets/11_vs_11_stochastic_l0_r0.cjb +0 -0
- cuju/data/assets/1_vs_1_easy_l0_r0.cjb +0 -0
- cuju/data/assets/5_vs_5_l0_r0.cjb +0 -0
- cuju/data/assets/academy_3_vs_1_with_keeper_l0_r0.cjb +0 -0
- cuju/data/assets/academy_corner_l0_r0.cjb +0 -0
- cuju/data/assets/academy_counterattack_easy_l0_r0.cjb +0 -0
- cuju/data/assets/academy_counterattack_hard_l0_r0.cjb +0 -0
- cuju/data/assets/academy_empty_goal_close_l0_r0.cjb +0 -0
- cuju/data/assets/academy_empty_goal_l0_r0.cjb +0 -0
- cuju/data/assets/academy_pass_and_shoot_with_keeper_l0_r0.cjb +0 -0
- cuju/data/assets/academy_run_pass_and_shoot_with_keeper_l0_r0.cjb +0 -0
- cuju/data/assets/academy_run_to_score_l0_r0.cjb +0 -0
- cuju/data/assets/academy_run_to_score_with_keeper_l0_r0.cjb +0 -0
- cuju/data/assets/academy_single_goal_versus_lazy_l0_r0.cjb +0 -0
- cuju/data/assets/test_example_multiagent_l0_r0.cjb +0 -0
- cuju/data/digests.json +186 -0
- cuju/data/render_digests.json.z +0 -0
- cuju/data/scenarios/11_vs_11_competition.py +45 -0
- cuju/data/scenarios/11_vs_11_easy_stochastic.py +44 -0
- cuju/data/scenarios/11_vs_11_hard_stochastic.py +44 -0
- cuju/data/scenarios/11_vs_11_kaggle.py +46 -0
- cuju/data/scenarios/11_vs_11_stochastic.py +44 -0
- cuju/data/scenarios/1_vs_1_easy.py +25 -0
- cuju/data/scenarios/5_vs_5.py +33 -0
- cuju/data/scenarios/academy_3_vs_1_with_keeper.py +28 -0
- cuju/data/scenarios/academy_corner.py +45 -0
- cuju/data/scenarios/academy_counterattack_easy.py +45 -0
- cuju/data/scenarios/academy_counterattack_hard.py +45 -0
- cuju/data/scenarios/academy_empty_goal.py +25 -0
- cuju/data/scenarios/academy_empty_goal_close.py +25 -0
- cuju/data/scenarios/academy_pass_and_shoot_with_keeper.py +27 -0
- cuju/data/scenarios/academy_run_pass_and_shoot_with_keeper.py +27 -0
- cuju/data/scenarios/academy_run_to_score.py +30 -0
- cuju/data/scenarios/academy_run_to_score_with_keeper.py +30 -0
- cuju/data/scenarios/academy_single_goal_versus_lazy.py +49 -0
- cuju/data/scenarios/test_example_multiagent.py +28 -0
- cuju/dumps.py +942 -0
- cuju/engine_status.py +106 -0
- cuju/env.py +1168 -0
- cuju/errors.py +108 -0
- cuju/gfootball/GRF_SOURCES +86 -0
- cuju/gfootball/__init__.py +60 -0
- cuju/gfootball/_cv2.py +43 -0
- cuju/gfootball/_gym/__init__.py +88 -0
- cuju/gfootball/_gym/_vendored/GYM_SOURCES +22 -0
- cuju/gfootball/_gym/_vendored/__init__.py +43 -0
- cuju/gfootball/_gym/_vendored/core.py +361 -0
- cuju/gfootball/_gym/_vendored/envs/__init__.py +19 -0
- cuju/gfootball/_gym/_vendored/envs/registration.py +284 -0
- cuju/gfootball/_gym/_vendored/error.py +230 -0
- cuju/gfootball/_gym/_vendored/logger.py +56 -0
- cuju/gfootball/_gym/_vendored/spaces/__init__.py +30 -0
- cuju/gfootball/_gym/_vendored/spaces/box.py +171 -0
- cuju/gfootball/_gym/_vendored/spaces/dict.py +162 -0
- cuju/gfootball/_gym/_vendored/spaces/discrete.py +41 -0
- cuju/gfootball/_gym/_vendored/spaces/multi_binary.py +61 -0
- cuju/gfootball/_gym/_vendored/spaces/multi_discrete.py +77 -0
- cuju/gfootball/_gym/_vendored/spaces/space.py +94 -0
- cuju/gfootball/_gym/_vendored/spaces/tuple.py +96 -0
- cuju/gfootball/_gym/_vendored/spaces/utils.py +228 -0
- cuju/gfootball/_gym/_vendored/utils/__init__.py +13 -0
- cuju/gfootball/_gym/_vendored/utils/closer.py +72 -0
- cuju/gfootball/_gym/_vendored/utils/colorize.py +36 -0
- cuju/gfootball/_gym/_vendored/utils/ezpickle.py +37 -0
- cuju/gfootball/_gym/_vendored/utils/json_utils.py +27 -0
- cuju/gfootball/_gym/_vendored/utils/seeding.py +101 -0
- cuju/gfootball/_gym/_vendored/version.py +5 -0
- cuju/gfootball/_gym/_vendored/wrappers/__init__.py +8 -0
- cuju/gfootball/_gym/_vendored/wrappers/order_enforcing.py +20 -0
- cuju/gfootball/_gym/_vendored/wrappers/time_limit.py +31 -0
- cuju/gfootball/dump_to_txt.py +49 -0
- cuju/gfootball/dump_to_video.py +33 -0
- cuju/gfootball/env/__init__.py +271 -0
- cuju/gfootball/env/config.py +163 -0
- cuju/gfootball/env/constants.py +33 -0
- cuju/gfootball/env/controller_base.py +114 -0
- cuju/gfootball/env/event_queue.py +54 -0
- cuju/gfootball/env/football_action_set.py +220 -0
- cuju/gfootball/env/football_action_set_test.py +76 -0
- cuju/gfootball/env/football_env.py +225 -0
- cuju/gfootball/env/football_env_core.py +447 -0
- cuju/gfootball/env/football_env_test.py +692 -0
- cuju/gfootball/env/gym_test.py +47 -0
- cuju/gfootball/env/observation_preprocessing.py +94 -0
- cuju/gfootball/env/observation_processor.py +526 -0
- cuju/gfootball/env/observation_rotation.py +163 -0
- cuju/gfootball/env/observation_rotation_test.py +84 -0
- cuju/gfootball/env/player_base.py +46 -0
- cuju/gfootball/env/players/__init__.py +16 -0
- cuju/gfootball/env/players/agent.py +39 -0
- cuju/gfootball/env/players/bot.py +217 -0
- cuju/gfootball/env/players/gamepad.py +81 -0
- cuju/gfootball/env/players/keyboard.py +72 -0
- cuju/gfootball/env/players/lazy.py +31 -0
- cuju/gfootball/env/players/ppo2_cnn.py +119 -0
- cuju/gfootball/env/players/replay.py +40 -0
- cuju/gfootball/env/remote_football_env.py +189 -0
- cuju/gfootball/env/scenario_builder.py +125 -0
- cuju/gfootball/env/script_helpers.py +130 -0
- cuju/gfootball/env/script_helpers_test.py +107 -0
- cuju/gfootball/env/wrappers.py +440 -0
- cuju/gfootball/env/wrappers_test.py +150 -0
- cuju/gfootball/eval_server/__init__.py +17 -0
- cuju/gfootball/eval_server/config.py +30 -0
- cuju/gfootball/eval_server/example_client.py +107 -0
- cuju/gfootball/eval_server/proto/game_server.proto +62 -0
- cuju/gfootball/eval_server/proto/game_server_pb2.py +501 -0
- cuju/gfootball/eval_server/proto/game_server_pb2_grpc.py +113 -0
- cuju/gfootball/eval_server/proto/master.proto +25 -0
- cuju/gfootball/eval_server/proto/master_pb2.py +184 -0
- cuju/gfootball/eval_server/proto/master_pb2_grpc.py +62 -0
- cuju/gfootball/eval_server/utils.py +52 -0
- cuju/gfootball/examples/__init__.py +16 -0
- cuju/gfootball/examples/models.py +66 -0
- cuju/gfootball/examples/run_multiagent_rllib.py +140 -0
- cuju/gfootball/examples/run_ppo2.py +123 -0
- cuju/gfootball/play_game.py +73 -0
- cuju/gfootball/replay.py +43 -0
- cuju/gfootball/scenarios/11_vs_11_competition.py +59 -0
- cuju/gfootball/scenarios/11_vs_11_easy_stochastic.py +57 -0
- cuju/gfootball/scenarios/11_vs_11_hard_stochastic.py +57 -0
- cuju/gfootball/scenarios/11_vs_11_kaggle.py +60 -0
- cuju/gfootball/scenarios/11_vs_11_stochastic.py +57 -0
- cuju/gfootball/scenarios/1_vs_1_easy.py +39 -0
- cuju/gfootball/scenarios/5_vs_5.py +46 -0
- cuju/gfootball/scenarios/__init__.py +31 -0
- cuju/gfootball/scenarios/academy_3_vs_1_with_keeper.py +42 -0
- cuju/gfootball/scenarios/academy_corner.py +59 -0
- cuju/gfootball/scenarios/academy_counterattack_easy.py +59 -0
- cuju/gfootball/scenarios/academy_counterattack_hard.py +59 -0
- cuju/gfootball/scenarios/academy_empty_goal.py +39 -0
- cuju/gfootball/scenarios/academy_empty_goal_close.py +39 -0
- cuju/gfootball/scenarios/academy_pass_and_shoot_with_keeper.py +41 -0
- cuju/gfootball/scenarios/academy_run_pass_and_shoot_with_keeper.py +41 -0
- cuju/gfootball/scenarios/academy_run_to_score.py +44 -0
- cuju/gfootball/scenarios/academy_run_to_score_with_keeper.py +44 -0
- cuju/gfootball/scenarios/academy_single_goal_versus_lazy.py +63 -0
- cuju/gfootball/scenarios/test_example_multiagent.py +42 -0
- cuju/gfootball/scenarios/tests/11_vs_11_deterministic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_11_easy_deterministic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_11_hard_deterministic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_11_single_goal_deterministic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_11_single_goal_stochastic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_4_offence_deterministic.py +47 -0
- cuju/gfootball/scenarios/tests/11_vs_4_offence_stochastic.py +47 -0
- cuju/gfootball/scenarios/tests/__init__.py +31 -0
- cuju/gfootball/scenarios/tests/corner_test.py +38 -0
- cuju/gfootball/scenarios/tests/goal_test.py +35 -0
- cuju/gfootball/scenarios/tests/keeper_test.py +38 -0
- cuju/gfootball/scenarios/tests/kickoff_for_goal_loosing_team_test.py +34 -0
- cuju/gfootball/scenarios/tests/multiagent_wrapper.py +60 -0
- cuju/gfootball/scenarios/tests/offside_test.py +39 -0
- cuju/gfootball/scenarios/tests/penalty.py +34 -0
- cuju/gfootball/scenarios/tests/second_half.py +33 -0
- cuju/gfootball/scenarios/tests/symmetric.py +53 -0
- cuju/gfootball_engine/__init__.py +31 -0
- cuju/gfootball_engine/_gameplayfootball.py +1624 -0
- cuju/gfootball_engine/_native.py +193 -0
- cuju/gfootball_engine/_pickle_names.py +114 -0
- cuju/gfootball_engine/_render_hooks.py +156 -0
- cuju/kaggle.py +425 -0
- cuju/multi_device.py +401 -0
- cuju/players.py +260 -0
- cuju/raw.py +93 -0
- cuju/scenarios.py +661 -0
- cuju/state.py +211 -0
- cuju/wrappers.py +325 -0
- cuju_engine-0.3.0.dist-info/METADATA +325 -0
- cuju_engine-0.3.0.dist-info/RECORD +222 -0
- cuju_engine-0.3.0.dist-info/WHEEL +5 -0
- cuju_engine-0.3.0.dist-info/entry_points.txt +4 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSE +202 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/BSD-3-Clause.txt +31 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/BSL-1.0.txt +23 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/LGPL-2.1-or-later.txt +502 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/MIT-Mesa.txt +26 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/MIT-gym.txt +23 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/MIT.txt +23 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/SunPro.txt +8 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/Zlib.txt +20 -0
- cuju_engine-0.3.0.dist-info/licenses/NOTICE +56 -0
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
# Copyright (c) 2016 OpenAI (https://openai.com)
|
|
2
|
+
# Modified by the cuju Authors in 2026, in the lines that ci/port_gfootball.py lists.
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
|
|
5
|
+
from abc import abstractmethod
|
|
6
|
+
|
|
7
|
+
from cuju.gfootball._gym import _vendored as gym
|
|
8
|
+
from cuju.gfootball._gym._vendored import error
|
|
9
|
+
from cuju.gfootball._gym._vendored.utils import closer
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Env(object):
|
|
13
|
+
"""The main OpenAI Gym class. It encapsulates an environment with
|
|
14
|
+
arbitrary behind-the-scenes dynamics. An environment can be
|
|
15
|
+
partially or fully observed.
|
|
16
|
+
|
|
17
|
+
The main API methods that users of this class need to know are:
|
|
18
|
+
|
|
19
|
+
step
|
|
20
|
+
reset
|
|
21
|
+
render
|
|
22
|
+
close
|
|
23
|
+
seed
|
|
24
|
+
|
|
25
|
+
And set the following attributes:
|
|
26
|
+
|
|
27
|
+
action_space: The Space object corresponding to valid actions
|
|
28
|
+
observation_space: The Space object corresponding to valid observations
|
|
29
|
+
reward_range: A tuple corresponding to the min and max possible rewards
|
|
30
|
+
|
|
31
|
+
Note: a default reward range set to [-inf,+inf] already exists. Set it if you want a narrower range.
|
|
32
|
+
|
|
33
|
+
The methods are accessed publicly as "step", "reset", etc...
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
# Set this in SOME subclasses
|
|
37
|
+
metadata = {"render.modes": []}
|
|
38
|
+
reward_range = (-float("inf"), float("inf"))
|
|
39
|
+
spec = None
|
|
40
|
+
|
|
41
|
+
# Set these in ALL subclasses
|
|
42
|
+
action_space = None
|
|
43
|
+
observation_space = None
|
|
44
|
+
|
|
45
|
+
@abstractmethod
|
|
46
|
+
def step(self, action):
|
|
47
|
+
"""Run one timestep of the environment's dynamics. When end of
|
|
48
|
+
episode is reached, you are responsible for calling `reset()`
|
|
49
|
+
to reset this environment's state.
|
|
50
|
+
|
|
51
|
+
Accepts an action and returns a tuple (observation, reward, done, info).
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
action (object): an action provided by the agent
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
observation (object): agent's observation of the current environment
|
|
58
|
+
reward (float) : amount of reward returned after previous action
|
|
59
|
+
done (bool): whether the episode has ended, in which case further step() calls will return undefined results
|
|
60
|
+
info (dict): contains auxiliary diagnostic information (helpful for debugging, and sometimes learning)
|
|
61
|
+
"""
|
|
62
|
+
raise NotImplementedError
|
|
63
|
+
|
|
64
|
+
@abstractmethod
|
|
65
|
+
def reset(self):
|
|
66
|
+
"""Resets the environment to an initial state and returns an initial
|
|
67
|
+
observation.
|
|
68
|
+
|
|
69
|
+
Note that this function should not reset the environment's random
|
|
70
|
+
number generator(s); random variables in the environment's state should
|
|
71
|
+
be sampled independently between multiple calls to `reset()`. In other
|
|
72
|
+
words, each call of `reset()` should yield an environment suitable for
|
|
73
|
+
a new episode, independent of previous episodes.
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
observation (object): the initial observation.
|
|
77
|
+
"""
|
|
78
|
+
raise NotImplementedError
|
|
79
|
+
|
|
80
|
+
@abstractmethod
|
|
81
|
+
def render(self, mode="human"):
|
|
82
|
+
"""Renders the environment.
|
|
83
|
+
|
|
84
|
+
The set of supported modes varies per environment. (And some
|
|
85
|
+
environments do not support rendering at all.) By convention,
|
|
86
|
+
if mode is:
|
|
87
|
+
|
|
88
|
+
- human: render to the current display or terminal and
|
|
89
|
+
return nothing. Usually for human consumption.
|
|
90
|
+
- rgb_array: Return an numpy.ndarray with shape (x, y, 3),
|
|
91
|
+
representing RGB values for an x-by-y pixel image, suitable
|
|
92
|
+
for turning into a video.
|
|
93
|
+
- ansi: Return a string (str) or StringIO.StringIO containing a
|
|
94
|
+
terminal-style text representation. The text can include newlines
|
|
95
|
+
and ANSI escape sequences (e.g. for colors).
|
|
96
|
+
|
|
97
|
+
Note:
|
|
98
|
+
Make sure that your class's metadata 'render.modes' key includes
|
|
99
|
+
the list of supported modes. It's recommended to call super()
|
|
100
|
+
in implementations to use the functionality of this method.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
mode (str): the mode to render with
|
|
104
|
+
|
|
105
|
+
Example:
|
|
106
|
+
|
|
107
|
+
class MyEnv(Env):
|
|
108
|
+
metadata = {'render.modes': ['human', 'rgb_array']}
|
|
109
|
+
|
|
110
|
+
def render(self, mode='human'):
|
|
111
|
+
if mode == 'rgb_array':
|
|
112
|
+
return np.array(...) # return RGB frame suitable for video
|
|
113
|
+
elif mode == 'human':
|
|
114
|
+
... # pop up a window and render
|
|
115
|
+
else:
|
|
116
|
+
super(MyEnv, self).render(mode=mode) # just raise an exception
|
|
117
|
+
"""
|
|
118
|
+
raise NotImplementedError
|
|
119
|
+
|
|
120
|
+
def close(self):
|
|
121
|
+
"""Override close in your subclass to perform any necessary cleanup.
|
|
122
|
+
|
|
123
|
+
Environments will automatically close() themselves when
|
|
124
|
+
garbage collected or when the program exits.
|
|
125
|
+
"""
|
|
126
|
+
pass
|
|
127
|
+
|
|
128
|
+
def seed(self, seed=None):
|
|
129
|
+
"""Sets the seed for this env's random number generator(s).
|
|
130
|
+
|
|
131
|
+
Note:
|
|
132
|
+
Some environments use multiple pseudorandom number generators.
|
|
133
|
+
We want to capture all such seeds used in order to ensure that
|
|
134
|
+
there aren't accidental correlations between multiple generators.
|
|
135
|
+
|
|
136
|
+
Returns:
|
|
137
|
+
list<bigint>: Returns the list of seeds used in this env's random
|
|
138
|
+
number generators. The first value in the list should be the
|
|
139
|
+
"main" seed, or the value which a reproducer should pass to
|
|
140
|
+
'seed'. Often, the main seed equals the provided 'seed', but
|
|
141
|
+
this won't be true if seed=None, for example.
|
|
142
|
+
"""
|
|
143
|
+
return
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def unwrapped(self):
|
|
147
|
+
"""Completely unwrap this env.
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
gym.Env: The base non-wrapped gym.Env instance
|
|
151
|
+
"""
|
|
152
|
+
return self
|
|
153
|
+
|
|
154
|
+
def __str__(self):
|
|
155
|
+
if self.spec is None:
|
|
156
|
+
return "<{} instance>".format(type(self).__name__)
|
|
157
|
+
else:
|
|
158
|
+
return "<{}<{}>>".format(type(self).__name__, self.spec.id)
|
|
159
|
+
|
|
160
|
+
def __enter__(self):
|
|
161
|
+
"""Support with-statement for the environment."""
|
|
162
|
+
return self
|
|
163
|
+
|
|
164
|
+
def __exit__(self, *args):
|
|
165
|
+
"""Support with-statement for the environment."""
|
|
166
|
+
self.close()
|
|
167
|
+
# propagate exception
|
|
168
|
+
return False
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class GoalEnv(Env):
|
|
172
|
+
"""A goal-based environment. It functions just as any regular OpenAI Gym environment but it
|
|
173
|
+
imposes a required structure on the observation_space. More concretely, the observation
|
|
174
|
+
space is required to contain at least three elements, namely `observation`, `desired_goal`, and
|
|
175
|
+
`achieved_goal`. Here, `desired_goal` specifies the goal that the agent should attempt to achieve.
|
|
176
|
+
`achieved_goal` is the goal that it currently achieved instead. `observation` contains the
|
|
177
|
+
actual observations of the environment as per usual.
|
|
178
|
+
"""
|
|
179
|
+
|
|
180
|
+
def reset(self):
|
|
181
|
+
# Enforce that each GoalEnv uses a Goal-compatible observation space.
|
|
182
|
+
if not isinstance(self.observation_space, gym.spaces.Dict):
|
|
183
|
+
raise error.Error(
|
|
184
|
+
"GoalEnv requires an observation space of type gym.spaces.Dict"
|
|
185
|
+
)
|
|
186
|
+
for key in ["observation", "achieved_goal", "desired_goal"]:
|
|
187
|
+
if key not in self.observation_space.spaces:
|
|
188
|
+
raise error.Error(
|
|
189
|
+
'GoalEnv requires the "{}" key to be part of the observation dictionary.'.format(
|
|
190
|
+
key
|
|
191
|
+
)
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
@abstractmethod
|
|
195
|
+
def compute_reward(self, achieved_goal, desired_goal, info):
|
|
196
|
+
"""Compute the step reward. This externalizes the reward function and makes
|
|
197
|
+
it dependent on a desired goal and the one that was achieved. If you wish to include
|
|
198
|
+
additional rewards that are independent of the goal, you can include the necessary values
|
|
199
|
+
to derive it in 'info' and compute it accordingly.
|
|
200
|
+
|
|
201
|
+
Args:
|
|
202
|
+
achieved_goal (object): the goal that was achieved during execution
|
|
203
|
+
desired_goal (object): the desired goal that we asked the agent to attempt to achieve
|
|
204
|
+
info (dict): an info dictionary with additional information
|
|
205
|
+
|
|
206
|
+
Returns:
|
|
207
|
+
float: The reward that corresponds to the provided achieved goal w.r.t. to the desired
|
|
208
|
+
goal. Note that the following should always hold true:
|
|
209
|
+
|
|
210
|
+
ob, reward, done, info = env.step()
|
|
211
|
+
assert reward == env.compute_reward(ob['achieved_goal'], ob['desired_goal'], info)
|
|
212
|
+
"""
|
|
213
|
+
raise NotImplementedError
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
class Wrapper(Env):
|
|
217
|
+
"""Wraps the environment to allow a modular transformation.
|
|
218
|
+
|
|
219
|
+
This class is the base class for all wrappers. The subclass could override
|
|
220
|
+
some methods to change the behavior of the original environment without touching the
|
|
221
|
+
original code.
|
|
222
|
+
|
|
223
|
+
.. note::
|
|
224
|
+
|
|
225
|
+
Don't forget to call ``super().__init__(env)`` if the subclass overrides :meth:`__init__`.
|
|
226
|
+
|
|
227
|
+
"""
|
|
228
|
+
|
|
229
|
+
def __init__(self, env):
|
|
230
|
+
self.env = env
|
|
231
|
+
|
|
232
|
+
self._action_space = None
|
|
233
|
+
self._observation_space = None
|
|
234
|
+
self._reward_range = None
|
|
235
|
+
self._metadata = None
|
|
236
|
+
|
|
237
|
+
def __getattr__(self, name):
|
|
238
|
+
if name.startswith("_"):
|
|
239
|
+
raise AttributeError(
|
|
240
|
+
"attempted to get missing private attribute '{}'".format(name)
|
|
241
|
+
)
|
|
242
|
+
return getattr(self.env, name)
|
|
243
|
+
|
|
244
|
+
@property
|
|
245
|
+
def spec(self):
|
|
246
|
+
return self.env.spec
|
|
247
|
+
|
|
248
|
+
@classmethod
|
|
249
|
+
def class_name(cls):
|
|
250
|
+
return cls.__name__
|
|
251
|
+
|
|
252
|
+
@property
|
|
253
|
+
def action_space(self):
|
|
254
|
+
if self._action_space is None:
|
|
255
|
+
return self.env.action_space
|
|
256
|
+
return self._action_space
|
|
257
|
+
|
|
258
|
+
@action_space.setter
|
|
259
|
+
def action_space(self, space):
|
|
260
|
+
self._action_space = space
|
|
261
|
+
|
|
262
|
+
@property
|
|
263
|
+
def observation_space(self):
|
|
264
|
+
if self._observation_space is None:
|
|
265
|
+
return self.env.observation_space
|
|
266
|
+
return self._observation_space
|
|
267
|
+
|
|
268
|
+
@observation_space.setter
|
|
269
|
+
def observation_space(self, space):
|
|
270
|
+
self._observation_space = space
|
|
271
|
+
|
|
272
|
+
@property
|
|
273
|
+
def reward_range(self):
|
|
274
|
+
if self._reward_range is None:
|
|
275
|
+
return self.env.reward_range
|
|
276
|
+
return self._reward_range
|
|
277
|
+
|
|
278
|
+
@reward_range.setter
|
|
279
|
+
def reward_range(self, value):
|
|
280
|
+
self._reward_range = value
|
|
281
|
+
|
|
282
|
+
@property
|
|
283
|
+
def metadata(self):
|
|
284
|
+
if self._metadata is None:
|
|
285
|
+
return self.env.metadata
|
|
286
|
+
return self._metadata
|
|
287
|
+
|
|
288
|
+
@metadata.setter
|
|
289
|
+
def metadata(self, value):
|
|
290
|
+
self._metadata = value
|
|
291
|
+
|
|
292
|
+
def step(self, action):
|
|
293
|
+
return self.env.step(action)
|
|
294
|
+
|
|
295
|
+
def reset(self, **kwargs):
|
|
296
|
+
return self.env.reset(**kwargs)
|
|
297
|
+
|
|
298
|
+
def render(self, mode="human", **kwargs):
|
|
299
|
+
return self.env.render(mode, **kwargs)
|
|
300
|
+
|
|
301
|
+
def close(self):
|
|
302
|
+
return self.env.close()
|
|
303
|
+
|
|
304
|
+
def seed(self, seed=None):
|
|
305
|
+
return self.env.seed(seed)
|
|
306
|
+
|
|
307
|
+
def compute_reward(self, achieved_goal, desired_goal, info):
|
|
308
|
+
return self.env.compute_reward(achieved_goal, desired_goal, info)
|
|
309
|
+
|
|
310
|
+
def __str__(self):
|
|
311
|
+
return "<{}{}>".format(type(self).__name__, self.env)
|
|
312
|
+
|
|
313
|
+
def __repr__(self):
|
|
314
|
+
return str(self)
|
|
315
|
+
|
|
316
|
+
@property
|
|
317
|
+
def unwrapped(self):
|
|
318
|
+
return self.env.unwrapped
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
class ObservationWrapper(Wrapper):
|
|
322
|
+
def reset(self, **kwargs):
|
|
323
|
+
observation = self.env.reset(**kwargs)
|
|
324
|
+
return self.observation(observation)
|
|
325
|
+
|
|
326
|
+
def step(self, action):
|
|
327
|
+
observation, reward, done, info = self.env.step(action)
|
|
328
|
+
return self.observation(observation), reward, done, info
|
|
329
|
+
|
|
330
|
+
@abstractmethod
|
|
331
|
+
def observation(self, observation):
|
|
332
|
+
raise NotImplementedError
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
class RewardWrapper(Wrapper):
|
|
336
|
+
def reset(self, **kwargs):
|
|
337
|
+
return self.env.reset(**kwargs)
|
|
338
|
+
|
|
339
|
+
def step(self, action):
|
|
340
|
+
observation, reward, done, info = self.env.step(action)
|
|
341
|
+
return observation, self.reward(reward), done, info
|
|
342
|
+
|
|
343
|
+
@abstractmethod
|
|
344
|
+
def reward(self, reward):
|
|
345
|
+
raise NotImplementedError
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
class ActionWrapper(Wrapper):
|
|
349
|
+
def reset(self, **kwargs):
|
|
350
|
+
return self.env.reset(**kwargs)
|
|
351
|
+
|
|
352
|
+
def step(self, action):
|
|
353
|
+
return self.env.step(self.action(action))
|
|
354
|
+
|
|
355
|
+
@abstractmethod
|
|
356
|
+
def action(self, action):
|
|
357
|
+
raise NotImplementedError
|
|
358
|
+
|
|
359
|
+
@abstractmethod
|
|
360
|
+
def reverse_action(self, action):
|
|
361
|
+
raise NotImplementedError
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Copyright 2026 The cuju Authors
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
"""The registry of the vendored gym, the names of gym 0.21.0's `gym/envs/__init__.py`.
|
|
4
|
+
|
|
5
|
+
gym's own module registers its bundled environments and loads the plugins of the entry point
|
|
6
|
+
group `gym.envs` at import. This one registers nothing, since the registry holds GRF's ids alone.
|
|
7
|
+
Plugins, written against an installed gym, register with that gym, so `load_env_plugins` stays a
|
|
8
|
+
function to call by hand.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from cuju.gfootball._gym._vendored.envs.registration import (
|
|
12
|
+
load_env_plugins,
|
|
13
|
+
make,
|
|
14
|
+
register,
|
|
15
|
+
registry,
|
|
16
|
+
spec,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
__all__ = ['load_env_plugins', 'make', 'register', 'registry', 'spec']
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# Copyright (c) 2016 OpenAI (https://openai.com)
|
|
2
|
+
# Modified by the cuju Authors in 2026, in the lines that ci/port_gfootball.py lists.
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
import sys
|
|
7
|
+
import copy
|
|
8
|
+
import importlib
|
|
9
|
+
import contextlib
|
|
10
|
+
|
|
11
|
+
if sys.version_info < (3, 8):
|
|
12
|
+
import importlib_metadata as metadata
|
|
13
|
+
else:
|
|
14
|
+
import importlib.metadata as metadata
|
|
15
|
+
|
|
16
|
+
from cuju.gfootball._gym._vendored import error, logger
|
|
17
|
+
|
|
18
|
+
# This format is true today, but it's *not* an official spec.
|
|
19
|
+
# [username/](env-name)-v(version) env-name is group 1, version is group 2
|
|
20
|
+
#
|
|
21
|
+
# 2016-10-31: We're experimentally expanding the environment ID format
|
|
22
|
+
# to include an optional username.
|
|
23
|
+
env_id_re = re.compile(r"^(?:[\w:-]+\/)?([\w:.-]+)-v(\d+)$")
|
|
24
|
+
|
|
25
|
+
# Whitelist of plugins which can hook into the `gym.envs.internal` entry point.
|
|
26
|
+
plugin_internal_whitelist = {"ale_py.gym"}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def load(name):
|
|
30
|
+
mod_name, attr_name = name.split(":")
|
|
31
|
+
mod = importlib.import_module(mod_name)
|
|
32
|
+
fn = getattr(mod, attr_name)
|
|
33
|
+
return fn
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class EnvSpec(object):
|
|
37
|
+
"""A specification for a particular instance of the environment. Used
|
|
38
|
+
to register the parameters for official evaluations.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
id (str): The official environment ID
|
|
42
|
+
entry_point (Optional[str]): The Python entrypoint of the environment class (e.g. module.name:Class)
|
|
43
|
+
reward_threshold (Optional[int]): The reward threshold before the task is considered solved
|
|
44
|
+
nondeterministic (bool): Whether this environment is non-deterministic even after seeding
|
|
45
|
+
max_episode_steps (Optional[int]): The maximum number of steps that an episode can consist of
|
|
46
|
+
order_enforce (Optional[int]): Whether to wrap the environment in an orderEnforcing wrapper
|
|
47
|
+
kwargs (dict): The kwargs to pass to the environment class
|
|
48
|
+
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
def __init__(
|
|
52
|
+
self,
|
|
53
|
+
id,
|
|
54
|
+
entry_point=None,
|
|
55
|
+
reward_threshold=None,
|
|
56
|
+
nondeterministic=False,
|
|
57
|
+
max_episode_steps=None,
|
|
58
|
+
order_enforce=True,
|
|
59
|
+
kwargs=None,
|
|
60
|
+
):
|
|
61
|
+
self.id = id
|
|
62
|
+
self.entry_point = entry_point
|
|
63
|
+
self.reward_threshold = reward_threshold
|
|
64
|
+
self.nondeterministic = nondeterministic
|
|
65
|
+
self.max_episode_steps = max_episode_steps
|
|
66
|
+
self.order_enforce = order_enforce
|
|
67
|
+
self._kwargs = {} if kwargs is None else kwargs
|
|
68
|
+
|
|
69
|
+
match = env_id_re.search(id)
|
|
70
|
+
if not match:
|
|
71
|
+
raise error.Error(
|
|
72
|
+
"Attempted to register malformed environment ID: {}. (Currently all IDs must be of the form {}.)".format(
|
|
73
|
+
id, env_id_re.pattern
|
|
74
|
+
)
|
|
75
|
+
)
|
|
76
|
+
self._env_name = match.group(1)
|
|
77
|
+
|
|
78
|
+
def make(self, **kwargs):
|
|
79
|
+
"""Instantiates an instance of the environment with appropriate kwargs"""
|
|
80
|
+
if self.entry_point is None:
|
|
81
|
+
raise error.Error(
|
|
82
|
+
"Attempting to make deprecated env {}. (HINT: is there a newer registered version of this env?)".format(
|
|
83
|
+
self.id
|
|
84
|
+
)
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
_kwargs = self._kwargs.copy()
|
|
88
|
+
_kwargs.update(kwargs)
|
|
89
|
+
|
|
90
|
+
if callable(self.entry_point):
|
|
91
|
+
env = self.entry_point(**_kwargs)
|
|
92
|
+
else:
|
|
93
|
+
cls = load(self.entry_point)
|
|
94
|
+
env = cls(**_kwargs)
|
|
95
|
+
|
|
96
|
+
# Make the environment aware of which spec it came from.
|
|
97
|
+
spec = copy.deepcopy(self)
|
|
98
|
+
spec._kwargs = _kwargs
|
|
99
|
+
env.unwrapped.spec = spec
|
|
100
|
+
if env.spec.max_episode_steps is not None:
|
|
101
|
+
from cuju.gfootball._gym._vendored.wrappers.time_limit import TimeLimit
|
|
102
|
+
|
|
103
|
+
env = TimeLimit(env, max_episode_steps=env.spec.max_episode_steps)
|
|
104
|
+
else:
|
|
105
|
+
if self.order_enforce:
|
|
106
|
+
from cuju.gfootball._gym._vendored.wrappers.order_enforcing import OrderEnforcing
|
|
107
|
+
|
|
108
|
+
env = OrderEnforcing(env)
|
|
109
|
+
return env
|
|
110
|
+
|
|
111
|
+
def __repr__(self):
|
|
112
|
+
return "EnvSpec({})".format(self.id)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class EnvRegistry(object):
|
|
116
|
+
"""Register an env by ID. IDs remain stable over time and are
|
|
117
|
+
guaranteed to resolve to the same environment dynamics (or be
|
|
118
|
+
desupported). The goal is that results on a particular environment
|
|
119
|
+
should always be comparable, and not depend on the version of the
|
|
120
|
+
code that was running.
|
|
121
|
+
"""
|
|
122
|
+
|
|
123
|
+
def __init__(self):
|
|
124
|
+
self.env_specs = {}
|
|
125
|
+
self._ns = None
|
|
126
|
+
|
|
127
|
+
def make(self, path, **kwargs):
|
|
128
|
+
if len(kwargs) > 0:
|
|
129
|
+
logger.info("Making new env: %s (%s)", path, kwargs)
|
|
130
|
+
else:
|
|
131
|
+
logger.info("Making new env: %s", path)
|
|
132
|
+
spec = self.spec(path)
|
|
133
|
+
env = spec.make(**kwargs)
|
|
134
|
+
return env
|
|
135
|
+
|
|
136
|
+
def all(self):
|
|
137
|
+
return self.env_specs.values()
|
|
138
|
+
|
|
139
|
+
def spec(self, path):
|
|
140
|
+
if ":" in path:
|
|
141
|
+
mod_name, _, id = path.partition(":")
|
|
142
|
+
try:
|
|
143
|
+
importlib.import_module(mod_name)
|
|
144
|
+
except ModuleNotFoundError:
|
|
145
|
+
raise error.Error(
|
|
146
|
+
"A module ({}) was specified for the environment but was not found, make sure the package is installed with `pip install` before calling `gym.make()`".format(
|
|
147
|
+
mod_name
|
|
148
|
+
)
|
|
149
|
+
)
|
|
150
|
+
else:
|
|
151
|
+
id = path
|
|
152
|
+
|
|
153
|
+
match = env_id_re.search(id)
|
|
154
|
+
if not match:
|
|
155
|
+
raise error.Error(
|
|
156
|
+
"Attempted to look up malformed environment ID: {}. (Currently all IDs must be of the form {}.)".format(
|
|
157
|
+
id.encode("utf-8"), env_id_re.pattern
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
try:
|
|
162
|
+
return self.env_specs[id]
|
|
163
|
+
except KeyError:
|
|
164
|
+
# Parse the env name and check to see if it matches the non-version
|
|
165
|
+
# part of a valid env (could also check the exact number here)
|
|
166
|
+
env_name = match.group(1)
|
|
167
|
+
matching_envs = [
|
|
168
|
+
valid_env_name
|
|
169
|
+
for valid_env_name, valid_env_spec in self.env_specs.items()
|
|
170
|
+
if env_name == valid_env_spec._env_name
|
|
171
|
+
]
|
|
172
|
+
algorithmic_envs = [
|
|
173
|
+
"Copy",
|
|
174
|
+
"RepeatCopy",
|
|
175
|
+
"DuplicatedInput",
|
|
176
|
+
"Reverse",
|
|
177
|
+
"ReversedAdiiton",
|
|
178
|
+
"ReversedAddition3",
|
|
179
|
+
]
|
|
180
|
+
toytext_envs = [
|
|
181
|
+
"KellyCoinflip",
|
|
182
|
+
"KellyCoinflipGeneralized",
|
|
183
|
+
"NChain",
|
|
184
|
+
"Roulette",
|
|
185
|
+
"GuessingGame",
|
|
186
|
+
"HotterColder",
|
|
187
|
+
]
|
|
188
|
+
if matching_envs:
|
|
189
|
+
raise error.DeprecatedEnv(
|
|
190
|
+
"Env {} not found (valid versions include {})".format(
|
|
191
|
+
id, matching_envs
|
|
192
|
+
)
|
|
193
|
+
)
|
|
194
|
+
elif env_name in algorithmic_envs:
|
|
195
|
+
raise error.UnregisteredEnv(
|
|
196
|
+
"Algorithmic environment {} has been moved out of Gym. Install it via `pip install gym-algorithmic` and add `import gym_algorithmic` before using it.".format(
|
|
197
|
+
id
|
|
198
|
+
)
|
|
199
|
+
)
|
|
200
|
+
elif env_name in toytext_envs:
|
|
201
|
+
raise error.UnregisteredEnv(
|
|
202
|
+
"Toytext environment {} has been moved out of Gym. Install it via `pip install gym-legacy-toytext` and add `import gym_toytext` before using it.".format(
|
|
203
|
+
id
|
|
204
|
+
)
|
|
205
|
+
)
|
|
206
|
+
else:
|
|
207
|
+
raise error.UnregisteredEnv("No registered env with id: {}".format(id))
|
|
208
|
+
|
|
209
|
+
def register(self, id, **kwargs):
|
|
210
|
+
if self._ns is not None:
|
|
211
|
+
if "/" in id:
|
|
212
|
+
namespace, id = id.split("/")
|
|
213
|
+
logger.warn(
|
|
214
|
+
f"Custom namespace '{namespace}' is being overrode by namespace '{self._ns}'. "
|
|
215
|
+
"If you are developing a plugin you shouldn't specify a namespace in `register` calls. "
|
|
216
|
+
"The namespace is specified through the entry point key."
|
|
217
|
+
)
|
|
218
|
+
id = f"{self._ns}/{id}"
|
|
219
|
+
if id in self.env_specs:
|
|
220
|
+
logger.warn("Overriding environment {}".format(id))
|
|
221
|
+
self.env_specs[id] = EnvSpec(id, **kwargs)
|
|
222
|
+
|
|
223
|
+
@contextlib.contextmanager
|
|
224
|
+
def namespace(self, ns):
|
|
225
|
+
self._ns = ns
|
|
226
|
+
yield
|
|
227
|
+
self._ns = None
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
# Have a global registry
|
|
231
|
+
registry = EnvRegistry()
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def register(id, **kwargs):
|
|
235
|
+
return registry.register(id, **kwargs)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def make(id, **kwargs):
|
|
239
|
+
return registry.make(id, **kwargs)
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def spec(id):
|
|
243
|
+
return registry.spec(id)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
@contextlib.contextmanager
|
|
247
|
+
def namespace(ns):
|
|
248
|
+
with registry.namespace(ns):
|
|
249
|
+
yield
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def load_env_plugins(entry_point="gym.envs"):
|
|
253
|
+
# Load third-party environments
|
|
254
|
+
for plugin in metadata.entry_points(group=entry_point):
|
|
255
|
+
# Python 3.8 doesn't support plugin.module, plugin.attr
|
|
256
|
+
# So we'll have to try and parse this ourselves
|
|
257
|
+
try:
|
|
258
|
+
module, attr = plugin.module, plugin.attr
|
|
259
|
+
except AttributeError:
|
|
260
|
+
if ":" in plugin.value:
|
|
261
|
+
module, attr = plugin.value.split(":", maxsplit=1)
|
|
262
|
+
else:
|
|
263
|
+
module, attr = plugin.value, None
|
|
264
|
+
finally:
|
|
265
|
+
if attr is None:
|
|
266
|
+
raise error.Error(
|
|
267
|
+
f"Gym environment plugin `{module}` must specify a function to execute, not a root module"
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
context = namespace(plugin.name)
|
|
271
|
+
if plugin.name == "__internal__":
|
|
272
|
+
if module in plugin_internal_whitelist:
|
|
273
|
+
context = contextlib.nullcontext()
|
|
274
|
+
else:
|
|
275
|
+
logger.warn(
|
|
276
|
+
f"Trying to register an internal environment when `{module}` is not in the whitelist"
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
with context:
|
|
280
|
+
fn = plugin.load()
|
|
281
|
+
try:
|
|
282
|
+
fn()
|
|
283
|
+
except Exception as e:
|
|
284
|
+
logger.warn(str(e))
|