Global Functions#
Gurobi global functions. 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 statementdel m
. A call todisposeDefaultEnv
prints the messageFreeing 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 then
individual Gurobi tuple dictionaries (stored astupledict
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
orQuadExpr
objects). The function takes a list of terms as its argument.Note that while
quicksum
is much faster thansum
, it isn’t the fastest approach for building a large expression. UseaddTerms
or theLinExpr()
constructor if you want the quickest possible expression construction.
- 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.zip
,.gz
,.bz2
,.7z
or.xz
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 the parameter that you would like to modify. The case of
paramname
is ignored, as are underscores.newvalue – Desired new value for the parameter.
- Example:
gp.setParam("Cuts", 2)
Deprecated since version 12.0: Wildcard name matching using
'*'
and'?'
inparamname
is deprecated. Passing'default'
asnewvalue
is deprecated.