GRBEnv#

GRBEnv#

Gurobi environment object. Gurobi models are always associated with an environment. You must create an environment before you can create and populate a model. You will generally only need a single environment object in your program.

Objects of this class have unmanaged resources associated with them. The class implements the IDisposable interface.

The methods on environment objects are mainly used to manage Gurobi parameters (e.g., Get, GetParamInfo, Set).

While the .NET garbage collector will eventually collect an unused GRBEnv object, an environment will hold onto resources (Gurobi licenses, file descriptors, etc.) until that collection occurs. If your program creates multiple GRBEnv objects, we recommend that you call GRBEnv.Dispose when you are done using one (or use the .NET using statement).

GRBEnv GRBEnv()#

Constructor for GRBEnv object that creates a Gurobi environment (with logging disabled). This method will also populate any parameter (ComputeServer, TokenServer, ServerPassword, etc.) specified in your gurobi.lic file. This method will also check the current working directory for a file named gurobi.env, and it will attempt to read parameter settings from this file if it exists. The file should be in PRM format (briefly, each line should contain a parameter name, followed by the desired value for that parameter).

In general, you should aim to create a single Gurobi environment in your program, even if you plan to work with multiple models. Reusing one environment is much more efficient than creating and destroying multiple environments. The one exception is if you are writing a multi-threaded program, since environments are not thread safe. In this case, you will need a separate environment for each of your threads.

Returns:

An environment object (with no associated log file).

GRBEnv GRBEnv(string logFileName)#

Constructor for GRBEnv object that creates a Gurobi environment (with logging enabled). This method will also populate any parameter (ComputeServer, TokenServer, ServerPassword, etc.) specified in your gurobi.lic file. This method will also check the current working directory for a file named gurobi.env, and it will attempt to read parameter settings from this file if it exists. The file should be in PRM format (briefly, each line should contain a parameter name, followed by the desired value for that parameter).

In general, you should aim to create a single Gurobi environment in your program, even if you plan to work with multiple models. Reusing one environment is much more efficient than creating and destroying multiple environments. The one exception is if you are writing a multi-threaded program, since environments are not thread safe. In this case, you will need a separate environment for each of your threads.

Parameters:

logFileName – The desired log file name.

Returns:

An environment object.

GRBEnv GRBEnv(bool empty)#

Constructor for GRBEnv object. If empty=true, creates an empty Gurobi environment. Use Start to start the environment. If empty=false, the result is the same as providing no arguments to the constructor.

In general, you should aim to create a single Gurobi environment in your program, even if you plan to work with multiple models. Reusing one environment is much more efficient than creating and destroying multiple environments. The one exception is if you are writing a multi-threaded program, since environments are not thread safe. In this case, you will need a separate environment for each of your threads.

Parameters:

empty – Indicates whether the environment should be empty. You should use empty=true if you want to set parameters before actually starting the environment. This can be useful if you want to connect to a Compute Server, a Token Server, the Gurobi Instant Cloud, a Cluster Manager or use a WLS license. See the Environment Section for more details.

Returns:

An environment object.

void Dispose()#

Release the resources associated with a GRBEnv object. While the .NET garbage collector will eventually reclaim these resources, we recommend that you call the Dispose method when you are done using an environment if your program creates more than one.

The Dispose method on a GRBEnv should be called only after you have called Dispose on all of the models that were created within that environment. You should not attempt to use a GRBEnv object after calling Dispose.

string ErrorMsg#

(Property) The error message for the most recent exception associated with this environment.

double Get(GRB.DoubleParam param)#

Query the value of a double-valued parameter.

Parameters:

param – The parameter being queried. Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

Returns:

The current value of the requested parameter.

int Get(GRB.IntParam param)#

Query the value of an int-valued parameter.

Parameters:

param – The parameter being queried. Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

Returns:

The current value of the requested parameter.

string Get(GRB.StringParam param)#

Query the value of a string-valued parameter.

