Cplex: MIP Control Callback

*本文主要记录和分享学习到的知识,算不上原创

*参考文献见链接

之前,我们有简单提到Cplex中的MIP Callback Interface,包括了Informational callback, query callback以及control callback,个人认为其中control callback的使用效果最为明显,也最经常使用。所以本文主要讲述Cplex的Control callback。

目录

  What is Control Callback?

  How to implement callback?

  CallBack Interface

    NodeCallback

    SolveCallback

    UserCutCallback

    LazyConstraintCallback

    HeuristicCallback

    BranchCallback

    IncumbentCallback

Control Callback

What is Control Callback?

Control callbacks allow you to control the branch & cut search during the optimization of MIP problems. Because control callbacks intervene in the search, the presence of a control callback in an application will cause CPLEX to turn off dynamic search.

As the reader is no doubt familiar, the process of solving a mixed integer programming problem involves exploring a tree of linear programming relaxations. CPLEX repeatedly selects a node from the tree, solves the LP relaxation at that node, attempts to generate cutting planes to cut off the current solution, invokes a heuristic to try to find an integer feasible solution “close” to the current relaxation solution, selects a branching variable (an integer variable whose value in the current relaxation is fractional), and finally places the two nodes that result from branching up or down on the branching variable back into the tree.

The user should be aware that the branch & cut process works with the presolved problem.

These callbacks allow sophisticated users to control the details of the branch & cut process. Specifically, users can choose the next node to explore, choose the branching variable, add their own cutting planes, place additional restrictions on integer solutions, or insert their own heuristic solutions.

 How to implement callback?

(1)选择想要实现的Callback类,写一个子类;

(2)重写子类中的main()方法;

(3)然后用IloCplex.use()调用。

CallBack Interface

If you determine that your application needs to seize control, intervene in the search, and redirect the optimizer, then the following control callbacks are available to do so.

The node callback allows you to query and optionally overwrite the next node CPLEX will process during a branch & cut search.
  IloCplex.NodeCallback in the Java API
The solve callback allows you to specify and configure the optimizer option to be used for solving the LP at each individual node.   IloCplex.SolveCallback in the Java API
The user cut callback allows you to add problem-specific user-defined cuts at each node.   IloCplex.UserCutCallback in the Java API
The lazy constraint callback allows you to add lazy constraints; that is, constraints that are not evaluated unless they are violated.   IloCplex.LazyConstraintCallback in the Java API
The heuristic callback allows you to implement a heuristic that tries to generate a new incumbent from the solution of the LP relaxation at each node.   IloCplex.HeuristicCallback in the Java API
The branch callback allows you to query and optionally overwrite the way CPLEX will branch at each node.   IloCplex.BranchCallback in the Java API
The incumbent callback allows you to check and optionally reject incumbents found by CPLEX during the search.   IloCplex.IncumbentCallback in the Java API

In Java, there is no way of removing individual callbacks from yourIloCplexorCplexobject. Instead, you remove all callbacks by calling the methodIloCplex.clearCallbacksorCplexClearCallbacks.

Node Callback

What is node callback?

The user can influence the order in which nodes are explored by installing a node selection callback.

It allows the user to select the node to process next during the branch-and-cut search.

Node callbacks allow you to determine which node from among the unexplored nodes IloCplex will chose to explore next in the branch-and-cut search.

To identify a node uniquely, an instance of IloCplex also assigns a unique identifier (IloCplex.NodeId) to each node. That unique identifier remains unchanged throughout the search.

The method getNodeId(int i) allows you to access the IloCplex.NodeId for each of the remaining nodes

When to call?

This callback will be used before CPLEX enters a node, and can select a different node to be entered instead.

Some Methods

getBranchVar(long node)                  Returns the variable that was branched upon to create the node specified by number node.

getDepth(long node)              Returns the depth of the node in the search tree.

getNodeId(long node)                        Returns the node identifier of the node specified by the node number node.

getNodeNumber(IloCplex.NodeId nodeid)              Returns the node number of the node specified by the node identifier.

getObjValue(long node)                   Returns the objective value of the node specified by the node number.

selectNode(long node)           Selects the next node to be processed by its number.

Examples

Solve Callback

What is it?

The solve callback can be used to solve subproblems during a branch-and-cut search. It allows you to set a starting point when a node is being solved or to select the solution algorithm on a per-node basis.

When to call?

Some Methods

setStart()                     Specifies a starting point for the next invocation of the method solve during the current solve callback.

