In floating license environments, it is possible that all of the tokens are in use. When this happens, no other applications will be able to use Gurobi to solve optimization problems until a token is released.
Checking the availability of tokens from the command line
From the command line, the availability of the token server can be checked with:
gurobi_cl --tokens
However, it is not advisable to parse the output of this command to determine whether a token is available. By the time the output is parsed and the optimization application has started, there may no longer be any tokens available.
Checking the availability of tokens from within an application
Instead of parsing output from gurobi_cl --tokens, applications can interact directly with the token server to determine the availability of tokens.
A Gurobi environment must be created before modeling and solving an optimization problem. When using a client license file that points to a token server, the construction of the Gurobi environment requires communication with the token server to determine if any tokens are available.
- If a token is available, it is issued to the Gurobi environment. This environment can then be used to solve optimization problems. The token remains with this Gurobi environment until the environment is destroyed.
- On the other hand, if no tokens are available when attempting to construct a Gurobi environment, a Gurobi exception is raised and no environment is created.
For example, in gurobipy, a Gurobi environment is created with a call to the Env() constructor:
env = Env('gurobi.log')
If no tokens are available from the token server, a GurobiError exception is raised:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "env.pxi", line 48, in gurobipy.Env.__init__
gurobipy.GurobiError: Request denied: use limit (2) exceeded
To avoid such errors, a simple loop with exception handling can be used. The following code snippet attempts to construct a Gurobi environment once per minute until a token is available. Once a Gurobi environment is successfully created, the environment can be used to build and solve an optimization problem. The code is also using the context manager to avoid having to manually dispose the environment or the model at the end.
import gurobipy as gp
import time
def start_env():
""" Try (forever!) to create an environment. Return the
environment once started. """
while True:
try:
# Attempt to construct a Gurobi environment
env = gp.Env('gurobi.log')
print("Token retrieved!")
return env
except gp.GurobiError:
print("No tokens available...")
# Wait 60 seconds
time.sleep(60)
with start_env() as env, gp.Model(env=env) as model:
try:
# build the model
model.optimize()
except gp.GurobiError as e:
print(e.message)
This is a basic example; improvements include limiting the number of tries to avoid an infinite loop.
Checking the availability of tokens within other modeling frameworks
In other modeling frameworks, similar ideas apply. All modeling frameworks that facilitate the use of Gurobi must at some point construct a Gurobi environment.
Starting with Pyomo 6.6.1, one can pass the environment options directly along with the manage_env flag
options = {"TokenServer": "<SERVER>"} with SolverFactory(
"gurobi", solver_io="python", manage_env=True, options=options
) as opt:
while not opt.available(exception_flag=False):
# Wait 60 seconds
time.sleep(60) opt.solve(model, tee=True)
Note: The token server does not queue requests, and it is not advisable to flood the server with unnecessary network traffic to retrieve the next available token. It may be preferable to increase the number of tokens rather than work around several different applications vying for a limited number of tokens.
Comments
0 comments
Article is closed for comments.