Detect linear problem
AnsweredI add constraints to my model within the C++ API automatically based on user input.
I do not know in advance if the constraint given by the user will be linear or quadratic.
At the moment I will just build the model using GRBQuadExpr. I assumed that Gurobi would have a simple check to see if the objective and all constraints (even if given as GRBQuadExpr) are linear and call an LP algorithm. However, it seems that Gurobi is not doing that.
In my specific example, the model in the original .lp file can not be successfully solved (suboptimal termination) but when I remove all empty square brackets (" [ ] ") from the model and rename the constraints to, e.g., lc01, the model is optimized without a problem.
Is there already a way to enable the behavior that I want, or would this be a good candidate for a new feature?
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,
If we would catch this special case, then we could argue about checking for fixed quadratic terms and removing these as well. Then again we could argue about checking for quadratic terms which can be removed via presolving and then fixing of variables. This cycle can be made arbitrary complex so we decided to just trust the user in their input and handle the constraint as quadratic if there is evidence of quadratic terms (even if it's empty).
You can still achieve your goal by constructing the user input as a QuadExpr and then checking its size argument. If it is \(0\), then you know that it is indeed a LinExpr. You can then get the LinExpr from your QuadExpr via the GRBQuadExpr::getLinExpr function.
Best regards,
Jaromił0 -
Hi Jaromił,
thanks for your suggestion. Indeed, this is very similar to the workaround currently employed.
As you pointed out, you could always check for more special cases. Personally, I would argue that this a very basic check, similar to unused variables. On the other hand, I imagine most users will not run into this issue and people writing, e.g., wrappers can easily employ the mentioned strategy of checking the size of the QuadExpr.
Maybe this could rather added to the documentation.
0
Post is closed for comments.
Comments
3 comments