Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark mode
Gurobi Optimizer Reference Manual
Light LogoDark Logo

Concepts

Features

Reference

Gurobi
Back to top

GRBEnv#

GRBEnv#

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

The methods on environment objects are mainly used to manage Gurobiparameters (e.g.,get,getParamInfo,set).

While the Java garbage collector will eventually collect an unusedGRBEnv object, an environment will hold onto resources (Gurobilicenses, file descriptors, etc.) until that collection occurs. If yourprogram creates multipleGRBEnv objects, we recommend that you callGRBEnv.dispose when you are done using one.

GRBEnvGRBEnv()#

Constructor forGRBEnv object that creates a Gurobi environment (withlogging disabled). 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 parameter settingsfrom this file if it exists. The file should be inPRMformat (briefly, each line should contain a parameter name, followed bythe 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.

Return value:

An environment object (with no associated log file).

Example:
// Create environmentGRBEnvenv=newGRBEnv();
GRBEnvGRBEnv(booleanempty)#

Constructor forGRBEnv object. Ifempty=true, creates anempty environment. Usestart to startthe environment. Ifempty=false, the result is the same asproviding no arguments to the constructor.

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.

Arguments:

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

Return value:

An environment object.

Example:
// Create empty environmentGRBEnvenv=newGRBEnv(true);// Populate Compute Server parametersenv.set("ComputeServer","server1:61000");env.set("ServerPassword","passwd");// Start the environmentenv.start();
GRBEnvGRBEnv(StringlogFileName)#

Constructor forGRBEnv object that creates a Gurobi environment(with logging enabled). This method willalso populate any parameter (ComputeServer,TokenServer,ServerPassword, etc.)specified in yourgurobi.lic file. This method will also check thecurrent working directory for a file namedgurobi.env, and it willattempt to read parameter settings from this file if it exists. The fileshould be inPRM format (briefly, each line shouldcontain a parameter name, followed by the desired value for thatparameter).

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.

Arguments:

logFileName – The desired log file name.

Return value:

An environment object.

Example:
// Create environment with associated log fileGRBEnvenv=newGRBEnv("gurobi.log");
voiddispose()#

Release the resources associated with aGRBEnv object. While theJava garbage collector will eventually reclaim these resources, werecommend that you call thedispose method when you are done usingan environment if your program creates more than one.

Thedispose method on aGRBEnv should be called only after youhave calleddispose on all of the models that were created withinthat environment. You should not attempt to use aGRBEnv objectafter callingdispose.

Example:
// Create environmentGRBEnvenv=newGRBEnv();// ...// Clean up environmentenv.dispose()
doubleget(GRB.DoubleParamparam)#
intget(GRB.IntParamparam)#
Stringget(GRB.StringParamparam)#

Query the value of a parameter.

Arguments:

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

Return value:

The current value of the requested parameter.

Example:
// Query value of TimeLimit parameterdoubletimelimit=env.get(GRB.DoubleParam.TimeLimit);// Query value of PumpPasses parameterintpumppasses=env.get(GRB.IntParam.PumpPasses);// Query value of LogFile parameterStringlogfile=env.get(GRB.StringParam.LogFile);
StringgetErrorMsg()#

Query the error message for the most recent exception associated withthis environment.

Return value:

The error string.

Example:
// Query the error message of the most recent exceptionStringmsg=env.getErrorMsg();
voidgetParamInfo(GRB.DoubleParamparam,double[]info)#
voidgetParamInfo(GRB.IntParamparam,int[]info)#
voidgetParamInfo(GRB.StringParamparam,String[]info)#

Obtain detailed information about a parameter.

Arguments:
  • param – The parameter of interest. Please consult theparameter section for a complete list of Gurobiparameters, including descriptions of their purposes and their minimum,maximum, and default values.

  • info – The returned information. For double and integer parameters,the result will contain four entries: the current value of theparameter, the minimum allowed value, the maximum allowed value, and thedefault value. For string parameters, the result will contain twoentries: the current value of the parameter and the default value.

Example:
// Query information about TimeLimit parameterdouble[]timelimit=newdouble[4];env.getParamInfo(GRB.DoubleParam.TimeLimit,timelimit);// Query information about PumpPasses parameterint[]pumppasses=newint[4];env.getParamInfo(GRB.IntParam.PumpPasses,pumppasses);// Query information about LogFile parameterString[]logfile=newString[2];env.getParamInfo(GRB.StringParam.LogFile,logfile);
voidmessage(Stringmessage)#

Write a message to the console and the log file.

Arguments:

message – The message to be written.

Note

This call has no effect unless theOutputFlag parameter is set. In addition, it is ignoredfrom within aMESSAGE callback (seeWHERE values)and logging callback. The console logging can becontroled withLogToConsole.

Example:
// Write messageenv.message("Hello Gurobi!");
voidreadParams(StringparamFile)#

Read new parameter settings from a file.

Please consult theparameter section for a complete list of Gurobiparameters, including descriptions of their purposes and their minimum,maximum, and default values.

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

# Gurobi parameter fileThreads 1MIPGap 0

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

Arguments:

paramFile – Name of the file containing parameter settings.

Example:
// Read params.prm fileenv.readParams("params.prm");
voidrelease()#

Release the license associated with this environment. You will no longerbe able to calloptimize on modelscreated with this environment after the license has been released.

Example:
// Release license from environmentenv.release();
voidresetParams()#

Reset all parameters to their default values.

Please consult theparameter section for a complete list of Gurobiparameters, including descriptions of their purposes and their minimum,maximum, and default values.

Example:
// Reset all parameters to their default valuesenv.resetParams();
voidset(GRB.DoubleParamparam,doublenewval)#
voidset(GRB.IntParamparam,intnewval)#
voidset(GRB.StringParamparam,Stringnewval)#

Set the value of a parameter.

Arguments:
  • param – The parameter being modified. Please consult theparameter section for a complete list of Gurobiparameters, including descriptions of their purposes and their minimum,maximum, and default values.

  • newval – The desired new value of the parameter.

Note

Note that a model gets its own copy of the environment when it iscreated. Changes to the original environment have no effect on the copy,and vice versa. Use the appropriate version of the overloaded methodGRBModel.setto change a parameter on an existing model.

Example:
// Set TimeLimit parameterenv.set(GRB.DoubleParam.TimeLimit,2.0);// Set PumpPasses parameterenv.set(GRB.IntParam.PumpPasses,10);// Set LogFile parameterenv.set(GRB.StringParam.LogFile,"mylog.log");
voidset(Stringparam,Stringnewval)#

Set the value of any parameter using strings alone.

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

  • newval – The desired new value of the parameter.

Note

Note that a model gets its own copy of the environment when it iscreated. Changes to the original environment have no effect on the copy,and vice versa. Use the appropriate version of the overloaded methodGRBModel.setto change a parameter on an existing model.

Example:
// Set TimeLimit parameter via stringsenv.set("TimeLimit","2.0");
voidsetLogCallback(java.util.function.Consumer<String>logCallback)#

Sets a logging callback function to query all output posted by theenvironment object. Can be set after anempty environment wascreated.

Arguments:

logCallback – The logging callback function.

voidstart()#

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.

Please consult theparameter section for a complete list of Gurobiparameters, including descriptions of their purposes and their minimum,maximum, and default values.

Example:
// Create empty environmentGRBEnvenv=newGRBEnv(true);// Populate Compute Server parametersenv.set("ComputeServer","server1:61000");env.set("ServerPassword","passwd");// Start the environmentenv.start()
voidwriteParams(StringparamFile)#

Write all non-default parameter settings to a file.

Please consult theparameter section for a complete list of Gurobiparameters, including descriptions of their purposes and their minimum,maximum, and default values.

Arguments:

paramFile – Name of the file to which non-default parametersettings should be written. The previous contents are overwritten.

Example:
// Write all non-default parameters to fileenv.writeParams("myParams.prm");

Help and Feedback

On this page

[8]ページ先頭

©2009-2025 Movatter.jp