-
Notifications
You must be signed in to change notification settings - Fork 111
/
helper_gen_iadd.py
executable file
·219 lines (193 loc) · 6.09 KB
/
helper_gen_iadd.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
import itertools
header_content = """
import unittest
import warnings
from abc import ABC, abstractmethod
import numpy as np
import grid2op
from grid2op.Action import *
from grid2op.dtypes import dt_float
"""
print(header_content)
base_content = """
class Test_iadd_Base(ABC):
@abstractmethod
def _action_setup(self):
pass
def _skipMissingKey(self, key):
if key not in self.action_t.authorized_keys:
skip_msg = "Skipped: Missing authorized_key {key}"
unittest.TestCase.skipTest(self, skip_msg)
@classmethod
def setUpClass(cls):
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
cls.action_t = cls._action_setup()
cls.env = grid2op.make("rte_case14_realistic",
test=True,
action_class=cls.action_t,
_add_to_name=type(self).__name__)
@classmethod
def tearDownClass(cls):
cls.env.close()
"""
print(base_content)
actions_names = ["dn", "set_line", "change_line", "set_bus", "change_bus", "redisp"]
actions_skip = [
""" # No skip for do nothing""",
""" self._skipMissingKey("set_line_status")""",
""" self._skipMissingKey("change_line_status")""",
""" self._skipMissingKey("set_bus")""",
""" self._skipMissingKey("change_bus")""",
""" self._skipMissingKey("redispatch")""",
]
actions = [
"""self.env.action_space({})""",
"""self.env.action_space({
"set_line_status": [(0, -1)]
})""",
"""self.env.action_space({
"change_line_status": [0]
})""",
"""self.env.action_space({
"set_bus": {
"substations_id": [
(0, [2] + [0] * (self.env.sub_info[0] - 1))
]
}
})""",
"""self.env.action_space({
"change_bus": {
"substations_id": [
(0, [True] + [False] * (self.env.sub_info[0] - 1))
]
}
})""",
"""self.env.action_space({
"redispatch": {
2: 1.42
}
})""",
]
actions_test = [
"""
assert np.all({1}._set_line_status == 0)
assert np.all({1}._switch_line_status == False)
assert np.all({1}._set_topo_vect == 0)
assert np.all({1}._change_bus_vect == 0)
assert np.all({1}._redispatch == 0.0)
""",
"""
assert {1}._set_line_status[0] == -1
assert np.all({1}._set_line_status[1:] == 0)
assert np.all({1}._switch_line_status == False)
assert np.all({1}._set_topo_vect == 0)
assert np.all({1}._change_bus_vect == 0)
assert np.all({1}._redispatch == 0.0)
""",
"""
assert np.all({1}._set_line_status == 0)
assert {1}._switch_line_status[0] == True
assert np.all({1}._switch_line_status[1:] == False)
assert np.all({1}._set_topo_vect == 0)
assert np.all({1}._change_bus_vect == 0)
assert np.all({1}._redispatch == 0.0)
""",
"""
assert np.all({1}._set_line_status == 0)
assert np.all({1}._switch_line_status == False)
assert {1}._set_topo_vect[0] == 2
assert np.all({1}._set_topo_vect[1:] == 0)
assert np.all({1}._change_bus_vect == 0)
assert np.all({1}._redispatch == 0.0)
""",
"""
assert np.all({1}._set_line_status == 0)
assert np.all({1}._switch_line_status == False)
assert np.all({1}._set_topo_vect == 0)
assert {1}._change_bus_vect[0] == True
assert np.all({1}._change_bus_vect[1:] == False)
assert np.all({1}._redispatch == 0.0)
""",
"""
assert np.all({1}._set_line_status == 0)
assert np.all({1}._switch_line_status == False)
assert np.all({1}._set_topo_vect == 0)
assert np.all({1}._change_bus_vect == 0)
assert {1}._redispatch[2] == dt_float(1.42)
assert np.all({1}._redispatch[:2] == 0.0)
assert np.all({1}._redispatch[3:] == 0.0)
""",
]
fn_name = " def test_{0}_iadd_{1}(self):"
a_create = """
# Create action me [{0}]
act_me = {1}
"""
a_test = " # Test action me [{0}]"
b_create = """ # Create action oth [{0}]
act_oth = {1}
"""
b_test = " # Test action oth [{0}]"
iadd_content = """ # Iadd actions
act_me += act_oth
"""
test_content = """ # Test combination:
assert False, "TODO {} += {} test dumdumb"
"""
import sys
sys.exit(0) # because of the above it do not WORK !!!!!!!!!!!!!!!!!!
for c in itertools.product(range(len(actions)), repeat=2):
a_idx = c[0]
b_idx = c[1]
a_skip = actions_skip[a_idx]
b_skip = actions_skip[b_idx]
a_name = actions_names[a_idx]
b_name = actions_names[b_idx]
a_act = actions[a_idx]
b_act = actions[b_idx]
a_t = actions_test[a_idx]
b_t = actions_test[b_idx]
print(fn_name.format(a_name, b_name))
if len(a_skip) > 0:
print(a_skip)
if len(b_skip) > 0:
print(b_skip)
print(a_create.format(a_name, a_act))
print((a_test + a_t).format(a_name, "act_me"))
print(b_create.format(b_name, b_act))
print((b_test + b_t).format(b_name, "act_oth"))
print(iadd_content)
print(test_content.format(a_name, b_name))
classes_names = [
"CompleteAction",
"DispatchAction",
"DontAct",
"PlayableAction",
"PowerlineChangeAction",
"PowerlineChangeAndDispatchAction",
"PowerlineSetAction",
"PowerlineSetAndDispatchAction",
"TopologyAction",
"TopologyAndDispatchAction",
"TopologyChangeAction",
"TopologyChangeAndDispatchAction",
"TopologySetAction",
"TopologySetAndDispatchAction",
]
class_suite = """
class Test_iadd_{0}(Test_iadd_Base, unittest.TestCase):
\"""
Action iadd test suite for subclass: {0}
\"""
@classmethod
def _action_setup(self):
return {0}
"""
for c_name in classes_names:
print(class_suite.format(c_name))
main_content = """
if __name__ == "__main__":
unittest.main()"""
print(main_content)