Add more optimization tests (#171)

* Integrated single_test_optimization into pytest to run a basic optimization test with tolerance set to 1e-6, ensuring quick detection of deviations.
* Added a long-run test (400 generations, like single_test_optimization), which can be triggered using --full-run in pytest.
* Mocked PDF creation in optimization tests and added a new PDF generation test with image comparison validation.

Note: Current tolerance is set to 1e-6; feedback on whether this tolerance is tight enough is welcome.

---------

Co-authored-by: Normann <github@koldrack.com>
Co-authored-by: Michael Osthege <michael.osthege@outlook.com>
This commit is contained in:
Dominique Lasserre
2024-11-10 23:22:30 +01:00
committed by GitHub
parent de9fe2ddc9
commit 3652298134
10 changed files with 395 additions and 10 deletions

View File

@@ -1,16 +1,39 @@
import json
from pathlib import Path
from typing import Any
from unittest.mock import patch
import pytest
from akkudoktoreos.class_optimize import optimization_problem
from akkudoktoreos.config import output_dir
DIR_TESTDATA = Path(__file__).parent / "testdata"
@pytest.mark.parametrize("fn_in, fn_out", [("optimize_input_1.json", "optimize_result_1.json")])
def test_optimize(fn_in, fn_out):
def compare_dict(actual: dict[str, Any], expected: dict[str, Any]):
assert set(actual) == set(expected)
for key, value in expected.items():
if isinstance(value, dict):
assert isinstance(actual[key], dict)
compare_dict(actual[key], value)
elif isinstance(value, list):
assert isinstance(actual[key], list)
assert actual[key] == pytest.approx(value)
else:
assert actual[key] == pytest.approx(value)
@pytest.mark.parametrize(
"fn_in, fn_out, ngen",
[
("optimize_input_1.json", "optimize_result_1.json", 3),
("optimize_input_2.json", "optimize_result_2.json", 3),
("optimize_input_2.json", "optimize_result_2_full.json", 400),
],
)
@patch("akkudoktoreos.class_optimize.visualisiere_ergebnisse")
def test_optimize(visualisiere_ergebnisse_patch, fn_in: str, fn_out: str, ngen: int, is_full_run):
# Load input and output data
with open(DIR_TESTDATA / fn_in, "r") as f_in:
input_data = json.load(f_in)
@@ -23,16 +46,20 @@ def test_optimize(fn_in, fn_out):
)
start_hour = 10
if ngen > 10 and not is_full_run:
pytest.skip()
# Call the optimization function
ergebnis = opt_class.optimierung_ems(parameter=input_data, start_hour=start_hour, ngen=3)
# with open("new.json", "w") as f_out:
# json.dump(ergebnis, f_out, indent=4)
ergebnis = opt_class.optimierung_ems(parameter=input_data, start_hour=start_hour, ngen=ngen)
# with open(f"new_{fn_out}", "w") as f_out:
# from akkudoktoreos.class_numpy_encoder import NumpyEncoder
# json_data_str = NumpyEncoder.dumps(ergebnis)
# json.dump(json.loads(json_data_str), f_out, indent=4)
# Assert that the output contains all expected entries.
# This does not assert that the optimization always gives the same result!
# Reproducibility and mathematical accuracy should be tested on the level of individual components.
assert set(ergebnis) == set(expected_output_data)
compare_dict(ergebnis, expected_output_data)
# The function creates a visualization result PDF as a side-effect.
fp_viz = Path(output_dir) / "visualization_results.pdf"
assert fp_viz.exists()
visualisiere_ergebnisse_patch.assert_called_once()