Skip to content
Closed
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 @@ -10,6 +10,7 @@ Version 0.5.6

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

**Breaking Changes**

Expand Down
88 changes: 43 additions & 45 deletions linopy/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import re
import subprocess as sub
import sys
import warnings
from abc import ABC, abstractmethod
from collections import namedtuple
from collections.abc import Callable, Generator
Expand Down Expand Up @@ -100,10 +101,8 @@
import mosek

with contextlib.suppress(mosek.Error):
with mosek.Env() as m:
t = m.Task()
t.optimize()
m.checkinall()
t = mosek.Task()
t.optimize()

available_solvers.append("mosek")

Expand Down Expand Up @@ -1661,33 +1660,32 @@ def solve_problem_from_model(
Path to the warmstart file.
basis_fn : Path, optional
Path to the basis file.
env : None, optional
Mosek environment for the solver
env : None, optional, deprecated
Ignored.
explicit_coordinate_names : bool, optional
Transfer variable and constraint names to the solver (default: False)

Returns
-------
Result
"""
with contextlib.ExitStack() as stack:
if env is None:
env_ = stack.enter_context(mosek.Env())

with env_.Task() as m:
m = model.to_mosek(
m, explicit_coordinate_names=explicit_coordinate_names
)
if env is not None:
warnings.warn(
"Argument 'env' in solve_problem_from_model", DeprecationWarning
)
with mosek.Task() as m:
m = model.to_mosek(m, explicit_coordinate_names=explicit_coordinate_names)

return self._solve(
m,
solution_fn=solution_fn,
log_fn=log_fn,
warmstart_fn=warmstart_fn,
basis_fn=basis_fn,
io_api="direct",
sense=model.sense,
)
return self._solve(
m,
solution_fn=solution_fn,
log_fn=log_fn,
warmstart_fn=warmstart_fn,
basis_fn=basis_fn,
io_api="direct",
sense=model.sense,
)

def solve_problem_from_file(
self,
Expand All @@ -1714,34 +1712,34 @@ def solve_problem_from_file(
Path to the warmstart file.
basis_fn : Path, optional
Path to the basis file.
env : None, optional
Mosek environment for the solver
env : None, optional, deprecated.
Ignored.

Returns
-------
Result
"""
with contextlib.ExitStack() as stack:
if env is None:
env_ = stack.enter_context(mosek.Env())

with env_.Task() as m:
# read sense and io_api from problem file
sense = read_sense_from_problem_file(problem_fn)
io_api = read_io_api_from_problem_file(problem_fn)
# for Mosek solver, the path needs to be a string
problem_fn_ = path_to_string(problem_fn)
m.readdata(problem_fn_)

return self._solve(
m,
solution_fn=solution_fn,
log_fn=log_fn,
warmstart_fn=warmstart_fn,
basis_fn=basis_fn,
io_api=io_api,
sense=sense,
)
if env is not None:
warnings.warn(
"Argument 'env' in solve_problem_from_model", DeprecationWarning
)
with mosek.Task() as m:
# read sense and io_api from problem file
sense = read_sense_from_problem_file(problem_fn)
io_api = read_io_api_from_problem_file(problem_fn)
# for Mosek solver, the path needs to be a string
problem_fn_ = path_to_string(problem_fn)
m.readdata(problem_fn_)

return self._solve(
m,
solution_fn=solution_fn,
log_fn=log_fn,
warmstart_fn=warmstart_fn,
basis_fn=basis_fn,
io_api=io_api,
sense=sense,
)

def _solve(
self,
Expand Down
Loading