Skip to content

Commit

Permalink
i'll work on grid2op / lightsim2grid weird storage issue, see #85
Browse files Browse the repository at this point in the history
  • Loading branch information
BDonnot committed Jul 24, 2024
1 parent c412bf2 commit 3bc118b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 55 deletions.
File renamed without changes.
50 changes: 25 additions & 25 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,26 +160,26 @@ jobs:
strategy:
matrix:
python:
# - {
# name: cp37,
# version: '3.7',
# }
# - {
# name: cp38,
# version: '3.8',
# }
# - {
# name: cp39,
# version: '3.9',
# }
# - {
# name: cp310,
# version: '3.10',
# }
# - {
# name: cp311,
# version: '3.11',
# }
- {
name: cp37,
version: '3.7',
}
- {
name: cp38,
version: '3.8',
}
- {
name: cp39,
version: '3.9',
}
- {
name: cp310,
version: '3.10',
}
- {
name: cp311,
version: '3.11',
}
- {
name: cp312,
version: '3.12',
Expand Down Expand Up @@ -258,9 +258,9 @@ jobs:
python -c "from lightsim2grid import LightSimBackend"
python -c "from lightsim2grid import LightSimBackend; import grid2op; env = grid2op.make('l2rpn_case14_sandbox', test=True, backend=LightSimBackend())"
- name: Debug windows CI
run: |
python -m unittest lightsim2grid.tests.test_n1contingencyrewards.TestN1ContingencyReward_Base.test_do_nothing
# - name: Debug windows CI
# run: |
# python -m unittest lightsim2grid.tests.test_n1contingencyrewards.TestN1ContingencyReward_Base.test_do_nothing

- name: Upload wheel
uses: actions/upload-artifact@v3
Expand Down Expand Up @@ -524,8 +524,8 @@ jobs:
name: Package wheels
runs-on: ubuntu-latest
# needs: [manylinux_build, windows_build, macos_build_37_]
# needs: [exotic_build, macos_build_37_, windows_build, manylinux_build]
needs: [windows_build]
needs: [exotic_build, macos_build_37_, windows_build, manylinux_build]
# needs: [windows_build]

steps:
- name: Download wheels
Expand Down
2 changes: 1 addition & 1 deletion lightsim2grid/lightSimBackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ def apply_action(self, backendAction: Union["grid2op.Action._backendAction._Back
raise BackendError(f"{exc_}") from exc_

if self.__has_storage:
print(f"\t (in backend) storage_power {backendAction.storage_power.values[backendAction.storage_power.changed]}") # TODO DEBUG WINDOWS
# print(f"\t (in backend) storage_power {backendAction.storage_power.values[backendAction.storage_power.changed]}") # TODO DEBUG WINDOWS
try:
self._grid.update_storages_p(backendAction.storage_power.changed,
backendAction.storage_power.values)
Expand Down
56 changes: 30 additions & 26 deletions lightsim2grid/tests/test_n1contingencyrewards.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def l_ids(self):
return None

def setUp(self) -> None:
import sys
if sys.platform == 'win32':
self.skipTest("Not working, see issue https://github.com/BDonnot/lightsim2grid/issues/85")
reward = N1ContingencyReward(dc=self.is_dc(),
threshold_margin=self.threshold_margin(),
l_ids=self.l_ids())
Expand All @@ -73,18 +76,18 @@ def setUp(self) -> None:
reward_class=reward,
action_class=CompleteAction,
_add_to_name=type(self).__name__)
self.params = self.env.parameters
self.params.MAX_LINE_STATUS_CHANGED = 999999
self.params.MAX_SUB_CHANGED = 999999
self.params.NB_TIMESTEP_COOLDOWN_LINE = 0
self.params.NB_TIMESTEP_COOLDOWN_SUB = 0
self.params.ACTIVATE_STORAGE_LOSS = False
self.env.change_parameters(self.params)
self.env_params = self.env.parameters
self.env_params.MAX_LINE_STATUS_CHANGED = 999999
self.env_params.MAX_SUB_CHANGED = 999999
self.env_params.NB_TIMESTEP_COOLDOWN_LINE = 0
self.env_params.NB_TIMESTEP_COOLDOWN_SUB = 0
self.env_params.ACTIVATE_STORAGE_LOSS = False
self.env.change_parameters(self.env_params)
self.forecast_params = copy.deepcopy(self.env_params)
if self.is_dc():
self.params = copy.deepcopy(self.params)
self.params.ENV_DC = True
self.params.FORECAST_DC = True
self.env.change_forecast_parameters(self.params)
self.forecast_params.ENV_DC = True
self.forecast_params.FORECAST_DC = True
self.env.change_forecast_parameters(self.forecast_params)
assert (self.env.get_thermal_limit() == TH_LIM_A_REF).all()
self.my_ids = self.l_ids()
if self.my_ids is None:
Expand All @@ -94,8 +97,8 @@ def setUp(self) -> None:
return super().setUp()

def aux_reset_correctly(self):
self.env.change_parameters(self.params)
self.env.change_forecast_parameters(self.params)
self.env.change_parameters(self.env_params)
self.env.change_forecast_parameters(self.forecast_params)
obs = self.env.reset(seed=0, options={"time serie id": 0})
return obs

Expand Down Expand Up @@ -128,20 +131,21 @@ def _aux_test_reward(self, obs, reward):
# print(f"{V.shape[0] = }, {V[0]:.3f}, {V[1]:.3f}") # TODO DEBUG WINDOWS
# print(f"Obs env {obs._obs_env.backend._grid.get_solver_type()}") # TODO DEBUG WINDOWS
# print(f"Obs env {obs._obs_env.backend._grid.get_dc_solver_type()}") # TODO DEBUG WINDOWS
print("HERE HERE HERE") # TODO DEBUG WINDOWS
print(f"env.storage_loss = {self.env.storage_loss}")
print(f"_obs_env.storage_loss = {obs._obs_env.storage_loss}")
print(f"_amount_storage = {obs._obs_env._amount_storage}")
print(f"_amount_storage_prev = {obs._obs_env._amount_storage_prev}")
print(f"storage p (from obs): {obs.storage_power}") # TODO DEBUG WINDOWS
print(f"storage_power_target p (from obs): {obs.storage_power_target}") # TODO DEBUG WINDOWS
print(f"_obs_env._storage_power : {obs._obs_env._storage_power}") # TODO DEBUG WINDOWS
print(f"_obs_env.delta_time_seconds : {obs._obs_env.delta_time_seconds}") # TODO DEBUG WINDOWS
# print("HERE HERE HERE") # TODO DEBUG WINDOWS
# print(f"env.storage_loss = {self.env.storage_loss}")
# print(f"_obs_env.storage_loss = {obs._obs_env.storage_loss}")
# print(f"_amount_storage = {obs._obs_env._amount_storage}")
# print(f"_amount_storage_prev = {obs._obs_env._amount_storage_prev}")
# print(f"storage p (from obs): {obs.storage_power}") # TODO DEBUG WINDOWS
# print(f"storage_power_target p (from obs): {obs.storage_power_target}") # TODO DEBUG WINDOWS
# print(f"_obs_env._storage_power : {obs._obs_env._storage_power}") # TODO DEBUG WINDOWS
# print(f"_obs_env.delta_time_seconds : {obs._obs_env.delta_time_seconds}") # TODO DEBUG WINDOWS
# print(f"storage p seen from backend of obs_env {obs._obs_env.backend._storage_res[0]}") # TODO DEBUG WINDOWS
sim_obs, sim_r, sim_d, sim_i = obs.simulate(self.env.action_space(), time_step=0)
# print(f"without contingency: {sim_d = }, {sim_i['exception']}") # TODO DEBUG WINDOWS
print(f"without contingency: {sim_d = }, {sim_i}")
assert not sim_d # TODO DEBUG WINDOWS
return # TODO DEBUG WINDOWS
# print(f"without contingency: {sim_d = }, {sim_i}")
assert not sim_d, "base powerflow should not diverge"
# return # TODO DEBUG WINDOWS
# print("test:")
for l_id in self.my_ids:
sim_obs, sim_r, sim_d, sim_i = obs.simulate(self.env.action_space({"set_line_status": [(l_id, -1)]}),
Expand All @@ -153,7 +157,7 @@ def _aux_test_reward(self, obs, reward):
else:
if np.any(np.abs(sim_obs.p_or) > th_lim_p) or sim_d:
unsafe_cont += 1
print(f"{unsafe_cont = }")
# print(f"{unsafe_cont = }") # TODO DEBUG WINDOWS
assert reward == (len(self.my_ids) - unsafe_cont), f"{reward} vs {(len(self.my_ids) - unsafe_cont)}"

def test_do_nothing(self):
Expand Down
6 changes: 3 additions & 3 deletions src/powerflow_algorithm/BaseDCAlgo.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ bool BaseDCAlgo<LinearSolver>::compute_pf(const Eigen::SparseMatrix<cplx_type> &
// std::cout << "\t\tBaseDCAlgo.tpp: dcYbus_noslack_ (max): " << dcYbus_noslack_.coeffs().maxCoeff() << std::endl; // TODO DEBUG WINDOWS
// std::cout << "\t\tBaseDCAlgo.tpp: dcYbus_noslack_ (sum): " << dcYbus_noslack_.coeffs().abs().sum() << std::endl; // TODO DEBUG WINDOWS
// std::cout << "\t\tBaseDCAlgo.tpp: Va_dc_without_slack (inf norm): " << Va_dc_without_slack.lpNorm<Eigen::Infinity>() << std::endl; // TODO DEBUG WINDOWS
std::cout << "\t\tBaseDCAlgo.tpp: Va_dc_without_slack (l1 norm): " << Va_dc_without_slack.lpNorm<1>() << std::endl; // TODO DEBUG WINDOWS
// std::cout << "\t\tBaseDCAlgo.tpp: Va_dc_without_slack (l1 norm): " << Va_dc_without_slack.lpNorm<1>() << std::endl; // TODO DEBUG WINDOWS
// std::cout << "\t\tBaseDCAlgo.tpp: V (l1 norm): " << V.lpNorm<1>() << std::endl; // TODO DEBUG WINDOWS
std::cout << "\t\tBaseDCAlgo.tpp: Sbus (l1 norm): " << Sbus.lpNorm<1>() << std::endl; // TODO DEBUG WINDOWS
// std::cout << "\t\tBaseDCAlgo.tpp: Sbus (l1 norm): " << Sbus.lpNorm<1>() << std::endl; // TODO DEBUG WINDOWS
ErrorType error = _linear_solver.solve(dcYbus_noslack_, Va_dc_without_slack, has_just_been_factorized);
if(error != ErrorType::NoError){
err_ = error;
Expand All @@ -131,7 +131,7 @@ bool BaseDCAlgo<LinearSolver>::compute_pf(const Eigen::SparseMatrix<cplx_type> &
if(!Va_dc_without_slack.array().allFinite() || (Va_dc_without_slack.lpNorm<Eigen::Infinity>() >= 1e6)){
// for convergence, all values should be finite
// and it's not realistic if some Va are too high
std::cout << "\t\tBaseDCAlgo.tpp: Va_dc_without_slack: " << Va_dc_without_slack.lpNorm<Eigen::Infinity>() << std::endl; // TODO DEBUG WINDOWS
// std::cout << "\t\tBaseDCAlgo.tpp: Va_dc_without_slack: " << Va_dc_without_slack.lpNorm<Eigen::Infinity>() << std::endl; // TODO DEBUG WINDOWS
err_ = ErrorType::SolverSolve;
V = CplxVect();
V_ = CplxVect();
Expand Down

0 comments on commit 3bc118b

Please sign in to comment.