From da1dbfa7c9a8b6c4553967cc20cfd04637ac87d7 Mon Sep 17 00:00:00 2001 From: Tomasini Luca Date: Wed, 11 Dec 2024 14:06:39 +0100 Subject: [PATCH] Start subset modification --- .../baseline/second_stage/variables_docs.csv | 12 +- scripts/baseline_optimizazion.py | 2 +- .../second_stage/second_stage_pipeline.py | 52 +- .../baseline/second_stage/sets.py | 9 +- tests/base_line.ipynb | 729 +++++++++++++----- tests/pyomo_test.py | 44 +- 6 files changed, 624 insertions(+), 224 deletions(-) diff --git a/docs/tables/baseline/second_stage/variables_docs.csv b/docs/tables/baseline/second_stage/variables_docs.csv index 044a7d2..49fddd3 100644 --- a/docs/tables/baseline/second_stage/variables_docs.csv +++ b/docs/tables/baseline/second_stage/variables_docs.csv @@ -4,11 +4,11 @@ ":math:`V_\text{BAS, END}^{b}`",,:math:`\mathbb{R}^{+}`,:math:`\mathrm{m}^{3}` ":math:`dV_\text{+}^{t,~b}`",,:math:`\mathbb{R}^{+}`,:math:`\mathrm{m}^{3}` ":math:`dV_\text{-}^{t,~b}`",,:math:`\mathbb{R}^{+}`,:math:`\mathrm{m}^{3}` -":math:`P^{t,~h}`",Electrical power produced by a hydropower plant,:math:`\mathbb{R}^{+}`,:math:`\mathrm{MW}` +":math:`P^{t,~h}`",Electrical power produced (or consumed) by a hydropower plant,:math:`\mathbb{R}^{+}`,:math:`\mathrm{MW}` ":math:`Q^{t,~h}`",Amount of water that flows through a hydropower plant,:math:`\mathbb{R}^{+}`,:math:`\mathrm{m}^{3}/\mathrm{s}` ":math:`S_\text{BAS}^{t,~b,~s_b}`","Binary variable that indicates the basin's volume state derived from its volume: :math:`S_\text{BAS}^{t, b, s_b} = \begin{cases} 1 \text{ if } V_\text{BAS}^{t, b} \in \left[ V_\text{BAS, MIN}^{b,~s_b} ;V_\text{BAS, MAX}^{b,~s_b} \right] \\ 0 \text{ otherwise} \end{cases}`",:math:`\mathbb{B}`, -":math:`F_\text{BAS}^{t,~h,~f}`",Binary variable that indicates the hydo power plant's flow state (for turbine and pump),:math:`\mathbb{B}`, -":math:`P_\text{CAL}^{t,~h}`",Electrical power produced by a pump,:math:`\mathbb{R}^{+}`,:math:`\mathrm{MW}` -":math:`Q_\text{CAL}^{t,~h}`",Amount of water that flows through a pump,:math:`\mathbb{R}^{+}`,:math:`\mathrm{m}^{3}/\mathrm{s}` -":math:`P_\text{S}^{t,~h,~s_h}`",Amount of water that flows through a turbine for each hydro powerplant state,:math:`\mathbb{R}^{+}`,:math:`\mathrm{m}^{3}/\mathrm{s}` -":math:`Q_\text{S}^{t,~h,~s_h}`",Amount of water that flows through a pump for each hydro powerplant state,:math:`\mathbb{R}^{+}`,:math:`\mathrm{m}^{3}/\mathrm{s}` +":math:`S_\text{FLOW}^{t,~h,~s_q}`",Binary variable that indicates the hydo power plant's flow state (for turbine and pump),:math:`\mathbb{B}`, +":math:`P_\text{CAL}^{t,~h,~s_q}`",Calculated electrical power produced (or consumed) by a hydropower plant for each flow state,:math:`\mathbb{R}^{+}`,:math:`\mathrm{MW}` +":math:`Q_\text{CAL}^{t,~h,~s_q}`",Calculated water that flows through a hydropower plant for each flow state,:math:`\mathbb{R}^{+}`,:math:`\mathrm{m}^{3}/\mathrm{s}` +":math:`P_\text{S}^{t,~h,~s_q}`","Real electrical power produced (or consumed) by a hydropower plant for each flow state: :math:`P_\text{S}^{t,~h,~s_q} = \begin{cases} P_\text{CAL}^{t,~h,~s_q} \text{ if } S_\text{FLOW}^{t,~h,~s_q} = 1 \\ 0 \text{ otherwise} \end{cases}`",:math:`\mathbb{R}^{+}`,:math:`\mathrm{m}^{3}/\mathrm{s}` +":math:`Q_\text{S}^{t,~h,~s_q}`","Real amount of water that flows through a pump for each hydro powerplant state: :math:`Q_\text{S}^{t,~h,~s_q} = \begin{cases} Q_\text{CAL}^{t,~h,~s_q} \text{ if } S_\text{FLOW}^{t,~h,~s_q} = 1 \\ 0",:math:`\mathbb{R}^{+}`,:math:`\mathrm{m}^{3}/\mathrm{s}` diff --git a/scripts/baseline_optimizazion.py b/scripts/baseline_optimizazion.py index 63ee588..e50c39f 100644 --- a/scripts/baseline_optimizazion.py +++ b/scripts/baseline_optimizazion.py @@ -22,7 +22,7 @@ SIMULATION_SETTING = { "1": {"quantile": 0, "buffer": 0.2, "powered_volume_enabled": True, "with_penalty": True}, "2": {"quantile": 0.15, "buffer": 0.3, "powered_volume_enabled": True, "with_penalty": True}, - "3": {"quantile": 0.15, "buffer": 0.3, "powered_volume_enabled": False, "with_penalty": True}, + "3": {"quantile": 0.25, "buffer": 0.3, "powered_volume_enabled": False, "with_penalty": True}, # "4": {"quantile": 0, "buffer": 0.2, "powered_volume_enabled": True, "with_penalty": False}, } output_file_names: dict[str, str] = json.load(open(settings.OUTPUT_FILE_NAMES)) diff --git a/src/pyomo_models/baseline/second_stage/second_stage_pipeline.py b/src/pyomo_models/baseline/second_stage/second_stage_pipeline.py index 7f80b3b..0b9da7a 100644 --- a/src/pyomo_models/baseline/second_stage/second_stage_pipeline.py +++ b/src/pyomo_models/baseline/second_stage/second_stage_pipeline.py @@ -1,6 +1,7 @@ import warnings from typing import Optional from datetime import datetime, timedelta, timezone +from annotated_types import T from patito import col import polars as pl from polars import col as c @@ -394,9 +395,10 @@ def solve_model(self): self.log_mip_gap(solution) self.extract_result() else: - self.infeasible_constraints = check_infeasible_constraints(model=self.model_instance) - self.calculated_feasibility() - break + solved = self.solve_changing_powered_volume_constraint() + if not solved: + break + logging.getLogger('pyomo.core').setLevel(logging.WARNING) self.finalizes_results_processing() @@ -404,7 +406,7 @@ def calculated_feasibility(self): start_basin_volume = self.start_basin_volume.filter(c("sim_nb") == self.sim_nb)[["B", "start_basin_volume"]] remaining_volume = self.remaining_volume.filter(c("sim_nb") == self.sim_nb)[["H", "remaining_volume"]] powered_volume = self.powered_volume.filter(c("sim_nb") == self.sim_nb)[["H", "powered_volume"]] - volume_buffer = self.powered_volume.filter(c("sim_nb") == self.sim_nb)[["H", "volume_buffer"]] + volume_buffer = self.volume_buffer.filter(c("sim_nb") == self.sim_nb)[["H", "volume_buffer"]] basin_power_mapping =pl.from_dicts(self.power_performance_table).drop("power_performance").with_columns( pl.all().cast(pl.UInt32) ) @@ -416,15 +418,49 @@ def calculated_feasibility(self): .join(volume_buffer, on="H", how="inner")\ .join(self.index["water_basin"][["B", "volume_max", "volume_min"]], on="B", how="inner")\ .with_columns( - (c("start_basin_volume") - c("powered_volume") + c("volume_buffer") + - pl.when(c("remaining_volume") > 0).then(c("remaining_volume")).otherwise(0) - ).alias("pos_boundary"), - (c("start_basin_volume") - c("powered_volume") - c("volume_buffer") + + (c("start_basin_volume") - c("powered_volume") + c("volume_buffer") - pl.when(c("remaining_volume") < 0).then(c("remaining_volume")).otherwise(0) + ).alias("pos_boundary"), + (c("start_basin_volume") - c("powered_volume") - c("volume_buffer") - + pl.when(c("remaining_volume") > 0).then(c("remaining_volume")).otherwise(0) ).alias("neg_boundary"), ).to_dicts()[0] ) + def solve_changing_powered_volume_constraint(self) -> bool: + if self.powered_volume_enabled == True: + self.powered_volume_enabled = False + self.generate_model_instance() + solution = self.solver.solve(self.model_instance, tee=self.log_solver_info) + if solution["Solver"][0]["Status"] == "ok": + self.extract_result() + elif solution["Solver"][0]["Status"] == "aborted": + self.log_mip_gap(solution) + self.extract_result() + else: + self.infeasible_constraints = check_infeasible_constraints(model=self.model_instance) + self.calculated_feasibility() + log.warning(f"Second stage optimization problem is infeasible for sim_nb: {self.sim_nb}") + return False + else: + self.infeasible_constraints = check_infeasible_constraints(model=self.model_instance) + self.calculated_feasibility() + log.warning(f"Second stage optimization problem is infeasible for sim_nb: {self.sim_nb}") + return False + self.powered_volume_enabled = True + return True + + def log_unbounded(self): + self.log_book = pl.concat([ + self.log_book, + pl.DataFrame( + { + "sim_nb": [self.sim_nb], + "unbounded": [True] + } + ) + ], how="diagonal_relaxed") + def log_mip_gap(self, solution): self.log_book = pl.concat([ self.log_book, diff --git a/src/pyomo_models/baseline/second_stage/sets.py b/src/pyomo_models/baseline/second_stage/sets.py index 2ba947b..f6d3c9c 100644 --- a/src/pyomo_models/baseline/second_stage/sets.py +++ b/src/pyomo_models/baseline/second_stage/sets.py @@ -1,4 +1,4 @@ -""" +r""" The sets :math:`T`, :math:`S_H` and :math:`S_B` are arranged in a specific order such that indexing a variable with the first or last element of a set (i.e :math:`t^{0}` and :math:`t^{\\text{END}} \in T`) corresponds respectively to the lowest/first or highest/final element of that variable. @@ -23,6 +23,8 @@ :math:`S_{BH} \in \{b, h\, s_b, s_h\}=\{(1, ~1, ~1, ~1),~(1, ~1, ~2, ~2),~(1, ~1, ~3, ~3)\}` + + """ import pyomo.environ as pyo @@ -35,11 +37,12 @@ def baseline_sets(model): # index gathering the state per basin and the hydro powerplants model.S_B = pyo.Set(model.B) model.S_H = pyo.Set(model.H) - model.F = pyo.Set(model.H) # index gathering the state of every basin and hydro powerplants model.BS = pyo.Set(dimen=2, initialize=lambda model: [(b, s_b) for b in model.B for s_b in model.S_B[b]]) model.HS = pyo.Set(dimen=2, initialize=lambda model: [(h, s_h) for h in model.H for s_h in model.S_H[h]]) - model.HF = pyo.Set(dimen=2, initialize=lambda model: [(h, f) for h in model.H for f in model.F[h]]) + + model.S_Q = pyo.Set(model.HS) + model.HF = pyo.Set(dimen=3, initialize=lambda model: [(i, j, f) for (i, j) in model.JI for f in model.F[i, j]]) model.HSF = pyo.Set( dimen=3, diff --git a/tests/base_line.ipynb b/tests/base_line.ipynb index 056d1d9..7e51463 100644 --- a/tests/base_line.ipynb +++ b/tests/base_line.ipynb @@ -66,8 +66,24 @@ "name": "stderr", "output_type": "stream", "text": [ - "Read and validate tables from small_flex_input_data.db file: 100%|████████████████████████████████████████████████████| 12/12 [00:01<00:00, 9.83it/s]\n", - "Solving first stage optimization problem: 100%|██████████| 1/1 [00:25<00:00, 25.46s/it]\n" + "Read and validate tables from small_flex_input_data.db file: 100%|████████████████████████████████████████████████████| 12/12 [00:01<00:00, 9.78it/s]\n", + "Solving first stage optimization problem: 100%|██████████| 1/1 [00:17<00:00, 17.81s/it]\n", + "Solving second stage optimization problem: 36%|███▌ | 33/92 [00:55<03:43, 3.79s/it]2024-12-11 13:33:38 B01BPC15 pyomo_models.baseline.second_stage.second_stage_pipeline[61717] WARNING Second stage optimization problem is infeasible for sim_nb: 33\n", + "Solving second stage optimization problem: 36%|███▌ | 33/92 [00:56<01:40, 1.70s/it]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'B': 0, 'start_basin_volume': 2.037239534957608, 'H': 0, 'remaining_volume': 0.0004748724386257411, 'powered_volume': 0.23633856000000275, 'volume_buffer': 0.19353599999999999, 'volume_max': 18.243199999999998, 'volume_min': 0.056996, 'pos_boundary': 1.9944369749576052, 'neg_boundary': 1.6068901025189797}\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n" ] } ], @@ -78,39 +94,15 @@ "baseline_input = BaseLineInput(\n", " input_schema_file_name=output_file_names[\"duckdb_input\"],\n", " real_timestep=timedelta(hours=1),\n", - " year=2020,\n", + " year=2023,\n", " hydro_power_mask = c(\"name\").is_in([\"Aegina hydro\"]),\n", " volume_factor=1e-6,\n", ")\n", "baseline_first_stage = BaselineFirstStage(\n", - " baseline_input, timestep=timedelta(days=1), turbine_factor=0.8)\n", + " baseline_input, timestep=timedelta(days=1), turbine_factor=0.7)\n", "baseline_first_stage.solve_model() \n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Solving second stage optimization problem: 0%| | 0/92 [00:00 310\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mconstructor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalues\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstrict\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 311\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n", - "\u001b[0;31mTypeError\u001b[0m: 'str' object cannot be interpreted as an integer", - "\nDuring handling of the above exception, another exception occurred:\n", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "File \u001b[0;32m~/git/smallflex/smallflex-vpp-codes/.venv/lib/python3.12/site-packages/polars/_utils/getitem.py:167\u001b[0m, in \u001b[0;36mget_df_item_by_key\u001b[0;34m(df, key)\u001b[0m\n\u001b[1;32m 166\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 167\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_select_rows\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdf\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# type: ignore[arg-type]\u001b[39;00m\n\u001b[1;32m 168\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n", - "File \u001b[0;32m~/git/smallflex/smallflex-vpp-codes/.venv/lib/python3.12/site-packages/polars/_utils/getitem.py:306\u001b[0m, in \u001b[0;36m_select_rows\u001b[0;34m(df, key)\u001b[0m\n\u001b[1;32m 305\u001b[0m _raise_on_boolean_mask()\n\u001b[0;32m--> 306\u001b[0m s \u001b[38;5;241m=\u001b[39m \u001b[43mpl\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mSeries\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mInt64\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 307\u001b[0m indices \u001b[38;5;241m=\u001b[39m _convert_series_to_indices(s, df\u001b[38;5;241m.\u001b[39mheight)\n", - "File \u001b[0;32m~/git/smallflex/smallflex-vpp-codes/.venv/lib/python3.12/site-packages/polars/series/series.py:289\u001b[0m, in \u001b[0;36mSeries.__init__\u001b[0;34m(self, name, values, dtype, strict, nan_to_null)\u001b[0m\n\u001b[1;32m 288\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(values, Sequence):\n\u001b[0;32m--> 289\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_s \u001b[38;5;241m=\u001b[39m \u001b[43msequence_to_pyseries\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 290\u001b[0m \u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 291\u001b[0m \u001b[43m \u001b[49m\u001b[43mvalues\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 292\u001b[0m \u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 293\u001b[0m \u001b[43m \u001b[49m\u001b[43mstrict\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstrict\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 294\u001b[0m \u001b[43m \u001b[49m\u001b[43mnan_to_null\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mnan_to_null\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 295\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 297\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m values \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", - "File \u001b[0;32m~/git/smallflex/smallflex-vpp-codes/.venv/lib/python3.12/site-packages/polars/_utils/construction/series.py:135\u001b[0m, in \u001b[0;36msequence_to_pyseries\u001b[0;34m(name, values, dtype, strict, nan_to_null)\u001b[0m\n\u001b[1;32m 134\u001b[0m constructor \u001b[38;5;241m=\u001b[39m polars_type_to_constructor(dtype)\n\u001b[0;32m--> 135\u001b[0m pyseries \u001b[38;5;241m=\u001b[39m \u001b[43m_construct_series_with_fallbacks\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 136\u001b[0m \u001b[43m \u001b[49m\u001b[43mconstructor\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalues\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstrict\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstrict\u001b[49m\n\u001b[1;32m 137\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 138\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m dtype \u001b[38;5;129;01min\u001b[39;00m (\n\u001b[1;32m 139\u001b[0m Date,\n\u001b[1;32m 140\u001b[0m Datetime,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 146\u001b[0m Decimal,\n\u001b[1;32m 147\u001b[0m ):\n", - "File \u001b[0;32m~/git/smallflex/smallflex-vpp-codes/.venv/lib/python3.12/site-packages/polars/_utils/construction/series.py:315\u001b[0m, in \u001b[0;36m_construct_series_with_fallbacks\u001b[0;34m(constructor, name, values, dtype, strict)\u001b[0m\n\u001b[1;32m 314\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 315\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mPySeries\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnew_from_any_values_and_dtype\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 316\u001b[0m \u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalues\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstrict\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstrict\u001b[49m\n\u001b[1;32m 317\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", - "\u001b[0;31mTypeError\u001b[0m: unexpected value while building Series of type Int64; found value of type String: \"H\"\n\nHint: Try setting `strict=False` to allow passing data with mixed types.", - "\nDuring handling of the above exception, another exception occurred:\n", - "\u001b[0;31mColumnNotFoundError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mbaseline_second_stage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcalculated_feasibility\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/git/smallflex/smallflex-vpp-codes/src/pyomo_models/baseline/second_stage/second_stage_pipeline.py:407\u001b[0m, in \u001b[0;36mBaselineSecondStage.calculated_feasibility\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 405\u001b[0m remaining_volume \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mremaining_volume\u001b[38;5;241m.\u001b[39mfilter(c(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msim_nb\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msim_nb)[[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mH\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mremaining_volume\u001b[39m\u001b[38;5;124m\"\u001b[39m]]\n\u001b[1;32m 406\u001b[0m powered_volume \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpowered_volume\u001b[38;5;241m.\u001b[39mfilter(c(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msim_nb\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msim_nb)[[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mH\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpowered_volume\u001b[39m\u001b[38;5;124m\"\u001b[39m]]\n\u001b[0;32m--> 407\u001b[0m volume_buffer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpowered_volume\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfilter\u001b[49m\u001b[43m(\u001b[49m\u001b[43mc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43msim_nb\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m==\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msim_nb\u001b[49m\u001b[43m)\u001b[49m\u001b[43m[\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mH\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mvolume_buffer\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 408\u001b[0m basin_power_mapping \u001b[38;5;241m=\u001b[39mpl\u001b[38;5;241m.\u001b[39mfrom_dicts(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpower_performance_table)\u001b[38;5;241m.\u001b[39mdrop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpower_performance\u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39mwith_columns(\n\u001b[1;32m 409\u001b[0m pl\u001b[38;5;241m.\u001b[39mall()\u001b[38;5;241m.\u001b[39mcast(pl\u001b[38;5;241m.\u001b[39mUInt32)\n\u001b[1;32m 410\u001b[0m )\n\u001b[1;32m 412\u001b[0m \u001b[38;5;28mprint\u001b[39m(start_basin_volume\\\n\u001b[1;32m 413\u001b[0m \u001b[38;5;241m.\u001b[39mjoin(basin_power_mapping, on\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mB\u001b[39m\u001b[38;5;124m\"\u001b[39m, how\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minner\u001b[39m\u001b[38;5;124m\"\u001b[39m)\\\n\u001b[1;32m 414\u001b[0m \u001b[38;5;241m.\u001b[39mjoin(remaining_volume, on\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mH\u001b[39m\u001b[38;5;124m\"\u001b[39m, how\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minner\u001b[39m\u001b[38;5;124m\"\u001b[39m)\\\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 425\u001b[0m )\u001b[38;5;241m.\u001b[39mto_dicts()[\u001b[38;5;241m0\u001b[39m]\n\u001b[1;32m 426\u001b[0m )\n", - "File \u001b[0;32m~/git/smallflex/smallflex-vpp-codes/.venv/lib/python3.12/site-packages/polars/dataframe/frame.py:1353\u001b[0m, in \u001b[0;36mDataFrame.__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 1216\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__getitem__\u001b[39m(\n\u001b[1;32m 1217\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 1218\u001b[0m key: (\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1227\u001b[0m ),\n\u001b[1;32m 1228\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m DataFrame \u001b[38;5;241m|\u001b[39m Series \u001b[38;5;241m|\u001b[39m Any:\n\u001b[1;32m 1229\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1230\u001b[0m \u001b[38;5;124;03m Get part of the DataFrame as a new DataFrame, Series, or scalar.\u001b[39;00m\n\u001b[1;32m 1231\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1351\u001b[0m \u001b[38;5;124;03m └─────┴─────┴─────┘\u001b[39;00m\n\u001b[1;32m 1352\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1353\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mget_df_item_by_key\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/git/smallflex/smallflex-vpp-codes/.venv/lib/python3.12/site-packages/polars/_utils/getitem.py:169\u001b[0m, in \u001b[0;36mget_df_item_by_key\u001b[0;34m(df, key)\u001b[0m\n\u001b[1;32m 167\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _select_rows(df, key) \u001b[38;5;66;03m# type: ignore[arg-type]\u001b[39;00m\n\u001b[1;32m 168\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[0;32m--> 169\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_select_columns\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdf\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/git/smallflex/smallflex-vpp-codes/.venv/lib/python3.12/site-packages/polars/_utils/getitem.py:217\u001b[0m, in \u001b[0;36m_select_columns\u001b[0;34m(df, key)\u001b[0m\n\u001b[1;32m 215\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _select_columns_by_index(df, key) \u001b[38;5;66;03m# type: ignore[arg-type]\u001b[39;00m\n\u001b[1;32m 216\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(first, \u001b[38;5;28mstr\u001b[39m):\n\u001b[0;32m--> 217\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_select_columns_by_name\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdf\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# type: ignore[arg-type]\u001b[39;00m\n\u001b[1;32m 218\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 219\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcannot select columns using Sequence with elements of type \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(first)\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n", - "File \u001b[0;32m~/git/smallflex/smallflex-vpp-codes/.venv/lib/python3.12/site-packages/polars/_utils/getitem.py:265\u001b[0m, in \u001b[0;36m_select_columns_by_name\u001b[0;34m(df, key)\u001b[0m\n\u001b[1;32m 264\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_select_columns_by_name\u001b[39m(df: DataFrame, key: Iterable[\u001b[38;5;28mstr\u001b[39m]) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m DataFrame:\n\u001b[0;32m--> 265\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m df\u001b[38;5;241m.\u001b[39m_from_pydf(\u001b[43mdf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_df\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mselect\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m)\n", - "\u001b[0;31mColumnNotFoundError\u001b[0m: \"volume_buffer\" not found" - ] + "data": { + "text/html": [ + "
\n", + "shape: (8_167, 4)
nameindexvaluebound
strlist[i32]nulli64
"basin_volume_evolution"[1, 0]null4
"basin_volume_evolution"[1, 1]null4
"basin_volume_evolution"[2, 0]null4
"basin_volume_evolution"[2, 1]null4
"basin_volume_evolution"[3, 0]null4
"power_constraint"[92, 0]null4
"power_constraint"[93, 0]null4
"power_constraint"[94, 0]null4
"power_constraint"[95, 0]null4
"power_constraint"[96, 0]null4
" + ], + "text/plain": [ + "shape: (8_167, 4)\n", + "┌────────────────────────┬───────────┬───────┬───────┐\n", + "│ name ┆ index ┆ value ┆ bound │\n", + "│ --- ┆ --- ┆ --- ┆ --- │\n", + "│ str ┆ list[i32] ┆ null ┆ i64 │\n", + "╞════════════════════════╪═══════════╪═══════╪═══════╡\n", + "│ basin_volume_evolution ┆ [1, 0] ┆ null ┆ 4 │\n", + "│ basin_volume_evolution ┆ [1, 1] ┆ null ┆ 4 │\n", + "│ basin_volume_evolution ┆ [2, 0] ┆ null ┆ 4 │\n", + "│ basin_volume_evolution ┆ [2, 1] ┆ null ┆ 4 │\n", + "│ basin_volume_evolution ┆ [3, 0] ┆ null ┆ 4 │\n", + "│ … ┆ … ┆ … ┆ … │\n", + "│ power_constraint ┆ [92, 0] ┆ null ┆ 4 │\n", + "│ power_constraint ┆ [93, 0] ┆ null ┆ 4 │\n", + "│ power_constraint ┆ [94, 0] ┆ null ┆ 4 │\n", + "│ power_constraint ┆ [95, 0] ┆ null ┆ 4 │\n", + "│ power_constraint ┆ [96, 0] ┆ null ┆ 4 │\n", + "└────────────────────────┴───────────┴───────┴───────┘" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "baseline_second_stage.calculated_feasibility()" + "baseline_second_stage.infeasible_constraints" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "baseline_second_stage.powered_volume_enabled = False\n", + "baseline_second_stage.solve_one_instance(sim_nb=27)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 10, 19, 28, 36]\n", - "shape: (4, 8)\n", - "┌────────────┬────────────┬────────────┬───────────┬───────────┬───────────┬───────────┬───────────┐\n", - "│ height ┆ flow_turbi ┆ flow_pumpe ┆ electrica ┆ electrica ┆ volume ┆ alpha_tur ┆ alpha_pum │\n", - "│ --- ┆ ned ┆ d ┆ l_power_t ┆ l_power_p ┆ --- ┆ bined ┆ ped │\n", - "│ struct[4] ┆ --- ┆ --- ┆ urbined ┆ umped ┆ struct[4] ┆ --- ┆ --- │\n", - "│ ┆ struct[4] ┆ struct[4] ┆ --- ┆ --- ┆ ┆ struct[4] ┆ struct[4] │\n", - "│ ┆ ┆ ┆ struct[4] ┆ struct[4] ┆ ┆ ┆ │\n", - "╞════════════╪════════════╪════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡\n", - "│ {2350.0,23 ┆ {2.3248,2. ┆ {2.7354,2. ┆ {7.2683,7 ┆ {12.108,1 ┆ {1.39706, ┆ {3.126419 ┆ {4.426409 │\n", - "│ 55.0,2360. ┆ 3506,2.376 ┆ 6739,2.612 ┆ .4618,7.6 ┆ 1.981,11. ┆ 2.901725, ┆ ,3.173902 ┆ ,4.482,4. │\n", - "│ 0,10.0} ┆ 4,0.0516} ┆ 4,-0.123} ┆ 553,0.387 ┆ 854,-0.25 ┆ 4.40639,3 ┆ ,3.221385 ┆ 53759,0.1 │\n", - "│ ┆ ┆ ┆ } ┆ 4} ┆ .00… ┆ ,0.… ┆ 111… │\n", - "│ {2360.0,23 ┆ {2.3764,2. ┆ {2.6124,2. ┆ {7.6553,7 ┆ {11.854,1 ┆ {4.40639, ┆ {3.221385 ┆ {4.53759, │\n", - "│ 64.5,2369. ┆ 39918,2.42 ┆ 55674,2.50 ┆ .82915,8. ┆ 1.7366,11 ┆ 6.43919,8 ┆ ,3.262867 ┆ 4.591632, │\n", - "│ 0,9.0} ┆ 196,0.0455 ┆ 108,-0.111 ┆ 003,0.347 ┆ .6192,-0. ┆ .47199,4. ┆ ,3.304349 ┆ 4.645673, │\n", - "│ ┆ … ┆ … ┆ 7} ┆ 234… ┆ 065… ┆ ,0.… ┆ 0.1… │\n", - "│ {2369.0,23 ┆ {2.42196,2 ┆ {2.50108,2 ┆ {8.003,8. ┆ {11.6192, ┆ {8.47199, ┆ {3.304349 ┆ {4.645673 │\n", - "│ 73.5,2378. ┆ .44431,2.4 ┆ .44524,2.3 ┆ 17648,8.3 ┆ 11.4984,1 ┆ 10.942595 ┆ ,3.344738 ┆ ,4.703685 │\n", - "│ 0,9.0} ┆ 6666,0.044 ┆ 894,-0.111 ┆ 4996,0.34 ┆ 1.3776,-0 ┆ ,13.4132, ┆ ,3.385128 ┆ ,4.761697 │\n", - "│ ┆ … ┆ … ┆ 696… ┆ .24… ┆ 4.9… ┆ ,0.… ┆ ,0.… │\n", - "│ {2378.0,23 ┆ {2.46666,2 ┆ {2.3894,2. ┆ {8.34996, ┆ {11.3776, ┆ {13.4132, ┆ {3.385128 ┆ {4.761697 │\n", - "│ 82.0,2386. ┆ .48625,2.5 ┆ 33252,2.27 ┆ 8.50395,8 ┆ 11.2651,1 ┆ 15.8282,1 ┆ ,3.420116 ┆ ,4.83128, │\n", - "│ 0,8.0} ┆ 0584,0.039 ┆ 564,-0.113 ┆ .65794,0. ┆ 1.1526,-0 ┆ 8.2432,4. ┆ ,3.455105 ┆ 4.900863, │\n", - "│ ┆ … ┆ … ┆ 307… ┆ .22… ┆ 83} ┆ ,0.… ┆ 0.1… │\n", - "└────────────┴────────────┴────────────┴───────────┴───────────┴───────────┴───────────┴───────────┘\n" - ] + "data": { + "text/plain": [ + "1.015914096" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "power_performance_table = baseline_first_stage.power_performance_table\n", - "\n", - "state_index: pl.DataFrame = pl.DataFrame()\n", - "for data in power_performance_table: \n", - " power_performance: pl.DataFrame = data[\"power_performance\"]\n", - " y_cols: list[str] = power_performance.select(cs.starts_with(\"alpha\")).columns\n", - " \n", - " segments = generate_state_index_using_errors(data=power_performance, column_list=y_cols, error_percent=1.3)\n", - " print(segments)\n", - " if len(segments) > 10:\n", - " log.warning(f\"{len(segments)} states found in {data[\"H\"]} hydro power plant\")\n", - " state_performance_table = filter_by_index(data=power_performance, index_list=segments)\\\n", - " .with_columns(\n", - " c(col).abs().pipe(get_min_avg_max_diff).alias(col) for col in power_performance.columns\n", - " ).slice(offset=0, length=len(segments)-1) \n", - " print(state_performance_table)\n", - " state_index = pl.concat([\n", - " state_index , \n", - " state_performance_table.with_columns(\n", - " pl.lit(data[\"H\"]).alias(\"H\"),\n", - " pl.lit(data[\"B\"]).alias(\"B\"))\n", - " ], how=\"diagonal\")\n", - " # Add every downstream basin\n" + "2.351653 * 3600*120 *1e-6 " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [ { "data": { - "text/html": [ - "
\n", - "shape: (5, 8)
heightflow_turbinedflow_pumpedelectrical_power_turbinedelectrical_power_pumpedvolumealpha_turbinedalpha_pumped
struct[4]struct[4]struct[4]struct[4]struct[4]struct[4]struct[4]struct[4]
{2350.0,2355.0,2360.0,10.0}{2.3248,2.3506,2.3764,0.0516}{2.7354,2.6739,2.6124,-0.123}{7.2683,7.4618,7.6553,0.387}{12.108,11.981,11.854,-0.254}{1.39706,2.901725,4.40639,3.00933}{3.126419,3.173902,3.221385,0.094966}{4.426409,4.482,4.53759,0.111181}
{2360.0,2364.5,2369.0,9.0}{2.3764,2.39918,2.42196,0.04556}{2.6124,2.55674,2.50108,-0.11132}{7.6553,7.82915,8.003,0.3477}{11.854,11.7366,11.6192,-0.2348}{4.40639,6.43919,8.47199,4.0656}{3.221385,3.262867,3.304349,0.082963}{4.53759,4.591632,4.645673,0.108083}
{2369.0,2373.5,2378.0,9.0}{2.42196,2.44431,2.46666,0.0447}{2.50108,2.44524,2.3894,-0.11168}{8.003,8.17648,8.34996,0.34696}{11.6192,11.4984,11.3776,-0.2416}{8.47199,10.942595,13.4132,4.94121}{3.304349,3.344738,3.385128,0.08078}{4.645673,4.703685,4.761697,0.116024}
{2378.0,2382.0,2386.0,8.0}{2.46666,2.48625,2.50584,0.03918}{2.3894,2.33252,2.27564,-0.11376}{8.34996,8.50395,8.65794,0.30798}{11.3776,11.2651,11.1526,-0.225}{13.4132,15.8282,18.2432,4.83}{3.385128,3.420116,3.455105,0.069977}{4.761697,4.83128,4.900863,0.139166}
{2386.0,null,null,null}{2.50584,null,null,null}{2.27564,null,null,null}{8.65794,null,null,null}{11.1526,null,null,null}{18.2432,null,null,null}{3.455105,null,null,null}{4.900863,null,null,null}
" - ], "text/plain": [ - "shape: (5, 8)\n", - "┌────────────┬────────────┬────────────┬───────────┬───────────┬───────────┬───────────┬───────────┐\n", - "│ height ┆ flow_turbi ┆ flow_pumpe ┆ electrica ┆ electrica ┆ volume ┆ alpha_tur ┆ alpha_pum │\n", - "│ --- ┆ ned ┆ d ┆ l_power_t ┆ l_power_p ┆ --- ┆ bined ┆ ped │\n", - "│ struct[4] ┆ --- ┆ --- ┆ urbined ┆ umped ┆ struct[4] ┆ --- ┆ --- │\n", - "│ ┆ struct[4] ┆ struct[4] ┆ --- ┆ --- ┆ ┆ struct[4] ┆ struct[4] │\n", - "│ ┆ ┆ ┆ struct[4] ┆ struct[4] ┆ ┆ ┆ │\n", - "╞════════════╪════════════╪════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡\n", - "│ {2350.0,23 ┆ {2.3248,2. ┆ {2.7354,2. ┆ {7.2683,7 ┆ {12.108,1 ┆ {1.39706, ┆ {3.126419 ┆ {4.426409 │\n", - "│ 55.0,2360. ┆ 3506,2.376 ┆ 6739,2.612 ┆ .4618,7.6 ┆ 1.981,11. ┆ 2.901725, ┆ ,3.173902 ┆ ,4.482,4. │\n", - "│ 0,10.0} ┆ 4,0.0516} ┆ 4,-0.123} ┆ 553,0.387 ┆ 854,-0.25 ┆ 4.40639,3 ┆ ,3.221385 ┆ 53759,0.1 │\n", - "│ ┆ ┆ ┆ } ┆ 4} ┆ .00… ┆ ,0.… ┆ 111… │\n", - "│ {2360.0,23 ┆ {2.3764,2. ┆ {2.6124,2. ┆ {7.6553,7 ┆ {11.854,1 ┆ {4.40639, ┆ {3.221385 ┆ {4.53759, │\n", - "│ 64.5,2369. ┆ 39918,2.42 ┆ 55674,2.50 ┆ .82915,8. ┆ 1.7366,11 ┆ 6.43919,8 ┆ ,3.262867 ┆ 4.591632, │\n", - "│ 0,9.0} ┆ 196,0.0455 ┆ 108,-0.111 ┆ 003,0.347 ┆ .6192,-0. ┆ .47199,4. ┆ ,3.304349 ┆ 4.645673, │\n", - "│ ┆ … ┆ … ┆ 7} ┆ 234… ┆ 065… ┆ ,0.… ┆ 0.1… │\n", - "│ {2369.0,23 ┆ {2.42196,2 ┆ {2.50108,2 ┆ {8.003,8. ┆ {11.6192, ┆ {8.47199, ┆ {3.304349 ┆ {4.645673 │\n", - "│ 73.5,2378. ┆ .44431,2.4 ┆ .44524,2.3 ┆ 17648,8.3 ┆ 11.4984,1 ┆ 10.942595 ┆ ,3.344738 ┆ ,4.703685 │\n", - "│ 0,9.0} ┆ 6666,0.044 ┆ 894,-0.111 ┆ 4996,0.34 ┆ 1.3776,-0 ┆ ,13.4132, ┆ ,3.385128 ┆ ,4.761697 │\n", - "│ ┆ … ┆ … ┆ 696… ┆ .24… ┆ 4.9… ┆ ,0.… ┆ ,0.… │\n", - "│ {2378.0,23 ┆ {2.46666,2 ┆ {2.3894,2. ┆ {8.34996, ┆ {11.3776, ┆ {13.4132, ┆ {3.385128 ┆ {4.761697 │\n", - "│ 82.0,2386. ┆ .48625,2.5 ┆ 33252,2.27 ┆ 8.50395,8 ┆ 11.2651,1 ┆ 15.8282,1 ┆ ,3.420116 ┆ ,4.83128, │\n", - "│ 0,8.0} ┆ 0584,0.039 ┆ 564,-0.113 ┆ .65794,0. ┆ 1.1526,-0 ┆ 8.2432,4. ┆ ,3.455105 ┆ 4.900863, │\n", - "│ ┆ … ┆ … ┆ 307… ┆ .22… ┆ 83} ┆ ,0.… ┆ 0.1… │\n", - "│ {2386.0,nu ┆ {2.50584,n ┆ {2.27564,n ┆ {8.65794, ┆ {11.1526, ┆ {18.2432, ┆ {3.455105 ┆ {4.900863 │\n", - "│ ll,null,nu ┆ ull,null,n ┆ ull,null,n ┆ null,null ┆ null,null ┆ null,null ┆ ,null,nul ┆ ,null,nul │\n", - "│ ll} ┆ ull} ┆ ull} ┆ ,null} ┆ ,null} ┆ ,null} ┆ l,null} ┆ l,null} │\n", - "└────────────┴────────────┴────────────┴───────────┴───────────┴───────────┴───────────┴───────────┘" + "2.1449348257360974" ] }, - "execution_count": 8, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "filter_by_index(data=power_performance, index_list=segments).with_columns(\n", - " c(col).abs().pipe(get_min_avg_max_diff).alias(col) for col in power_performance.columns\n", - " )" + "2.760282537736097 - 0.421811712 - 0.193536" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, "outputs": [ { "data": { - "text/html": [ - "
\n", - "shape: (4, 14)
FminmaxSHBflowd_flowelectrical_powerd_electrical_powerS_BHBSHSHSF
u32f64f64u32i32i64f64f64f64f64list[i64]list[i64]list[i64]list[i64]
014.002116.4183000-2.37680.023442-11.35080.046602[0, 0, … 0][0, 0][0, 0][0, 0, 0]
114.002116.41830002.471580.008128.388480.063745[0, 0, … 0][0, 0][0, 0][0, 0, 1]
214.002116.41830000.00.00.00.0[0, 0, … 0][0, 0][0, 0][0, 0, 2]
null0.01.01null1nullnullnullnull[null, 1, … 1][1, 1][null, 1][null, 1, null]
" - ], "text/plain": [ - "shape: (4, 14)\n", - "┌──────┬─────────┬─────────┬─────┬───┬────────────────┬───────────┬───────────┬─────────────────┐\n", - "│ F ┆ min ┆ max ┆ S ┆ … ┆ S_BH ┆ BS ┆ HS ┆ HSF │\n", - "│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │\n", - "│ u32 ┆ f64 ┆ f64 ┆ u32 ┆ ┆ list[i64] ┆ list[i64] ┆ list[i64] ┆ list[i64] │\n", - "╞══════╪═════════╪═════════╪═════╪═══╪════════════════╪═══════════╪═══════════╪═════════════════╡\n", - "│ 0 ┆ 14.0021 ┆ 16.4183 ┆ 0 ┆ … ┆ [0, 0, … 0] ┆ [0, 0] ┆ [0, 0] ┆ [0, 0, 0] │\n", - "│ 1 ┆ 14.0021 ┆ 16.4183 ┆ 0 ┆ … ┆ [0, 0, … 0] ┆ [0, 0] ┆ [0, 0] ┆ [0, 0, 1] │\n", - "│ 2 ┆ 14.0021 ┆ 16.4183 ┆ 0 ┆ … ┆ [0, 0, … 0] ┆ [0, 0] ┆ [0, 0] ┆ [0, 0, 2] │\n", - "│ null ┆ 0.0 ┆ 1.0 ┆ 1 ┆ … ┆ [null, 1, … 1] ┆ [1, 1] ┆ [null, 1] ┆ [null, 1, null] │\n", - "└──────┴─────────┴─────────┴─────┴───┴────────────────┴───────────┴───────────┴─────────────────┘" + "1.9532515881533397" ] }, - "execution_count": 9, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "baseline_first_stage.index[\"state\"].unnest([\"volume\"])" + "2.760282537736097 - baseline_second_stage.result_flow.filter(c(\"sim_nb\") == 27)[\"flow\"].sum() * 3600*1e-6" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " sim_nb H volume_buffer\n", + "0 0 0 0.048384\n", + "1 1 0 0.193536\n", + "2 2 0 0.193536\n", + "3 3 0 0.193536\n", + "4 4 0 0.193536\n", + "5 5 0 0.193536\n", + "6 6 0 0.193536\n", + "7 7 0 0.193536\n", + "8 8 0 0.193536\n", + "9 9 0 0.193536\n", + "10 10 0 0.193536\n", + "11 11 0 0.193536\n", + "12 12 0 0.193536\n", + "13 13 0 0.193536\n", + "14 14 0 0.193536\n", + "15 15 0 0.193536\n", + "16 16 0 0.193536\n", + "17 17 0 0.193536\n", + "18 18 0 0.193536\n", + "19 19 0 0.193536\n", + "20 20 0 0.193536\n", + "21 21 0 0.193536\n", + "22 22 0 0.193536\n", + "23 23 0 0.193536\n", + "24 24 0 0.193536\n", + "25 25 0 0.193536\n", + "26 26 0 0.193536\n", + "27 27 0 0.193536\n", + "28 28 0 0.193536\n", + "29 29 0 0.193536\n", + "30 30 0 0.193536\n", + "31 31 0 0.193536\n", + "32 32 0 0.193536\n", + "33 33 0 0.193536\n", + "34 34 0 0.193536\n", + "35 35 0 0.193536\n", + "36 36 0 0.193536\n", + "37 37 0 0.193536\n", + "38 38 0 0.193536\n", + "39 39 0 0.193536\n", + "40 40 0 0.193536\n", + "41 41 0 0.193536\n", + "42 42 0 0.193536\n", + "43 43 0 0.193536\n", + "44 44 0 0.193536\n", + "45 45 0 0.193536\n", + "46 46 0 0.193536\n", + "47 47 0 0.193536\n", + "48 48 0 0.193536\n", + "49 49 0 0.193536\n", + "50 50 0 0.193536\n", + "51 51 0 0.193536\n", + "52 52 0 0.193536\n", + "53 53 0 0.193536\n", + "54 54 0 0.193536\n", + "55 55 0 0.193536\n", + "56 56 0 0.193536\n", + "57 57 0 0.193536\n", + "58 58 0 0.193536\n", + "59 59 0 0.193536\n", + "60 60 0 0.193536\n", + "61 61 0 0.193536\n", + "62 62 0 0.193536\n", + "63 63 0 0.193536\n", + "64 64 0 0.193536\n", + "65 65 0 0.193536\n", + "66 66 0 0.193536\n", + "67 67 0 0.193536\n", + "68 68 0 0.193536\n", + "69 69 0 0.193536\n", + "70 70 0 0.193536\n", + "71 71 0 0.193536\n", + "72 72 0 0.193536\n", + "73 73 0 0.193536\n", + "74 74 0 0.193536\n", + "75 75 0 0.193536\n", + "76 76 0 0.193536\n", + "77 77 0 0.193536\n", + "78 78 0 0.193536\n", + "79 79 0 0.193536\n", + "80 80 0 0.193536\n", + "81 81 0 0.193536\n", + "82 82 0 0.193536\n", + "83 83 0 0.193536\n", + "84 84 0 0.193536\n", + "85 85 0 0.193536\n", + "86 86 0 0.193536\n", + "87 87 0 0.193536\n", + "88 88 0 0.193536\n", + "89 89 0 0.193536\n", + "90 90 0 0.193536\n", + "91 91 0 0.193536\n" + ] + } + ], + "source": [ + "self = baseline_second_stage\n", + "print(self.volume_buffer.to_pandas().to_string())" ] } ], diff --git a/tests/pyomo_test.py b/tests/pyomo_test.py index 0d50d75..f52a04f 100644 --- a/tests/pyomo_test.py +++ b/tests/pyomo_test.py @@ -9,11 +9,12 @@ model.T = pyo.Set() model.I = pyo.Set() model.J = pyo.Set(model.I) - model.F = pyo.Set(model.I) + model.JI = pyo.Set(dimen=2, initialize=lambda model: [(i, j) for i in model.I for j in model.J[i]]) - model.JFI = pyo.Set( + model.F = pyo.Set(model.JI) + model.JIF = pyo.Set( dimen=3, - initialize=lambda model: [(i, j, f) for i in model.I for j, f in product(model.J[i], model.F[i])] + initialize=lambda model: [(i, j, f) for (i, j) in model.JI for f in model.F[i, j]] ) model.x = pyo.Var(model.JI, domain=pyo.NonNegativeReals) @@ -27,36 +28,39 @@ 0: [0], 1: [1], 2: [2, 3], - 3: [3, 4, 5]}, + 3: [4, 5, 6]}, "F": { - 0: [0, 1], - 1: [2], - 2: [3], - 3: [4, 4] + (0, 0): [0, 1], + (1, 1): [2], + (2, 2): [3], + (2, 3): [4, 5], + (3, 4): [6, 7, 8], + (3, 5): [9, 10, 11], + (3, 6): [12, 13, 14] }, 'z': {0 : 100.0, 1: 50.0, 2: 200.0} } } - @model.Constraint(model.I) # type: ignore - def test(model, i): - return model.z[i] == sum(model.x[i, j] for j in model.J[i]) + # @model.Constraint(model.I) # type: ignore + # def test(model, i): + # return model.z[i] == sum(model.x[i, j] for j in model.J[i]) - @model.Constraint(model.JI) # type: ignore - def test_2(model, i, j): - return model.x[i, j] >= model.z[model.I.last()]/10 + # @model.Constraint(model.JI) # type: ignore + # def test_2(model, i, j): + # return model.x[i, j] >= model.z[model.I.last()]/10 instance: pyo.Model = model.create_instance(data) - solver = pyo.SolverFactory('gurobi') + # solver = pyo.SolverFactory('gurobi') - solver.solve(instance) - for c in instance.component_objects(pyo.Param, active=True): - print(f"Constraint: {c.name}") - print(c.display()) + # solver.solve(instance) + # for c in instance.component_objects(pyo.Param, active=True): + # print(f"Constraint: {c.name}") + # print(c.display()) - print(list(instance.JFI)) # type: ignore + print(list(instance.JIF)) # type: ignore