-
Notifications
You must be signed in to change notification settings - Fork 3
WIP: upgrade to rtc-tools 2.7.3, casadi 3.7.2, rtctools-highs 0.1.3 #483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,6 @@ | |
| MILP optimization for design and operational optimization | ||
| """ | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| from setuptools import find_packages, setup | ||
|
|
@@ -30,12 +29,6 @@ | |
| Operating System :: MacOS | ||
| """ | ||
|
|
||
| if sys.version_info < (3, 10): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update with main before continuing, since there has been some fixes regarding network settings which might have an impact on the head loss testing |
||
| sys.exit(f"Sorry, Python 3.10 to 3.11 is required. You are using {sys.version_info}") | ||
|
|
||
| if sys.version_info >= (3, 12): | ||
| sys.exit(f"Sorry, Python 3.10 to 3.11 is required. You are using {sys.version_info}") | ||
|
|
||
| setup( | ||
| name="mesido", | ||
| version=versioneer.get_version(), | ||
|
|
@@ -58,19 +51,19 @@ | |
| "influxdb >= 5.3.1", | ||
| "pyecore >= 0.13.2", | ||
| "pymoca >= 0.9.0", | ||
| "rtc-tools-gil-comp == 2.6.1", | ||
| "rtc-tools == 2.7.3", | ||
| # setuptools version limitations currently: | ||
| # < 81.0.0 needed for pandapipes (still to be removed) | ||
| # < 82.0.0 needed for pkg_resources (used in rtctools) | ||
| "setuptools <= 80.9.0", | ||
| "pyesdl == 26.3", | ||
| "pandas >= 1.3.1, < 2.0", | ||
| "casadi-gil-comp == 3.6.7", | ||
| "rtctools-highs == 0.1.3", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently the specific HIGHS and CASADI version cannot be specified in MESIDO. Would it be possible to change the rtc-tools-casadi-plugins such that the HIGHS and CASADI version numbers are inputs? |
||
| "StrEnum == 0.4.15", | ||
| "CoolProp==6.6.0", | ||
| ], | ||
| include_package_data=True, | ||
| python_requires=">=3.10,<3.12", | ||
| python_requires=">=3.10,<3.13", # pandas<2.0 does not provide wheels for 3.13+ | ||
| cmdclass=versioneer.get_cmdclass(), | ||
| entry_points={"rtctools.libraries.modelica": ["library_folder = mesido:modelica"]}, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,6 +125,9 @@ def solver_options(self): | |
| """ | ||
| options = super().solver_options() | ||
| options["casadi_solver"] = self._qpsol | ||
| highs_options = options.setdefault("highs", {}) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not ideal. Any other insights to potentially not turn off presolve? This one is needed for tests/test_ates.py::TestAtes::test_ates |
||
| # workaround for HiGHS 1.14.0 presolve bug (ERGO-Code/HiGHS#2388) | ||
| highs_options["presolve"] = "off" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HiGHS#2388 -> I assume this refers to a specific issue number, but I cannot find it in https://github.com/ERGO-Code/HiGHS/issues?q=is%3Aissue%20state%3Aopen Can you please clarify? |
||
| return options | ||
|
|
||
| def constraints(self, ensemble_member: int): | ||
|
|
@@ -164,6 +167,11 @@ class HeatProblemPlacingOverTime(HeatProblem): | |
| achieved by having an upper limit on the investment per time-step. | ||
| """ | ||
|
|
||
| def solver_options(self): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am aware that you will still investigate potential solutions such that we do not have to turn of presolve. But this part is not needed, since presolve is already turned off in the inherited problem class (HeatProblem) |
||
| options = super().solver_options() | ||
| options.get("highs", {}).pop("presolve", None) | ||
| return options | ||
|
|
||
| def energy_system_options(self): | ||
| """ | ||
| In this problem we are optimizing when the assets are realized over time, hence we set the | ||
|
|
@@ -272,11 +280,10 @@ def energy_system_options(self): | |
|
|
||
| def solver_options(self): | ||
| options = super().solver_options() | ||
| options["solver"] = "highs" | ||
| highs_options = options["highs"] = {} | ||
| highs_options = options.setdefault("highs", {}) | ||
| highs_options["mip_rel_gap"] = 0.02 | ||
| highs_options["presolve"] = "on" | ||
|
|
||
| # workaround for HiGHS 1.14.0 presolve bug (ERGO-Code/HiGHS#2388) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not ideal. Any other insights in this one to potential get rid of this? So this one is here for test_setpoint_constraints.py (test_run_small_ates_timed_setpoints_2_changes, test_run_small_ates_timed_setpoints_0_changes, test_run_small_ates_timed_setpoints_multiple_constraint --> presolve_infeasible_or_unbounded_priority_1) |
||
| highs_options["presolve"] = "off" | ||
| return options | ||
|
|
||
| def constraints(self, ensemble_member): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """Tests for HiGHS 1.14.0 integration via rtctools-highs. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am a bit uncertain about this test. Since the idea originally was to be able to specify the version number of casadi and highs in mesido (de-linked via mesido). I will think about this topic a bit futher and get back to you But here is some feedback: |
||
|
|
||
| Verifies: | ||
| 1. rtctools_highs registers HiGHS 1.14.0 (not CasADi's bundled 1.10.0). | ||
| 2. The Python GIL is released during CasADi solves (WITH_PYTHON_GIL_RELEASE=ON | ||
| in casadi 3.7.2), enabling concurrent Python threads while HiGHS runs. | ||
| """ | ||
|
|
||
| import re | ||
| import threading | ||
| import time | ||
|
|
||
| import casadi as ca | ||
|
|
||
| import rtctools_highs # noqa: F401 — registers HiGHS 1.14.0 plugin with CasADi | ||
|
|
||
|
|
||
| def _make_solver(**highs_opts): | ||
| x = ca.MX.sym("x") | ||
| qp = {"x": x, "f": (x - 1) ** 2, "g": x} | ||
| return ca.qpsol("s", "highs", qp, {"highs": highs_opts}) | ||
|
|
||
|
|
||
| class TestHiGHSVersion: | ||
| def test_highs_version(self, tmp_path): | ||
| """HiGHS 1.14.0 must be used — not CasADi's bundled 1.10.0.""" | ||
| log_file = str(tmp_path / "highs.log") | ||
| solver = _make_solver(output_flag=True, log_file=log_file) | ||
| solver(lbx=-10, ubx=10, lbg=0, ubg=2) | ||
|
|
||
| assert solver.stats()["return_status"] == "Optimal" | ||
|
|
||
| log = (tmp_path / "highs.log").read_text() | ||
| match = re.search(r"Running HiGHS (\S+)", log) | ||
| assert match, f"HiGHS version line not found in log:\n{log}" | ||
| assert match.group(1) == "1.14.0", ( | ||
| f"Expected HiGHS 1.14.0 but got {match.group(1)} — " | ||
| "CasADi's bundled HiGHS 1.10.0 may have been loaded instead" | ||
| ) | ||
|
|
||
|
|
||
| class TestGILRelease: | ||
| """Verify casadi 3.7.2 releases the GIL during solves. | ||
|
|
||
| WITH_PYTHON_GIL_RELEASE=ON means Python threads can run concurrently | ||
| while CasADi/HiGHS is solving. We verify this by running a HiGHS solve | ||
| in a background thread and confirming a Python counter increments during | ||
| the solve — which only happens if the GIL is released. | ||
| """ | ||
|
|
||
| def test_gil_released_during_solve(self): | ||
| solver = _make_solver() | ||
| counter = {"n": 0} | ||
| solve_done = threading.Event() | ||
|
|
||
| def run_solve(): | ||
| solver(lbx=-10, ubx=10, lbg=0, ubg=2) | ||
| assert solver.stats()["return_status"] == "Optimal" | ||
| solve_done.set() | ||
|
|
||
| def increment_counter(): | ||
| while not solve_done.is_set(): | ||
| counter["n"] += 1 | ||
| time.sleep(0.0001) | ||
|
|
||
| counter_thread = threading.Thread(target=increment_counter, daemon=True) | ||
| solve_thread = threading.Thread(target=run_solve) | ||
|
|
||
| solve_thread.start() | ||
| counter_thread.start() | ||
| solve_thread.join(timeout=30) | ||
| assert not solve_thread.is_alive(), "Solve timed out" | ||
|
|
||
| assert counter["n"] > 0, ( | ||
| "Counter did not increment during solve — GIL may not have been released. " | ||
| "Check that casadi was built with WITH_PYTHON_GIL_RELEASE=ON." | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not remove this but rather update the version numbers