problem.addcbgapnotify
problem.addcbgapnotify |
Purpose
Declares a gap notification callback, to be called when a MIP solve reaches a predefined target, set using the
miprelgapnotify,
mipabsgapnotify,
mipabsgapnotifyobj, and/or
mipabsgapnotifybound controls.
Synopsis
problem.addcbgapnotify(callback, data, priority)
(RelGapNotify, AbsGapNotify, AbsGapNotifyObj, AbsGapNotifyBound) = callback(my_prob, my_object)
Arguments
callback
|
The callback function.
|
object
|
A user-defined object that wil be passed into the callback
callback.
|
priority
|
An integer that determines the order in which multiple gap notification callbacks will be invoked. The callback added with the higher priority will be called before a callback with a lower priority. Set to
0 if not required.
|
my_prob
|
The current problem.
|
my_object
|
The user-defined object passed as
object when setting up the callback with
addcbgapnotify.
|
RelGapNotify
|
The value the
miprelgapnotify control will be set to after this callback. May be modified within the callback in order to set a new notification target. Can be
None.
|
AbsGapNotify
|
The value the
mipabsgapnotify control will be set to after this callback. May be modified within the callback in order to set a new notification target. Can be
None.
|
AbsGapNotifyObj
|
The value the
mipabsgapnotifyobj control will be set to after this callback. May be modified within the callback in order to set a new notification target. Can be
None.
|
AbsGapNotifyBound
|
The value the
mipabsgapnotifybound control will be set to after this callback. May be modified within the callback in order to set a new notification target. Can be
None.
|
data
|
A user-defined object to be passed to the callback function,
callback.
|
priority
|
An integer that determines the order in which multiple estimate callbacks will be invoked. The callback added with a higher priority will be called before a callback with a lower priority. Set to 0 if not required.
|
Example
The following example prints a message when the gap reaches 10% and 1%
def gapnotify(prob, object): obj = prob.attributes.mipobjval bound = prob.attributes.bestbound # If no solutions were found, just return a tuple of None's if prob.attributes.mipsols == 0: return None, None, None, None if obj != 0 and bound != 0: relgap = abs((obj - bound) / max(abs(obj), abs(bound))) else: relgap = 0 newRelGapNotifyTarget = -1 if relgap <= 0.1: print('Gap reached 10%') newRelGapNotifyTarget = 0.1 if relgap <= 0.01: print('Gap reached 1%') newRelGapNotifyTarget = -1 # Don't call gapnotify again # return a quadruple with new values, or # None for those that should not be set return (newRelGapNotifyTarget, None, None, None) prob.controls.miprelgapnotify = 0.1 prob.addcbgapnotify(gapnotify, None, 0) prob.mipoptimize('')
Further information
The target values that caused the callback to be triggered will automatically be reset to prevent the same callback from being fired again.
Related topics
MIPRELGAPNOTIFY,
MIPABSGAPNOTIFY,
MIPABSGAPNOTIFYOBJ,
MIPABSGAPNOTIFYBOUND,
problem.removecbgapnotify.