|
4 | 4 | # pylint: disable=too-many-lines |
5 | 5 | """Tests for distribution algorithm.""" |
6 | 6 | import math |
| 7 | +from collections.abc import Sequence, Set |
7 | 8 | from dataclasses import dataclass |
8 | 9 | from datetime import datetime, timezone |
9 | 10 |
|
@@ -1023,6 +1024,20 @@ def assert_result(result: DistributionResult, expected: DistributionResult) -> N |
1023 | 1024 | assert result.distribution == approx(expected.distribution, abs=0.01) |
1024 | 1025 | assert result.remaining_power == approx(expected.remaining_power, abs=0.01) |
1025 | 1026 |
|
| 1027 | + @staticmethod |
| 1028 | + def assert_any_result( |
| 1029 | + result: DistributionResult, |
| 1030 | + expected_distribution_ids: Set[int], |
| 1031 | + expected_distribution_powers: Sequence[float], |
| 1032 | + expected_remaining_power: float, |
| 1033 | + ) -> None: |
| 1034 | + """Assert the result is as expected, disregarding which power goes to which component.""" |
| 1035 | + assert result.remaining_power == expected_remaining_power |
| 1036 | + assert {*result.distribution.keys()} == expected_distribution_ids |
| 1037 | + assert list(sorted(result.distribution.values())) == list( |
| 1038 | + sorted(expected_distribution_powers) |
| 1039 | + ) |
| 1040 | + |
1026 | 1041 | def test_scenario_1(self) -> None: |
1027 | 1042 | """Test scenario 1. |
1028 | 1043 |
|
@@ -1349,19 +1364,27 @@ def test_scenario_4(self) -> None: |
1349 | 1364 |
|
1350 | 1365 | algorithm = BatteryDistributionAlgorithm() |
1351 | 1366 |
|
1352 | | - self.assert_result( |
| 1367 | + # The assignment of power to each inverter can be swapped, as they both have the |
| 1368 | + # same bounds, none will be preferred |
| 1369 | + self.assert_any_result( |
1353 | 1370 | algorithm.distribute_power(-300, components), |
1354 | | - DistributionResult({2: -300, 3: 0}, remaining_power=0.0), |
| 1371 | + expected_distribution_ids={2, 3}, |
| 1372 | + expected_distribution_powers=[-300, 0], |
| 1373 | + expected_remaining_power=0, |
1355 | 1374 | ) |
1356 | 1375 |
|
1357 | | - self.assert_result( |
| 1376 | + self.assert_any_result( |
1358 | 1377 | algorithm.distribute_power(300, components), |
1359 | | - DistributionResult({2: 300, 3: 0}, remaining_power=0.0), |
| 1378 | + expected_distribution_ids={2, 3}, |
| 1379 | + expected_distribution_powers=[300, 0], |
| 1380 | + expected_remaining_power=0, |
1360 | 1381 | ) |
1361 | 1382 |
|
1362 | | - self.assert_result( |
| 1383 | + self.assert_any_result( |
1363 | 1384 | algorithm.distribute_power(-1800, components), |
1364 | | - DistributionResult({2: -1000, 3: -500}, remaining_power=-300.0), |
| 1385 | + expected_distribution_ids={2, 3}, |
| 1386 | + expected_distribution_powers=[-1000, -500], |
| 1387 | + expected_remaining_power=-300, |
1365 | 1388 | ) |
1366 | 1389 |
|
1367 | 1390 | def test_scenario_5(self) -> None: |
@@ -1431,33 +1454,37 @@ def test_scenario_5(self) -> None: |
1431 | 1454 |
|
1432 | 1455 | algorithm = BatteryDistributionAlgorithm() |
1433 | 1456 |
|
1434 | | - self.assert_result( |
| 1457 | + self.assert_any_result( |
1435 | 1458 | algorithm.distribute_power(-300, components), |
1436 | | - DistributionResult({10: -200, 11: 0, 20: -100, 21: 0}, remaining_power=0.0), |
| 1459 | + expected_distribution_ids={10, 11, 20, 21}, |
| 1460 | + expected_distribution_powers=[-200, 0, -100, 0], |
| 1461 | + expected_remaining_power=0.0, |
1437 | 1462 | ) |
1438 | 1463 |
|
1439 | | - self.assert_result( |
| 1464 | + self.assert_any_result( |
1440 | 1465 | algorithm.distribute_power(300, components), |
1441 | | - DistributionResult({10: 200, 11: 0, 20: 100, 21: 0}, remaining_power=0.0), |
| 1466 | + expected_distribution_ids={10, 11, 20, 21}, |
| 1467 | + expected_distribution_powers=[200, 0, 100, 0], |
| 1468 | + expected_remaining_power=0.0, |
1442 | 1469 | ) |
1443 | 1470 |
|
1444 | | - self.assert_result( |
| 1471 | + self.assert_any_result( |
1445 | 1472 | algorithm.distribute_power(-1800, components), |
1446 | | - DistributionResult( |
1447 | | - {10: -300, 11: 0, 20: -1000, 21: -500}, remaining_power=0.0 |
1448 | | - ), |
| 1473 | + expected_distribution_ids={10, 11, 20, 21}, |
| 1474 | + expected_distribution_powers=[-300, 0, -1000, -500], |
| 1475 | + expected_remaining_power=0.0, |
1449 | 1476 | ) |
1450 | 1477 |
|
1451 | | - self.assert_result( |
| 1478 | + self.assert_any_result( |
1452 | 1479 | algorithm.distribute_power(3000, components), |
1453 | | - DistributionResult( |
1454 | | - {10: 1000, 11: 500, 20: 1000, 21: 500}, remaining_power=0.0 |
1455 | | - ), |
| 1480 | + expected_distribution_ids={10, 11, 20, 21}, |
| 1481 | + expected_distribution_powers=[1000, 500, 1000, 500], |
| 1482 | + expected_remaining_power=0.0, |
1456 | 1483 | ) |
1457 | 1484 |
|
1458 | | - self.assert_result( |
| 1485 | + self.assert_any_result( |
1459 | 1486 | algorithm.distribute_power(3500, components), |
1460 | | - DistributionResult( |
1461 | | - {10: 1000, 11: 500, 20: 1000, 21: 500}, remaining_power=500.0 |
1462 | | - ), |
| 1487 | + expected_distribution_ids={10, 11, 20, 21}, |
| 1488 | + expected_distribution_powers=[1000, 500, 1000, 500], |
| 1489 | + expected_remaining_power=500.0, |
1463 | 1490 | ) |
0 commit comments