|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright 2023 The OpenRL Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""""" |
| 18 | + |
| 19 | +#!/usr/bin/env python |
| 20 | +# -*- coding: utf-8 -*- |
| 21 | +# Copyright 2023 The OpenRL Authors. |
| 22 | +# |
| 23 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 24 | +# you may not use this file except in compliance with the License. |
| 25 | +# You may obtain a copy of the License at |
| 26 | +# |
| 27 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 28 | +# |
| 29 | +# Unless required by applicable law or agreed to in writing, software |
| 30 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 31 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 32 | +# See the License for the specific language governing permissions and |
| 33 | +# limitations under the License. |
| 34 | + |
| 35 | +"""""" |
| 36 | + |
| 37 | +from pettingzoo.butterfly import cooperative_pong_v5 |
| 38 | +from pettingzoo.classic import connect_four_v3, go_v5, texas_holdem_no_limit_v6 |
| 39 | +from pettingzoo.mpe import simple_push_v3 |
| 40 | + |
| 41 | +from examples.custom_env.rock_paper_scissors import RockPaperScissors |
| 42 | +from openrl.arena import make_arena |
| 43 | +from openrl.arena.agents.local_agent import LocalAgent |
| 44 | +from openrl.envs.PettingZoo.registration import register |
| 45 | +from openrl.envs.wrappers.pettingzoo_wrappers import RecordWinner |
| 46 | + |
| 47 | + |
| 48 | +def ConnectFourEnv(render_mode, **kwargs): |
| 49 | + return connect_four_v3.env(render_mode) |
| 50 | + |
| 51 | + |
| 52 | +def RockPaperScissorsEnv(render_mode, **kwargs): |
| 53 | + return RockPaperScissors(render_mode) |
| 54 | + |
| 55 | + |
| 56 | +def GoEnv(render_mode, **kwargs): |
| 57 | + return go_v5.env(render_mode=render_mode, board_size=5, komi=7.5) |
| 58 | + |
| 59 | + |
| 60 | +def TexasHoldemEnv(render_mode, **kwargs): |
| 61 | + return texas_holdem_no_limit_v6.env(render_mode=render_mode) |
| 62 | + |
| 63 | + |
| 64 | +# MPE |
| 65 | +def SimplePushEnv(render_mode, **kwargs): |
| 66 | + return simple_push_v3.env(render_mode=render_mode) |
| 67 | + |
| 68 | + |
| 69 | +def CooperativePongEnv(render_mode, **kwargs): |
| 70 | + return cooperative_pong_v5.env(render_mode=render_mode) |
| 71 | + |
| 72 | + |
| 73 | +def register_new_envs(): |
| 74 | + new_env_dict = { |
| 75 | + "connect_four_v3": ConnectFourEnv, |
| 76 | + "RockPaperScissors": RockPaperScissorsEnv, |
| 77 | + "go_v5": GoEnv, |
| 78 | + "texas_holdem_no_limit_v6": TexasHoldemEnv, |
| 79 | + "simple_push_v3": SimplePushEnv, |
| 80 | + "cooperative_pong_v5": CooperativePongEnv, |
| 81 | + } |
| 82 | + |
| 83 | + for env_id, env in new_env_dict.items(): |
| 84 | + register(env_id, env) |
| 85 | + return new_env_dict.keys() |
| 86 | + |
| 87 | + |
| 88 | +def run_arena( |
| 89 | + env_id: str, |
| 90 | + parallel: bool = True, |
| 91 | + seed=0, |
| 92 | + total_games: int = 10, |
| 93 | + max_game_onetime: int = 5, |
| 94 | +): |
| 95 | + env_wrappers = [RecordWinner] |
| 96 | + |
| 97 | + arena = make_arena(env_id, env_wrappers=env_wrappers, use_tqdm=False) |
| 98 | + |
| 99 | + agent1 = LocalAgent("../selfplay/opponent_templates/random_opponent") |
| 100 | + agent2 = LocalAgent("../selfplay/opponent_templates/random_opponent") |
| 101 | + |
| 102 | + arena.reset( |
| 103 | + agents={"agent1": agent1, "agent2": agent2}, |
| 104 | + total_games=total_games, |
| 105 | + max_game_onetime=max_game_onetime, |
| 106 | + seed=seed, |
| 107 | + ) |
| 108 | + result = arena.run(parallel=parallel) |
| 109 | + arena.close() |
| 110 | + print(result) |
| 111 | + return result |
| 112 | + |
| 113 | + |
| 114 | +def test_new_envs(): |
| 115 | + env_ids = register_new_envs() |
| 116 | + seed = 0 |
| 117 | + for env_id in env_ids: |
| 118 | + run_arena(env_id=env_id, seed=seed, parallel=False, total_games=1) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + test_new_envs() |
0 commit comments