Gurobi provides WLS license which connects to a global token.gurobi.com server.
If any network issue, there maybe a need to failover to a local token server hosted within the intranet.
Gurobi doesn't seem to recommend specifying different license types in the same .lic file, although you can specify multiple token servers in the .lic file for the FU license.
However, you can do it programmatically by creating an environment that calls WLS first. If it fails, then call the FU licenses and use that.
The solution is using the Gurobi Environment to specify WLS / FU connection, and using try - except to implement the failover.
Example code as below:
import time
import gurobipy as gp
from gurobipy import GRB
class GurobiLicenseFailover:
def __init__(self, wls_config, floating_config):
self.wls = wls_config
self.floating = floating_config
def create_environment(self, max_retries=3, retry_delay=5):
def try_wls():
print("Uing WLS license…")
return gp.Env(params={
'WLSACCESSID': self.wls['WLSACCESSID'],
'WLSSECRET': self.wls['WLSSECRET'],
'LICENSEID': self.wls['LICENSEID'],
})
def try_floating():
print("Trying floating license…")
# Must return the Env object!
return gp.Env(params={
'TokenServer': f"{self.floating['tokenserver']}",
})
# 1) WLS attempts
for i in range(1, max_retries+1):
try:
env = try_wls()
return env, 'wls', None # <— return on success
except gp.GurobiError as e:
print(f" WLS attempt {i} failed: {e}")
time.sleep(retry_delay)
# 2) Fallback to floating once
try:
env = try_floating()
return env, 'floating', None # <— return on success
except gp.GurobiError as e:
# If you raise here, you won’t hit a None return
raise RuntimeError("Both WLS and floating license acquisition failed.") from e
def cleanup_environment(self, env, license_type, cleanup_info):
env.dispose()
print(f"Cleaned up {license_type} environment")
This Python script automates “try WLS license → if that fails, use FU license,” then runs your optimization and reliably cleans up the environment afterward.
To use it, you can pass your WLS and FU license credentials to the GurobiLicenseFailover() and it will try to connect to WLS. Since it might be a networking issue blocking access to the WLS server, trying several times before switching to the FU could make sense. In addition, this applies if the license has expired or the credentials are incorrect. So, you have to be careful.
In this example, you will try 3 times with a 5-second delay between each.
wls_license = {
'WLSACCESSID': 'XXX',
'WLSSECRET': 'XXX',
'LICENSEID': XXX,
}
floating_license = {
'tokenserver': 'LOCAL_TOKEN_SERVER',
}
# Instantiate properly (no-arg call removed!)
failover = GurobiLicenseFailover(wls_license, floating_license)
env, license_type, cleanup_info = failover.create_environment(
max_retries=3, # you can decide the number of trials
retry_delay=5 # time gap between each two trials
)
try:
print(f"Using license type: {license_type}")
with gp.Model(env=env) as model:
# A simple optimization
x = model.addVar(vtype=GRB.INTEGER, name="x")
y = model.addVar(vtype=GRB.INTEGER, name="y")
model.setObjective(3 * x + 4 * y, GRB.MAXIMIZE)
model.addConstr(x + 2 * y <= 14, "c0")
model.addConstr(3 * x - y >= 0, "c1")
model.addConstr(x - y <= 2, "c2")
model.optimize()
if model.status == GRB.OPTIMAL:
print(f"Optimal objective value: {model.ObjVal}")
print(f"x = {x.X}")
print(f"y = {y.X}")
else:
print("No optimal solution found.")
finally:
failover.cleanup_environment(env, license_type, cleanup_info)
A more concise version. thanks to Gurobi tech support.
import time
import gurobipy as gp
WLS_params = {
'WLSACCESSID': "...",
'WLSSECRET': "...",
'LICENSEID': 0,
}
FU_params = {
'TokenServer': "...",
'ServerPassword': "...",
}
def polling_env(env_params_list, delay=1):
for env_params in env_params_list:
try:
env = gp.Env(params=env_params)
return env
except gp.GurobiError:
# log error
...
time.sleep(delay)
raise RuntimeError(f"Failed to start environment after {len(env_params_list)} attempts.")
# 3 tries with WLS, then switch to token server. Attempts 10 seconds apart.
with polling_env([WLS_params]*3 + [FU_params], delay=10) as env, gp.Model(env=env):
...