Skip to content

Instantly share code, notes, and snippets.

@rocarvaj
Last active May 5, 2020 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rocarvaj/3813871 to your computer and use it in GitHub Desktop.
Save rocarvaj/3813871 to your computer and use it in GitHub Desktop.
Cut callback to get LP with cuts

See here for a much better version of this code by @lyeskhalil

Callback function that obtains LP after CPLEX has added cuts at root node

  • Callback function is in Callback.cpp

  • To use the callback function, add it to the CPLEX env before calling CPXmipopt:

    status = CPXsetusercutcallbackfunc(mainEnv, cutCallbackShareRootCuts, this);
    DBG_IF_RETURN(status, 1, "Error setting cut callback");
int cutCallbackShareRootCuts (CPXCENVptr env,
void *cbdata,
int wherefrom,
void *cbhandle,
int *useraction_p)
{
int status;
*useraction_p = CPX_CALLBACK_DEFAULT;
Problem *tempProblem = (Problem*) cbhandle;
Problem &problem = *tempProblem;
// cutCallbackRootCalled is a flag to avoid calling the callback again and again
// CPX_CALLBACK_MIP_CUT_LAST indicates that CPLEX is done adding cuts and the user has a last chance to add cuts
if(problem.cutCallbackRootCalled == 0 && wherefrom == CPX_CALLBACK_MIP_CUT_LAST)
{
// With this, we stop the optimization after the callback is executed
*useraction_p = CPX_CALLBACK_FAIL;
problem.cutCallbackRootCalled = 1;
CPXLPptr tmpLp;
status = CPXgetcallbacknodelp(env, cbdata, wherefrom, &tmpLp);
DBG_IF_RETURN(status, 1, "Error getting callback lp");
status = CPXwriteprob(env, tmpLp, "ProblemWithCuts.lp", NULL);
DBG_IF_RETURN(status, 1, "Error writing cut callback lp");
}
return 0;
}
@maiklb2005
Copy link

Hello, thank you for the code. Since the file has the .cpp extension, I was wondering if the code can run in C++ API? Thank you.

@lyeskhalil
Copy link

lyeskhalil commented May 5, 2020

Put this gist together based on your work Rodolfo: https://gist.github.com/lyeskhalil/5f86c4e3bd23e353d98c72999e423355

Thanks for sharing!

@rocarvaj
Copy link
Author

rocarvaj commented May 5, 2020

Put this gist together based on your work Rodrigo: https://gist.github.com/lyeskhalil/5f86c4e3bd23e353d98c72999e423355

Thanks for sharing!

Thanks a lot yo you for sharing your version, Elias. It's a huge improvement!
Just a heads up, my name is Rodolfo :) I don't want to be confused with my nemesis: Rodrigo Carvajal.

@lyeskhalil
Copy link

Put this gist together based on your work Rodrigo: https://gist.github.com/lyeskhalil/5f86c4e3bd23e353d98c72999e423355
Thanks for sharing!

Thanks a lot yo you for sharing your version, Elias. It's a huge improvement!
Just a heads up, my name is Rodolfo :) I don't want to be confused with my nemesis: Rodrigo Carvajal.

I have no idea what you're talking about :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment