Help with Unbounded or Infeasible
AnsweredI have the following problem with the C++ API.
My understanding is that the following code:
#include <iostream>
#include <limits>
#include "gurobi_c++.h"
int main() {
// construct model
GRBEnv env;
GRBModel model(env);
GRBVar x = model.addVar(-std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity(), 0, GRB_CONTINUOUS, "x");
model.addQConstr(GRBQuadExpr(x, 1.0), GRB_LESS_EQUAL, 0, "");
model.setObjective(GRBQuadExpr(x, 1.0));
model.optimize();
// if status is inf_or_unbd -> reoptimize
switch (model.get(GRB_IntAttr_Status)) {
case GRB_INF_OR_UNBD:
std::cout << "------------>inf_or_unbd" << std::endl;
model.set(GRB_IntParam_DualReductions, 0);
model.optimize();
break;
default:
std::cout << "unrelevant status code" << std::endl;
return 0;
}
// check again if status code is more precise
switch (model.get(GRB_IntAttr_Status)) {
case GRB_INF_OR_UNBD:
std::cout << "------------>inf_or_unbd" << std::endl; return 0;
case GRB_UNBOUNDED:
std::cout << "This is the expected behaviour" << std::endl; return 0;
default:
std::cout << "This would be even more unexpected" << std::endl; return 0;
}
}
should return "This is the expected behaviour". This is of course a minimal example, but this also appears in larger models. In my applications, it is important to know if the model is infeasible or unbounded.
Is this an error inside gurobi or do I need to change part of my code to work as expected?
Regards
-
Official comment
This post is more than three 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 why not try our AI Gurobot?. -
Hi Aron,
Gurobi will try to re-use all information it has from a previous solve. In this case, you re-optimize a non-changed model, thus Gurobi ends up with the same result. You would have to use the GRBModel::reset() function to make Gurobi re-optimize and obtain the information you seek.
[...]
model.reset();
model.set(GRB_IntParam_DualReductions, 0);
model.optimize();
[...]Btw, greetings to the office :)
Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments