Skip to content

Commit

Permalink
Merge pull request #15 from BDonnot/nan_tovect
Browse files Browse the repository at this point in the history
fixing bug for Nan in observation after some powerline change bus
  • Loading branch information
BDonnot committed Dec 17, 2019
2 parents de5a9a6 + 2bdfa7f commit b37d85b
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
=============

[0.4.1] - 2019-12-17
--------------------
- [FIXED] Bug#14 : Nan in the observation space after switching one powerline [PandaPowerBackend]
- [UPDATED] plot now improved for buses in substations

[0.4.0] - 2019-12-04
--------------------
- [ADDED] Basic tools for plotting with the `PlotPlotly` module
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
author = 'Benjamin Donnot'

# The full version, including alpha/beta/rc tags
release = '0.4.0'
release = '0.4.1'
version = '0.4'


Expand Down
7 changes: 4 additions & 3 deletions grid2op/BackendPandaPower.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,13 @@ def apply_action(self, action: Action):
nb_bus_now = len(np.unique(actual_topo))
if nb_bus_before > nb_bus_now:
# i must deactivate the unused bus
self._grid.bus["in_service"].iloc[sub_id + self.n_substations] = False
self._grid.bus["in_service"][sub_id + self.n_substations] = False
# print("I deactivated bus {} [before: {}; after {}]".format(sub_id,nb_bus_before,nb_bus_now))
elif nb_bus_before < nb_bus_now:
# i must activate the new bus
self._grid.bus["in_service"].iloc[sub_id + self.n_substations] = True
# print("I reactivated bus {} [before: {}; after {}]".format(sub_id,nb_bus_before,nb_bus_now))
self._grid.bus["in_service"][sub_id + self.n_substations] = True
# print("I reactivated second bus of substation {} [before: {}; after {}]".format(sub_id,nb_bus_before,nb_bus_now))

# now assign the proper bus to each element
for i, (table, col_name, row_id) in enumerate(self._what_object_where[sub_id]):
self._grid[table][col_name].iloc[row_id] = sub_id if actual_topo[i] == 1 else sub_id + self.n_substations
Expand Down
5 changes: 5 additions & 0 deletions grid2op/PlotPlotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,11 @@ def _draw_topos(self, observation, fig):
else:
buses_z[this_el_bus] += dict_el["z"]
buses_z = [el / nb for el, nb in zip(buses_z, nb_co)]
theta_z = [cmath.phase((el - z_sub)) for el in buses_z]
m_ = np.mean(theta_z) - cmath.pi / 2
theta_z = [el-m_ for el in theta_z]
buses_z = [z_sub + (self.radius_sub-self.bus_radius)*0.75*cmath.exp(1j * theta) for theta in theta_z]

# TODO don't just do the average, but afterwards split it more evenly, and at a fixed distance from the
# center of the substation

Expand Down
2 changes: 1 addition & 1 deletion grid2op/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import pkg_resources

__version__ = '0.4.0'
__version__ = '0.4.1'

__all__ = ['Action', "BackendPandaPower", "Agent", "Backend", "ChronicsHandler", "Environment", "Exceptions",
"Observation", "Parameters", "GameRules", "Reward", "Runner", "main", "Utils", "PlotPlotly"]
Expand Down
3 changes: 3 additions & 0 deletions grid2op/tests/test_Action.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
from Action import HelperAction, Action
from GameRules import GameRules

from grid2op.MakeEnv import make

# TODO test that "twice change" is reset to normal. when i update an action twice, nothing is done.
# TODO test for all class of Action

# TODO clean the test to have it for all class of actions without recoding everything each time


