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
@@ -276,7 +276,10 @@ def build_terminal_value_curve(
|
||||
# LCOS is charged on delivered DC energy; the curve is in AC energy.
|
||||
lcos_per_wh_ac = (lcos_euro_per_kwh / 1000.0) / max(dc_to_ac_efficiency, 1e-9)
|
||||
|
||||
order = np.argsort(-prices)
|
||||
# Stable: slots with the same price keep their order. The default (quicksort) leaves the
|
||||
# order of equal prices to the platform - NumPy sorts with SIMD kernels on x86-64 - and that
|
||||
# order decides where the curve is cut at the battery capacity, i.e. the plan.
|
||||
order = np.argsort(-prices, kind="stable")
|
||||
energy_points: list[float] = [0.0]
|
||||
value_points: list[float] = [0.0]
|
||||
marginals: list[float] = []
|
||||
|
||||
@@ -14,6 +14,29 @@ from akkudoktoreos.utils.gridinterpolator import (
|
||||
)
|
||||
|
||||
|
||||
def fixed_order_dot(a: np.ndarray, b: np.ndarray) -> float:
|
||||
"""Return the dot product of two vectors, summed from the first element to the last.
|
||||
|
||||
``np.dot`` calls BLAS, whose kernel is chosen per CPU at run time (OpenBLAS
|
||||
``DYNAMIC_ARCH``, Accelerate). The kernels sum in different orders and some use fused
|
||||
multiply-add, so the last bit of the result depends on the machine. The optimizer
|
||||
compares fitness values exactly, so one bit is enough to give a different plan for the
|
||||
same input and seed. Here every product is rounded and added in index order, with
|
||||
Python floats: the same result on every CPU.
|
||||
|
||||
Args:
|
||||
a: First vector.
|
||||
b: Second vector, same length as ``a``.
|
||||
|
||||
Returns:
|
||||
``sum(a[i] * b[i])`` in index order.
|
||||
"""
|
||||
total = 0.0
|
||||
for x, y in zip(a.tolist(), b.tolist(), strict=True):
|
||||
total += x * y
|
||||
return total
|
||||
|
||||
|
||||
class SelfConsumptionProbabilityInterpolator:
|
||||
LOAD_DISTRIBUTION_CACHE_SIZE = 4096
|
||||
DIRECT_CONSUMPTION_CACHE_SIZE = 8192
|
||||
@@ -166,15 +189,16 @@ class SelfConsumptionProbabilityInterpolator:
|
||||
return 0.0
|
||||
|
||||
load_levels_w, probabilities = self._load_distribution(mean_load_power_w)
|
||||
modeled_mean_load_w = float(np.dot(probabilities, load_levels_w))
|
||||
# fixed_order_dot instead of np.dot: same result on every CPU (see there).
|
||||
modeled_mean_load_w = fixed_order_dot(probabilities, load_levels_w)
|
||||
if modeled_mean_load_w <= 0.0:
|
||||
return 0.0
|
||||
|
||||
# Preserve the requested mean load while keeping the conditional shape
|
||||
# from the probability table.
|
||||
normalized_load_levels_w = load_levels_w * (mean_load_power_w / modeled_mean_load_w)
|
||||
expected_direct_power_w = float(
|
||||
np.dot(probabilities, np.minimum(normalized_load_levels_w, pv_power_w))
|
||||
expected_direct_power_w = fixed_order_dot(
|
||||
probabilities, np.minimum(normalized_load_levels_w, pv_power_w)
|
||||
)
|
||||
# Scalar bounds: min/max instead of np.clip (same result, no array round trip).
|
||||
return min(max(expected_direct_power_w, 0.0), min(mean_load_power_w, pv_power_w))
|
||||
|
||||
Reference in New Issue
Block a user