Initializing help system before first use

Using Callbacks

This chapter shows how to define and use callback functions from the Xpress Python interface. The design of this part of the interface reflects as closely as possible the design of the callback functions defined in the C API of the Xpress Optimizer.

Callback functions are a useful tool for adapting the Xpress Optimizer to the solution of various classes of problems, in particular Mixed Integer Programming (MIP) problems, with linear or nonlinear constraints. Their main purpose is to provide the user with a point of entry into the branch-and-bound, which is the workhorse algorithm for MIPs.

Using callback functions is simple: the user first defines a function (say myfunction) that is to be run every time the branch-and-bound reaches a well-specified point; second, the user calls a function (such as addPreIntsolCallback) with myfunction as its argument. Finally, the user calls the optimize function that launches the branch-and-bound, the simplex solver, or the barrier solver; it is while these are run that myfunction is called.

A callback function, hence, is passed once as an argument and used possibly many times. It is called while a solver is running, and it is passed the following:

  • a problem object, of the same class as an object declared with p = xpress.problem();
  • a data object.

The data object is user-defined and is given to the problem when adding the callback function. It can be used to store information that the user can read and/or modify within the callback. For instance, the following code shows how to add a callback function, preintsolcb, that is called every time a new integer solution is found.

import xpress as xp

class foo:
    "Simple class"
    bar = 0
    def __init__(self):
        self.bar = 1
    def update(self):
        self.bar += 1

def preintsolcb(prob, data, isheuristic, cutoff):
   """
   Callback to be used when an integer solution is found. The
   "data" parameter is of class foo
   """

p = xp.problem()
p.readProb('myprob.lp')  # reads in a problem, let's say a MIP

baz = foo()

p.addPreIntsolCallback(preintsolcb, baz, 3)
p.optimize()

While the function argument is necessary for all add*Callback functions, the data object can be specified as None. In that case, the callback will be run with None as its data argument. The call also specifies a priority with which the callback should be called: the larger the (positive) priority, the more urgently it is called.

Any call to an add*Callback function, as the names imply, only adds a function to a list of callback functions for that specific point of the BB algorithm. For instance, two calls to addPreIntsolCallback with two functions preint1 and preint2, respectively with priority 3 and 5, puts the two functions in a list. The two functions will be called (preint2 first) whenever the BB algorithm finds an integer solution.

In order to remove a callback function that was added with add*Callback, a corresponding remove*Callback function is provided, for instance removePreIntsolCallback. This function takes two arguments, i.e., the callback function and the data object, and deletes all elements of the list of callbacks that were added with the corresponding add*Callback function that match the function and the data.

The None keyword acts as a wildcard that matches any function/data object: if remove*Callback is called with None as the function, then all callbacks matching the data will be deleted. If the data is also None, all callback functions of that type are deleted; this can be obtained by passing no argument to remove*Callback.

The arguments and return value of the callback functions reflect those in the C API, and this holds for parameter names as well. As for the other API functions of the Python interface, there are a few exceptions:

  • If a function in the C API requires a parameter n to indicate the size of an array argument to follow, n is not required in the corresponding Python function;
  • If a function in the C API uses passing by reference as a means to allow for modifying a value and returning it as an output, the Python counterpart will have this as the return value of the function. Where multiple output values are comprised in the list of parameters, the return value is a tuple composed of the returned values. Elements of this tuple can be None if no change was made to that output value.

Below is a list of all available callbacks on the problem class:

