n
Some checks failed
CI and deploy / migration-check (push) Failing after 14s
CI and deploy / deploy (push) Has been skipped

This commit is contained in:
Dusan Vojacek
2026-05-25 03:00:51 +02:00
parent f90004142c
commit b8e47e2623
5 changed files with 55 additions and 22 deletions

View File

@@ -65,8 +65,8 @@ NEG_SELL_PV_B_VENT_PENALTY_CZK_KWH = 4.0
EXTREME_BUY_DUMP_PREWINDOW_SLOTS = 12
NEG_SELL_BAT_DUMP_SHORTFALL_PENALTY_CZK_KWH = 80.0
NEG_BUY_CHARGE_SHORTFALL_PENALTY_CZK_KWH = 100.0
PRE_NEG_CHARGE_PENALTY_CZK_KWH = 250.0
PLANNER_BUILD_TAG = "2026-05-28-buy-sell-split-v22"
PRE_NEG_CHARGE_PENALTY_CZK_KWH = 400.0
PLANNER_BUILD_TAG = "2026-05-28-buy-sell-split-v22b"
CORRECTION_WINDOW_H = 1 # hodina zpět pro výpočet korekčního faktoru
CORRECTION_MIN_CLAMP = 0.5 # spodní limit korekčního faktoru
CORRECTION_MAX_CLAMP = 1.5 # horní limit korekčního faktoru
@@ -1061,11 +1061,13 @@ def solve_dispatch(
charge_commitment_prev_w: Optional[list[Optional[float]]] = None,
planner_version: str | None = None,
relaxed_expensive_import: bool = False,
relaxed_neg_buy_charge: bool = False,
) -> tuple[list[DispatchResult], int, dict[str, Any]]:
"""
LP solver pro dispatch optimalizaci.
Vrátí (výsledky, solver_duration_ms, solver_debug_snapshot).
relaxed_expensive_import: nouzový režim po Infeasible — síť smí krmit baseload v drahých slotech.
relaxed_neg_buy_charge: druhý nouzový retry bez neg_buy charge shortfall.
"""
T = len(slots)
if T < 1:
@@ -1409,12 +1411,15 @@ def solve_dispatch(
))
sf = pulp.LpVariable(f"export_shortfall_{t}", 0, cap_w)
peak_export_shortfall.append((t, sf, cap_w))
for t in range(T):
if float(slots[t].buy_price) >= 0.0:
continue
cap_w = float(battery.max_charge_power_w)
sf_nb = pulp.LpVariable(f"neg_buy_charge_sf_{t}", 0, cap_w)
neg_buy_charge_shortfall.append((t, sf_nb, cap_w))
if not relaxed_neg_buy_charge:
neg_buy_slot_indices = [
t for t, s in enumerate(slots) if float(s.buy_price) < 0.0
]
if neg_buy_slot_indices:
t_nb_last = max(neg_buy_slot_indices)
cap_w = float(battery.max_charge_power_w)
sf_nb = pulp.LpVariable(f"neg_buy_charge_sf_{t_nb_last}", 0, cap_w)
neg_buy_charge_shortfall.append((t_nb_last, sf_nb, cap_w))
for t in range(T):
if float(slots[t].sell_price) >= 0:
continue
@@ -1771,6 +1776,9 @@ def solve_dispatch(
if s.sell_price < 0:
prob += w_arb[t] == 0
prob += bd[t] <= pulp.lpSum(ev_via_bat[e][t] for e in range(EV))
# buy<0: export už zakázán výše; neaplikovat sell<0 ventil (bilance / infeasible).
if float(s.buy_price) < 0.0:
continue
block_neg_sell_export_t = bool(
getattr(grid, "block_export_on_negative_sell", False)
)
@@ -1986,14 +1994,7 @@ def solve_dispatch(
before_neg_buy = (
first_neg_buy_idx is not None and t < first_neg_buy_idx
)
if (
before_neg_buy
and float(s.buy_price) >= 0.0
and bool(getattr(s, "is_daytime_pv_surplus_slot", False))
):
prob += bc_pv[t] == 0
prob += bc_gi[t] == 0
elif before_neg_buy and sell_t_pre < 0.0 and pv_surplus_w > 0:
if before_neg_buy and sell_t_pre < 0.0 and pv_surplus_w > 0:
# Ranní sell<0 před buy<0: PV do sítě/curtail, ne do baterie (kapacita na import).
prob += bc_pv[t] == 0
elif t not in charge_slots:
@@ -2040,6 +2041,10 @@ def solve_dispatch(
and pre_neg_export_last_t is not None
and t <= pre_neg_export_last_t
and sell_t >= 0
and (
first_neg_buy_idx is None
or t < first_neg_buy_idx
)
)
pv_store_val = _pv_store_value_czk_kwh(s, min_spread)
skip_pv_store_block = (
@@ -2189,6 +2194,26 @@ def solve_dispatch(
planner_version=planner_version,
relaxed_expensive_import=True,
)
if not relaxed_neg_buy_charge:
logger.warning(
"solve_dispatch still Infeasible, retry without neg_buy_charge_shortfall"
)
return solve_dispatch(
slots,
battery,
heat_pump,
grid,
ev_sessions,
vehicles,
current_soc_wh,
current_tuv_temp_c,
tuv_delta_stats=tuv_delta_stats,
operating_mode=operating_mode,
charge_commitment_prev_w=charge_commitment_prev_w,
planner_version=planner_version,
relaxed_expensive_import=True,
relaxed_neg_buy_charge=True,
)
raise RuntimeError(f"Solver: {pulp.LpStatus[status]}")
# --- Post-processing ---