When using Gurobi Remote Services, it is important to take communication overhead into consideration, especially if the Compute Server is not on your local network. There are a couple of mechanisms for efficiently accessing variables and constraints by name in Compute Server and Cloud deployment scenarios.
Accessing constraint names one by one:
Starting with Gurobi v9.0.1, variable and constraint names are cached locally upon invocation of getVars()
and getConstrs()
. In particular with Gurobi v9.0.1 and later, running:
constrs = model.getConstrs()
for c in constrs:
print(c.constrName)
has near-minimal latency. Thus to access constraint attributes using constraint names, the simplest approach as presented is recommended.
Querying all constraint names in one call:
If there is a delay in searching the constraints, adding a query to pull all the constraint names at once:
names = model.getAttr('constrName', model.getConstrs())
then working with the local Cnames
array for accessing the constraint names may reduce the delay.
Storing constraint names during model creation
Another alternative, which applies to earlier Gurobi versions as well, is to build the dictionary for constraint names locally during model creation:
my_constr = {}
my_constr["myConstrName"] = model.addConstr(x + y <= 1, name="myConstrName")
Similar considerations apply to variable names.
Querying by name
Searching model components by name using getVarByName
or getConstrByName
may still result in noticeable communication overhead. There is a great difference because string values (like a variable name) are not cached:
name_single = [v.VarName for v in model.getVars()] # time: 46.20
name_all = model.getAttr('VarName', model.getVars()) # time: 0.05
They are not cached because they usually require a lot of memory (when used). Gurobi does not pay attention to these values internally but only stores them as part of the model. The reason you see a delay here is that every request for an attribute value results in a new request sent to the server.
For this reason, running code such as the following should be avoided in Cloud and Compute Server environments, because it will open a new communication for every call:
for name in names:
x = model.getVarByName(name)
Comments
0 comments
Article is closed for comments.