Open callumtilbury opened 1 year ago
PS: GitHub Markdown should renders code blocks that are linked from GitHub itself, but that isn't working here (weirdly). I've pasted the code manually.
+1. Basically makes the environment unusable unless you fork the repo and work from there.
When installing
lb-foraging
with certain versions ofgym
, the import hangs for a (very) long time.Consider a simple script,
import_tests.py
:With
gym==0.21.*
:vs. with
gym==0.22.*
:This massive bottleneck is caused by the way
gym
registers environments from v0.22 onwards. Consider thegym/envs/registrations.py::EnvRegistry::register()
method in v0.21:https://github.com/openai/gym/blob/c755d5c35a25ab118746e2ba885894ff66fb8c43/gym/envs/registration.py#L205-L217
See how this is different in v0.22:
https://github.com/openai/gym/blob/95063a08943e1f587c58be7435d94f81ccac8fd9/gym/envs/registration.py#L542-L596
Specifically, the issue here is the addition of: https://github.com/openai/gym/blob/95063a08943e1f587c58be7435d94f81ccac8fd9/gym/envs/registration.py#L559
which calls https://github.com/openai/gym/blob/95063a08943e1f587c58be7435d94f81ccac8fd9/gym/envs/registration.py#L220
which eventually hits: https://github.com/openai/gym/blob/95063a08943e1f587c58be7435d94f81ccac8fd9/gym/envs/registration.py#L293
Herein lies the problem:
lbforaging
attempts to register a large number of environments (9720 by default). With each new environment,gym
checks whether any environment has already been registered with a similar name. Naturally, this fuzzy match scales really badly with a large number of environments being registered.v0.23 of
gym
takes the same approach to registration as v0.22, and thus faces the same issue. In v0.24-v0.26, the registration no longer looks for a fuzzy match (with difflib), but there is still a bottleneck due to an iteration over all registered envs: https://github.com/openai/gym/blob/dcd185843a62953e27c2d54dc8c2d647d604b635/gym/envs/registration.py#L379-L403thus still yielding bad performance (definitely not as bad as v0.22 & v0.23 though):
I'm not sure what the solution for
lbforaging
is here?Note: I am aware that gym has officially moved to https://github.com/Farama-Foundation/Gymnasium, but the issue remains there with gymnasium v0.27: https://github.com/Farama-Foundation/Gymnasium/blob/6f35e7f87fc5b455b8cc70e366016c463fa52850/gymnasium/envs/registration.py#L298-L321