Python API - Global Functions#

Gurobi global functions. These functions can be accessed from the main Gurobi shell prompt. In contrast to all other methods in the Gurobi Python interface, these functions do not require a Gurobi object to invoke them.

disposeDefaultEnv()#

Dispose of the default environment.

Calling this function releases the default environment created by the Gurobi Python module. This function is particularly useful in a long-running Python session (e.g., within a Jupyter notebook), where the Gurobi environment would otherwise continue to exist for the full duration of the session.

Note that models built with the default environment must be garbage collected before the default environment can be freed. You can force a model m to be garbage collected with the statement del m. A call to disposeDefaultEnv prints the message

Freeing default Gurobi environment

and then attempts to free the default environment. Note however that this attempt may fail if some models referencing the default environment have not yet been disposed of. If that is the case, the default environment will be automatically freed when the last model referencing it is disposed of.

Example:
gp.disposeDefaultEnv()
multidict(data)#

This function splits a single dictionary into multiple dictionaries. The input dictionary should map each key to a list of n values. The function returns a list of the shared keys as its first result, followed by the n individual Gurobi tuple dictionaries (stored as tupledict objects).

Parameters:

data – A Python dictionary. Each key should map to a list of values.

Returns:

A list, where the first member contains the shared key values, and the following members contain the dictionaries that result from splitting the value lists from the input dictionary.

Example:
keys, dict1, dict2 = gp.multidict( {
  'key1': [1, 2],
  'key2': [1, 3],
  'key3': [1, 4] } )
paramHelp(paramname)#

Obtain help about a Gurobi parameter.

Parameters:

paramname – String containing the name of parameter that you would like help with. The name can include ‘*’ and ‘?’ wildcards. If more than one parameter matches, the matching names are listed. Note that case is ignored.

Example:
gp.paramHelp("Cuts")
gp.paramHelp("Heu*")
gp.paramHelp("*cuts")
quicksum(data)#

A version of the Python sum function that is much more efficient for building large Gurobi expressions (LinExpr or QuadExpr objects). The function takes a list of terms as its argument.

Note that while quicksum is much faster than sum, it isn’t the fastest approach for building a large expression. Use addTerms or the LinExpr() constructor if you want the quickest possible expression construction.

Parameters:

data – List of terms to add. The terms can be constants, Var objects, LinExpr objects, or QuadExpr objects.

Returns:

An expression that represents the sum of the terms in the input list.

Example:
expr = gp.quicksum([2*x, 3*y+1, 4*z*z])
expr = gp.quicksum(model.getVars())
read(filename, env=defaultEnv)#

Read a model from a file.

Parameters:
  • filename – Name of file containing model. Note that the type of the file is encoded in the file name suffix. Valid suffixes are .mps, .rew, .lp, .rlp, .dua, .dlp, .ilp, or .opb. The files can be compressed, so additional suffixes of .gz, .bz2, .zip, or .7z are accepted. The file name may contain * or ? wildcards. No file is read when no wildcard match is found. If more than one match is found, this routine will attempt to read the first matching file.

  • env – Environment in which to create the model. Creating your environment (using the Env constructor) gives you more control over Gurobi licensing, but it can make your program more complex. Use the default environment unless you know that you need to control your own environments.

Returns:

Model object containing the model that was read from the input file.

Example:
m = gp.read("afiro.mps")
m.optimize()
readParams(filename)#

Read a set of parameter settings from a file. The file name must end in .prm, and the file must be in PRM format.

Parameters:

filename – Name of file containing parameter settings.

Example:
gp.readParams("params.prm")
resetParams()#

Reset the values of all parameters to their default values. Note that existing models that are stored inside Python data structures (lists, dictionaries, etc.), or inside user classes aren’t affected.

Example:
gp.resetParams()
setParam(paramname, newvalue)#

Set the value of a parameter to a new value. Note that existing models that are stored inside Python data structures (lists, dictionaries, etc.), or inside user classes aren’t affected.

Parameters:
  • paramname – String containing the name of parameter that you would like to modify. The name can include ‘*’ and ‘?’ wildcards. If more than one parameter matches, the matching names are listed and none are modified. Note that case is ignored.

  • newvalue – Desired new value for parameter. Can be ‘default’, which indicates that the parameter should be reset to its default value.

Example:
gp.setParam("Cuts", 2)
gp.setParam("Heu*", 0.5)
setParam("*Interval", 10)
writeParams(filename)#

Write all modified parameters to a file. The file is written in PRM format.

Example:
gp.setParam("Heu*", 0.5)
gp.writeParams("params.prm")  # file will contain changed parameter
gp.system("cat params.prm")