Skip to content

Improve run_gurobi to wait for available token #281

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Version 0.3.9

* Up to now the `rhs` argument in the `add_constraints` function was not supporting an expression as an input type. This is now added.

* Improve `run_gurobi` to optionally wait for an available token.

* Linopy now supports python 3.12.

**Deprecations**
Expand Down
27 changes: 25 additions & 2 deletions linopy/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,25 @@
return Result(status, solution, m)


def start_gurobi_env(wait=60):
"""
Try (forever!) to create an environment. Return the environment once started. Waiting time is in seconds.
"""
# see https://support.gurobi.com/hc/en-us/articles/360029879251-How-do-I-check-the-availability-of-floating-license-tokens
import time

Check warning on line 591 in linopy/solvers.py

View check run for this annotation

Codecov / codecov/patch

linopy/solvers.py#L591

Added line #L591 was not covered by tests

while True:
try:

Check warning on line 594 in linopy/solvers.py

View check run for this annotation

Codecov / codecov/patch

linopy/solvers.py#L593-L594

Added lines #L593 - L594 were not covered by tests
# Attempt to construct a Gurobi environment
env = gurobipy.Env()
logging.debug("Gurobi: Token retrieved!")
return env
except gurobipy.GurobiError:
logging.debug("Gurobi: No tokens available...")

Check warning on line 600 in linopy/solvers.py

View check run for this annotation

Codecov / codecov/patch

linopy/solvers.py#L596-L600

Added lines #L596 - L600 were not covered by tests
# Wait x seconds
time.sleep(wait)

Check warning on line 602 in linopy/solvers.py

View check run for this annotation

Codecov / codecov/patch

linopy/solvers.py#L602

Added line #L602 was not covered by tests


def run_gurobi(
model,
io_api=None,
Expand Down Expand Up @@ -627,7 +646,10 @@

with contextlib.ExitStack() as stack:
if env is None:
env = stack.enter_context(gurobipy.Env())
if solver_options.get("WaitTime"):
env = stack.enter_context(start_gurobi_env(solver_options["WaitTime"]))

Check warning on line 650 in linopy/solvers.py

View check run for this annotation

Codecov / codecov/patch

linopy/solvers.py#L650

Added line #L650 was not covered by tests
else:
env = stack.enter_context(gurobipy.Env())

if io_api is None or io_api in FILE_IO_APIS:
problem_fn = model.to_file(problem_fn, io_api=io_api)
Expand All @@ -643,7 +665,8 @@

if solver_options is not None:
for key, value in solver_options.items():
m.setParam(key, value)
if key != "WaitTime":
m.setParam(key, value)
if log_fn is not None:
m.setParam("logfile", log_fn)

Expand Down
Loading