LP first zjednoduseni
This commit is contained in:
@@ -38,6 +38,8 @@ SOLVER_TIME_LIMIT = 10 # sekund
|
||||
# (rezerva z DB). Při relaxaci spodku před extrémně záporným buy je podlaha soc_panel_min[t]
|
||||
# (planner floor), jinak by šlo jen do zátěže a nešlo by „vypustit do sítě“ před levným nákupem.
|
||||
GE_MIN_EXPORT_W = 1.0
|
||||
# Dvouprůchodové solve: stop když acquisition z pass1 vs pass2 se liší méně než (Kč/kWh).
|
||||
ACQUISITION_TWO_PASS_EPS_KWH = 0.05
|
||||
# Dokud je kotva pro hluboký dump (první sell < 0 v horizontu, jinak první extrémní buy) dál než
|
||||
# tento počet 15min slotů, držíme plánovací spodek na rezervě (arb_base_wh) místo planner floor —
|
||||
# priorita: beze „ztráty na prodeji“ (sell >= 0) držet buffer, hluboký vývoz až těsně před záporným prodejem.
|
||||
@@ -579,6 +581,113 @@ def apply_forecast_correction(
|
||||
# LP Solver
|
||||
# ============================================================
|
||||
|
||||
def _recompute_charge_acquisition_from_results(
|
||||
slots: list[PlanningSlot],
|
||||
results: list["DispatchResult"],
|
||||
battery,
|
||||
) -> float:
|
||||
"""Vážený buy z nabíjecích slotů (grid import + bat charge) z prvního solve."""
|
||||
wh_total = 0.0
|
||||
cost = 0.0
|
||||
for s, r in zip(slots, results):
|
||||
if not s.allow_charge:
|
||||
continue
|
||||
gi_w = max(0, int(r.grid_setpoint_w or 0))
|
||||
bc_w = max(0, int(r.battery_setpoint_w or 0))
|
||||
wh = (gi_w + bc_w) * INTERVAL_H
|
||||
if wh <= 0:
|
||||
continue
|
||||
wh_total += wh
|
||||
cost += float(s.buy_price) * wh
|
||||
if wh_total <= 0:
|
||||
raw = getattr(slots[0], "charge_acquisition_buy_czk_kwh", None)
|
||||
if raw is not None:
|
||||
return float(raw)
|
||||
return min(float(s.buy_price) for s in slots)
|
||||
return cost / wh_total
|
||||
|
||||
|
||||
def _slots_with_charge_acquisition(
|
||||
slots: list[PlanningSlot],
|
||||
acquisition_czk_kwh: float,
|
||||
) -> list[PlanningSlot]:
|
||||
return [
|
||||
replace(s, charge_acquisition_buy_czk_kwh=acquisition_czk_kwh)
|
||||
for s in slots
|
||||
]
|
||||
|
||||
|
||||
def solve_dispatch_two_pass(
|
||||
slots: list[PlanningSlot],
|
||||
battery,
|
||||
heat_pump,
|
||||
grid,
|
||||
ev_sessions: list,
|
||||
vehicles: list,
|
||||
current_soc_wh: float,
|
||||
current_tuv_temp_c: float,
|
||||
*,
|
||||
tuv_delta_stats: Optional[dict[tuple[int, int], float]] = None,
|
||||
operating_mode: str = "AUTO",
|
||||
charge_commitment_prev_w: Optional[list[Optional[float]]] = None,
|
||||
planner_version: str | None = None,
|
||||
) -> tuple[list["DispatchResult"], int, dict[str, Any]]:
|
||||
"""
|
||||
Dva průchody solve_dispatch: pass2 používá acquisition z váženého buy nabíjení v pass1.
|
||||
"""
|
||||
results1, ms1, snap1 = 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,
|
||||
)
|
||||
acq1 = float(
|
||||
snap1.get("inputs", {}).get("charge_acquisition_buy_czk_kwh")
|
||||
or getattr(slots[0], "charge_acquisition_buy_czk_kwh", None)
|
||||
or min(float(s.buy_price) for s in slots)
|
||||
)
|
||||
acq2 = _recompute_charge_acquisition_from_results(slots, results1, battery)
|
||||
converged = abs(acq2 - acq1) < ACQUISITION_TWO_PASS_EPS_KWH
|
||||
if converged:
|
||||
if isinstance(snap1.get("inputs"), dict):
|
||||
snap1["inputs"]["acquisition_pass1_czk_kwh"] = round(acq1, 6)
|
||||
snap1["inputs"]["acquisition_pass2_czk_kwh"] = round(acq2, 6)
|
||||
snap1["inputs"]["two_pass_enabled"] = True
|
||||
snap1["inputs"]["two_pass_converged"] = True
|
||||
return results1, ms1, snap1
|
||||
|
||||
slots2 = _slots_with_charge_acquisition(slots, acq2)
|
||||
results2, ms2, snap2 = solve_dispatch(
|
||||
slots2,
|
||||
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,
|
||||
)
|
||||
if isinstance(snap2.get("inputs"), dict):
|
||||
snap2["inputs"]["acquisition_pass1_czk_kwh"] = round(acq1, 6)
|
||||
snap2["inputs"]["acquisition_pass2_czk_kwh"] = round(acq2, 6)
|
||||
snap2["inputs"]["two_pass_enabled"] = True
|
||||
snap2["inputs"]["two_pass_converged"] = False
|
||||
snap2["inputs"]["solver_duration_ms_pass1"] = ms1
|
||||
return results2, ms1 + ms2, snap2
|
||||
|
||||
|
||||
def solve_dispatch(
|
||||
slots: list[PlanningSlot],
|
||||
battery,
|
||||
@@ -823,14 +932,15 @@ def solve_dispatch(
|
||||
commit_lp.append((t, cv, cap_prev))
|
||||
|
||||
# --- Účelová funkce (jen OTE sloty; terminal SoC shadow price na konci horizontu) ---
|
||||
# Arbitráž baterie: ge_bat v exportních slotech + charge_acquisition (SQL, před 1. exportem).
|
||||
# Viz docs/04-modules/planning-arbitrage-accounting.md — ne stejnoslotové buy/sell.
|
||||
# Kanály: gi×buy, −ge_pv×sell, −ge_bat×sell, +ge_bat×acquisition (export bat. jen v discharge slotách).
|
||||
# Viz docs/04-modules/planning-arbitrage-accounting.md — mezi-slotová arbitráž, ne sell vs buy v jednom slotu.
|
||||
prob += (
|
||||
pulp.lpSum(
|
||||
gi[t] * slots[t].buy_price * INTERVAL_H / 1000
|
||||
- ge[t] * slots[t].sell_price * INTERVAL_H / 1000
|
||||
- ge_pv[t] * slots[t].sell_price * INTERVAL_H / 1000
|
||||
- ge_bat[t] * slots[t].sell_price * INTERVAL_H / 1000
|
||||
+ (
|
||||
ge[t] * SELF_SUSTAIN_EXPORT_PENALTY_CZK_KWH * INTERVAL_H / 1000
|
||||
ge_pv[t] * SELF_SUSTAIN_EXPORT_PENALTY_CZK_KWH * INTERVAL_H / 1000
|
||||
if om == "SELF_SUSTAIN"
|
||||
else 0
|
||||
)
|
||||
@@ -1095,41 +1205,13 @@ def solve_dispatch(
|
||||
0.0,
|
||||
float(s.pv_a_forecast_w) + float(s.pv_b_forecast_w) - load_t,
|
||||
)
|
||||
# Mezi-slotová FVE arbitráž: export jen když (prodat teď − levný nákup později)
|
||||
# ≥ (večerní špička − acquisition). Jinak drž PV v baterii na peak sell.
|
||||
fso_t = float(
|
||||
s.future_sell_opportunity_czk_kwh
|
||||
if s.future_sell_opportunity_czk_kwh is not None
|
||||
else sell_t
|
||||
)
|
||||
future_chg_buys = [
|
||||
float(slots[ts].buy_price)
|
||||
for ts in range(t + 1, T)
|
||||
if ts in charge_slots
|
||||
]
|
||||
min_future_chg_buy = (
|
||||
min(future_chg_buys)
|
||||
if future_chg_buys
|
||||
else charge_acquisition_czk_kwh
|
||||
)
|
||||
export_refill_net = sell_t - min_future_chg_buy
|
||||
store_peak_net = fso_t - charge_acquisition_czk_kwh
|
||||
cross_slot_pv_export = (
|
||||
t not in charge_slots
|
||||
and pv_surplus_w > 0
|
||||
and future_chg_buys
|
||||
and export_refill_net >= store_peak_net + min_spread
|
||||
)
|
||||
# Ztrátový export FVE (sell ≪ buy): zakázat jen pokud jde energii do baterie.
|
||||
# Výjimky: plná baterie (ventil), neriťitelné pv_b s přebytkem, cross-slot výše.
|
||||
if sell_t < buy_t - min_spread:
|
||||
# FVE export: zakázat jen okamžitě ztrátový výkup vs plánovaná zásoba (ne sell < buy ve slotu).
|
||||
if sell_t < charge_acquisition_czk_kwh - min_spread:
|
||||
block_loss_pv_export = not (
|
||||
float(s.pv_b_forecast_w) > 0 and pv_surplus_w > 0
|
||||
)
|
||||
if t == 0 and current_soc_wh >= float(battery.soc_max_wh) - soc_headroom_wh:
|
||||
block_loss_pv_export = False
|
||||
if cross_slot_pv_export:
|
||||
block_loss_pv_export = False
|
||||
if block_loss_pv_export:
|
||||
prob += ge_pv[t] == 0
|
||||
# Drahý nákup oproti horizontu: import jen na load + EV + TČ, ne na grid-nabíjení.
|
||||
@@ -1411,12 +1493,21 @@ async def run_daily_plan(
|
||||
planner_version_resolved = _planner_engine_version(planner_version)
|
||||
slots = await _load_slots(site_id, horizon_from, horizon_to, db, soc_wh=soc_wh)
|
||||
|
||||
results, duration_ms, solver_snapshot = solve_dispatch(
|
||||
slots, battery, hp, grid, ev_sessions, vehicles, soc_wh, tuv_temp,
|
||||
tuv_delta_stats=tuv_stats,
|
||||
operating_mode=operating_mode or "AUTO",
|
||||
planner_version=planner_version_resolved,
|
||||
)
|
||||
om = operating_mode or "AUTO"
|
||||
if om == "AUTO":
|
||||
results, duration_ms, solver_snapshot = solve_dispatch_two_pass(
|
||||
slots, battery, hp, grid, ev_sessions, vehicles, soc_wh, tuv_temp,
|
||||
tuv_delta_stats=tuv_stats,
|
||||
operating_mode=om,
|
||||
planner_version=planner_version_resolved,
|
||||
)
|
||||
else:
|
||||
results, duration_ms, solver_snapshot = solve_dispatch(
|
||||
slots, battery, hp, grid, ev_sessions, vehicles, soc_wh, tuv_temp,
|
||||
tuv_delta_stats=tuv_stats,
|
||||
operating_mode=om,
|
||||
planner_version=planner_version_resolved,
|
||||
)
|
||||
comparison_ctx = _maybe_add_planner_comparison(
|
||||
slots=slots,
|
||||
battery=battery,
|
||||
@@ -1426,7 +1517,7 @@ async def run_daily_plan(
|
||||
vehicles=vehicles,
|
||||
current_soc_wh=soc_wh,
|
||||
current_tuv_temp_c=tuv_temp,
|
||||
operating_mode=operating_mode or "AUTO",
|
||||
operating_mode=om,
|
||||
tuv_delta_stats=tuv_stats,
|
||||
active_version=planner_version_resolved,
|
||||
)
|
||||
@@ -1600,13 +1691,23 @@ async def run_rolling_replan(
|
||||
|
||||
commitment_prev = await _load_previous_plan_charge_commitment_prev_w(site_id, slots, db)
|
||||
|
||||
results, duration_ms, solver_snapshot = solve_dispatch(
|
||||
slots, battery, hp, grid, ev_sessions, vehicles, soc_wh, tuv_temp,
|
||||
tuv_delta_stats=tuv_stats,
|
||||
operating_mode=operating_mode or "AUTO",
|
||||
charge_commitment_prev_w=commitment_prev,
|
||||
planner_version=planner_version_resolved,
|
||||
)
|
||||
om = operating_mode or "AUTO"
|
||||
if om == "AUTO":
|
||||
results, duration_ms, solver_snapshot = solve_dispatch_two_pass(
|
||||
slots, battery, hp, grid, ev_sessions, vehicles, soc_wh, tuv_temp,
|
||||
tuv_delta_stats=tuv_stats,
|
||||
operating_mode=om,
|
||||
charge_commitment_prev_w=commitment_prev,
|
||||
planner_version=planner_version_resolved,
|
||||
)
|
||||
else:
|
||||
results, duration_ms, solver_snapshot = solve_dispatch(
|
||||
slots, battery, hp, grid, ev_sessions, vehicles, soc_wh, tuv_temp,
|
||||
tuv_delta_stats=tuv_stats,
|
||||
operating_mode=om,
|
||||
charge_commitment_prev_w=commitment_prev,
|
||||
planner_version=planner_version_resolved,
|
||||
)
|
||||
comparison_ctx = _maybe_add_planner_comparison(
|
||||
slots=slots,
|
||||
battery=battery,
|
||||
@@ -1616,7 +1717,7 @@ async def run_rolling_replan(
|
||||
vehicles=vehicles,
|
||||
current_soc_wh=soc_wh,
|
||||
current_tuv_temp_c=tuv_temp,
|
||||
operating_mode=operating_mode or "AUTO",
|
||||
operating_mode=om,
|
||||
tuv_delta_stats=tuv_stats,
|
||||
active_version=planner_version_resolved,
|
||||
charge_commitment_prev_w=commitment_prev,
|
||||
|
||||
Reference in New Issue
Block a user