Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Upcoming Version
* Add ``linopy.breakpoints()`` factory for convenient breakpoint construction from lists, dicts, or keyword arguments. Includes ``breakpoints.segments()`` for disjunctive formulations.
* Add the `sphinx-copybutton` to the documentation
* Add SOS1 and SOS2 reformulations for solvers not supporting them.
* Expose the knitro context to allow for more flexible use of the knitro python API.


Version 0.6.4
Expand Down
18 changes: 13 additions & 5 deletions linopy/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,7 @@ def get_solver_solution() -> Solution:
return Result(status, solution, m)


KnitroResult = namedtuple("KnitroResult", "reported_runtime")
KnitroResult = namedtuple("KnitroResult", "knitro_context reported_runtime")


class Knitro(Solver[None]):
Expand Down Expand Up @@ -1808,7 +1808,13 @@ def _extract_values(
if n == 0:
return pd.Series(dtype=float)

values = get_values_fn(kc, n - 1)
try:
# Compatible with KNITRO >= 15
values = get_values_fn(kc)
except TypeError:
# Fallback for older wrappers requiring explicit indices
values = get_values_fn(kc, list(range(n)))

names = list(get_names_fn(kc))
return pd.Series(values, index=names, dtype=float)

Expand Down Expand Up @@ -1931,12 +1937,14 @@ def get_solver_solution() -> Solution:
knitro.KN_write_mps_file(kc, path_to_string(solution_fn))

return Result(
status, solution, KnitroResult(reported_runtime=reported_runtime)
status,
solution,
KnitroResult(knitro_context=kc, reported_runtime=reported_runtime),
)

finally:
with contextlib.suppress(Exception):
knitro.KN_free(kc)
# Intentionally keep the Knitro context alive; do not free `kc` here.
pass


mosek_bas_re = re.compile(r" (XL|XU)\s+([^ \t]+)\s+([^ \t]+)| (LL|UL|BS)\s+([^ \t]+)")
Expand Down