pry oprava uplne chybneho rizeni (prodaval za levneji nez nakoupil)
This commit is contained in:
@@ -707,6 +707,13 @@ def solve_dispatch(
|
||||
)
|
||||
|
||||
om = (operating_mode or "AUTO").strip().upper()
|
||||
charge_slots: set[int] = set()
|
||||
discharge_export_slots: set[int] = set()
|
||||
if om == "AUTO":
|
||||
charge_slots = {t for t, s in enumerate(slots) if s.allow_charge}
|
||||
discharge_export_slots = {
|
||||
t for t, s in enumerate(slots) if s.allow_discharge_export
|
||||
}
|
||||
# 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,
|
||||
@@ -939,14 +946,28 @@ def solve_dispatch(
|
||||
arb_cap_t = min(arb_t, soc_low_t)
|
||||
else:
|
||||
arb_cap_t = arb_t
|
||||
prob += soc_prev_expr >= (arb_cap_t - (arb_cap_t - soc_low_t) * (1 - w_arb[t]))
|
||||
prob += bd[t] <= (
|
||||
s.load_baseline_w
|
||||
+ ev_total_t
|
||||
+ hp[t]
|
||||
+ bc[t]
|
||||
+ battery.max_discharge_power_w * w_arb[t]
|
||||
)
|
||||
if om == "AUTO" and t not in discharge_export_slots:
|
||||
# PASSIVE na střídači: EMS neplánuje vybíjení do load (Deye pokryje skutečnou zátěž).
|
||||
pass
|
||||
elif om == "AUTO" and t in discharge_export_slots:
|
||||
prob += soc_prev_expr >= (
|
||||
arb_cap_t - (arb_cap_t - soc_low_t) * (1 - w_arb[t])
|
||||
)
|
||||
prob += bd[t] <= (
|
||||
battery.max_discharge_power_w * w_arb[t]
|
||||
+ pulp.lpSum(ev_via_bat[e][t] for e in range(EV))
|
||||
)
|
||||
else:
|
||||
prob += soc_prev_expr >= (
|
||||
arb_cap_t - (arb_cap_t - soc_low_t) * (1 - w_arb[t])
|
||||
)
|
||||
prob += bd[t] <= (
|
||||
s.load_baseline_w
|
||||
+ ev_total_t
|
||||
+ hp[t]
|
||||
+ bc[t]
|
||||
+ battery.max_discharge_power_w * w_arb[t]
|
||||
)
|
||||
|
||||
# Významný export ⇒ koncové SoC ≥ podlaha (viz soc_panel_min / arb_base).
|
||||
m_ge = float(grid.max_export_power_w)
|
||||
@@ -1001,18 +1022,12 @@ def solve_dispatch(
|
||||
|
||||
# Slot pre-selection (z DB fn_load_planning_slots_full → allow_*)
|
||||
if om == "AUTO":
|
||||
charge_slots = {t for t, s in enumerate(slots) if s.allow_charge}
|
||||
discharge_export_slots = {t for t, s in enumerate(slots) if s.allow_discharge_export}
|
||||
for t in range(T):
|
||||
if t not in charge_slots:
|
||||
prob += bc[t] == 0
|
||||
|
||||
if t not in discharge_export_slots:
|
||||
s = slots[t]
|
||||
ev_total_t = pulp.lpSum(
|
||||
ev_direct[e][t] + ev_via_bat[e][t] for e in range(EV)
|
||||
)
|
||||
prob += bd[t] <= s.load_baseline_w + ev_total_t + hp[t]
|
||||
prob += bd[t] == 0
|
||||
prob += w_arb[t] == 0
|
||||
|
||||
# Deadline constraints pro EV
|
||||
for e, session in enumerate(ev_sessions):
|
||||
@@ -1083,10 +1098,18 @@ def solve_dispatch(
|
||||
if grid_w < 0:
|
||||
export_mode = "BATTERY_SELL" if batt_w < 0 else "PV_SURPLUS"
|
||||
|
||||
# Primární klasifikace fyzického režimu pro Deye: explicitně do plánu (Variant A).
|
||||
# Default PASSIVE; SELL při export+vybíjení; CHARGE při import+nabíjení.
|
||||
# Deye: default PASSIVE (střídač pokryje load). CHARGE/SELL jen v maskovaných AUTO slotech.
|
||||
deye_mode = "PASSIVE"
|
||||
if batt_w < 0 and grid_w < 0:
|
||||
if om == "AUTO":
|
||||
if (
|
||||
slots[t].allow_discharge_export
|
||||
and batt_w < 0
|
||||
and grid_w < 0
|
||||
):
|
||||
deye_mode = "SELL"
|
||||
elif slots[t].allow_charge and batt_w > 0 and grid_w > 0:
|
||||
deye_mode = "CHARGE"
|
||||
elif batt_w < 0 and grid_w < 0:
|
||||
deye_mode = "SELL"
|
||||
elif batt_w > 0 and grid_w > 0:
|
||||
deye_mode = "CHARGE"
|
||||
|
||||
@@ -1009,6 +1009,71 @@ class PlanningDispatchMilpTests(unittest.TestCase):
|
||||
self.assertGreater(results[0].battery_setpoint_w, 0, "surplus PV should charge")
|
||||
|
||||
|
||||
class AutoPassiveNoLoadFollowingDischargeTests(unittest.TestCase):
|
||||
"""AUTO bez allow_discharge_export: žádné plánované vybíjení do load (Deye PASSIVE)."""
|
||||
|
||||
def test_no_battery_export_on_inflated_baseline_without_discharge_mask(self) -> None:
|
||||
slots = [
|
||||
PlanningSlot(
|
||||
interval_start=datetime(2026, 5, 16, 9, 45, tzinfo=timezone.utc),
|
||||
buy_price=0.77,
|
||||
sell_price=0.09,
|
||||
pv_a_forecast_w=0,
|
||||
pv_b_forecast_w=0,
|
||||
load_baseline_w=8542,
|
||||
ev1_connected=False,
|
||||
ev2_connected=False,
|
||||
is_predicted_price=False,
|
||||
allow_charge=False,
|
||||
allow_discharge_export=False,
|
||||
)
|
||||
]
|
||||
battery = _battery(uc_wh=20_000.0, min_pct=10.0, arb_pct=20.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=8000)
|
||||
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,
|
||||
),
|
||||
]
|
||||
soc0 = 0.45 * battery.usable_capacity_wh
|
||||
results, _, _ = solve_dispatch(
|
||||
slots,
|
||||
battery,
|
||||
hp,
|
||||
grid,
|
||||
[None, None],
|
||||
vehicles,
|
||||
soc0,
|
||||
50.0,
|
||||
tuv_delta_stats=None,
|
||||
operating_mode="AUTO",
|
||||
)
|
||||
self.assertEqual(len(results), 1)
|
||||
self.assertGreaterEqual(
|
||||
results[0].battery_setpoint_w,
|
||||
0,
|
||||
msg="must not plan load-following discharge when allow_discharge_export=false",
|
||||
)
|
||||
self.assertEqual(results[0].deye_physical_mode, "PASSIVE")
|
||||
self.assertGreaterEqual(
|
||||
results[0].grid_setpoint_w,
|
||||
0,
|
||||
msg="must not export at a loss when discharge is disallowed",
|
||||
)
|
||||
|
||||
|
||||
class TerminalSocShadowTests(unittest.TestCase):
|
||||
"""Terminal SoC shadow price v objective drží konec horizontu nad holým minimem."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user