Skip to content

Commit 78d8d4c

Browse files
Amend deficit calculation in the power distributor (#577)
- [x] Remove absolute tolerance in floating-point comparison: see #562 (comment) for more details - [x] Remove redundant deficits updates: see #562 (comment) for more details - [x] Consolidate deficits for-loops: see #562 (comment) for more details This is an internal refactoring for the power distribution algorithm maintaining its functionality, so release notes don't need to be updated. Fixes #572
2 parents 7bbfd10 + 91fe22a commit 78d8d4c

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

src/frequenz/sdk/power/_distribution_algorithm.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -404,32 +404,24 @@ def _distribute_power( # pylint: disable=too-many-arguments
404404
take_from = max(excess_reserved.items(), key=lambda item: item[1])
405405
if is_close_to_zero(take_from[1]) or take_from[1] < 0.0:
406406
break
407-
if take_from[1] >= -deficit or math.isclose(
408-
take_from[1], -deficit, abs_tol=1e-6
409-
):
407+
if take_from[1] >= -deficit or math.isclose(take_from[1], -deficit):
410408
excess_reserved[take_from[0]] += deficit
411409
deficits[inverter_id] = 0.0
412410
deficit = 0.0
413411
else:
414412
deficit += excess_reserved[take_from[0]]
415413
deficits[inverter_id] = deficit
416414
excess_reserved[take_from[0]] = 0.0
417-
418-
for inverter_id, excess in excess_reserved.items():
419-
distribution[inverter_id] += excess
420-
distributed_power += excess
421-
422-
for inverter_id, deficit in deficits.items():
423415
if deficit < -0.1:
424416
left_over = power_w - distributed_power
425417
if left_over > -deficit:
426418
distributed_power += deficit
427-
deficit = 0.0
428-
deficits[inverter_id] = 0.0
429419
elif left_over > 0.0:
430-
deficit += left_over
431420
distributed_power += left_over
432-
deficits[inverter_id] = deficit
421+
422+
for inverter_id, excess in excess_reserved.items():
423+
distribution[inverter_id] += excess
424+
distributed_power += excess
433425

434426
left_over = power_w - distributed_power
435427
dist = DistributionResult(distribution, left_over)

0 commit comments

Comments
 (0)