Movatterモバイル変換


[0]ホーム

URL:


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

Concepts

Features

Reference

Gurobi
Back to top

GRBCallback#

GRBCallback#

Gurobi callback class. This is an abstract class. To implement acallback, you should create a subclass of this class and implement acallback() method. If you pass an object of this subclass to methodGRBModel.setCallback before callingGRBModel.optimize orGRBModel.computeIIS, thecallback() method of the class will be called periodically.Depending on where the callback is called from, you will be able toobtain various information about the progress of the optimization.

Note that this class contains one protectedint member variable:where. You can query this variable from yourcallback() methodto determine where the callback was called from.

Gurobi callbacks can be used both to monitor the progress of theoptimization and to modify the behavior of the Gurobi Optimizer. Asimple user callback function might call theGRBCallback.getIntInfo orGRBCallback.getDoubleInfo methods to produce a customdisplay, or perhaps to terminate optimization early (usingGRBCallback.abort) or to proceed to the next phase of thecomputation (usingGRBCallback.proceed). More sophisticatedMIP callbacks might useGRBCallback.getNodeRel orGRBCallback.getSolution to retrieve values from thesolution to the current node, and then useGRBCallback.addCut orGRBCallback.addLazy toadd a constraint to cut off that solution, orGRBCallback.setSolution to import a heuristic solutionbuilt from that solution. For multi-objective problems, you might useGRBCallback.stopOneMultiObj to interruptthe optimization process of one of theoptimization steps in a multi-objective MIP problem without stopping thehierarchical optimization process.

When solving a model using multiple threads, the user callback is onlyever called from a single thread, so you don’t need to worry about thethread-safety of your callback.

A few parameters arecallback settable. These parameters can be modified from withina callback call usingGRBCallback.set methods.

You can look at theCallback.javaexample for details of how to use Gurobi callbacks.

GRBCallbackGRBCallback()#

Callback constructor.

Return value:

A callback object.

voidabort()#

Abort optimization. When the optimization stops, theStatusattribute will be equal toGRB.Status.INTERRUPTED.

voidaddCut(GRBLinExprlhsExpr,charsense,doublerhs)#

Add a cutting plane to the MIP model from within a callback function.Note that this method can only be invoked when thewhere membervariable is equal toGRB.CB_MIPNODE (see theCallback Codes section in the Reference Manualfor more information).

Cutting planes can be added at any node of the branch-and-cut tree.However, they should be added sparingly, since they increase the size ofthe relaxation model that is solved at each node and can significantlydegrade node processing speed.

Cutting planes are typically used to cut off the current relaxationsolution. To retrieve the relaxation solution at the current node, youshould first callgetNodeRel.

You should consider setting parameterPreCrush to value1 when adding your own cuts. This setting shuts off a few presolvereductions that can sometimes prevent your cut from being applied to thepresolved model (which would result in your cut being silently ignored).

Note that cutting planes added through this method must truly be cuttingplanes – they can cut off continuous solutions, but they may not cutoff integer solutions that respect the original constraints of themodel. Ignoring this restriction will lead to incorrect solutions.

Arguments:
  • lhsExpr – Left-hand side expression for new cutting plane.

  • sense – Sense for new cutting plane (GRB.LESS_EQUAL,GRB.EQUAL, orGRB.GREATER_EQUAL).

  • rhs – Right-hand side value for new cutting plane.

voidaddLazy(GRBLinExprlhsExpr,charsense,doublerhs)#

Add a lazy constraint to the MIP model from within a callback function.Note that this method can only be invoked when thewhere membervariable is equal toGRB.CB_MIPNODE orGRB.CB_MIPSOL (see theCallback Codes section in the Reference Manualfor more information).

Lazy constraints are typically used when the full set of constraints fora MIP model is too large to represent explicitly. By only including theconstraints that are actually violated by solutions found during thebranch-and-cut search, it is sometimes possible to find a proven optimalsolution while only adding a fraction of the full set of constraints.

You would typically add a lazy constraint by first querying the currentnode solution (by callinggetSolution from aGRB.CB_MIPSOL callback, orgetNodeRel from aGRB.CB_MIPNODE callback), and then callingaddLazy() to add aconstraint that cuts off the solution. Gurobi guarantees that you willhave the opportunity to cut off any solutions that would otherwise beconsidered feasible.

MIP solutions may be generated outside of a MIP node. Thus, generatinglazy constraints is optional when thewhere value in the callbackfunction equalsGRB.CB_MIPNODE. To avoid this, we recommend toalways check when thewhere value equalsGRB.CB_MIPSOL.

Your callback should be prepared to cut off solutions that violate anyof your lazy constraints, including those that have already been added.Node solutions will usually respect previously added lazy constraints,but not always.

Note that you must set theLazyConstraints parameter ifyou want to use lazy constraints.

Arguments:
  • lhsExpr – Left-hand side expression for new lazy constraint.

  • sense – Sense for new lazy constraint (GRB.LESS_EQUAL,GRB.EQUAL, orGRB.GREATER_EQUAL).

  • rhs – Right-hand side value for new lazy constraint.