Declares a callback which will be called after each objective in a multi-objective problem is solved.
[Callback, Multiobjective]
Declares a barrier iteration callback function, called after each iteration during the interior point algorithm, with the ability to access the current barrier solution/slack/duals or reduced cost values, and to ask barrier to stop. This callback function will be called in addition to any callbacks already added by problem.addBarIterationCallback. This functionality is not available for the hybrid gradient method.
[Barrier, Callback]
Declares a barrier log callback function, called at each iteration during the interior point algorithm. This callback function will be called in addition to any barrier log callbacks already added by problem.addBarlogCallback. This functionality is not available for the hybrid gradient method.
[Barrier, Callback]
Declares a callback which will be called before each objective in a multi-objective problem is solved.
[Callback, Multiobjective]
Declares a callback function that will be called after the selection of a MIP entity to branch on.
[Branching, Callback]
Declares a callback function which is called every time the Optimizer checks if the time limit has been reached. This callback function will be called in addition to any callbacks already added by problem.addCheckTimeCallback.
[Callback, Limits]
Declares a callback to be called when a solve executed in compute mode needs to be restarted.
[Callback, Compute Interface]
Declares a cut log callback function, called each time the cut log is printed.
[Callback, Logging]
Declares a callback function that is called when the Optimizer could separate cutting planes during the branch and bound search.
[Callback, Cuts]
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.
[Callback, Limits]
Declares a user infeasible node callback function, called after the current node has been found to be infeasible during the Branch and Bound search.
[Branch and Bound Search, Callback]
Declares a user integer solution callback function, called every time an integer solution is found by heuristics or during the Branch and Bound search. This callback function will be called in addition to any callbacks already added by problem.addIntsolCallback.
[Callback, Solution]
Declares a simplex log callback function which is called after every LPLOG iterations of the simplex algorithm. This callback function will be called in addition to any callbacks already added by problem.addLplogCallback.
[Callback, LP]
Declares an output callback function, called every time a text line relating to the given problem is output by the Optimizer.
[Callback, Logging]
Declares a MIP log callback function, called each time the MIP log is printed. This callback function will be called in addition to any callbacks already added by problem.addMiplogCallback.
[Callback, Logging]
Declares a MIP thread callback function, called every time a MIP worker problem is created by the parallel MIP code. This callback function will be called in addition to any callbacks already added by problem.addMipThreadCallback.
[Callback, Parallel]
Declares a destroy MIP thread callback function, called every time a MIP thread is destroyed by the parallel MIP code. This callback function will be called in addition to any callbacks already added by problem.addMipThreadDestroyCallback.
[Callback, Parallel]
Add a user callback to be called every time a new multistart job finishes. Can be used to overwrite the default solution ranking function
[Callback, Multistart]
Add a user callback to be called every time a new multistart job is created, and the pre-loaded settings are applied
[Callback, Multistart]
Add a user callback to be called every time a multistart winner has been declared
[Callback, Multistart]
Declares a callback function that will be called every time a new node is created during the branch and bound search. This callback function will be called in addition to any callbacks already added by problem.addNewnodeCallback.
[Branch and Bound Search, Callback]
Add a user callback to be called when an evaluation of a coefficient fails during the solve
[Callback, Numerics]
Declares a user node cutoff callback function, called every time a node is cut off as a result of an improved integer solution being found during the branch and bound search. This callback function will be called in addition to any callbacks already added by problem.addNodecutoffCallback.
[Branch and Bound Search, Callback]
Declares a callback function, called during the branch and bound search, after the LP relaxation has been solved for the current node, but before any internal cuts and heuristics have been applied. This callback function will be called in addition to any callbacks already added by problem.addNodeLPSolvedCallback.
[Branch and Bound Search, Callback]
Declares an optimal node callback function, called during the branch and bound search, after the LP relaxation has been solved for the current node, and after any internal cuts and heuristics have been applied, but before the Optimizer checks if the current node should be branched. This callback function will be called in addition to any callbacks already added by problem.addOptnodeCallback.
[Branch and Bound Search, Callback]
Declares a user integer solution callback function, called when an integer solution is found by heuristics or during the branch and bound search, but before it is accepted by the Optimizer.
[Callback, Solution]
Declares a preprocess node callback function, called before the LP relaxation of a node has been optimized, so the solution at the node will not be available. This callback function will be called in addition to any callbacks already added by problem.addPrenodeCallback.
[Branch and Bound Search, Callback]
Declares a callback to be called after presolve has been performed.
[Callback, Presolve]
Add a user callback to be called at the end of the cascading process, after the last variable has been cascaded
[Callback, Cascading, SLP]
Add a user callback to be called at the start of the cascading process, before any variables have been cascaded
[Callback, Cascading, SLP]
Add a user callback to be called after each column has been cascaded
[Callback, Cascading, SLP]
Add a user callback to be called after cascading a column was not successful
[Callback, Cascading, SLP]
Add a user callback to be called during the Xpress-SLP augmentation process
[Callback, SLP]
Add a user callback used to override the update of variables with small determining column
[Callback, Cascading, SLP]
Add a user callback to be called during MISLP when an integer solution is obtained
[Callback, MISLP]
Add a user callback to be called at the end of each SLP iteration
[Callback, SLP]
Add a user callback to be called at the start of each SLP iteration
[Callback, SLP]
Add a user callback to be called after each column has been tested for convergence
[Callback, SLP, SLP-convergence]
Add a user callback to be called before the linearization is updated
[Callback, SLP]
Declares a callback function to be called each time a solution added by problem.addMipSol has been processed. This callback function will be called in addition to any callbacks already added by problem.addUserSolNotifyCallback.
[Callback, Solution]

© 2001-2026 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.