Parameters:

param – The parameter being queried. Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

Returns:

The current value of the requested parameter.

void GetParamInfo(GRB.DoubleParam param, double[] info)#

Obtain detailed information about a double parameter.

Parameters:
  • param – The parameter of interest. Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

  • info – The returned information. The result will contain four entries: the current value of the parameter, the minimum allowed value, the maximum allowed value, and the default value.

void GetParamInfo(GRB.IntParam param, int[] info)#

Obtain detailed information about an integer parameter.

Parameters:
  • param – The parameter of interest. Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

  • info – The returned information. The result will contain four entries: the current value of the parameter, the minimum allowed value, the maximum allowed value, and the default value.

void GetParamInfo(GRB.StringParam param, string[] info)#

Obtain detailed information about a string parameter.

Parameters:
  • param – The parameter of interest. Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

  • info – The returned information. The result will contain two entries: the current value of the parameter and the default value.

void Message(string message)#

Write a message to the console and the log file.

Parameters:

message – Print a message to the console and to the log file. Note that this call has no effect unless the OutputFlag parameter is set.

void ReadParams(string paramfile)#

Read new parameter settings from a file.

Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

Parameters should be listed one per line, with the parameter name first and the desired value second. For example:

# Gurobi parameter file
Threads 1
MIPGap 0

Blank lines and lines that begin with the hash symbol are ignored.

Parameters:

paramfile – Name of the file containing parameter settings.

void Release()#

Release the license associated with this environment. You will no longer be able to call Optimize on models created with this environment after the license has been released.

void ResetParams()#

Reset all parameters to their default values.

Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

void Set(GRB.DoubleParam param, double newvalue)#

Set the value of a double-valued parameter.

Parameters:
  • param – The parameter being modified. Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

  • newvalue – The desired new value of 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 vice versa. Use the appropriate version of the overloaded method GRBModel.Set to change a parameter on an existing model.

void Set(GRB.IntParam param, int newvalue)#

Set the value of an int-valued parameter.

Parameters:
  • param – The parameter being modified. Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

  • newvalue – The desired new value of 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 vice versa. Use the appropriate version of the overloaded method GRBModel.Set to change a parameter on an existing model.

void Set(GRB.StringParam param, string newvalue)#

Set the value of a string-valued parameter.

Parameters:
  • param – The parameter being modified. Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

  • newvalue – The desired new value of 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 vice versa. Use the appropriate version of the overloaded method GRBModel.Set to change a parameter on an existing model.

void Set(string param, string newvalue)#

Set the value of any parameter using strings alone.

Parameters:
  • param – The name of the parameter being modified. Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

  • newvalue – The desired new value of 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 vice versa. Use the appropriate version of the overloaded method GRBModel.Set to change a parameter on an existing model.

void Start()#

Start an empty environment. If the environment has already been started, this method will do nothing. If the call fails, the environment will have 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 your gurobi.lic file. This method will also check the current working directory for a file named gurobi.env, and it will attempt to read parameter settings from this file if it exists. The file should be in PRM format (briefly, each line should contain a parameter name, followed by the desired value for that parameter). After that, it will apply all parameter changes specified by the user prior to this call. Note that this might overwrite parameters set in the license file, or in the gurobi.env file, if present.

After all these changes are performed, the code will actually activate the environment, and make it ready to work with models.

In general, you should aim to create a single Gurobi environment in your program, even if you plan to work with multiple models. Reusing one environment is much more efficient than creating and destroying multiple environments. The one exception is if you are writing a multi-threaded program, since environments are not thread safe. In this case, you will need a separate environment for each of your threads.

Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

void WriteParams(string paramfile)#

Write all non-default parameter settings to a file.

Please consult the parameter section for a complete list of Gurobi parameters, including descriptions of their purposes and their minimum, maximum, and default values.

Parameters:

paramfile – Name of the file to which non-default parameter settings should be written. The previous contents are overwritten.