EOS_connect icon indicating copy to clipboard operation
EOS_connect copied to clipboard

Local EVopt backend: tight_M sizing ignores configured grid import/export limits, causing spurious Infeasible (regression from #268 smart defaults)

Open muxodo opened this issue 5 months ago • 2 comments

Hi, just updated to the latest develop version from latest relase (not really shure) and eos_connect was not starting correctly. Together with claude I identified an possible bug.

BTW: Thanks for this wonderful software!

Bug report from claude:

Environment:

  • Backend: local_evopt (built-in MILP)
  • Time frame: 900s (15 min)
  • Battery: 28000 Wh capacity, 20000 W max charge power, charging curve enabled
  • Inverter: Max Grid Charge Rate = 20000 W, Max PV Charge Rate = 10000 W

Description:

After updating EOS_connect, optimization runs started failing with status: Infeasible although the request itself is straightforward (no explicit local_evopt_max_grid_import_w/export_w set — both were left at 0).

Log excerpt:

[OPT-LocalEVopt] Smart forecast extension: ... PV capacity: 3445 W
[OPT-LocalEVopt] Building MILP: T=161 slots, dt=900s, tight_M=3073 (vs default M=1e6, ratio=325x smaller)
[OPT-LocalEVopt] Solved in 0 min 0.06 sec — status: Infeasible
[OPT-LocalEVopt] Solver returned non-optimal status 'Infeasible'; returning safe EOS infeasible payload.

Root cause:

The recent smart-defaults fix for #268 (commit 567684d) changed optimization_interface.py so that a local_evopt_max_grid_import_w/export_w value of 0 no longer resolves to None ("no limit"), but instead falls back to the configured inverter limits (inverter.max_grid_charge_rate / max_pv_charge_rate):

_max_imp = config.get("local_evopt_max_grid_import_w") or 0
if not _max_imp or _max_imp <= 0:
    _max_imp = self.inverter_max_grid_charge_rate_w

This means the local solver now (almost) always receives a real numeric p_max_imp/p_max_exp, activating the grid-limit constraint blocks in optimizer.py.

Those blocks rely on a global Big-M (tight_M, computed in optimization_backend_local_evopt.py::_build_optimizer) that is derived only from battery charge/discharge power and PV/load energy magnitudes — it does not take the configured/defaulted grid limit into account at all:

_max_bat_flow = max(..., c_max * dt/3600, d_max * dt/3600)
_max_energy = max(gt + ft)
tight_M = max(_max_bat_flow + _max_energy, 100.0) * 2

When p_max_imp * dt/3600 (or p_max_exp * dt/3600) exceeds tight_M, the big-M constraints in _add_energy_balance_constraints:

self.problem += (
    self.grid.p_max_imp * dt/3600 - self.variables['n'][t]
    <= self.M * self.variables['z_imp_lim'][t]
)

force a minimum grid import/export in every slot that can't actually be satisfied by the real energy balance — producing a genuine (but spurious) infeasibility. This is independent of the battery's SOC vs. max_soc_percentage (which is only soft-penalized in this model, see s_max_pen/s_min_pen).

Reproduction:

  1. Use local_evopt backend with local_evopt_max_grid_import_w/export_w left at 0.
  2. Configure inverter.max_grid_charge_rate (or max_pv_charge_rate) high enough that limit * dt/3600 > tight_M for a typical run (in our case 20000 W → 5000 Wh/slot vs. tight_M ≈ 3073).
  3. Optimization returns Infeasible.

Workaround found: explicitly setting local_evopt_max_grid_import_w/export_w to a value low enough that value * dt/3600 < tight_M (e.g. 10000 W → 2500 Wh/slot) avoids the mismatch. This is fragile since tight_M is recomputed per run from live PV/load/battery data and can shrink below the configured limit on low-yield days.

Suggested fix: include the effective p_max_imp/p_max_exp (whichever is active — explicit config or inverter-limit fallback) in the tight_M computation in _build_optimizer, e.g.:

_max_grid_flow = max(
    (max_grid_import_w or 0) * _max_dt / 3600,
    (max_grid_export_w or 0) * _max_dt / 3600,
)
tight_M = max(_max_bat_flow + _max_energy + _max_grid_flow, 100.0) * 2

This keeps the performance benefit of the tight Big-M while guaranteeing it never falls below the values the grid-limit constraints themselves rely on.

Related: #268 (introduced the smart-defaults fallback for this field).

muxodo avatar Jul 05 '26 12:07 muxodo

thanks for the finding ... check with last develop

ohAnd avatar Jul 05 '26 18:07 ohAnd

Seems to be fixed in 0.3.36.315-develop. Thanks!

muxodo avatar Jul 06 '26 08:07 muxodo