Skip to content

Commit b14208f

Browse files
FabianHofmannulfworsoepre-commit-ci[bot]lkstrp
authored
MOSEK: Remove explicit use of mosek.Env (refactored) (#488)
* MOSEK: Remove use of Env * release note for MOSEK * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * WIP * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix broken call in mosek setup * Improve deprecation warnings and docstrings for MOSEK env parameter --------- Co-authored-by: ulfw <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Lukas Trippe <[email protected]>
1 parent 8266b8b commit b14208f

File tree

2 files changed

+54
-45
lines changed

2 files changed

+54
-45
lines changed

doc/release_notes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Version 0.5.6
1010

1111
* Improved variable/expression arithmetic methods so that they correctly handle types
1212
* Gurobi: Pass dictionary as env argument `env={...}` through to gurobi env creation
13+
* Mosek: Remove explicit use of Env, use global env instead
1314

1415
**Breaking Changes**
1516

linopy/solvers.py

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import re
1414
import subprocess as sub
1515
import sys
16+
import warnings
1617
from abc import ABC, abstractmethod
1718
from collections import namedtuple
1819
from collections.abc import Callable, Generator
@@ -100,10 +101,8 @@
100101
import mosek
101102

102103
with contextlib.suppress(mosek.Error):
103-
with mosek.Env() as m:
104-
t = m.Task()
105-
t.optimize()
106-
m.checkinall()
104+
t = mosek.Task()
105+
t.optimize()
107106

108107
available_solvers.append("mosek")
109108

@@ -1661,33 +1660,37 @@ def solve_problem_from_model(
16611660
Path to the warmstart file.
16621661
basis_fn : Path, optional
16631662
Path to the basis file.
1664-
env : None, optional
1665-
Mosek environment for the solver
1663+
env : None, optional, deprecated
1664+
Deprecated. This parameter is ignored. MOSEK now uses the global
1665+
environment automatically. Will be removed in a future version.
16661666
explicit_coordinate_names : bool, optional
16671667
Transfer variable and constraint names to the solver (default: False)
16681668
16691669
Returns
16701670
-------
16711671
Result
16721672
"""
1673-
with contextlib.ExitStack() as stack:
1674-
if env is None:
1675-
env_ = stack.enter_context(mosek.Env())
16761673

1677-
with env_.Task() as m:
1678-
m = model.to_mosek(
1679-
m, explicit_coordinate_names=explicit_coordinate_names
1680-
)
1674+
if env is not None:
1675+
warnings.warn(
1676+
"The 'env' parameter in solve_problem_from_model is deprecated and will be "
1677+
"removed in a future version. MOSEK now uses the global environment "
1678+
"automatically, avoiding unnecessary license checkouts.",
1679+
DeprecationWarning,
1680+
stacklevel=2,
1681+
)
1682+
with mosek.Task() as m:
1683+
m = model.to_mosek(m, explicit_coordinate_names=explicit_coordinate_names)
16811684

1682-
return self._solve(
1683-
m,
1684-
solution_fn=solution_fn,
1685-
log_fn=log_fn,
1686-
warmstart_fn=warmstart_fn,
1687-
basis_fn=basis_fn,
1688-
io_api="direct",
1689-
sense=model.sense,
1690-
)
1685+
return self._solve(
1686+
m,
1687+
solution_fn=solution_fn,
1688+
log_fn=log_fn,
1689+
warmstart_fn=warmstart_fn,
1690+
basis_fn=basis_fn,
1691+
io_api="direct",
1692+
sense=model.sense,
1693+
)
16911694

16921695
def solve_problem_from_file(
16931696
self,
@@ -1714,34 +1717,39 @@ def solve_problem_from_file(
17141717
Path to the warmstart file.
17151718
basis_fn : Path, optional
17161719
Path to the basis file.
1717-
env : None, optional
1718-
Mosek environment for the solver
1720+
env : None, optional, deprecated
1721+
Deprecated. This parameter is ignored. MOSEK now uses the global
1722+
environment automatically. Will be removed in a future version.
17191723
17201724
Returns
17211725
-------
17221726
Result
17231727
"""
1724-
with contextlib.ExitStack() as stack:
1725-
if env is None:
1726-
env_ = stack.enter_context(mosek.Env())
1727-
1728-
with env_.Task() as m:
1729-
# read sense and io_api from problem file
1730-
sense = read_sense_from_problem_file(problem_fn)
1731-
io_api = read_io_api_from_problem_file(problem_fn)
1732-
# for Mosek solver, the path needs to be a string
1733-
problem_fn_ = path_to_string(problem_fn)
1734-
m.readdata(problem_fn_)
1735-
1736-
return self._solve(
1737-
m,
1738-
solution_fn=solution_fn,
1739-
log_fn=log_fn,
1740-
warmstart_fn=warmstart_fn,
1741-
basis_fn=basis_fn,
1742-
io_api=io_api,
1743-
sense=sense,
1744-
)
1728+
if env is not None:
1729+
warnings.warn(
1730+
"The 'env' parameter in solve_problem_from_file is deprecated and will be "
1731+
"removed in a future version. MOSEK now uses the global environment "
1732+
"automatically, avoiding unnecessary license checkouts.",
1733+
DeprecationWarning,
1734+
stacklevel=2,
1735+
)
1736+
with mosek.Task() as m:
1737+
# read sense and io_api from problem file
1738+
sense = read_sense_from_problem_file(problem_fn)
1739+
io_api = read_io_api_from_problem_file(problem_fn)
1740+
# for Mosek solver, the path needs to be a string
1741+
problem_fn_ = path_to_string(problem_fn)
1742+
m.readdata(problem_fn_)
1743+
1744+
return self._solve(
1745+
m,
1746+
solution_fn=solution_fn,
1747+
log_fn=log_fn,
1748+
warmstart_fn=warmstart_fn,
1749+
basis_fn=basis_fn,
1750+
io_api=io_api,
1751+
sense=sense,
1752+
)
17451753

17461754
def _solve(
17471755
self,

0 commit comments

Comments
 (0)