1+ # Copyright (c) 2025, RTE (https://www.rte-france.com)
2+ # See AUTHORS.txt
3+ # This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
4+ # If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
5+ # you can obtain one at http://mozilla.org/MPL/2.0/.
6+ # SPDX-License-Identifier: MPL-2.0
7+ # This file is part of Grid2Op, Grid2Op a testbed platform to model sequential decision making in power systems.
8+
9+ import unittest
10+ import warnings
11+
12+ import numpy as np
13+ import grid2op
14+
15+
16+ class TestSoftOverflow (unittest .TestCase ):
17+ def setUp (self ):
18+ with warnings .catch_warnings ():
19+ warnings .filterwarnings ("ignore" )
20+ self .env = grid2op .make ("l2rpn_case14_sandbox" , test = True )
21+ self .init_obs = self .env .reset (seed = 0 , options = {"time serie id" : 0 })
22+ obs , * _ = self .env .step (self .env .action_space ())
23+ self .th_lim = 2. * obs .a_or
24+ mask_ = self .init_obs .a_or > obs .a_or
25+ self .th_lim [mask_ ] = 2. * self .init_obs .a_or [mask_ ]
26+ self .line_id = 1
27+ assert not mask_ [self .line_id ] # flow on line self.line_id should increase
28+ self .th_lim [self .line_id ] = self .init_obs .a_or [self .line_id ] + 1e-5
29+ # flow on line self.line_id should be overflown when t >= 1
30+ return super ().setUp ()
31+
32+ def tearDown (self ):
33+ self .env .close ()
34+ return super ().tearDown ()
35+
36+ def test_whenO (self ):
37+ params = self .env .parameters
38+ params .NB_TIMESTEP_OVERFLOW_ALLOWED = 0
39+ self .env .change_parameters (params )
40+ obs0 = self .env .reset (seed = 0 , options = {"time serie id" : 0 , "thermal limit" : self .th_lim })
41+ assert obs0 .a_or [self .line_id ] > 1e-5
42+ assert obs0 .line_status [self .line_id ]
43+ assert obs0 .timestep_overflow [self .line_id ] == 0
44+
45+ obs , * _ = self .env .step (self .env .action_space ())
46+ assert np .abs (obs .a_or [self .line_id ]) <= 1e-8
47+ assert not obs .line_status [self .line_id ]
48+ assert obs .timestep_overflow [self .line_id ] == 0
49+
50+ def test_when1 (self ):
51+ params = self .env .parameters
52+ params .NB_TIMESTEP_OVERFLOW_ALLOWED = 1
53+ self .env .change_parameters (params )
54+ obs0 = self .env .reset (seed = 0 , options = {"time serie id" : 0 , "thermal limit" : self .th_lim })
55+ assert obs0 .a_or [self .line_id ] > 1e-5
56+ assert obs0 .line_status [self .line_id ]
57+ assert obs0 .timestep_overflow [self .line_id ] == 0
58+
59+ obs1 , * _ = self .env .step (self .env .action_space ())
60+ assert np .abs (obs1 .a_or [self .line_id ]) > 1e-8
61+ assert obs1 .line_status [self .line_id ]
62+ assert obs1 .timestep_overflow [self .line_id ] == 1
63+
64+ obs2 , * _ = self .env .step (self .env .action_space ())
65+ assert np .abs (obs2 .a_or [self .line_id ]) <= 1e-8
66+ assert not obs2 .line_status [self .line_id ]
67+ assert obs2 .timestep_overflow [self .line_id ] == 0
68+
69+ def test_when2 (self ):
70+ params = self .env .parameters
71+ params .NB_TIMESTEP_OVERFLOW_ALLOWED = 2
72+ self .env .change_parameters (params )
73+ obs0 = self .env .reset (seed = 0 , options = {"time serie id" : 0 , "thermal limit" : self .th_lim })
74+ assert obs0 .a_or [self .line_id ] > 1e-5
75+ assert obs0 .line_status [self .line_id ]
76+ assert obs0 .timestep_overflow [self .line_id ] == 0
77+
78+ obs1 , * _ = self .env .step (self .env .action_space ())
79+ assert np .abs (obs1 .a_or [self .line_id ]) > 1e-8
80+ assert obs1 .line_status [self .line_id ]
81+ assert obs1 .timestep_overflow [self .line_id ] == 1
82+
83+ obs2 , * _ = self .env .step (self .env .action_space ())
84+ assert np .abs (obs2 .a_or [self .line_id ]) > 1e-8
85+ assert obs2 .line_status [self .line_id ]
86+ assert obs2 .timestep_overflow [self .line_id ] == 2
87+
88+ obs3 , * _ = self .env .step (self .env .action_space ())
89+ assert np .abs (obs3 .a_or [self .line_id ]) <= 1e-8
90+ assert not obs3 .line_status [self .line_id ]
91+ assert obs3 .timestep_overflow [self .line_id ] == 0
92+
93+ def test_when3 (self ):
94+ params = self .env .parameters
95+ params .NB_TIMESTEP_OVERFLOW_ALLOWED = 3
96+ self .env .change_parameters (params )
97+ obs0 = self .env .reset (seed = 0 , options = {"time serie id" : 0 , "thermal limit" : self .th_lim })
98+ assert obs0 .a_or [self .line_id ] > 1e-5
99+ assert obs0 .line_status [self .line_id ]
100+ assert obs0 .timestep_overflow [self .line_id ] == 0
101+
102+ obs1 , * _ = self .env .step (self .env .action_space ())
103+ assert np .abs (obs1 .a_or [self .line_id ]) > 1e-8
104+ assert obs1 .line_status [self .line_id ]
105+ assert obs1 .timestep_overflow [self .line_id ] == 1
106+
107+ obs2 , * _ = self .env .step (self .env .action_space ())
108+ assert np .abs (obs2 .a_or [self .line_id ]) > 1e-8
109+ assert obs2 .line_status [self .line_id ]
110+ assert obs2 .timestep_overflow [self .line_id ] == 2
111+
112+ obs3 , * _ = self .env .step (self .env .action_space ())
113+ assert np .abs (obs3 .a_or [self .line_id ]) > 1e-8
114+ assert obs3 .line_status [self .line_id ]
115+ assert obs3 .timestep_overflow [self .line_id ] == 3
116+
117+ obs4 , * _ = self .env .step (self .env .action_space ())
118+ assert np .abs (obs4 .a_or [self .line_id ]) <= 1e-8
119+ assert not obs4 .line_status [self .line_id ]
120+ assert obs4 .timestep_overflow [self .line_id ] == 0
121+
122+ def test_when4 (self ):
123+ params = self .env .parameters
124+ params .NB_TIMESTEP_OVERFLOW_ALLOWED = 4
125+ self .env .change_parameters (params )
126+ obs0 = self .env .reset (seed = 0 , options = {"time serie id" : 0 , "thermal limit" : self .th_lim })
127+ assert obs0 .a_or [self .line_id ] > 1e-5
128+ assert obs0 .line_status [self .line_id ]
129+ assert obs0 .timestep_overflow [self .line_id ] == 0
130+
131+ obs1 , * _ = self .env .step (self .env .action_space ())
132+ assert np .abs (obs1 .a_or [self .line_id ]) > 1e-8
133+ assert obs1 .line_status [self .line_id ]
134+ assert obs1 .timestep_overflow [self .line_id ] == 1
135+
136+ obs2 , * _ = self .env .step (self .env .action_space ())
137+ assert np .abs (obs2 .a_or [self .line_id ]) > 1e-8
138+ assert obs2 .line_status [self .line_id ]
139+ assert obs2 .timestep_overflow [self .line_id ] == 2
140+
141+ obs3 , * _ = self .env .step (self .env .action_space ())
142+ assert np .abs (obs3 .a_or [self .line_id ]) > 1e-8
143+ assert obs3 .line_status [self .line_id ]
144+ assert obs3 .timestep_overflow [self .line_id ] == 3
145+
146+ obs4 , * _ = self .env .step (self .env .action_space ())
147+ assert np .abs (obs4 .a_or [self .line_id ]) > 1e-8
148+ assert obs4 .line_status [self .line_id ]
149+ assert obs4 .timestep_overflow [self .line_id ] == 4
150+
151+ obs5 , * _ = self .env .step (self .env .action_space ())
152+ assert np .abs (obs5 .a_or [self .line_id ]) <= 1e-8
153+ assert not obs5 .line_status [self .line_id ]
154+ assert obs5 .timestep_overflow [self .line_id ] == 0
155+
156+ def test_when5 (self ):
157+ params = self .env .parameters
158+ params .NB_TIMESTEP_OVERFLOW_ALLOWED = 5
159+ self .env .change_parameters (params )
160+ obs0 = self .env .reset (seed = 0 , options = {"time serie id" : 0 , "thermal limit" : self .th_lim })
161+ assert obs0 .a_or [self .line_id ] > 1e-5
162+ assert obs0 .line_status [self .line_id ]
163+ assert obs0 .timestep_overflow [self .line_id ] == 0
164+
165+ obs1 , * _ = self .env .step (self .env .action_space ())
166+ assert np .abs (obs1 .a_or [self .line_id ]) > 1e-8
167+ assert obs1 .line_status [self .line_id ]
168+ assert obs1 .timestep_overflow [self .line_id ] == 1
169+
170+ obs2 , * _ = self .env .step (self .env .action_space ())
171+ assert np .abs (obs2 .a_or [self .line_id ]) > 1e-8
172+ assert obs2 .line_status [self .line_id ]
173+ assert obs2 .timestep_overflow [self .line_id ] == 2
174+
175+ obs3 , * _ = self .env .step (self .env .action_space ())
176+ assert np .abs (obs3 .a_or [self .line_id ]) > 1e-8
177+ assert obs3 .line_status [self .line_id ]
178+ assert obs3 .timestep_overflow [self .line_id ] == 3
179+
180+ obs4 , * _ = self .env .step (self .env .action_space ())
181+ assert np .abs (obs4 .a_or [self .line_id ]) > 1e-8
182+ assert obs4 .line_status [self .line_id ]
183+ assert obs4 .timestep_overflow [self .line_id ] == 4
184+
185+ obs5 , * _ = self .env .step (self .env .action_space ())
186+ assert np .abs (obs5 .a_or [self .line_id ]) > 1e-8
187+ assert obs5 .line_status [self .line_id ]
188+ assert obs5 .timestep_overflow [self .line_id ] == 5
189+
190+ obs6 , * _ = self .env .step (self .env .action_space ())
191+ assert np .abs (obs6 .a_or [self .line_id ]) <= 1e-8
192+ assert not obs6 .line_status [self .line_id ]
193+ assert obs6 .timestep_overflow [self .line_id ] == 0
194+
0 commit comments