Skip to content

Commit 3a4469e

Browse files
committed
Revise solve_release.py
1 parent 33446d8 commit 3a4469e

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

src/pownet/reservoir/solve_release.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ def solve_release_from_target_storage(
1414
max_storage: float,
1515
initial_storage: float,
1616
target_storage: pd.Series,
17-
min_flow: pd.Series,
17+
minflow: pd.Series,
1818
total_inflow: pd.Series,
1919
) -> tuple[pd.Series, pd.Series, pd.Series, float]:
2020
"""Build an optimization problem to find the optimal release from the reservoir.
2121
The objective is to minimize the storage deviation from the target storage with L1 norm.
2222
2323
OBJECTIVE FUNCTION:
24-
min | target_storage - storage |
24+
min | target_storage - storage | + spill
2525
2626
Rewrite this as:
2727
@@ -66,11 +66,11 @@ def solve_release_from_target_storage(
6666
When using max/min as a function, we need to use gp.max_ and gp.min_
6767
see https://support.gurobi.com/hc/en-us/community/posts/360078185112-gurobipy-Model-addGenConstrMin-Invalid-data-in-vars-array
6868
69-
spills[day] = max(0, total_inflow[day] + storage[day] - max_storage - release[day])
69+
spills[day] = max(0, total_inflow[day] + storage[day-1] - max_storage - release[day])
7070
spills[day] = gp.max_(0, spill_bar[day])
7171
7272
with
73-
spill_bar[day] = total_inflow[day] + storage[day] - max_storage - release[day]
73+
spill_bar[day] = total_inflow[day] + storage[day-1] - max_storage - release[day]
7474
"""
7575
spill_bar = model.addVars(
7676
timesteps,
@@ -80,7 +80,7 @@ def solve_release_from_target_storage(
8080

8181
# Create the objective function
8282
model.setObjective(
83-
gp.quicksum(sbar[day] for day in timesteps),
83+
gp.quicksum(sbar[day] + spill_vars[day] for day in timesteps),
8484
sense=gp.GRB.MINIMIZE,
8585
)
8686

@@ -99,23 +99,39 @@ def solve_release_from_target_storage(
9999
# Minimum release has not been enforced when defining
100100
# the variable.
101101
model.addConstrs(
102-
(release_vars[day] >= min_flow[day] for day in timesteps),
102+
(release_vars[day] >= minflow[day] for day in timesteps),
103103
name="c_min_release",
104104
)
105105
# Define spill
106106
model.addConstrs(
107107
(spill_vars[day] == gp.max_(0, spill_bar[day]) for day in timesteps),
108108
name="c_spill",
109109
)
110+
110111
# Define spill_bar
111-
model.addConstrs(
112-
(
113-
spill_bar[day]
114-
== total_inflow[day] + storage_vars[day] - max_storage - release_vars[day]
115-
for day in timesteps
116-
),
117-
name="c_define_spill_bar",
118-
)
112+
for day in timesteps:
113+
if day == start_day:
114+
model.addConstr(
115+
(
116+
spill_bar[day]
117+
== initial_storage
118+
+ total_inflow[day]
119+
- release_vars[day]
120+
- max_storage
121+
),
122+
name=f"c_define_spill_bar[{day}]",
123+
)
124+
else:
125+
model.addConstr(
126+
(
127+
spill_bar[day]
128+
== storage_vars[day - 1]
129+
+ total_inflow[day]
130+
- release_vars[day]
131+
- max_storage
132+
),
133+
name=f"c_define_spill_bar[day]",
134+
)
119135

120136
# The storage at the start day is the initial storage
121137
model.addConstr(
@@ -185,7 +201,7 @@ def solve_release_from_dispatch(
185201
min_release: float,
186202
max_release: float,
187203
max_generation: float,
188-
) -> tuple[float, float, float, float]:
204+
) -> tuple[float, float, float, float, float, float, float]:
189205
"""
190206
For each day, solve for release_t from daily dispatch_t as an optimization problem.
191207
@@ -216,7 +232,7 @@ def solve_release_from_dispatch(
216232
spill_t = max(0, spill_bar)
217233
218234
6. Definition of spill_bar
219-
spill_bar = INFLOW_t + storage_t - STORAGE_MAX - release_t
235+
spill_bar = INFLOW_t + storage_t-1 - STORAGE_MAX - release_t
220236
221237
Note that the objective function is not linear because of the absolute value.
222238
To make it linear, we introduce a new variable mismatch_t and rewrite
@@ -312,7 +328,7 @@ def solve_release_from_dispatch(
312328

313329
# (5) Define spill_bar
314330
model.addConstr(
315-
spill_bar == inflow + storage - storage_max - release,
331+
spill_bar == storage_t0 + inflow - storage_max - release,
316332
name="c_spill_bar",
317333
)
318334

0 commit comments

Comments
 (0)