dalsi snaha o fix
This commit is contained in:
@@ -633,6 +633,20 @@ def _pv_store_value_czk_kwh(slot: PlanningSlot, min_spread: float) -> float:
|
||||
return future - min_spread
|
||||
|
||||
|
||||
def _horizon_fixed_tariff_like(slots: list[PlanningSlot]) -> bool:
|
||||
"""
|
||||
Fixní nákup (KV1): buy v horizontu je prakticky konstantní.
|
||||
U spotu (home-01) nesmí expensive_import používat charge_acquisition — jinak
|
||||
buy > ~1 Kč označí téměř všechny sloty jako drahé (gi=0 pro dům) → Infeasible.
|
||||
"""
|
||||
buys = [float(s.buy_price) for s in slots if float(s.buy_price) >= 0.0]
|
||||
if not buys:
|
||||
return False
|
||||
if len(buys) == 1:
|
||||
return True
|
||||
return max(buys) - min(buys) < 0.25
|
||||
|
||||
|
||||
def _pre_negative_sell_export_window(
|
||||
slots: list[PlanningSlot],
|
||||
) -> tuple[int | None, int | None]:
|
||||
@@ -747,10 +761,12 @@ def solve_dispatch(
|
||||
operating_mode: str = "AUTO",
|
||||
charge_commitment_prev_w: Optional[list[Optional[float]]] = None,
|
||||
planner_version: str | None = None,
|
||||
relaxed_expensive_import: 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.
|
||||
"""
|
||||
T = len(slots)
|
||||
if T < 1:
|
||||
@@ -1364,15 +1380,19 @@ def solve_dispatch(
|
||||
):
|
||||
prob += ge_pv[t] == 0
|
||||
# Drahý nákup: dům + TČ z baterie (ne import ze sítě); síť jen EV (+ případně TČ).
|
||||
# Spot: buy > min horizontu. Fixní tarif (KV1): buy > charge_acquisition (jinak je vše „stejně drahé“).
|
||||
expensive_import_slot = buy_t > ref_buy_horizon + min_spread or (
|
||||
buy_t > charge_acquisition_czk_kwh + min_spread
|
||||
)
|
||||
# Spot (home-01): buy > min ne-záporného buy v horizontu.
|
||||
# Fixní tarif (KV1): navíc buy > charge_acquisition (konstantní buy ≈ ref).
|
||||
expensive_import_slot = buy_t > ref_buy_horizon + min_spread
|
||||
if _horizon_fixed_tariff_like(slots):
|
||||
expensive_import_slot = expensive_import_slot or (
|
||||
buy_t > charge_acquisition_czk_kwh + min_spread
|
||||
)
|
||||
if expensive_import_slot and t not in charge_slots:
|
||||
# Síť jen pro EV + skutečný výkon TČ v tomto slotu (hp[t]), ne celý dům.
|
||||
prob += gi[t] <= ev_cap_t + hp[t]
|
||||
if om == "AUTO":
|
||||
# Bazál + TČ z baterie/FVE; hp[t] může být 0 — nesmí se použít hp_rated (infeasible).
|
||||
# Strict: síť jen EV+TČ; baseload z baterie/FVE. Relaxed: síť smí krmit baseload (nouzový režim).
|
||||
prob += gi[t] <= ev_cap_t + hp[t] + (
|
||||
float(s.load_baseline_w) if relaxed_expensive_import else 0.0
|
||||
)
|
||||
if not relaxed_expensive_import and om == "AUTO":
|
||||
prob += (
|
||||
bd[t] + pv_ld[t]
|
||||
>= float(s.load_baseline_w) + hp[t]
|
||||
@@ -1434,7 +1454,27 @@ def solve_dispatch(
|
||||
status = prob.solve(solver)
|
||||
duration_ms = int((time.monotonic() - t_start) * 1000)
|
||||
|
||||
if pulp.LpStatus[status] != 'Optimal':
|
||||
if pulp.LpStatus[status] != "Optimal":
|
||||
if not relaxed_expensive_import:
|
||||
logger.warning(
|
||||
"solve_dispatch Infeasible, retry with relaxed_expensive_import "
|
||||
"(grid may supply baseload in expensive slots)"
|
||||
)
|
||||
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,
|
||||
)
|
||||
raise RuntimeError(f"Solver: {pulp.LpStatus[status]}")
|
||||
|
||||
# --- Post-processing ---
|
||||
@@ -1597,6 +1637,7 @@ def solve_dispatch(
|
||||
"planner_charge_commitment_penalty_czk_kwh": float(commit_pen),
|
||||
},
|
||||
"load_first_enabled": om == "AUTO",
|
||||
"relaxed_expensive_import": relaxed_expensive_import,
|
||||
"charge_acquisition_buy_czk_kwh": charge_acquisition_czk_kwh,
|
||||
"charge_acquisition_cutoff_at": (
|
||||
slots[0].charge_acquisition_cutoff_at.isoformat()
|
||||
|
||||
@@ -1285,6 +1285,47 @@ class AutoPassiveSelfConsumptionTests(unittest.TestCase):
|
||||
)
|
||||
self.assertEqual(len(results), 20)
|
||||
|
||||
def test_spot_low_acquisition_does_not_mark_all_slots_expensive(self) -> None:
|
||||
"""Spot + charge_acquisition ~0,9 nesmí z buy>acq udělat gi=0 pro dům ve všech slotech."""
|
||||
base = datetime(2026, 5, 22, 10, 0, tzinfo=timezone.utc)
|
||||
slots = [
|
||||
PlanningSlot(
|
||||
interval_start=base + timedelta(minutes=15 * i),
|
||||
buy_price=2.5 + 0.1 * i,
|
||||
sell_price=3.0,
|
||||
pv_a_forecast_w=2000,
|
||||
pv_b_forecast_w=3000,
|
||||
load_baseline_w=2000,
|
||||
ev1_connected=False,
|
||||
ev2_connected=False,
|
||||
allow_charge=False,
|
||||
allow_discharge_export=False,
|
||||
charge_acquisition_buy_czk_kwh=0.94,
|
||||
future_sell_opportunity_czk_kwh=5.5,
|
||||
)
|
||||
for i in range(24)
|
||||
]
|
||||
battery = _battery(uc_wh=64_000.0, min_pct=12.0, arb_pct=20.0)
|
||||
battery.planner_terminal_soc_value_factor = 0.0
|
||||
hp = SimpleNamespace(rated_heating_power_w=0, tuv_min_temp_c=45.0, tuv_target_temp_c=55.0)
|
||||
grid = SimpleNamespace(max_import_power_w=17_000, max_export_power_w=13_500)
|
||||
vehicles = [
|
||||
SimpleNamespace(max_charge_power_w=0, battery_capacity_kwh=1.0, default_target_soc_pct=80.0),
|
||||
SimpleNamespace(max_charge_power_w=0, battery_capacity_kwh=1.0, default_target_soc_pct=80.0),
|
||||
]
|
||||
results, _, _ = solve_dispatch(
|
||||
slots,
|
||||
battery,
|
||||
hp,
|
||||
grid,
|
||||
[None, None],
|
||||
vehicles,
|
||||
30_000.0,
|
||||
50.0,
|
||||
operating_mode="AUTO",
|
||||
)
|
||||
self.assertEqual(len(results), 24)
|
||||
|
||||
|
||||
class AutoPassiveNoLoadFollowingDischargeTests(unittest.TestCase):
|
||||
"""AUTO bez allow_discharge_export: žádný export do sítě (Deye PASSIVE)."""
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
- **Load-first (Deye, AUTO):** proměnné `pv_ld` (PV → load+EV+TČ), `pv_sp` (přebytek), `bc_pv` / `bc_gi`. Plná bilance `pv_a + pv_b + gi + bd = load + ev + hp + bc + ge`; `bc_pv + ge_pv ≤ pv_sp`; `gi ≤ load + bc_gi`; mimo `allow_discharge_export`: `bd ≤ load − pv_ld` a **`pv_ld ≥ load − gi − bd`**. Snapshot: `load_first_enabled=true`. Test `LoadFirstDispatchTests`.
|
||||
- **Tvrdé výkonové limity site/baterie:** `gi ≤ site_grid_connection.max_import_power_w` (breaker); **`bc_pv + bc_gi ≤ asset_battery.max_charge_power_w`**; **`ge ≤ max_export_power_w`** (proměnná `ge`, platí `ge = ge_pv + ge_bat`); **`bd + ge_bat ≤ asset_battery.max_discharge_power_w`** (vybíjení do domu + export z baterie nesmí současně překročit BMS). Dříve LP dovoloval import+nabíjení a dvojnásobné nabíjení; u prodeje hrozilo současné `bd` a `ge_bat` až 2× max discharge — viz `SitePowerCapTests`.
|
||||
- **Hodnota FVE (PV store value):** `ge_pv = 0`, pokud `sell < future_sell_opportunity − degradation` (ne `charge_acquisition` — u fixního KV1 by jinak blokoval export při sell 2 Kč). **Před prvním `sell < 0` v horizontu:** při `sell ≥ 0` smí `ge_pv` až do `pv_sp` (strategie BA81: vyvézt přes poledne, pak nabít z FVE v záporném okně). Výjimka **nucený vent** jen plná baterie. Testy `Home01PvStoreValueTests`, `PreNegativeSellExportTests`.
|
||||
- **Drahý nákup → vlastní spotřeba z baterie:** mimo `allow_charge` platí `bd + pv_ld ≥ load_baseline + hp[t]` a `gi ≤ EV + hp[t]` (ne `hp_rated` — jinak infeasible když TČ v slotu neběží). Referenční buy = **min buy ≥ 0** v horizontu (záporný OTE slot jinak označí všechny sloty jako drahé → `gi=0` pro dům → Infeasible na home-01). Platí při `buy > ref_buy + degradace` **nebo** `buy > charge_acquisition + degradace`. Testy `AutoPassiveSelfConsumptionTests`, `test_negative_buy_in_horizon_does_not_block_all_grid_import`, `test_expensive_slot_uses_hp_variable_not_rated`.
|
||||
- **Drahý nákup → vlastní spotřeba z baterie:** mimo `allow_charge` platí `bd + pv_ld ≥ load_baseline + hp[t]` a `gi ≤ EV + hp[t]` (ne `hp_rated`). **Spot:** drahý slot = `buy > min(buy≥0) + degradace`. **Fixní tarif (KV1):** navíc `buy > charge_acquisition + degradace` (`_horizon_fixed_tariff_like` — rozptyl buy < 0,25 Kč/kWh). Na spotu **nesmí** `charge_acquisition` (~0,9 Kč) označit všechny sloty jako drahé → Infeasible (home-01). Při **Infeasible** solver jednou opakuje s `relaxed_expensive_import` (síť smí krmit baseload v drahých slotech; v `solver_params.inputs.relaxed_expensive_import=true`). Testy `AutoPassiveSelfConsumptionTests`, `test_spot_low_acquisition_does_not_mark_all_slots_expensive`, `test_negative_buy_in_horizon_does_not_block_all_grid_import`.
|
||||
- **Pole B při sell<0 (home-01):** pokud `block_export_on_negative_sell = false`, LP nesmí vynutit `ge_pv = 0` (přebytek neriťitelného PV B). KV1 s `block_export = true` jen curtail A / nabíjení.
|
||||
- **`ref_buy_min` (brána exportu):** `min(buy_price)` horizontu — jen „existuje levný nákup?“, **ne** průměrná cena nabití přes hodiny. Export sloty: `sell > ref_buy_min + degradation` (spot). Viz [`planning-arbitrage-accounting.md`](planning-arbitrage-accounting.md).
|
||||
- Pokud `energy_to_fill <= 0` nebo `charge_slot_buffer = 0`: všechny sloty povoleny.
|
||||
|
||||
Reference in New Issue
Block a user