diff --git a/backend/services/planning_engine.py b/backend/services/planning_engine.py index 564dc24..906258a 100644 --- a/backend/services/planning_engine.py +++ b/backend/services/planning_engine.py @@ -71,7 +71,7 @@ NEG_BUY_CHARGE_SHORTFALL_PENALTY_CZK_KWH = 100.0 PRE_NEG_CHARGE_PENALTY_CZK_KWH = 400.0 PRE_NEG_BATT_EXPORT_SHORTFALL_PENALTY_CZK_KWH = 80.0 PRE_NEG_BATT_EXPORT_MIN_SELL_CZK_KWH = 1.0 -PLANNER_BUILD_TAG = "2026-06-01-fixed-grid-charge-min-sell-v59" +PLANNER_BUILD_TAG = "2026-06-01-spot-grid-charge-at-acq-buy-v61" # Ranní slabá FVE: neaplikovat pv_store ge_pv=0 (jinak curtail při sell < večerní peak). DAWN_LOW_PV_NO_CURTAIL_W = 1500 # BA81/KV1: PV→bat jen v těsné blízkosti nejnižšího sell v horizontu (≈ poledne), ne při ~3 Kč ráno. @@ -4053,10 +4053,14 @@ def solve_dispatch( fixed_grid_charge_unprofitable = ( purchase_fixed_pre and buy_t >= 0.0 - and ( - sell_t < buy_t + min_spread - or fixed_sell_above_horizon_min - ) + and fixed_sell_above_horizon_min + ) + # Spot: mezi-slotová arbitráž — sell= 0.0 + and buy_t > charge_acquisition_czk_kwh + min_spread ) fixed_pv_b_export_cap = ( purchase_fixed_pre @@ -4066,7 +4070,7 @@ def solve_dispatch( and not fixed_pre_neg_pv_export and int(s.pv_a_forecast_w) >= DAWN_LOW_PV_NO_CURTAIL_W ) - if fixed_grid_charge_unprofitable: + if fixed_grid_charge_unprofitable or spot_grid_charge_not_cheap_buy: prob += bc_gi[t] == 0 if fixed_high_sell_no_pv_charge: prob += bc_pv[t] == 0 diff --git a/backend/tests/test_planning_dispatch_milp.py b/backend/tests/test_planning_dispatch_milp.py index 5f6d46f..80d53a8 100644 --- a/backend/tests/test_planning_dispatch_milp.py +++ b/backend/tests/test_planning_dispatch_milp.py @@ -1757,9 +1757,24 @@ class NegativeSellPvChargeTests(unittest.TestCase): "min sell: nabíjení z PV", ) - def test_fixed_no_grid_charge_when_sell_below_buy(self) -> None: - """v59: KV1 — sell < buy nesmí grid→bat (22h za 6 Kč).""" + def test_fixed_no_grid_charge_when_sell_above_horizon_min(self) -> None: + """v59: KV1 — grid→bat jen u min sell, ne v noci za 6 Kč při sell 3,5.""" prague = ZoneInfo("Europe/Prague") + cheap = PlanningSlot( + interval_start=datetime(2026, 6, 2, 10, 15, tzinfo=prague).astimezone( + timezone.utc + ), + buy_price=6.353, + sell_price=1.45, + pv_a_forecast_w=0, + pv_b_forecast_w=0, + load_baseline_w=400, + ev1_connected=False, + ev2_connected=False, + allow_charge=True, + allow_discharge_export=False, + charge_acquisition_buy_czk_kwh=6.353, + ) slot = PlanningSlot( interval_start=datetime(2026, 6, 1, 22, 0, tzinfo=prague).astimezone( timezone.utc @@ -1802,7 +1817,7 @@ class NegativeSellPvChargeTests(unittest.TestCase): ] soc0 = 0.25 * battery.usable_capacity_wh results, _, _ = solve_dispatch( - [slot], + [cheap, slot], battery, hp, grid, @@ -1813,9 +1828,9 @@ class NegativeSellPvChargeTests(unittest.TestCase): operating_mode="AUTO", ) self.assertLessEqual( - results[0].battery_setpoint_w, + results[1].battery_setpoint_w, 200, - "sell < buy: žádné grid nabíjení", + "sell nad min horizontu: žádné grid nabíjení", ) def test_fixed_evening_push_no_charge_at_peak_sell(self) -> None: @@ -3530,6 +3545,92 @@ class Home01RegressionTests(unittest.TestCase): ) return results, snap + def test_spot_no_grid_charge_when_buy_above_acquisition(self) -> None: + """v61: spot grid→bat jen když buy≈acq; sell None: """Levné NT → večerní peak: nabíjení v cheap slotech, večer BATTERY_SELL (SoC ↑ před peakem).""" from test_planning_charge_slot_selection import ( diff --git a/docs/04-modules/planning.md b/docs/04-modules/planning.md index f69c0a1..25caff9 100644 --- a/docs/04-modules/planning.md +++ b/docs/04-modules/planning.md @@ -138,7 +138,9 @@ flowchart TD 14. **v59 — fixní grid jen u min sell:** `bc_gi = 0` při **`sell < buy`** nebo **`sell > min + 0,20`**; push bez charge; **`R__063`** `sell ASC`. Tag **`2026-06-01-fixed-grid-charge-min-sell-v59`**. -**Funkce:** … home-01 **v57**; BA81/KV1 fixed **v59** (+ `R__063`). +15. **v61 — spot: grid→bat jen při buy ≤ acq:** `sell < buy` ve slotu **není** kritérium (marže); zákaz nabíjení při **`buy > charge_acquisition + degrad`**. Zrušeno v60. Tag **`2026-06-01-spot-grid-charge-at-acq-buy-v61`**. + +**Funkce:** … home-01 **v61**; BA81/KV1 fixed **v59** (+ `R__063`). ### Arbitráž baterie — účtování mezi sloty (povinné čtení) diff --git a/docs/planning-changelog.md b/docs/planning-changelog.md index aa79276..8493def 100644 --- a/docs/planning-changelog.md +++ b/docs/planning-changelog.md @@ -5,6 +5,18 @@ Formát: **datum (ISO)** · stručný důvod · soubory · chování / ověřen --- +## 2026-06-01 — home-01: grid jen při buy ≤ acquisition (v61, zrušeno v60) + +**Problém:** **19:00** nabíjení za **buy ~5,5** při **`charge_acquisition ~3,25`** z rána → falešně ziskový večerní export. **v60** (`sell < buy` ve slotu) bylo **špatně**: u spotu (a často u fixního tarifu) je **`sell < buy` normální** (marže distributora) — arbitráž je **mezi sloty**, ne v jedné čtvrthodině. + +**Změna (v61):** spot: **`bc_gi = 0`** jen když **`buy[t] > charge_acquisition + degrad`** (nákup v drahém slotu, ne levný NT). Export/push: zpět **`sell > acq + degrad`**. Fixní: **`bc_gi`** dál jen **`sell > min_sell_horizon`** (v59 SQL min sell); bez pravidla `sell < buy`. + +Tag **`2026-06-01-spot-grid-charge-at-acq-buy-v61`**. + +**Ověření:** `pytest … -k spot_no_grid_charge_when_buy_above_acquisition`. + +--- + ## 2026-06-01 — BA81/KV1: zákaz grid nabíjení mimo min sell + večer bez charge (v59) **Problém (po v58):** **KV1** po večerním vývozu **22:00–22:15** nabíjel ze sítě za **buy ~6,35 Kč** (`allow_charge` z `R__063` podle `slot_ord`, ne nejnižší sell). **BA81** v špičce **sell ~9,6** částečně **nabíjela** (`allow_charge` + PV), místo čistého vývozu; **03:30** grid nabíjení před východem slunce.