class TestLoadingBackendFunc(unittest.TestCase):
def setUp(self):
"""
Expand Down
10 changes: 5 additions & 5 deletions grid2op/tests/test_Agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def setUp(self):
names_chronics_to_backend=self.names_chronics_to_backend,
rewardClass=self.rewardClass)

def _aux_test_agent(self, agent):
def _aux_test_agent(self, agent, i_max=30):
done = False
i = 0
beg_ = time.time()
Expand All @@ -105,7 +105,7 @@ def _aux_test_agent(self, agent):
time_act += end__ - beg__
cum_reward += reward
i += 1
if i > 30:
if i > i_max:
break

end_ = time.time()
Expand Down Expand Up @@ -134,9 +134,9 @@ def test_1_powerlineswitch(self):

def test_2_busswitch(self):
agent = TopologyGreedy(self.env.helper_action_player)
i, cum_reward = self._aux_test_agent(agent)
assert i == 31, "The powerflow diverged before step 30 for greedy agent"
assert np.abs(cum_reward - 619.999011) <= self.tol_one, "The reward has not been properly computed"
i, cum_reward = self._aux_test_agent(agent, i_max=10)
assert i == 11, "The powerflow diverged before step 30 for greedy agent"
assert np.abs(cum_reward - 219.99795) <= self.tol_one, "The reward has not been properly computed"


if __name__ == "__main__":
Expand Down
41 changes: 36 additions & 5 deletions grid2op/tests/test_PandaPowerBackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
from Environment import Environment
from Exceptions import *
from GameRules import GameRules
from MakeEnv import make

PATH_DATA_TEST = PATH_DATA_TEST_PP
import pandapower as pppp


class TestLoadingADN(unittest.TestCase):
def setUp(self):
self.tolvect = 1e-2
Expand Down Expand Up @@ -635,11 +637,11 @@ def test_topo_change_2sub(self):
assert np.all(topo_vect[self.backend.gen_pos_topo_vect[gen_ids]] == arr2[self.backend.gen_to_sub_pos[gen_ids]])

after_amps_flow = self.backend.get_line_flow()
after_amps_flow_th = np.array([ 564.86831329, 313.93253616, 14222.67349148, 29227.00333159,
18393.56319792, 13649.04803367, 13649.04803367, 311.98084347,
252.73768739, 80.124112 , 90.55351819, 220.8797656 ,
28364.56104918, 30242.87395452, 0. , 118.014306 ,
63.71939605, 147.6468084 , 702.15602054, 1067.37181783])
after_amps_flow_th = np.array([ 596.97014348, 342.10559579, 16615.11815357, 31328.50690716,
11832.77202397, 11043.10650167, 11043.10650167, 322.79533908,
273.86501458, 82.34066647, 80.89289074, 208.42396413,
22178.16766548, 27690.51322075, 38684.31540646, 129.44842477,
70.02629553, 185.67687123, 706.77680037, 1155.45754617])
assert self.compare_vect(after_amps_flow, after_amps_flow_th)

try:
Expand Down Expand Up @@ -865,6 +867,35 @@ def test_nb_timestep_overflow_disc2(self):
assert (not grid_tmp.get_line_status()[self.id_2nd_line_disco])


class TestChangeBusAffectRightBus(unittest.TestCase):
def test_set_bus(self):
env = make()
env.reset()
# action = env.helper_action_player({"change_bus": {"lines_or_id": [17]}})
action = env.helper_action_player({"set_bus": {"lines_or_id": [(17, 2)]}})
obs, reward, done, info = env.step(action)

assert np.all(np.isfinite(obs.v_or))
assert np.sum(env.backend._grid["bus"]["in_service"]) == 15

def test_change_bus(self):
env = make()
env.reset()
action = env.helper_action_player({"change_bus": {"lines_or_id": [17]}})
obs, reward, done, info = env.step(action)
assert np.all(np.isfinite(obs.v_or))
assert np.sum(env.backend._grid["bus"]["in_service"]) == 15

def test_change_bustwice(self):
env = make()
env.reset()
action = env.helper_action_player({"change_bus": {"lines_or_id": [17]}})
obs, reward, done, info = env.step(action)
obs, reward, done, info = env.step(action)
assert np.all(np.isfinite(obs.v_or))
assert np.sum(env.backend._grid["bus"]["in_service"]) == 14


# TODO test also the methods added for observation:
"""
self._line_status = copy.copy(backend.get_line_status())
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}

setup(name='Grid2Op',
version='0.4.0',
version='0.4.1',
description='An environment that allows to perform powergrid optimization.',
long_description='Built with modularity in mind, this package allows to perform the same operations independantly of the software used to compute powerflow or method to generate grid states or forecasts.',
classifiers=[
Expand Down

0 comments on commit b37d85b

Please sign in to comment.