solve()                   Solves the current node using the default algorithm (IloCplex.Algorithm.Auto).

solve(int alg)            Solves the current node using the algorithm alg.

useSolution()             Instructs IloCplex to use a solution.

                                                 ** This method instructs IloCplex to use the solution generated with this callback instead of solving the node itself.

Examples

Heuristic Callback

What is heuristic callback?

The heuristic callback allows one to implement a heuristic that tries to generate a new incumbent (integer-feasible solution) from the solution of the LP relaxation at each viable node.

(allows you to inject integer-feasible solutions during the branch-and-cut search.)

CPLEX calls heuristic callback routine with the solution vector for the current relaxation as input. The callback function should return a feasible solution vector, if one is found, as output.

We should also note that if a user wants to solve linear programs as part of a heuristic callback, the user must make a copy of the node LP (for example, usingCPXcloneprob). The user should not modify the CPLEX node LP

The constructor and methods of this class are protected to make sure that they are used only to derive a user-written callback class or to implement the main method in it.

When to call?

CPLEX calls heuristic callback function at every viable node in the branch & cut tree. (A node is viable if its LP relaxation is feasible and its relaxation objective value is better than that of the best available integer solution.)

(During branching, the heuristic callback is called after each node subproblem ((including all cuts generated at that node) has been solved. Before branching, at the root node, the heuristic callback is also called before each round of cuts is added to the problem and re-solved.)

Some methods

setBounds()                              Sets the bounds for a set of variables.

setSolution()                              Sets a solution to be used as the incumbent.

Examples

User Cut Callback

What is it?

Use cut callback allows you to add user cuts to the problem while the problem is being solved with branch-and-cut search.

A user cut (MIP cutting plane) is a constraint that can be derived from other constraints in the problem. Equally important, a MIP cutting plane does not cut off any integer feasible solutions but only cut off fractional solution.

It first gets the relaxation solution for the current node, and then calls a user separation routine to identify violated user cuts. These cuts are then added to the problem by add(), and the callback returns.

The added cuts must be globally valid because they are not removed automatically during backtracking.

The constructor and methods of this class are protected to make sure that they are used only to derive a user-written callback class or to implement the main method in it.

when to call?

CPLEX calls the user's callback routine at every viable node of the branch & cut tree.

Some Methods

add()                                       Adds a linear global cut to the current subproblem.

                                               **This cut must be globally valid. It will not be removed by backtracking or any other means during the search. The cut must also not cut off any integer feasible solution.

addLocal()                              Adds a linear local cut to the current subproblem.

isAfterCutLoop()                     Returns True if called after the cut loop, False otherwise.

abortCutLoop()                       Terminate the cut loop and proceed with branching.

Examples

Lazy Constraint Callback

What is it?

lazy constraint is a constraint that cannot be derived from other constraints and potentially cuts off integer feasible solutions. In other words, a lazy constraint changes the feasible region of the model.

The idea behind lazy constraints is that the LPs that are solved when the MIP is being solved can be kept smaller when these constraints are not included. IloCplex will, however, include a lazy constraint in the LP as soon as it becomes violated. In other words, the solution computed by IloCplex makes sure that all the lazy constraints that have been added are satisfied.

when to call?

Lazy constraints call will be used when CPLEX finds a new integer feasible solution and when CPLEX finds that the LP relaxation at the current node is unbounded.

Some Methods

add(IloRange cut)                        Adds a linear consraint cut as a global lazy constraint to the problem being solved.

                                                                  ** As a lazy constraint, this cut must be globally valid. It will not be removed by backtracking or any other means during the search.

addLocal(IloRange cut)           Adds cut as a local lazy constraint to the problem being solved.

isUnboundedNode()                     This method indicates whether or not the callback was invoked when the LP relaxation is found to be unbounded and the user needs to test whether a lazy constraint cuts off the unbounded direction.

Examples

Branch Callback

What is it?

This is a callback to control branching during the branch-and-cut search. It allows you to query how the invoking instance of IloCplex is about to create the children, and gives you the opportunity to override creation of this branch.

You can create one or two branches by calling one of the makeBranch methods once or twice. It is an error to call makeBranch more than twice.

By calling method prune, you remove the current node from the search tree. In other words, no subnodes from the current node will be added to the search tree. Calling both prune and makeBranch in one invocation of a branch callback is an error and yields unspecified behavior.

The constructor and methods of this class are protected to make sure that they are used only to derive a user-written callback class or to implement the main method in it.

When to call?

This callback will be used prior to branching (creation of the children) at a node in the branch and cut tree.

The branch callback routine is called for all viable nodes.

Some methods

getBranches()                 Returns one of the candidate branches at the current node if not overridden by the invoking callback.

getBranchType()                         Returns the type of the branch at the current node unless overridden by the invoking callback.

getNbranches()                Returns the number of branches CPLEX is going to create at the current node unless overridden by the invoking callback.

getNodeId()               Returns the node identifier of the current node.

isIntegerFeasible()         Returns true if the current node solution is considered to be integer feasible and thus potentially eligible as a new incumbent, but not suitable for branching.

makeBranch()                           Creates a child node for the current node

prune()                    Removes the current node from the search tree.

Examples

Incumbent Callback

What is it?

The incumbent callback is used to reject integer feasible solutions that do not meet additional restrictions the user imposes.

The user callback routine is called with the new feasible solution as input.

The callback function changes an argument or invokes a method to indicate whether or not the new solution should replace the incumbent solution.

If the callback is used to reject incumbents, the callback implicitly enforces a lazy constraint.

An incumbent callback is called whenever a new potential incumbent is found during the branch-and-cut search. It allows you to analyze the proposed incumbent and optionally reject it. In this case, CPLEX will continue the branch-and-cut search. However, since CPLEX considers the current node solution to be integer feasible, it will not be able to determine a reasonable way to branch. Thus, when this callback is implemented in such a way that it may reject an incumbent, it is typically paired with a branch callback to perform the branching after an incumbent has been rejected.

The constructor and methods of this class are protected to make sure that they are used only to derive a user-written callback class or to implement the main method in it.

When to call?

The incumbent callback will be called each time a new incumbent solution has been found, including when solutions are provided by the user’s heuristic callback routine.

Some Methods

getObjValue()             Returns the objective value of the potential incumbent.

reject()                   Rejects the proposed incumbent solution.

Examples

Routines

The advanced MIP control interface provides several routines that allow the user callback to gather information that may be useful in finding heuristic solutions.

 

The routines CPXgetcallbackgloballb and CPXgetcallbackglobalub, return the tightest known global lower and upper bounds on all the variables in the problem. No feasible solution whose objective is better than that of the best known solution can violate these bounds.

The routines CPXgetcallbacknodelb and CPXgetcallbacknodeub return variable bounds at this node. These reflect the bound adjustments made during branching.

The routine CPXgetcallbackincumbent returns the current incumbent - the best known feasible solution.

The routine CPXgetcallbacklp returns a pointer to the MIP problem. This pointer can be used to obtain various information about the problem.

The routine CPXgetcallbacknodelp, obtains a pointer to the node relaxation LP. This can be used to access information about the relaxation (row count, column count, etc.).

Some methods

setBounds                               Sets the bounds for a set of variables.

setSolution                              Sets a solution to be used as the incumbent.

Abort                                       Aborts current optimization.

GetBestObjValue                    This method returns a bound on the optimal solution value of the active problem at the moment the callback is called.

GetCplexStatus                       Returns the Cplex.CplexStatus for the current node.

GetCplexTime                         This method returns a time stamp, useful in measuring elapsed time in seconds. elapsed time=end time-start time

GetCutoff                                 Returns the cutoff for the objective value when nodes are being solved during branch-and-cut search.

GetEndTime                            This method returns a time stamp specifying when the time limit will occur. remaining time=getEndTime-getCplexTime

GetIncumbentObjValue           Returns the objective value of the current best integer solution at the moment the callback is called. (incumbent: best integer feasible solution)

GetModel                                Returns the model being solved when the invoking callback is called, that is the active model.

GetNiterations                         Returns the total number of iterations for solving node relaxations during the current optimization.

GetNnodes                             Returns the number of nodes processed so far in the active branch-and-cut search.

GetNremainingNodes             Returns the number of nodes remaining to be processed, or, equivalently, the number of active nodes in the tree.

GetObjValue                           Returns the objective function value of the solution of the continuous relaxation of the current node.

GetStartTime                          This method returns a time stamp specifying when the solving process was started.

GetStatus                                Returns the solution status for the current node.

HasIncumbent                        Returns true if an incumbent solution has been found when the callback is called.

Main                                        The method to be implemented by user callback classes.

Solve                                     Solves the current node relaxation using the default algorithm.

猜你喜欢

转载自www.cnblogs.com/liuyingsme/p/9929272.html