doublegetDoubleInfo(intwhat)#

Request double-valued callback information. The available informationdepends on the value of thewhere member. For information onpossible values ofwhere, and the double-valued information that canbe queried for different values ofwhere, please refer to theCallback Codes section of the ReferenceManual.

Arguments:

what – Information requested. Please refer to the list ofCallback Codes in the Reference Manual forpossible values.

Return value:

Value of requested callback information.

intgetIntInfo(intwhat)#

Request int-valued callback information. The available informationdepends on the value of thewhere member. For information onpossible values ofwhere, and the int-valued information that can bequeried for different values ofwhere, please refer to theCallback Codes section in the ReferenceManual.

Arguments:

what – Information requested. Please refer to the list ofCallback Codes in the Reference Manual forpossible values.

Return value:

Value of requested callback information.

doublegetNodeRel(GRBVarv)#

Retrieve a value from the node relaxation solution at the current node.Only available when thewhere member variable is equal toGRB.Callback.MIPNODE, andGRB.Callback.MIPNODE_STATUS is equal toGRB.Status.OPTIMAL (see theCallback Codessection for more information).

Note that the relaxation solutionretrieved at a node is not necessarily feasible for the user model.

Arguments:

v – The variable whose value is desired.

Return value:

The value of the specified variable in the node relaxation forthe current node.

double[]getNodeRel(GRBVar[]xvars)#

Retrieve values from the node relaxation solution at the current node.Only available when thewhere member variable is equal toGRB.Callback.MIPNODE, andGRB.Callback.MIPNODE_STATUS is equal toGRB.Status.OPTIMAL (see theCallback Codessection for more information).

Note that the relaxation solutionretrieved at a node is not necessarily feasible for the user model.

Arguments:

xvars – The list of variables whose values are desired.

Return value:

The values of the specified variables in the node relaxation forthe current node.

double[][]getNodeRel(GRBVar[][]xvars)#

Retrieve values from the node relaxation solution at the current node.Only available when thewhere member variable is equal toGRB.Callback.MIPNODE, andGRB.Callback.MIPNODE_STATUS is equal toGRB.Status.OPTIMAL (see theCallback Codessection for more information).

Note that the relaxation solutionretrieved at a node is not necessarily feasible for the user model.

Arguments:

xvars – The array of variables whose values are desired.

Return value:

The values of the specified variables in the node relaxation forthe current node.

doublegetSolution(GRBVarv)#

Retrieve values from the current solution vector. Only available whenthewhere member variable is equal toGRB.CB_MIPSOL orGRB.CB_MULTIOBJ.

Arguments:

v – The variable whose value is desired.

Return value:

The value of the specified variable in the current solutionvector.

double[]getSolution(GRBVar[]xvars)#

Retrieve values from the current solution vector. Only available whenthewhere member variable is equal toGRB.CB_MIPSOL orGRB.CB_MULTIOBJ.

Arguments:

xvars – The list of variables whose values are desired.

Return value:

The values of the specified variables in the current solution.

double[][]getSolution(GRBVar[][]xvars)#

Retrieve values from the current solution vector. Only available whenthewhere member variable is equal toGRB.CB_MIPSOL orGRB.CB_MULTIOBJ.

Arguments:

xvars – The array of variables whose values are desired.

Return value:

The values of the specified variables in the current solution.

StringgetStringInfo(intwhat)#

Request string-valued callback information. The available informationdepends on the value of thewhere member. For information onpossible values ofwhere, and the string-valued information that canbe queried for different values ofwhere, please refer to theCallback Codes section of the ReferenceManual.

Arguments:

what – Information requested. Please refer to the list ofCallback Codes in the Reference Manual forpossible values.

Return value:

Value of requested callback information.

voidproceed()#

Generate a request to proceed to the next phase of the computation. Notethat the request is only accepted in a few phases of the algorithm, andit won’t be acted upon immediately.

In the current Gurobi version, this callback allows you to proceed fromthe NoRel heuristic to the standard MIP search. You can determine thecurrent algorithm phase usingMIP_PHASE,MIPNODE_PHASE, orMIPSOL_PHASE queries from a callback.

voidset(GRB.DoubleParamparam,doublenewvalue)#

Allows to modifycallback settable double-valued parameters during a deterministic callback.

That is, when thewhere value isGRB.CB_PRESOLVED,GRB.CB_SIMPLEX,GRB.CB_MIP,GRB.CB_MIPSOL,GRB.CB_MIPNODE,GRB.CB_BARRIER, orGRB.CB_MULTIOBJ (see theCallback Codes section for more information)

In case of a remote server, the change of a parameter from within acallback may not be taken into account immediately.

Arguments:
  • param – The parameter being modified.

  • newvalue – The desired new value for the parameter.

voidset(GRB.IntParamparam,intnewvalue)#

Allows to modifycallback settable int-valued parameters during a deterministic callback.

That is, when thewhere value isGRB.CB_PRESOLVED,GRB.CB_SIMPLEX,GRB.CB_MIP,GRB.CB_MIPSOL,GRB.CB_MIPNODE,GRB.CB_BARRIER, orGRB.CB_MULTIOBJ (see theCallback Codes section for more information)

