|
135 | 135 | "\n", |
136 | 136 | "More information are provided here: https://grid2op.readthedocs.io/en/latest/environment.html#splitting-into-raining-validation-test-scenarios\n", |
137 | 137 | "\n", |
| 138 | + "### Use the `experimental_read_from_local_dir` flag\n", |
| 139 | + "\n", |
| 140 | + "This flag allows python to better \"understands\" the classes in grid2op and avoid lots of issue with pickle / multi processing etc.\n", |
| 141 | + "\n", |
| 142 | + "The complete documentation is available here https://grid2op.readthedocs.io/en/latest/environment.html#grid2op.Environment.BaseEnv.generate_classes\n", |
| 143 | + "\n", |
| 144 | + "Basically, once, and only once, outside of this process, you can call:\n", |
| 145 | + "\n", |
| 146 | + "```python\n", |
| 147 | + "import grid2op\n", |
| 148 | + "env_name = \"l2rpn_case14_sandbox\" # or any other name\n", |
| 149 | + "\n", |
| 150 | + "env = grid2op.make(env_name, ...) # again: redo this step each time you customize \"...\"\n", |
| 151 | + "# for example if you change the `action_class` or the `backend` etc.\n", |
| 152 | + "\n", |
| 153 | + "env.generate_classes()\n", |
| 154 | + "```\n", |
| 155 | + "\n", |
| 156 | + "Then, each time you want to reload the same environment, you can do:\n", |
| 157 | + "\n", |
| 158 | + "```python\n", |
| 159 | + "import grid2op\n", |
| 160 | + "env_name = SAME NAME AS ABOVE\n", |
| 161 | + "env = grid2op.make(env_name,\n", |
| 162 | + " experimental_read_from_local_dir=True,\n", |
| 163 | + " ..., # SAME ENV CUSTOMIZATION AS ABOVE\n", |
| 164 | + " )\n", |
| 165 | + "```\n", |
| 166 | + "\n", |
| 167 | + "This is known to solve bug related to multi processing, but also to reduce the amount of RAM taken (in some cases) as well as creation time (in some cases)\n", |
| 168 | + "\n", |
138 | 169 | "### Other steps\n", |
139 | 170 | "\n", |
140 | 171 | "The grid2op documentation is full of details to \"optimize\" the number of steps you can do per seconds. This number can rise from a few dozen per seconds to around a thousands per seconds with proper care.\n", |
|
190 | 221 | "metadata": {}, |
191 | 222 | "outputs": [], |
192 | 223 | "source": [ |
193 | | - "import gym\n", |
| 224 | + "import gymnasium\n", |
194 | 225 | "import numpy as np\n", |
195 | 226 | "from grid2op.gym_compat import GymEnv\n", |
196 | 227 | "env_gym_init = GymEnv(env_glop)\n", |
197 | 228 | "env_gym = GymEnv(env_glop)\n", |
198 | | - "print(f\"The \\\"env_gym\\\" is a gym environment: {isinstance(env_gym, gym.Env)}\")\n", |
199 | | - "obs_gym = env_gym.reset()" |
200 | | - ] |
201 | | - }, |
202 | | - { |
203 | | - "cell_type": "markdown", |
204 | | - "metadata": {}, |
205 | | - "source": [ |
206 | | - "<font size=\"3\" color=\"red\"> In this notebook, we only present some basic (and really \"detailed\" use of the `GymEnv`). </font>\n", |
207 | | - " \n", |
208 | | - "<font size=\"3\" color=\"red\"> This is especially suited for advanced users wanting a deep control over everything happening. </font>\n", |
209 | | - "\n", |
210 | | - "<font size=\"3\" color=\"red\"> For a less advanced usage, feel free to consult the l2rpn baselines package, that embed some usefull environments, compatible with gym, that can embed some heuristics and other \"quality of life\" features. Feel free to use the l2rpn baselines package for more information.\n", |
211 | | - "</font>" |
| 229 | + "print(f\"The \\\"env_gym\\\" is a gym environment: {isinstance(env_gym, gymnasium.Env)}\")\n", |
| 230 | + "obs_gym, info = env_gym.reset()" |
212 | 231 | ] |
213 | 232 | }, |
214 | 233 | { |
|
388 | 407 | "outputs": [], |
389 | 408 | "source": [ |
390 | 409 | "from grid2op.gym_compat import ScalerAttrConverter\n", |
391 | | - "from gym.spaces import Box\n", |
| 410 | + "from gymnasium.spaces import Box\n", |
392 | 411 | "ob_space = env_gym.observation_space\n", |
393 | 412 | "ob_space = ob_space.reencode_space(\"actual_dispatch\",\n", |
394 | 413 | " ScalerAttrConverter(substract=0.,\n", |
|
518 | 537 | "source": [ |
519 | 538 | "# gym specific, we simply do a copy paste of what we did in the previous cells, wrapping it in the\n", |
520 | 539 | "# MyEnv class, and train a Proximal Policy Optimisation based agent\n", |
521 | | - "import gym\n", |
| 540 | + "import gymnasium\n", |
522 | 541 | "import ray\n", |
523 | 542 | "import numpy as np\n", |
524 | 543 | " \n", |
525 | | - "class MyEnv(gym.Env):\n", |
| 544 | + "class MyEnv(gymnasium.Env):\n", |
526 | 545 | " def __init__(self, env_config):\n", |
527 | 546 | " import grid2op\n", |
528 | 547 | " from grid2op.gym_compat import GymEnv\n", |
|
537 | 556 | "\n", |
538 | 557 | " # 2. create the gym environment\n", |
539 | 558 | " self.env_gym = GymEnv(self.env_glop)\n", |
540 | | - " obs_gym = self.env_gym.reset()\n", |
| 559 | + " obs_gym, info = self.env_gym.reset()\n", |
541 | 560 | "\n", |
542 | 561 | " # 3. (optional) customize it (see section above for more information)\n", |
543 | 562 | " ## customize action space\n", |
|
576 | 595 | " # 4. bis: to avoid other type of issues, we recommend to build the action space and observation\n", |
577 | 596 | " # space directly from the spaces class.\n", |
578 | 597 | " d = {k: v for k, v in self.env_gym.observation_space.spaces.items()}\n", |
579 | | - " self.observation_space = gym.spaces.Dict(d)\n", |
| 598 | + " self.observation_space = gymnasium.spaces.Dict(d)\n", |
580 | 599 | " a = {k: v for k, v in self.env_gym.action_space.items()}\n", |
581 | | - " self.action_space = gym.spaces.Dict(a)\n", |
| 600 | + " self.action_space = gymnasium.spaces.Dict(a)\n", |
582 | 601 | "\n", |
583 | 602 | " def reset(self):\n", |
584 | 603 | " obs = self.env_gym.reset()\n", |
|
791 | 810 | " )\n", |
792 | 811 | " }\n", |
793 | 812 | " )\n", |
794 | | - "obs_gym = env_sb.reset()" |
| 813 | + "obs_gym, info = env_sb.reset()" |
795 | 814 | ] |
796 | 815 | }, |
797 | 816 | { |
|
877 | 896 | "outputs": [], |
878 | 897 | "source": [ |
879 | 898 | "from grid2op.gym_compat import BoxGymActSpace\n", |
880 | | - "scale_gen = env_sb.init_env.gen_max_ramp_up + env_sb.init_env.gen_max_ramp_down\n", |
881 | | - "scale_gen[~env_sb.init_env.gen_redispatchable] = 1.0\n", |
| 899 | + "scaler_gen = env_sb.init_env.gen_max_ramp_up + env_sb.init_env.gen_max_ramp_down\n", |
| 900 | + "scaler_gen = scaler_gen[env_sb.init_env.gen_redispatchable]\n", |
882 | 901 | "env_sb.action_space = BoxGymActSpace(env_sb.init_env.action_space,\n", |
883 | 902 | " attr_to_keep=[\"redispatch\"],\n", |
884 | | - " multiply={\"redispatch\": scale_gen},\n", |
| 903 | + " multiply={\"redispatch\": scaler_gen},\n", |
885 | 904 | " )\n", |
886 | | - "obs_gym = env_sb.reset()" |
| 905 | + "obs_gym, info = env_sb.reset()" |
887 | 906 | ] |
888 | 907 | }, |
889 | 908 | { |
|
937 | 956 | "reencoded_act_space = MultiDiscreteActSpace(env_sb.init_env.action_space,\n", |
938 | 957 | " attr_to_keep=[\"set_line_status\", \"set_bus\", \"redispatch\"])\n", |
939 | 958 | "env_sb.action_space = reencoded_act_space\n", |
940 | | - "obs_gym = env_sb.reset()" |
| 959 | + "obs_gym, info = env_sb.reset()" |
941 | 960 | ] |
942 | 961 | }, |
943 | 962 | { |
|
1041 | 1060 | " )\n", |
1042 | 1061 | " }\n", |
1043 | 1062 | " )\n", |
1044 | | - "obs_gym = env_tfa.reset()" |
| 1063 | + "obs_gym, info = env_tfa.reset()" |
1045 | 1064 | ] |
1046 | 1065 | }, |
1047 | 1066 | { |
|
1297 | 1316 | "name": "python", |
1298 | 1317 | "nbconvert_exporter": "python", |
1299 | 1318 | "pygments_lexer": "ipython3", |
1300 | | - "version": "3.8.10" |
| 1319 | + "version": "3.10.13" |
1301 | 1320 | } |
1302 | 1321 | }, |
1303 | 1322 | "nbformat": 4, |
|
0 commit comments