Skip to content

Commit 241c6e9

Browse files
committed
Document Knitro and improve file loading
1 parent 2337f16 commit 241c6e9

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

doc/release_notes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Release Notes
44
.. Upcoming Version
55
66
* Fix compatibility for xpress versions below 9.6 (regression)
7+
* Add support for the Artelys Knitro solver (via the Knitro Python API)
78
* Performance: Up to 50x faster ``repr()`` for variables/constraints via O(log n) label lookup and direct numpy indexing
89
* Performance: Up to 46x faster ``ncons`` property by replacing ``.flat.labels.unique()`` with direct counting
910

linopy/solvers.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,18 +1775,43 @@ def set_param_by_name(kc: Any, name: str, value: Any) -> None:
17751775
try:
17761776
# Read the problem file
17771777
problem_path = path_to_string(problem_fn)
1778-
if io_api is not None and io_api.startswith("lp"):
1779-
load_fn = getattr(knitro, "KN_load_lp_file", None)
1780-
if load_fn is None:
1781-
msg = "Knitro Python API does not support loading LP files (missing KN_load_lp_file)."
1782-
raise RuntimeError(msg)
1778+
suffix = problem_fn.suffix.lower()
1779+
if suffix == ".lp":
1780+
candidate_loaders = [
1781+
"KN_load_lp_file",
1782+
"KN_load_file",
1783+
"KN_load_mps_file",
1784+
]
1785+
elif suffix == ".mps":
1786+
candidate_loaders = [
1787+
"KN_load_mps_file",
1788+
"KN_load_file",
1789+
"KN_load_lp_file",
1790+
]
17831791
else:
1784-
load_fn = getattr(knitro, "KN_load_mps_file", None)
1792+
candidate_loaders = [
1793+
"KN_load_file",
1794+
"KN_load_mps_file",
1795+
"KN_load_lp_file",
1796+
]
1797+
1798+
last_ret: int | None = None
1799+
for candidate in candidate_loaders:
1800+
load_fn = getattr(knitro, candidate, None)
17851801
if load_fn is None:
1786-
msg = "Knitro Python API does not support loading MPS files (missing KN_load_mps_file)."
1787-
raise RuntimeError(msg)
1802+
continue
1803+
ret_val, _ret_rc = unpack_value_and_rc(load_fn(kc, problem_path))
1804+
last_ret = int(ret_val)
1805+
if last_ret == 0:
1806+
break
1807+
else:
1808+
msg = (
1809+
"Knitro Python API does not expose a suitable file loader for "
1810+
f"{suffix or 'unknown'} problems (tried: {', '.join(candidate_loaders)})."
1811+
)
1812+
raise RuntimeError(msg)
17881813

1789-
ret = int(load_fn(kc, problem_path))
1814+
ret = 0 if last_ret is None else last_ret
17901815
if ret != 0:
17911816
msg = f"Failed to load problem file: Knitro error code {ret}"
17921817
raise RuntimeError(msg)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ solvers = [
8181
"coptpy!=7.2.1",
8282
"xpress; platform_system != 'Darwin' and python_version < '3.11'",
8383
"pyscipopt; platform_system != 'Darwin'",
84+
"knitro"
8485
]
8586

8687
[tool.setuptools.packages.find]

0 commit comments

Comments
 (0)