Calback in C++ : list of arguments
AnsweredI'm trying to use a callback to add a cut that equals a Gurobi expression, which sums a variable, at different times.
If I keep the expression in the list of callback arguments, I get an error message related to the arguments (they don't match!). Once I remove it (GRBQuadExpr) the optimization is done without running the callback.
I appreciate any help for which I thank you in advance!
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Could you please share a minimal reproducible example of your code describing the issue?
Please also post the exact error you get when trying to execute your code.
0 -
Hallo Jaromil,
Please find below the instantiation of part of the callback, that will be applied at the solution node. After I commented Ecurrent expression, then the optimization runs, without running the callback.
class subproblem1 : public GRBCallback {
public:
int *Pwind;
int* t_D_f;
GRBVar** Pbss;
GRBVar** Pbssd;
GRBQuadExpr** Ecurrent;
subproblem1(int *xPwind,int *xt_D_f,GRBVar**xPbss,GRBVar**xPbssd){// numvars_sp1 = xnumvars;
Pwind = xPwind;
t_D_f = xt_D_f;
Pbss = xPbss;
Pbssd = xPbssd;
//Ecurrent = xEcurrent;
}
protected:void callback() {
try {
if (where == GRB_CB_MIPSOL) {// Query Zllower builds Zupper and compares its difference to epsilon tolerance
double zl = getDoubleInfo(GRB_CB_MIPSOL_OBJ);
int nodecnt = (int)getDoubleInfo(GRB_CB_MIPSOL_NODCNT); // solution at node .
double obj = getDoubleInfo(GRB_CB_MIPSOL_OBJ);
int solcnt = getIntInfo(GRB_CB_MIPSOL_SOLCNT);
int Kb = 0;
int kf = 0;
Kb= T - 1; //T is an extern const int variable//do something with the Gurobi expression to get the same Gurobi variables to count.
while (! Kb == 0) {
for (int bess = 0; bess < N_BESS; bess++) {p=Kb;
while (! p==Kb-6){
addCut(Ecurrent[bess][p] >= Ecurrent[bess][p-1]);
p -=1;
}
kf = p;
while (! kf==p+6){
addCut(Ecurrent[bess][kf] .>= Ecurrent[bess][kf + 1]);
kf +=1;
}Kb - = 1;
}
}
}}
} catch (GRBException e) {
cout << "Error number: " << e.getErrorCode() << endl;
cout << e.getMessage() << endl;
}
catch (...) {
cout << "Error during callback" << endl;
}}
};Thanks in advance!
0 -
What is the exact error you get when you run with your callback?
0 -
Hallo Jaromil,
I think we mismatch answers and questions please find above the piece of my callback.
0 -
These conditions
while (! Kb == 0) {
...
while (! p==Kb-6){
...
while (! kf==p+6){look strange. Did you mean
while ( Kb != 0) {
...
while (p != Kb-6){
...
while (kf != p+6){or (equivalently)
while (! (Kb == 0)) {
...
while (! (p==Kb-6)){
...
while (! (kf==p+6)){Your current code might be misunderstood as \(\texttt{(!Kb) == 0}\).
0 -
Could you also point me to which arguments don't match according to the error you see?
0 -
The constructor
subproblem1(int *xPwind,int *xt_D_f,GRBVar**xPbss,GRBVar**xPbssd)
is missing an argument for \(\texttt{Ecurrent}\).
0 -
Hallo Jaromil,
Yeah ... because if I join it then I get the error message about the list of arguments.
"subproblem1 cb = subproblem1(P_wind,t_D_f,Pbss,Pbssd,Ecurrent);
Severity Code Line Description Project File
Error (active) E0289 1626 no instance of constructor "subproblem1::subproblem1" matches the argument list "0 -
Hi Alessandra,
The argument \(\texttt{GRBQuadExpr** Ecurrent}\) is missing in the constructor \(\texttt{subproblem1}\).
Does the following work?
class subproblem1 : public GRBCallback {
public:
int *Pwind;
int* t_D_f;
GRBVar** Pbss;
GRBVar** Pbssd;
GRBQuadExpr** Ecurrent;
subproblem1(int *xPwind, int *xt_D_f,GRBVar **xPbss, GRBVar **xPbssd, GRBQuadExpr **xEcurrent){
// numvars_sp1 = xnumvars;
Pwind = xPwind;
t_D_f = xt_D_f;
Pbss = xPbss;
Pbssd = xPbssd;
Ecurrent = xEcurrent;
}
protected:
...Best regards,
Jaromił0 -
I already had noted my mistake but Thanks a lot!
0
Post is closed for comments.
Comments
11 comments