From 0d5e597778ab2ae24b6103e1ff2c3bda2b0b8d5f Mon Sep 17 00:00:00 2001 From: huangshiyu Date: Fri, 1 Sep 2023 15:03:17 +0800 Subject: [PATCH 1/2] format --- Makefile | 2 +- tests/test_env.py | 2 +- tizero/football_env/football_jidi_eval.py | 2 +- tizero/football_env/football_pettingzoo.py | 1 + .../scenarios/football_scenario.py | 4 +- tizero/utils/script_helpers.py | 20 +- .../visualize_tools/game_graph/game_graph.py | 6 +- .../visualizer/team_info_table.py | 4 +- .../visualizer/translation_cn.py | 216 ++++++++++++------ .../visualizer/translation_en.py | 201 +++++++++++----- 10 files changed, 315 insertions(+), 143 deletions(-) diff --git a/Makefile b/Makefile index 689b4cd..c757dc5 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ test: lint: $(call check_install, ruff) ruff ${PYTHON_FILES} --select=E9,F63,F7,F82 --show-source - ruff ${PYTHON_FILES} --exit-zero + ruff ${PYTHON_FILES} --exit-zero | grep -v '501\|405\|401\|402\|403\|722' format: $(call check_install, isort) diff --git a/tests/test_env.py b/tests/test_env.py index f3f8977..3ecf80f 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -5,7 +5,7 @@ def raw_env(): from tizero.football_env.football_jidi_eval import FootballJiDiEnv env = FootballJiDiEnv("11_vs_11_jidi_eval") - reset_obs = env.reset() + env.reset() single_action = np.zeros(20) single_action[-1] = 1 diff --git a/tizero/football_env/football_jidi_eval.py b/tizero/football_env/football_jidi_eval.py index 77eab0a..24df990 100644 --- a/tizero/football_env/football_jidi_eval.py +++ b/tizero/football_env/football_jidi_eval.py @@ -89,7 +89,7 @@ def reset(self): def add_control_index(self, raw_obs): for i, o in enumerate(raw_obs): - if not "controlled_player_index" in o: + if "controlled_player_index" not in o: o["controlled_player_index"] = i % 11 return raw_obs diff --git a/tizero/football_env/football_pettingzoo.py b/tizero/football_env/football_pettingzoo.py index f6980a7..764ed30 100644 --- a/tizero/football_env/football_pettingzoo.py +++ b/tizero/football_env/football_pettingzoo.py @@ -33,6 +33,7 @@ class FootballAECEnv(AECEnv): @property def agent_num(self): return self.player_each_side + def __init__(self, render_mode: Optional[str] = None, id: str = None): self.env = FootballJiDiEnv(scenario_name=id.split("/")[-1]) diff --git a/tizero/football_env/scenarios/football_scenario.py b/tizero/football_env/scenarios/football_scenario.py index 337c2ad..301a3e0 100644 --- a/tizero/football_env/scenarios/football_scenario.py +++ b/tizero/football_env/scenarios/football_scenario.py @@ -140,8 +140,8 @@ def test_scenario(): for i in range(200): obs = raw_o[0] active_position = obs["left_team"][obs["active"]] - sticky_actions = obs["sticky_actions"][:10] - active_direction = obs["left_team_direction"][obs["active"]] + # sticky_actions = obs["sticky_actions"][:10] + # active_direction = obs["left_team_direction"][obs["active"]] relative_ball_position = obs["ball"][:2] - active_position all_directions_vecs = [ np.array(v) / np.linalg.norm(np.array(v)) for v in ALL_DIRECTION_VECS diff --git a/tizero/utils/script_helpers.py b/tizero/utils/script_helpers.py index 89ec32c..0eca96c 100644 --- a/tizero/utils/script_helpers.py +++ b/tizero/utils/script_helpers.py @@ -82,15 +82,15 @@ def load_dump(self, dump_file): return dump dump.append(step) - def dump_to_txt(self, dump_file, output, include_debug): - with open(output, "w") as out_fd: - dump = self.load_dump(dump_file) - if not include_debug: - for s in dump: - if "debug" in s: - del s["debug"] - with open(output, "w") as f: - f.write(str(dump)) + # def dump_to_txt(self, dump_file, output, include_debug): + # with open(output, "w") as out_fd: + # dump = self.load_dump(dump_file) + # if not include_debug: + # for s in dump: + # if "debug" in s: + # del s["debug"] + # with open(output, "w") as f: + # f.write(str(dump)) def dump_to_video(self, dump_file, directory=None, episode_length=None): dump_path = Path(dump_file) @@ -124,7 +124,7 @@ def dump_to_video(self, dump_file, directory=None, episode_length=None): processor.update(dump[step]) del processor - processor = None + # processor = None avi_files = [] try_time = 0 diff --git a/tizero/utils/visualize_tools/game_graph/game_graph.py b/tizero/utils/visualize_tools/game_graph/game_graph.py index 8c300aa..6fe52de 100644 --- a/tizero/utils/visualize_tools/game_graph/game_graph.py +++ b/tizero/utils/visualize_tools/game_graph/game_graph.py @@ -286,9 +286,9 @@ def s_step(self): def e_step(self): return self.subgames[-1].e_step if len(self) > 0 else None - @property - def n_steps(self): - return self.e_step - self.s_step + 1 + # @property + # def n_steps(self): + # return self.e_step - self.s_step + 1 @property def n_left(self): diff --git a/tizero/utils/visualize_tools/visualizer/team_info_table.py b/tizero/utils/visualize_tools/visualizer/team_info_table.py index 6d5f9e3..d116be7 100644 --- a/tizero/utils/visualize_tools/visualizer/team_info_table.py +++ b/tizero/utils/visualize_tools/visualizer/team_info_table.py @@ -110,10 +110,10 @@ def get_df(self, obs, team, step): if team == "left": roles = self.left_team_roles - n = self.n_left + # n = self.n_left else: roles = self.right_team_roles - n = self.n_right + # n = self.n_right roles = [SIMPLE_ROLES(r) + str(idx) for idx, r in enumerate(roles)] diff --git a/tizero/utils/visualize_tools/visualizer/translation_cn.py b/tizero/utils/visualize_tools/visualizer/translation_cn.py index e262253..25ace41 100644 --- a/tizero/utils/visualize_tools/visualizer/translation_cn.py +++ b/tizero/utils/visualize_tools/visualizer/translation_cn.py @@ -11,67 +11,155 @@ # See the License for the specific language governing permissions and # limitations under the License. -TEAMS = lambda x: [ - "左", # left - "右", # right - "无", # free -][x] - -ROLES = lambda x: ["GK", "CB", "LB", "RB", "DM", "CM", "LM", "RM", "AM", "CF"][x] - -SIMPLE_ROLES = lambda x: ["G", "c", "l", "r", "D", "C", "L", "R", "A", "F"][x] - -GAME_MODES = lambda x: ["正常", "开球", "球门球", "任意球", "角球", "边线球", "点球"][x] - -ACTIONS = lambda x: [ - "空", - "\u2190", # 左 1 - "\u2196", # 上左 - "\u2191", # 上 - "\u2197", # 上右 - "\u2192", # 右 - "\u2198", # 下右 - "\u2193", # 下 - "\u2199", # 下左 8 - "长传", # 9 - "高传", # 10 - "短传", # 11 - "射门", # 12 - "冲刺", # 13 - "释放方向", # 14 - "释放冲刺", # 15 - "滑铲", # 16 - "盘球", # 17 - "释放盘球", # 18 - "内置AI", # 19 - "门将出击", # 20 - "施压", # 21 - "全队施压", # 22 - "切换", # 23 - "释放长传", # 24 - "释放高传", # 25 - "释放短传", # 26 - "释放射门", # 27 - "释放出击", # 28 - "释放铲球", # 29 - "释放施压", # 30 - "释放全压", # 31 - "释放切换", # 32 - "-", # not controlled -][x] - -DIRECTIONS = lambda x: [ - "\u2190", # 左 - "\u2196", # 上左 - "\u2191", # 上 - "\u2197", # 上右 - "\u2192", # 右 - "\u2198", # 下右 - "\u2193", # 下 - "\u2199", # 下左 - "-", -][x] - -INSTRUCTIONS = lambda x: ["跑动", "传球", "射门", "铲球"][x] - -USE_NEW_INSTRUCTIONS = lambda x: ["旧指令", "新指令"][x] +# TEAMS = lambda x: [ +# "左", # left +# "右", # right +# "无", # free +# ][x] + + +def TEAMS(x): + return [ + "左", # left + "右", # right + "无", # free + ][x] + + +# ROLES = lambda x: ["GK", "CB", "LB", "RB", "DM", "CM", "LM", "RM", "AM", "CF"][x] + + +def TEROLESAMS(x): + return ["GK", "CB", "LB", "RB", "DM", "CM", "LM", "RM", "AM", "CF"][x] + + +# SIMPLE_ROLES = lambda x: ["G", "c", "l", "r", "D", "C", "L", "R", "A", "F"][x] + + +def SIMPLE_ROLES(x): + return ["G", "c", "l", "r", "D", "C", "L", "R", "A", "F"][x] + + +# GAME_MODES = lambda x: ["正常", "开球", "球门球", "任意球", "角球", "边线球", "点球"][x] + + +def GAME_MODES(x): + return ["正常", "开球", "球门球", "任意球", "角球", "边线球", "点球"][x] + + +# ACTIONS = lambda x: [ +# "空", +# "\u2190", # 左 1 +# "\u2196", # 上左 +# "\u2191", # 上 +# "\u2197", # 上右 +# "\u2192", # 右 +# "\u2198", # 下右 +# "\u2193", # 下 +# "\u2199", # 下左 8 +# "长传", # 9 +# "高传", # 10 +# "短传", # 11 +# "射门", # 12 +# "冲刺", # 13 +# "释放方向", # 14 +# "释放冲刺", # 15 +# "滑铲", # 16 +# "盘球", # 17 +# "释放盘球", # 18 +# "内置AI", # 19 +# "门将出击", # 20 +# "施压", # 21 +# "全队施压", # 22 +# "切换", # 23 +# "释放长传", # 24 +# "释放高传", # 25 +# "释放短传", # 26 +# "释放射门", # 27 +# "释放出击", # 28 +# "释放铲球", # 29 +# "释放施压", # 30 +# "释放全压", # 31 +# "释放切换", # 32 +# "-", # not controlled +# ][x] + + +def ACTIONS(x): + return [ + "空", + "\u2190", # 左 1 + "\u2196", # 上左 + "\u2191", # 上 + "\u2197", # 上右 + "\u2192", # 右 + "\u2198", # 下右 + "\u2193", # 下 + "\u2199", # 下左 8 + "长传", # 9 + "高传", # 10 + "短传", # 11 + "射门", # 12 + "冲刺", # 13 + "释放方向", # 14 + "释放冲刺", # 15 + "滑铲", # 16 + "盘球", # 17 + "释放盘球", # 18 + "内置AI", # 19 + "门将出击", # 20 + "施压", # 21 + "全队施压", # 22 + "切换", # 23 + "释放长传", # 24 + "释放高传", # 25 + "释放短传", # 26 + "释放射门", # 27 + "释放出击", # 28 + "释放铲球", # 29 + "释放施压", # 30 + "释放全压", # 31 + "释放切换", # 32 + "-", # not controlled + ][x] + + +# DIRECTIONS = lambda x: [ +# "\u2190", # 左 +# "\u2196", # 上左 +# "\u2191", # 上 +# "\u2197", # 上右 +# "\u2192", # 右 +# "\u2198", # 下右 +# "\u2193", # 下 +# "\u2199", # 下左 +# "-", +# ][x] + + +def DIRECTIONS(x): + return [ + "\u2190", # 左 + "\u2196", # 上左 + "\u2191", # 上 + "\u2197", # 上右 + "\u2192", # 右 + "\u2198", # 下右 + "\u2193", # 下 + "\u2199", # 下左 + "-", + ][x] + + +# INSTRUCTIONS = lambda x: ["跑动", "传球", "射门", "铲球"][x] + + +def INSTRUCTIONS(x): + return ["跑动", "传球", "射门", "铲球"][x] + + +# USE_NEW_INSTRUCTIONS = lambda x: ["旧指令", "新指令"][x] + + +def USE_NEW_INSTRUCTIONS(x): + return ["旧指令", "新指令"][x] diff --git a/tizero/utils/visualize_tools/visualizer/translation_en.py b/tizero/utils/visualize_tools/visualizer/translation_en.py index fb01a03..15d2e85 100644 --- a/tizero/utils/visualize_tools/visualizer/translation_en.py +++ b/tizero/utils/visualize_tools/visualizer/translation_en.py @@ -11,62 +11,145 @@ # See the License for the specific language governing permissions and # limitations under the License. -TEAMS = lambda x: [ - "left", # left - "right", # right - "none", # free -][x] - -ROLES = lambda x: ["GK", "CB", "LB", "RB", "DM", "CM", "LM", "RM", "AM", "CF"][x] - -SIMPLE_ROLES = lambda x: ["G", "c", "l", "r", "D", "C", "L", "R", "A", "F"][x] - -GAME_MODES = lambda x: [ - "normal", - "kick off", - "goal kick", - "free kick", - "corner", - "throw-in", - "penalty", -][x] - -ACTIONS = lambda x: [ - "none", - "\u2190", # 左 1 - "\u2196", # 上左 - "\u2191", # 上 - "\u2197", # 上右 - "\u2192", # 右 - "\u2198", # 下右 - "\u2193", # 下 - "\u2199", # 下左 8 - "long pass", # 9 - "high pass", # 10 - "short pass", # 11 - "shot", # 12 - "sprint", # 13 - "no direction", # 14 - "no sprint", # 15 - "slide", # 16 - "dribble", # 17 - "no dribble", # 18 - "built-in ai", # 19 - "-", # not controlled -][x] - -DIRECTIONS = lambda x: [ - "\u2190", # 左 - "\u2196", # 上左 - "\u2191", # 上 - "\u2197", # 上右 - "\u2192", # 右 - "\u2198", # 下右 - "\u2193", # 下 - "\u2199", # 下左 - "-", -][x] - -INSTRUCTIONS = lambda x: ["RUN", "PASS", "SHOT", "SLIDE"][x] - -USE_NEW_INSTRUCTIONS = lambda x: ["OLD", "NEW"][x] +# TEAMS = lambda x: [ +# "left", # left +# "right", # right +# "none", # free +# ][x] + + +def TEAMS(x): + return [ + "left", # left + "right", # right + "none", # free + ][x] + + +# ROLES = lambda x: ["GK", "CB", "LB", "RB", "DM", "CM", "LM", "RM", "AM", "CF"][x] + + +def ROLES(x): + return ["GK", "CB", "LB", "RB", "DM", "CM", "LM", "RM", "AM", "CF"][x] + + +# SIMPLE_ROLES = lambda x: ["G", "c", "l", "r", "D", "C", "L", "R", "A", "F"][x] + + +def SIMPLE_ROLES(x): + return ["G", "c", "l", "r", "D", "C", "L", "R", "A", "F"][x] + + +# GAME_MODES = lambda x: [ +# "normal", +# "kick off", +# "goal kick", +# "free kick", +# "corner", +# "throw-in", +# "penalty", +# ][x] + + +def GAME_MODES(x): + return [ + "normal", + "kick off", + "goal kick", + "free kick", + "corner", + "throw-in", + "penalty", + ][x] + + +# ACTIONS = lambda x: [ +# "none", +# "\u2190", # 左 1 +# "\u2196", # 上左 +# "\u2191", # 上 +# "\u2197", # 上右 +# "\u2192", # 右 +# "\u2198", # 下右 +# "\u2193", # 下 +# "\u2199", # 下左 8 +# "long pass", # 9 +# "high pass", # 10 +# "short pass", # 11 +# "shot", # 12 +# "sprint", # 13 +# "no direction", # 14 +# "no sprint", # 15 +# "slide", # 16 +# "dribble", # 17 +# "no dribble", # 18 +# "built-in ai", # 19 +# "-", # not controlled +# ][x] + + +def ACTIONS(x): + return [ + "none", + "\u2190", # 左 1 + "\u2196", # 上左 + "\u2191", # 上 + "\u2197", # 上右 + "\u2192", # 右 + "\u2198", # 下右 + "\u2193", # 下 + "\u2199", # 下左 8 + "long pass", # 9 + "high pass", # 10 + "short pass", # 11 + "shot", # 12 + "sprint", # 13 + "no direction", # 14 + "no sprint", # 15 + "slide", # 16 + "dribble", # 17 + "no dribble", # 18 + "built-in ai", # 19 + "-", # not controlled + ][x] + + +# DIRECTIONS = lambda x: [ +# "\u2190", # 左 +# "\u2196", # 上左 +# "\u2191", # 上 +# "\u2197", # 上右 +# "\u2192", # 右 +# "\u2198", # 下右 +# "\u2193", # 下 +# "\u2199", # 下左 +# "-", +# ][x] + + +def DIRECTIONS(x): + return [ + "\u2190", # 左 + "\u2196", # 上左 + "\u2191", # 上 + "\u2197", # 上右 + "\u2192", # 右 + "\u2198", # 下右 + "\u2193", # 下 + "\u2199", # 下左 + "-", + ][x] + + +# INSTRUCTIONS = lambda x: ["RUN", "PASS", "SHOT", "SLIDE"][x] + + +def INSTRUCTIONS(x): + return ["RUN", "PASS", "SHOT", "SLIDE"][x] + + +# USE_NEW_INSTRUCTIONS = lambda x: ["OLD", "NEW"][x] + + +def USE_NEW_INSTRUCTIONS(x): + return ["OLD", "NEW"][x] From e70cae9a22dd23c1cc49fcfd44efc382f511f2fa Mon Sep 17 00:00:00 2001 From: huangshiyu Date: Fri, 1 Sep 2023 17:55:47 +0800 Subject: [PATCH 2/2] update readme and document --- README.md | 2 ++ docs/index.rst | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6184eb9..be61bd5 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ For example, you can evaluate tizero with random agent as below: tizero eval --left_agent submission/tizero --right_agent submission/random_agent --total_game 10 ``` +For evaluations for JiDi submissions on other games, please refer to the [Arena](https://openrl-docs.readthedocs.io/en/latest/arena/index.html) of OpenRL +and this [example](https://github.com/OpenRL-Lab/openrl/tree/main/examples/snake) for the snake game. ### Show a saved dump file diff --git a/docs/index.rst b/docs/index.rst index f93f6ff..d160834 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -12,11 +12,47 @@ Installation ____________ * Follow the instructions in `gfootball `_ to set up the environment. -* ``pip install gfootball`` +* ``pip install gfootball openrl "openrl[selfplay]"`` * ``pip install tizero`` (or clone this repo and ``pip install -e .`` ). * test the installation by ``python3 -m gfootball.play_game --action_set=full`` . +Evaluate JiDi submissions locally +____________ + +You can evaluate your agent locally using tizero: + +.. code-block:: bash + + tizero eval --left_agent submission_dir1 --right_agent submission_dir2 --total_game 10 + + +For example, you can evaluate tizero with random agent as below: + +.. code-block:: bash + + tizero eval --left_agent submission/tizero --right_agent submission/random_agent --total_game 10 + + +For evaluations for JiDi submissions on other games, please refer to the `Arena `_ of OpenRL +and this `example `_ for the snake game. + +Show a saved dump file +____________ + +* show detailed infomation of a match via: ``tizero show dump_file`` +* show keypoints of a mactch via: ``tizero keypoint dump_file`` + +You can download an example dump file from `here `_. + +Then execute: ``tizero show daily_6484285.dump`` or ``tizero keypoint daily_6484285.dump`` . Then you will see a GUI as below: + +.. image:: + _static/images/show_dump.png + :width: 1000 + :align: center + + Convert dump file to video ____________