6
6
7
7
import copy
8
8
import importlib
9
+ import warnings
9
10
from typing import Dict , List , Tuple , Union
10
11
11
12
import torch
27
28
def _get_envs ():
28
29
if not _has_pettingzoo :
29
30
raise ImportError ("PettingZoo is not installed in your virtual environment." )
30
- from pettingzoo .utils .all_modules import all_environments
31
+ try :
32
+ from pettingzoo .utils .all_modules import all_environments
33
+ except ModuleNotFoundError as err :
34
+ warnings .warn (
35
+ f"PettingZoo failed to load all modules with error message { err } , trying to load individual modules."
36
+ )
37
+ all_environments = _load_available_envs ()
31
38
32
39
return list (all_environments .keys ())
33
40
34
41
42
+ def _load_available_envs () -> Dict :
43
+ all_environments = {}
44
+ try :
45
+ from pettingzoo .mpe .all_modules import mpe_environments
46
+
47
+ all_environments .update (mpe_environments )
48
+ except ModuleNotFoundError as err :
49
+ warnings .warn (f"MPE environments failed to load with error message { err } ." )
50
+ try :
51
+ from pettingzoo .sisl .all_modules import sisl_environments
52
+
53
+ all_environments .update (sisl_environments )
54
+ except ModuleNotFoundError as err :
55
+ warnings .warn (f"SISL environments failed to load with error message { err } ." )
56
+ try :
57
+ from pettingzoo .classic .all_modules import classic_environments
58
+
59
+ all_environments .update (classic_environments )
60
+ except ModuleNotFoundError as err :
61
+ warnings .warn (f"Classic environments failed to load with error message { err } ." )
62
+ try :
63
+ from pettingzoo .atari .all_modules import atari_environments
64
+
65
+ all_environments .update (atari_environments )
66
+ except ModuleNotFoundError as err :
67
+ warnings .warn (f"Atari environments failed to load with error message { err } ." )
68
+ try :
69
+ from pettingzoo .butterfly .all_modules import butterfly_environments
70
+
71
+ all_environments .update (butterfly_environments )
72
+ except ModuleNotFoundError as err :
73
+ warnings .warn (
74
+ f"Butterfly environments failed to load with error message { err } ."
75
+ )
76
+ return all_environments
77
+
78
+
35
79
class PettingZooWrapper (_EnvWrapper ):
36
80
"""PettingZoo environment wrapper.
37
81
@@ -834,7 +878,8 @@ class PettingZooEnv(PettingZooWrapper):
834
878
neural network.
835
879
836
880
Args:
837
- task (str): the name of the pettingzoo task to create (for example, "multiwalker_v9").
881
+ task (str): the name of the pettingzoo task to create in the "<env>/<task>" format (for example, "sisl/multiwalker_v9")
882
+ or "<task>" format (for example, "multiwalker_v9").
838
883
parallel (bool): if to construct the ``pettingzoo.ParallelEnv`` version of the task or the ``pettingzoo.AECEnv``.
839
884
return_state (bool, optional): whether to return the global state from pettingzoo
840
885
(not available in all environments). Defaults to ``False``.
@@ -919,7 +964,13 @@ def _build_env(
919
964
]:
920
965
self .task_name = task
921
966
922
- from pettingzoo .utils .all_modules import all_environments
967
+ try :
968
+ from pettingzoo .utils .all_modules import all_environments
969
+ except ModuleNotFoundError as err :
970
+ warnings .warn (
971
+ f"PettingZoo failed to load all modules with error message { err } , trying to load individual modules."
972
+ )
973
+ all_environments = _load_available_envs ()
923
974
924
975
if task not in all_environments :
925
976
# Try looking at the literal translation of values
@@ -929,7 +980,9 @@ def _build_env(
929
980
task_module = value
930
981
break
931
982
if task_module is None :
932
- raise RuntimeError (f"Specified task not in { _get_envs ()} " )
983
+ raise RuntimeError (
984
+ f"Specified task not in available environments { all_environments } "
985
+ )
933
986
else :
934
987
task_module = all_environments [task ]
935
988
0 commit comments