-
Notifications
You must be signed in to change notification settings - Fork 113
/
test_pickling.py
98 lines (82 loc) · 3.26 KB
/
test_pickling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright (c) 2019-2020, RTE (https://www.rte-france.com)
# See AUTHORS.txt
# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
# you can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
# This file is part of Grid2Op, Grid2Op a testbed platform to model sequential decision making in power systems.
import unittest
import warnings
import copy
import multiprocessing as mp
import grid2op
from grid2op.gym_compat import (
ContinuousToDiscreteConverter,
GymEnv,
MultiToTupleConverter,
ScalerAttrConverter,
)
with warnings.catch_warnings():
# this needs to be imported in the main module for multiprocessing to work "approximately"
warnings.filterwarnings("ignore")
_ = grid2op.make("l2rpn_case14_sandbox", test=True, _add_to_name="for_mp_test")
class TestMultiProc(unittest.TestCase):
@staticmethod
def f(env_gym):
return env_gym.action_space.sample()
@staticmethod
def g(env_gym):
act = env_gym.action_space.sample()
return env_gym.step(act)[0]
def test_basic(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
env = grid2op.make(
"l2rpn_case14_sandbox", test=True, _add_to_name="for_mp_test"
)
env_gym = GymEnv(env)
obs_gym, *_ = env_gym.reset()
# 3. (optional) customize it (see section above for more information)
## customize action space
env_gym.action_space = env_gym.action_space.ignore_attr("set_bus").ignore_attr(
"set_line_status"
)
env_gym.action_space = env_gym.action_space.reencode_space(
"redispatch", ContinuousToDiscreteConverter(nb_bins=11)
)
env_gym.action_space = env_gym.action_space.reencode_space(
"change_bus", MultiToTupleConverter()
)
env_gym.action_space = env_gym.action_space.reencode_space(
"change_line_status", MultiToTupleConverter()
)
env_gym.action_space = env_gym.action_space.reencode_space(
"redispatch", MultiToTupleConverter()
)
## customize observation space
ob_space = env_gym.observation_space
ob_space = ob_space.keep_only_attr(
["rho", "gen_p", "load_p", "topo_vect", "actual_dispatch"]
)
ob_space = ob_space.reencode_space(
"actual_dispatch", ScalerAttrConverter(substract=0.0, divide=env.gen_pmax)
)
ob_space = ob_space.reencode_space(
"gen_p", ScalerAttrConverter(substract=0.0, divide=env.gen_pmax)
)
ob_space = ob_space.reencode_space(
"load_p",
ScalerAttrConverter(
substract=obs_gym["load_p"], divide=0.5 * obs_gym["load_p"]
),
)
env_gym.observation_space = ob_space
ctx = mp.get_context("spawn")
env_gym1 = copy.deepcopy(env_gym)
env_gym2 = copy.deepcopy(env_gym)
with ctx.Pool(2) as p:
p.map(TestMultiProc.f, [env_gym1, env_gym2])
with ctx.Pool(2) as p:
p.map(TestMultiProc.g, [env_gym1, env_gym2])
if __name__ == "__main__":
unittest.main()