Concepts
Features
Reference
Gurobi environment object. Note that environments play a much smallerrole in the Python interface than they do in other Gurobi language APIs,mainly because the Python interface has a default environment. Unlessyou explicitly pass your own environment to routines that require anenvironment, the default environment will be used.
The primary situations where you will want to use your own environmentare:
When you are using a Gurobi Compute Server and want to choose theserver from within your program.
When you need control over garbage collection of your environment.The Gurobi Python interface maintains a reference to the defaultenvironment, so by default it will never be garbage collected. Bycreating your own environment, you can control exactly when yourprogram releases any licensing tokens or Compute Servers it is using.
When you are usingconcurrentenvironments in one oftheconcurrent optimizers.
It is good practice to use thewith keyword when dealing withenvironment (and model) objects. That way the resources tied to theseobjects are properly released even if an exception is raised at somepoint. The following example illustrates two typical use patterns.
importgurobipyasgpwithgp.Env("gurobi.log")asenv,gp.Model(env=env)asmodel:# Populate model object here...model.optimize()withgp.Env(empty=True)asenv:env.setParam("ComputeServer","myserver1:32123")env.setParam("ServerPassword","pass")env.start()withgp.Model(env=env)asmodel:# Populate model object here...model.optimize()
Note that you can manually remove the reference to the defaultenvironment by callingdisposeDefaultEnv. After calling this,and after all models built within the default environment are eitherexplicitly closed or garbagecollected, the default environment will be freed. Anew default environment will be created automatically if you call aroutine that needs one.
Env constructor. Creating your own environment gives you explicitcontrol over cleanup of Gurobi resources. By creating your ownenvironment object and always passing it to methods that take anenvironment as input (read or theModelconstructor), you will avoid creating the default environment. Onceevery model created using an Env object is garbage collected, and oncethe Env object itself is no longer referenced, the garbage collectorwill reclaim the environment and release all associated resources.
If the environment is not empty, This method will also populate anyparameter (ComputeServer,TokenServer,ServerPassword, etc.) specified in yourgurobi.licfile. This method will also check the current working directory for afile namedgurobi.env, and it will attempt to read parametersettings from this file if it exists. The file should be inPRM format (briefly, each line should contain a parameter name,followed by the desired value for that parameter).
In general, you should aim to create asingle Gurobi environment in your program, even if you plan to work withmultiple models. Reusing one environment is much more efficient thancreating and destroying multiple environments. The one exception is ifyou are writing a multi-threaded program, since environments are notthread safe. In this case, you will need a separate environment for eachof your threads.
logfilename – Name of the log file for this environment. Pass anempty string if you don’t want a log file.
empty – Indicates whether the environment should be empty. Youshould useempty=True if you want to set parameters before actuallystarting the environment. This can be useful if you want to connect to aCompute Server, a Token Server, the Gurobi Instant Cloud, a ClusterManager or use a WLS license. See theEnvironment Section for more details.
params – A dict containing Gurobi parameter/value pairs that shouldbe set already upon environment creation. Any server related parameterscan be set through this dict, too.
New environment object.
env=gp.Env("gurobi.log")model=gp.read("misc07.mps",env)model.optimize()
p={"ComputeServer":"localhost:33322","ServerPassword":"pass","TimeLimit":120.0}withgp.Env(params=p)asenv,gp.read('misc07.mps',env=env)asmodel:model.optimize()
Free all resources associated with thisEnv object. This method is asynonym fordispose.
Users should close all models created in this environment before closingthisEnv object.
After this method is called, thisEnv object must no longer be used.
env=gp.Env()model=gp.read("misc07.mps",env)model.optimize()model.close()env.close()
Free all resources associated with this Env object. This method is asynonym forclose.
Users should dispose of all models created in this environment beforedisposing of this Env object.
After this method is called, this Env object must no longer be used.
env=gp.Env()model=gp.read("misc07.mps",env)model.optimize()model.dispose()env.dispose()
Get the current value of a parameter.
paramname – String containing the name of the parameter that youwould like to query. The case ofparamname is ignored, as areunderscores.
Current value of the parameter in this environment.
Reset the values of all parameters to their default values.
env.resetParams()
Set a parameter to a new value.
paramname – String containing the name of the parameter that youwould like to modify. The case ofparamname is ignored, as areunderscores.
newvalue – Desired new value for the parameter.
Note
Note that a model gets its own copy of the environment when it is created.Changes to the original environment have no effect on the copy, and viceversa. UseModel.setParam to change a parameter on an existingmodel.
env.setParam("Cuts",2)
Deprecated since version 12.0:Wildcard name matching using'*' and'?' inparamname isdeprecated. Passing'default' asnewvalue is deprecated.
Start an empty environment. If the environment has already been started,this method will do nothing. If the call fails, the environment willhave the same state as it had before the call to this method.
This method will also populate any parameter(ComputeServer,TokenServer,ServerPassword, etc.) specified in yourgurobi.licfile. This method will also check the current working directory for afile namedgurobi.env, and it will attempt to read parametersettings from this file if it exists. The file should be inPRM format (briefly, each line should contain a parameter name,followed by the desired value for that parameter). After that, it willapply all parameter changes specified by the user prior to this call.Note that this might overwrite parameters set in the license file, or inthegurobi.env file, if present.
After all these changes are performed, the code will actually activatethe environment, and make it ready to work with models.
In general, you should aim to create asingle Gurobi environment in your program, even if you plan to work withmultiple models. Reusing one environment is much more efficient thancreating and destroying multiple environments. The one exception is ifyou are writing a multi-threaded program, since environments are notthread safe. In this case, you will need a separate environment for eachof your threads.
env=gp.Env(empty=True)env.setParam('ComputeServer','server.mydomain.com:61000')env.setParam('ServerPassword','mypassword')env.start()
Help and Feedback