In case of a remote server, the change of a parameter from within acallback may not be taken into account immediately.

Arguments:
  • param – The parameter being modified.

  • newvalue – The desired new value for the parameter.

voidset(GRB.StringParamparam,Stringnewvalue)#

Allows to modifycallback settable string-valued parameters during a deterministic callback.

That is, when thewhere value isGRB.CB_PRESOLVED,GRB.CB_SIMPLEX,GRB.CB_MIP,GRB.CB_MIPSOL,GRB.CB_MIPNODE,GRB.CB_BARRIER, orGRB.CB_MULTIOBJ (see theCallback Codes section for more information)

In case of a remote server, the change of a parameter from within acallback may not be taken into account immediately.

Arguments:
  • param – The parameter being modified.

  • newvalue – The desired new value for the parameter.

voidsetSolution(GRBVarv,doubleval)#

Import solution values for a heuristic solution. Only available when thewhere member variable is equal toGRB.CB_MIP,GRB.CB_MIPNODE, orGRB.CB_MIPSOL (see theCallback Codes section for more information).

When you specify a heuristic solution from a callback, variablesinitially take undefined values. You should use this method to specifyvariable values. You can make multiple calls tosetSolution from onecallback invocation to specify values for multiple sets of variables.After the callback, if values have been specified for any variables, theGurobi Optimizer will try to compute a feasible solution from thespecified values, possibly filling in values for variables whose valueswere left undefined. You can also optionally calluseSolution within your callbackfunction to try to immediately compute a feasible solution from thespecified values.

In the context of a Compute Server environment, for performance reasons,the solutions sent throughsetSolution are not necessarily processed bythe Compute Server immediately. They may show up in the solving processa bit later after the client callback has sent them. In extreme cases,it could even be that the Compute Server optimization job terminatesbefore it processes the user solution.

Arguments:
  • v – The variable whose values is being set.

  • val – The value of the variable in the new solution.

voidsetSolution(GRBVar[]xvars,double[]sol)#

Import solution values for a heuristic solution. Only available when thewhere member variable is equal toGRB.CB_MIP,GRB.CB_MIPNODE, orGRB.CB_MIPSOL (see theCallback Codes section for more information).

When you specify a heuristic solution from a callback, variablesinitially take undefined values. You should use this method to specifyvariable values. You can make multiple calls tosetSolution from onecallback invocation to specify values for multiple sets of variables.After the callback, if values have been specified for any variables, theGurobi Optimizer will try to compute a feasible solution from thespecified values, possibly filling in values for variables whose valueswere left undefined. You can also optionally calluseSolution within your callbackfunction to try to immediately compute a feasible solution from thespecified values.

In the context of a Compute Server environment, for performance reasons,the solutions sent throughsetSolution are not necessarily processed bythe Compute Server immediately. They may show up in the solving processa bit later after the client callback has sent them. In extreme cases,it could even be that the Compute Server optimization job terminatesbefore it processes the user solution.

Arguments:
  • xvars – The variables whose values are being set.

  • sol – The desired values of the specified variables in the newsolution.

voidstopOneMultiObj(intobjcnt)#

Interrupt the optimization process of one of theoptimization steps in a multi-objective MIP problem without stopping thehierarchical optimization process. Only available for multi-objective MIPmodels and when the where member variable is not equal to GRB.CB_MULTIOBJ(see theCallback Codes section for moreinformation).

You would typically stop amulti-objective optimization step by querying the last finished numberof multi-objectives steps, and using that number to stop the currentstep and move on to the next hierarchical objective (if any) as shown inthe following example:

importgurobi.*;publicclassCallbackextendsGRBCallback{privateintobjcnt;privatelongstarttime;protectedvoidcallback(){try{if(where==GRB.CB_MULTIOBJ){/* get current objective number */objcnt=getIntInfo(GRB.CB_MULTIOBJ_OBJCNT);/* reset start time to current time */starttime=System.currentTimeMillis();}elseif(System.currentTimeMillis()-starttime>BIG||/* takes too long or good enough */){/* stop only this optimization step */stopOneMultiObj(objcnt);}}}}

You should refer to the section onMultiple Objectives for information onhow to specify multiple objective functions and control the trade-offbetween them.

Arguments:

objnum – The number of the multi-objective optimizationstep to interrupt. For processes running locally, this argument can have the specialvalue -1, meaning to stop the current step.

doubleuseSolution()#

Once you have imported solution values usingsetSolution, you can optionallycalluseSolution in aGRB.CB_MIPNODE callback to immediately usethese values to try to compute a heuristic solution. Alternatively, youcan calluseSolution in aGRB.CB_MIP orGRB.CB_MIPSOLcallback, which will store the solution until it can be processedinternally.

Return value:

The objective value for the solution obtained from your solutionvalues. It equalsGRB.INFINITY if no improved solution is found orthe method has been called from a callback other thanGRB.CB_MIPNODEas, in these contexts, the solution is stored instead of being processedimmediately.

Help and Feedback

On this page

[8]ページ先頭

©2009-2025 Movatter.jp