posledni dnesni fix
Some checks failed
CI and deploy / migration-check (push) Failing after 13s
CI and deploy / deploy (push) Has been skipped

This commit is contained in:
Dusan Vojacek
2026-05-25 01:46:06 +02:00
parent 254508fe1a
commit 5fb4c10ff6
5 changed files with 146 additions and 27 deletions

View File

@@ -64,7 +64,11 @@ NEG_SELL_PV_B_VENT_PENALTY_CZK_KWH = 4.0
# Výboj baterie při sell<0 jen těsně před extrémně záporným buy (round-trip arbitráž).
EXTREME_BUY_DUMP_PREWINDOW_SLOTS = 12
NEG_SELL_BAT_DUMP_SHORTFALL_PENALTY_CZK_KWH = 80.0
PLANNER_BUILD_TAG = "2026-05-27-site-export-cap-from-db-v18"
# Měkký tlak: v buy<0 slotech max import ze sítě do baterie (zisk z OTE záporného nákupu).
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-v19"
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
@@ -933,6 +937,31 @@ def _evening_battery_export_push_indices(
return sorted(out)
def _pre_neg_buy_discharge_push_indices(
slots: list[PlanningSlot],
pre_neg_buy_discharge_ts: set[int],
*,
max_slots_per_day: int = 8,
) -> list[int]:
"""
Tvrdý push ge_bat jen u nejlepších sell slotů před buy<0.
Jinak součet max výbojů přes celou noc může překročit SoC → Infeasible.
"""
by_day: dict = {}
for t in pre_neg_buy_discharge_ts:
d = _prague_calendar_date(slots[t])
by_day.setdefault(d, []).append(t)
out: list[int] = []
for d in sorted(by_day.keys()):
ranked = sorted(
by_day[d],
key=lambda i: float(slots[i].sell_price),
reverse=True,
)
out.extend(ranked[:max_slots_per_day])
return sorted(out)
def _planner_soc_for_solver(
current_soc_wh: float,
battery,
@@ -1185,6 +1214,7 @@ def solve_dispatch(
om = (operating_mode or "AUTO").strip().upper()
charge_slots: set[int] = set()
discharge_export_slots: set[int] = set()
pre_neg_buy_discharge_ts: set[int] = set()
if om == "AUTO":
charge_slots = {t for t, s in enumerate(slots) if s.allow_charge}
charge_slots |= {
@@ -1208,10 +1238,6 @@ def solve_dispatch(
}
# Vybití baterie před `buy<0` oknem: pokud je v horizontu buy<0, můžeme baterii
# vybít teď za `sell` a v buy<0 okně ji nabít za záporný buy (= příjem).
# Ekonomicky výhodné dokud: sell_t > avg_buy_neg + degradation
# (vybíjet ztratíme ~discharge_eff loss, nabíjení v buy<0 nás platí; marže ~ sell buy_neg degrad).
# Cílem je obejít to, že R__063 v noci dává allow_discharge_export=false a LP
# by jinak nemohl baterku vyklidit přes ge_bat.
_neg_buy_for_disch = next(
(t for t, s in enumerate(slots) if float(s.buy_price) < 0), None
)
@@ -1222,12 +1248,16 @@ def solve_dispatch(
if float(slots[t].buy_price) < 0
]
_avg_neg_buy = sum(_neg_buy_prices) / len(_neg_buy_prices) if _neg_buy_prices else 0.0
# Práh = avg buy<0 + degradation cycle overhead; default fallback 0.1 Kč/kWh
# když z nějakého důvodu neumíme spočítat (ochrana proti vybití do mínusu).
_disch_sell_thr = max(_avg_neg_buy + float(degradation_cost_effective), 0.1)
for t in range(_neg_buy_for_disch):
if float(slots[t].sell_price) >= _disch_sell_thr:
st = slots[t]
if float(st.sell_price) < _disch_sell_thr:
continue
# Jen sloty, kde SQL už povolilo export, nebo je výrazný kladný sell
# (rozšíření discharge_export_slots na celé dopoledne dělá krátké horizonty infeasible).
if st.allow_discharge_export or float(st.sell_price) >= 1.0:
discharge_export_slots.add(t)
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,
@@ -1405,6 +1435,9 @@ def solve_dispatch(
pv_charge_shortfall: list[tuple[int, pulp.LpVariable, float]] = []
neg_sell_bat_dump_shortfall: list[tuple[int, pulp.LpVariable, float]] = []
neg_sell_soc_underfill: list[tuple[int, pulp.LpVariable]] = []
neg_buy_soc_underfill: list[tuple[int, pulp.LpVariable]] = []
neg_buy_grid_shortfall: list[tuple[int, pulp.LpVariable, float]] = []
pre_neg_buy_export_shortfall: list[tuple[int, pulp.LpVariable, float]] = []
fixed_tariff_like = fixed_tariff_like_pre
block_export_neg_sell = bool(getattr(grid, "block_export_on_negative_sell", False))
if om == "AUTO":
@@ -1456,11 +1489,17 @@ def solve_dispatch(
cap_w = float(min(pv_surplus_w, battery.max_charge_power_w))
sf_pv = pulp.LpVariable(f"post_neg_pv_shortfall_{t}", 0, cap_w)
pv_charge_shortfall.append((t, sf_pv, cap_w))
_neg_buy_idx_soc = next(
(t for t, s in enumerate(slots) if float(s.buy_price) < 0), None
)
for t in range(T):
if float(slots[t].sell_price) >= 0:
continue
if t not in charge_slots:
continue
# Před buy<0 nehonit soc_max — kapacitu šetříme na záporný nákup.
if _neg_buy_idx_soc is not None and t < _neg_buy_idx_soc:
continue
pv_surplus_w = max(
0.0,
float(slots[t].pv_a_forecast_w)
@@ -1475,6 +1514,36 @@ 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 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:
if t not in discharge_export_slots:
continue
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)
@@ -1584,6 +1653,18 @@ def solve_dispatch(
us * NEG_SELL_SOC_UNDERFILL_PENALTY_CZK_PER_WH
for _t, us in neg_sell_soc_underfill
)
+ pulp.lpSum(
us * NEG_BUY_SOC_UNDERFILL_PENALTY_CZK_PER_WH
for _t, us in neg_buy_soc_underfill
)
+ pulp.lpSum(
sf * NEG_BUY_GRID_CHARGE_SHORTFALL_PENALTY_CZK_KWH * INTERVAL_H / 1000.0
for _t, sf, _cap in neg_buy_grid_shortfall
)
+ pulp.lpSum(
sf * PEAK_EXPORT_SHORTFALL_PENALTY_CZK_KWH * INTERVAL_H / 1000.0
for _t, sf, _cap in pre_neg_buy_export_shortfall
)
+ pulp.lpSum(
sf * NEG_SELL_BAT_DUMP_SHORTFALL_PENALTY_CZK_KWH * INTERVAL_H / 1000.0
for _t, sf, _cap in neg_sell_bat_dump_shortfall
@@ -1593,6 +1674,11 @@ def solve_dispatch(
for t in range(T)
if t in discharge_export_slots and t in profitable_export_ts_pre
)
+ pulp.lpSum(
-35.0 * z_export[t]
for t in pre_neg_buy_discharge_ts
if t in discharge_export_slots
)
)
# --- Omezení ---
@@ -1604,6 +1690,12 @@ def solve_dispatch(
prob += sf >= cap_w - ge_bat[t_sf]
for t_us, us in neg_sell_soc_underfill:
prob += us >= float(battery.soc_max_wh) - soc[t_us]
for t_us, us in neg_buy_soc_underfill:
prob += us >= float(battery.soc_max_wh) - soc[t_us]
for t_sf, sf, cap_w in neg_buy_grid_shortfall:
prob += sf >= cap_w - bc_gi[t_sf]
for t_sf, sf, cap_w in pre_neg_buy_export_shortfall:
prob += sf >= cap_w - ge_bat[t_sf]
preneg_export_min_soc_wh = float(min_soc_wh) + max(
float(battery.max_discharge_power_w)
* float(battery.discharge_efficiency)
@@ -1627,7 +1719,8 @@ def solve_dispatch(
if t_peak not in discharge_export_slots:
continue
prob += ge_bat[t_peak] >= export_push_w * z_export[t_peak]
# Ostatní profitable sloty: jen shortfall penalizace (ne tvrdý push na celý horizont).
# Pre-neg-buy: jen shortfall (tvrdý push by kolidoval s kotvou SoC v krátkých testech).
# Ostatní profitable sloty: shortfall penalizace (ne tvrdý push na celý horizont).
if t_anchor is not None and soc_anchor_slack is not None:
target_floor_wh = float(planner_floor_effective_wh)
prob += soc[t_anchor] <= target_floor_wh + soc_anchor_slack
@@ -1858,6 +1951,12 @@ def solve_dispatch(
# Bez hluboké relaxace: export končí ≥ rezerva. Při hluboké relaxaci (soc_panel_min pod min_soc)
# sladit s LP spodkem — jinak z_export vynutil arb_base a blokoval vývoz k planner floor.
if (
om == "AUTO"
and t in pre_neg_buy_discharge_ts
and floor_pct is not None
):
export_soc_floor_t = float(planner_floor_effective_wh)
elif (
om == "AUTO"
and first_neg_sell_idx is not None
and t < first_neg_sell_idx
@@ -1888,6 +1987,7 @@ def solve_dispatch(
tgt_s is not None
and not high_sell_slot[t]
and t not in profitable_export_ts_pre
and t not in pre_neg_buy_discharge_ts
and not (
om == "AUTO"
and t in discharge_export_slots
@@ -1967,24 +2067,27 @@ def solve_dispatch(
and float(s.buy_price) >= 0.0
):
prob += bc_gi[t] == 0
if t not in charge_slots:
pv_surplus_w = max(
0,
int(s.pv_a_forecast_w)
+ int(s.pv_b_forecast_w)
- int(s.load_baseline_w),
)
in_pre_neg_buy_window = (
_neg_buy_idx_main is not None and t < _neg_buy_idx_main
)
pv_surplus_w = max(
0,
int(s.pv_a_forecast_w)
+ int(s.pv_b_forecast_w)
- int(s.load_baseline_w),
)
in_pre_neg_buy_window = (
_neg_buy_idx_main is not None and t < _neg_buy_idx_main
)
if (
in_pre_neg_buy_window
and t in charge_slots
and float(s.sell_price) < 0.0
):
# sell<0+PV charge_slots před buy<0: neplnit z PV A (kapacita pro import).
prob += bc_pv[t] == 0
elif t not in charge_slots:
if float(s.buy_price) >= 0.0:
prob += bc_gi[t] == 0
if pv_surplus_w <= 0:
prob += bc_pv[t] == 0
elif in_pre_neg_buy_window:
# Strukturální preference: PV jde do gridu (sell≥0) nebo curtail,
# ne do baterie — kapacitu si šetříme na buy<0 okno.
prob += bc_pv[t] == 0
else:
prob += bc_pv[t] <= float(pv_surplus_w)
if t not in discharge_export_slots and t not in neg_sell_bat_dump_slots: