mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-11-13 09:13:30 +01:00
fix(genetic): same plan on every CPU (fixed-order dot products, stable sort of equal prices) (#1518)
* fix(genetic): sum the self-consumption dot products in a fixed order GENETIC gave different plans for the same input and seed on different CPUs. SelfConsumptionProbabilityInterpolator computed its two dot products with np.dot, which calls BLAS; OpenBLAS picks a ddot kernel per CPU at run time (and macOS uses Accelerate), and the kernels sum in different orders, so the result differs in the last bit. The optimizer compares fitness values exactly, so one bit is enough for a different plan. fixed_order_dot() adds the rounded products in index order with Python floats, without BLAS: the same result on every CPU. The vectors have one entry per 50 W load bin and the results are cached per (load, PV) pair, so the run time does not change noticeably. Plans change once in the last bits compared with the previous np.dot result of a given CPU. * fix(genetic): sort equal prices of the terminal value window in slot order np.argsort with the default kind (quicksort) leaves the order of equal values to the platform: NumPy sorts with SIMD kernels on x86-64 (x86-simd-sort) and with introsort elsewhere. Slots with the same price then fill the terminal value curve in another order, the battery capacity cuts it elsewhere, and the same input and seed gave another plan on x86-64 than on aarch64 (seen as a native-vs-Python compare difference on x86-64 in test_plan_keeps_reserve_for_a_peak_beyond_the_control_horizon). The window is now sorted with kind="stable": slots with the same price keep their slot order on every platform. Same goal as the dot product change in this PR: the same input gives the same plan on every CPU. Assisted-by: Claude Code (claude-opus-5-5) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
43cb0f04b2
commit
b4d2d7b2aa
@@ -127,3 +127,27 @@ def test_trailing_window_takes_the_end_of_the_horizon():
|
||||
# A window longer than the horizon yields what there is.
|
||||
assert list(trailing_window(values, end_slot=2, window_slots=5)) == [0.0, 1.0]
|
||||
assert list(trailing_window(None, end_slot=8, window_slots=3)) == []
|
||||
|
||||
|
||||
def test_equal_prices_keep_their_slot_order():
|
||||
"""Slots with the same price fill the curve in slot order, on every platform.
|
||||
|
||||
The capacity cuts the curve inside the equal-price slots, so their order decides the
|
||||
breakpoints; NumPy's default sort leaves it to the platform (SIMD kernels on x86-64).
|
||||
"""
|
||||
prices = np.array([0.0003] * 40 + [0.0004] + [0.0003] * 40)
|
||||
load = np.array([100.0 + slot for slot in range(81)])
|
||||
curve = _curve(
|
||||
prices_euro_per_wh=prices,
|
||||
load_wh=load,
|
||||
pv_wh=np.zeros(81),
|
||||
feed_in_euro_per_wh=np.zeros(81),
|
||||
max_energy_wh=1000.0,
|
||||
)
|
||||
|
||||
# The 0.40 slot (index 40, 140 Wh) first, then the 0.30 slots from index 0 on.
|
||||
expected = [0.0, 140.0]
|
||||
for slot in range(81):
|
||||
if slot != 40 and expected[-1] < 1000.0:
|
||||
expected.append(min(expected[-1] + 100.0 + slot, 1000.0))
|
||||
assert curve.energy_wh == pytest.approx(expected)
|
||||
|
||||
Reference in New Issue
Block a user