a dalsi
Some checks failed
CI and deploy / migration-check (push) Failing after 11s
CI and deploy / deploy (push) Has been skipped

This commit is contained in:
Dusan Vojacek
2026-05-25 01:59:33 +02:00
parent a2a35981a1
commit 161b463367
5 changed files with 82 additions and 38 deletions

View File

@@ -68,7 +68,7 @@ NEG_SELL_BAT_DUMP_SHORTFALL_PENALTY_CZK_KWH = 80.0
NEG_BUY_GRID_CHARGE_SHORTFALL_PENALTY_CZK_KWH = 120.0
# Měkký tlak: v buy<0 okně dobít na soc_max (ne zastavit na ~94 %).
NEG_BUY_SOC_UNDERFILL_PENALTY_CZK_PER_WH = 0.45
PLANNER_BUILD_TAG = "2026-05-27-pre-neg-buy-strategy-v19b"
PLANNER_BUILD_TAG = "2026-05-27-pre-neg-buy-strategy-v19c"
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
@@ -937,6 +937,16 @@ def _evening_battery_export_push_indices(
return sorted(out)
def _negative_buy_slot_indices(slots: list[PlanningSlot]) -> list[int]:
return [t for t, s in enumerate(slots) if float(s.buy_price) < 0.0]
def _neg_buy_soc_pressure_slots(slots: list[PlanningSlot]) -> list[int]:
"""Tlak na soc_max jen na konci buy<0 okna (ne každý 15min slot zvlášť)."""
neg = _negative_buy_slot_indices(slots)
return [neg[-1]] if neg else []
def _pre_neg_buy_discharge_push_indices(
slots: list[PlanningSlot],
pre_neg_buy_discharge_ts: set[int],
@@ -1088,11 +1098,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_pressure: 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_pressure: vypne měkké v19 shortfall (pre-neg export, neg-buy soc/grid).
"""
T = len(slots)
if T < 1:
@@ -1253,12 +1265,9 @@ def solve_dispatch(
st = slots[t]
if float(st.sell_price) < _disch_sell_thr:
continue
# Jen výrazný kladný sell; NE přidávat do discharge_export_slots (w_arb by
# zablokoval bd → load v noci → Infeasible). ge_bat povolíme přes pre_neg_buy_discharge_ts.
# Jen kladný sell (sell<0 + z_export by vynutilo ge_bat≥1 zároveň s ge_bat=0).
if float(st.sell_price) >= 1.0:
pre_neg_buy_discharge_ts.add(t)
elif st.allow_discharge_export:
pre_neg_buy_discharge_ts.add(t)
# SELF_SUSTAIN dřív vynucoval ge[t] == 0, což umí udělat MILP infeasible v okamžiku, kdy:
# - baterie je na max SoC (nelze nabíjet),
# - PV pole B není curtailable,
@@ -1515,34 +1524,35 @@ def solve_dispatch(
float(battery.usable_capacity_wh),
)
neg_sell_soc_underfill.append((t, us))
for t in range(T):
if float(slots[t].buy_price) >= 0:
continue
us_buy = pulp.LpVariable(
f"neg_buy_soc_under_{t}",
0,
float(battery.usable_capacity_wh),
)
neg_buy_soc_underfill.append((t, us_buy))
# Grid charge shortfall jen pokud je v slotu reálně headroom (soc panel min pod max).
headroom_wh = float(battery.soc_max_wh) - float(soc_panel_min[t])
if headroom_wh < 500.0:
continue
cap_gi = float(
min(
battery.max_charge_power_w,
grid.max_import_power_w,
headroom_wh / max(INTERVAL_H * battery.charge_efficiency, 1e-6),
if not relaxed_neg_buy_pressure:
for t in _neg_buy_soc_pressure_slots(slots):
us_buy = pulp.LpVariable(
f"neg_buy_soc_under_{t}",
0,
float(battery.usable_capacity_wh),
)
)
if cap_gi < 500.0:
continue
sf_gi = pulp.LpVariable(f"neg_buy_gi_shortfall_{t}", 0, cap_gi)
neg_buy_grid_shortfall.append((t, sf_gi, cap_gi))
for t in pre_neg_buy_discharge_ts:
cap_pre = float(_battery_export_cap_w(battery, grid))
sf_pre = pulp.LpVariable(f"preneg_buy_disch_sf_{t}", 0, cap_pre)
pre_neg_buy_export_shortfall.append((t, sf_pre, cap_pre))
neg_buy_soc_underfill.append((t, us_buy))
for t in range(T):
if float(slots[t].buy_price) >= 0:
continue
headroom_wh = float(battery.soc_max_wh) - float(soc_panel_min[t])
if headroom_wh < 500.0:
continue
cap_gi = float(
min(
battery.max_charge_power_w,
grid.max_import_power_w,
headroom_wh / max(INTERVAL_H * battery.charge_efficiency, 1e-6),
)
)
if cap_gi < 500.0:
continue
sf_gi = pulp.LpVariable(f"neg_buy_gi_shortfall_{t}", 0, cap_gi)
neg_buy_grid_shortfall.append((t, sf_gi, cap_gi))
for t in pre_neg_buy_discharge_ts:
cap_pre = float(_battery_export_cap_w(battery, grid))
sf_pre = pulp.LpVariable(f"preneg_buy_disch_sf_{t}", 0, cap_pre)
pre_neg_buy_export_shortfall.append((t, sf_pre, cap_pre))
for t in neg_sell_bat_dump_slots:
dump_target_w = _battery_export_cap_w(battery, grid)
sf_dump = pulp.LpVariable(f"neg_bat_dump_shortfall_{t}", 0, dump_target_w)
@@ -1843,7 +1853,7 @@ def solve_dispatch(
block_neg_sell_export_t = bool(
getattr(grid, "block_export_on_negative_sell", False)
)
if t not in neg_sell_bat_dump_slots:
if t not in neg_sell_bat_dump_slots and t not in pre_neg_buy_discharge_ts:
prob += ge_bat[t] == 0
ev_cap_neg = sum(
float(vehicles[e].max_charge_power_w)
@@ -2275,6 +2285,28 @@ def solve_dispatch(
charge_commitment_prev_w=charge_commitment_prev_w,
planner_version=planner_version,
relaxed_expensive_import=True,
relaxed_neg_buy_pressure=relaxed_neg_buy_pressure,
)
if not relaxed_neg_buy_pressure:
logger.warning(
"solve_dispatch Infeasible, retry with relaxed_neg_buy_pressure "
"(skip v19 soft shortfalls)"
)
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_pressure=True,
)
raise RuntimeError(f"Solver: {pulp.LpStatus[status]}")
@@ -2506,6 +2538,7 @@ def solve_dispatch(
},
"load_first_enabled": om == "AUTO",
"relaxed_expensive_import": relaxed_expensive_import,
"relaxed_neg_buy_pressure": relaxed_neg_buy_pressure,
"charge_acquisition_buy_czk_kwh": charge_acquisition_czk_kwh,
"charge_acquisition_cutoff_at": (
slots[0].charge_acquisition_cutoff_at.isoformat()