Monitoring Progress - Logging and Callbacks#
-
void GRBmsg(GRBenv *env, const char *message)#
Insert a message into the Gurobi log file.
- Arguments:
env – The environment whose log file should receive the message.
message – The message to be appended to the log.
- Example:
error = GRBmsg(env, "Add this message to the log");
-
int GRBsetcallbackfunc(GRBmodel *model, int (*cb)(GRBmodel *model, void *cbdata, int where, void *usrdata), void *usrdata)#
Set up a user callback function. Note that a model can only have a single callback method, so this call will replace an existing callback. To disable a previously set callback, call this function with a
cb
argument ofNULL
.When solving a model using multiple threads, the user callback is only ever called from a single thread, so you don’t need to worry about the thread-safety of your callback.
Note that changing parameters from within a callback is not supported, doing so may lead to undefined behavior.
- Return value:
A non-zero return value indicates that a problem occurred while setting the user callback. Refer to the Error Codes table for a list of possible return values. Details on the error can be obtained by calling
GRBgeterrormsg
.- Arguments:
model – The model in which the callback should be installed.
cb – A function pointer to the user callback function. The callback will be called regularly from the Gurobi optimizer. The
where
argument to the callback function will indicate where in the optimization process the callback was invoked. Possible values are described in the Callback Codes section. The user callback can then call a number of routines to retrieve additional details about the state of the optimization (e.g.,GRBcbget
), or to inject new information (e.g.,GRBcbcut
,GRBcbsolution
). The user callback function should return 0 if no error was encountered, or it can return one of the Gurobi Error Codes if the user callback would like the optimization to stop and return an error result.usrdata – An optional pointer to user data that will be passed back to the user callback function each time it is invoked (in the
usrdata
argument).
- Example:
int mycallback(GRBmodel *model, void *cbdata, int where, void *usrdata); error = GRBsetcallbackfunc(model, mycallback, NULL);
-
int GRBgetcallbackfunc(GRBmodel *model, int (**cb)(GRBmodel *model, void *cbdata, int where, void *usrdata))#
Retrieve the current user callback function.
- Return value:
A non-zero return value indicates that a problem occurred while retrieving the user callback. Refer to the Error Codes table for a list of possible return values. Details on the error can be obtained by calling
GRBgeterrormsg
.- Arguments:
model – The model in which the callback should be installed.
cb – A function pointer to the user callback function.
- Example:
int (*mycallback)(GRBmodel *model, void *cbdata, int where, void *usrdata); error = GRBgetcallbackfunc(model, &mycallback);
-
int GRBcbget(void *cbdata, int where, int what, void *resultP)#
Retrieve additional information about the progress of the optimization. Note that this routine can only be called from within a user callback function.
- Return value:
A non-zero return value indicates that a problem occurred while retrieving the requested data. Refer to the Error Codes table for a list of possible return values. Details on the error can be obtained by calling
GRBgeterrormsg
.- Arguments:
cbdata – The
cbdata
argument that was passed into the user callback by the Gurobi optimizer. This argument must be passed unmodified from the user callback toGRBcbget()
.where – The
where
argument that was passed into the user callback by the Gurobi optimizer. This argument must be passed unmodified from the user callback toGRBcbget()
.what – The data requested by the user callback. Valid values are described in the Callback Codes section.
resultP – The location in which the requested data should be placed.
- Example:
if (where == GRB_CB_MIP) { double nodecount; error = GRBcbget(cbdata, where, GRB_CB_MIP_NODECNT, (void *) &nodecount); if (error) return 0; printf("MIP node count is %d\n", nodecount); }
-
void GRBversion(int *majorP, int *minorP, int *technicalP)#
Return the Gurobi library version number (major, minor, and technical).
- Arguments:
majorP – The location in which the major version number should be placed. May be
NULL
.minorP – The location in which the minor version number should be placed. May be
NULL
.technicalP – The location in which the technical version number should be placed. May be
NULL
.
- Example:
int major, minor, technical; GRBversion(&major, &minor, &technical); printf("Gurobi library version %d.%d.%d\n", major, minor, technical);