-
Notifications
You must be signed in to change notification settings - Fork 113
/
test_score_idf_2023_assistant.py
142 lines (117 loc) · 6.65 KB
/
test_score_idf_2023_assistant.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
# Copyright (c) 2023, 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 warnings
import numpy as np
import unittest
import grid2op
from grid2op.utils import ScoreL2RPN2023
from grid2op.Agent.doNothing import DoNothingAgent, BaseAgent
from grid2op.Observation import BaseObservation
from grid2op.Action import BaseAction
from grid2op.Chronics import FromHandlers
from grid2op.Chronics.handlers import CSVHandler, PerfectForecastHandler
from _aux_opponent_for_test_alerts import TestOpponent, _get_blackout
class Alert_Blackout_Agent(BaseAgent):
def __init__(self, action_space,do_Alerts=False,blackout_step=None):
super().__init__(action_space)
self.do_Alerts = do_Alerts
self.blackout_step = blackout_step
def act(self, observation: BaseObservation, reward: float, done: bool = False) -> BaseAction:
act=self.action_space({})
if self.do_Alerts:
act+=self.action_space({"raise_alert": [i for i in range(len(observation.alertable_line_ids))]})#we don't know which line will get attacked, so we raise all alerts to be sure to raise an alert for the line attacked
if((self.blackout_step is not None) and (observation.current_step == self.blackout_step)):
#blackout_action = self.action_space({})
#blackout_action.gen_set_bus = [(0, -1)]
act+=_get_blackout(self.action_space)
return act
class TestScoreL2RPN2023Assist(unittest.TestCase):
"""test the "assistant" part of the l2rpn_idf_2023"""
def setUp(self) -> None:
env_name = "l2rpn_idf_2023"
ATTACKED_LINE = "48_50_136"
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
self.env = grid2op.make(env_name,
test=True)
self.env.set_max_iter(30)
params = self.env.parameters
params.NO_OVERFLOW_DISCONNECTION = True
params.LIMIT_INFEASIBLE_CURTAILMENT_STORAGE_ACTION = True
self.seed = 0
self.scen_id = 0
self.nb_scenario = 2
self.max_iter = 30 #if attacks are at timestep 13, needs at least 26 timsteps to get beyond the alert time window and see some relevant score in case of no blackout
def tearDown(self) -> None:
self.env.close()
return super().tearDown()
def test_score_helper(self):
"""basic tests for ScoreL2RPN2023 class for assistant score with nres score set to 0"
With those seeds, we observe an attack in both episodes at timestep 13"""
self.env.reset()
my_score = ScoreL2RPN2023(
self.env,
nb_scenario=self.nb_scenario,
env_seeds=[0 for _ in range(self.nb_scenario)],
agent_seeds=[0 for _ in range(self.nb_scenario)],
max_step=self.max_iter,
weight_op_score=0.6,
weight_assistant_score=0.4,
weight_nres_score=0.,
scale_nres_score=100,
scale_assistant_score=100,
min_nres_score=-100.,
min_assistant_score=-300)
try:
# test do nothing indeed gets 100.
res_dn = my_score.get(DoNothingAgent(self.env.action_space))
for scen_id, (ep_score, op_score, nres_score, assistant_score) in enumerate(res_dn[0]):
assert assistant_score == 100. #no blackout with no disconnections
assert ep_score == 0.6 * op_score + 0.4 * assistant_score
#raising alerts for attack but it should not as it gets no blackout. With the score L2RPN IDF parametrization, it gives a score of -300
res_agent = my_score.get(Alert_Blackout_Agent(self.env.action_space, do_Alerts=True))#attacks are at timestep 13 for both scenarios with those seeds
for scen_id, (ep_score, op_score, nres_score, assistant_score) in enumerate(res_agent[0]):
assert(assistant_score == -300)
assert ep_score == 0.6 * op_score + 0.4 * assistant_score
#raising no alert for attack and it gets no blackout. With the score L2RPN IDF parametrization, it gives a score of 100
res_agent = my_score.get(Alert_Blackout_Agent(self.env.action_space, do_Alerts=False))#attacks are at timestep 13 for both scenarios with those seeds
for scen_id, (ep_score, op_score, nres_score, assistant_score) in enumerate(res_agent[0]):
assert(assistant_score == 100)
assert ep_score == 0.6 * op_score + 0.4 * assistant_score
#raising alert for attack and it gets a blackout. With the score L2RPN IDF parametrization, it gives a score of 100
res_agent = my_score.get(Alert_Blackout_Agent(self.env.action_space, do_Alerts=True,blackout_step=15))#attacks are at timestep 13 for both scenarios with those seeds
for scen_id, (ep_score, op_score, nres_score, assistant_score) in enumerate(res_agent[0]):
assert(assistant_score == 100)
assert ep_score == 0.6 * op_score + 0.4 * assistant_score
finally:
my_score.clear_all()
def test_min_score(self):
"""test the score does not go bellow the minimum in input
With those seeds, we observe an attack in both episodes at timestep 13"""
try:
self.env.reset()
my_score = ScoreL2RPN2023(
self.env,
nb_scenario=self.nb_scenario,
env_seeds=[0 for _ in range(self.nb_scenario)],
agent_seeds=[0 for _ in range(self.nb_scenario)],
max_step=self.max_iter,
weight_op_score=0.8,
weight_assistant_score=0,
weight_nres_score=0.2,
scale_nres_score=100,
scale_assistant_score=100,
min_nres_score=-100.,
min_assistant_score=-300)
res_agent = my_score.get(Alert_Blackout_Agent(self.env.action_space, blackout_step=14))
for scen_id, (ep_score, op_score, nres_score, assistant_score) in enumerate(res_agent[0]):
assert(assistant_score == -300)#gets minimum score because blackout after attack with no alerts
finally:
my_score.clear_all()
if __name__ == "__main__":
unittest.main()