Remove a variable and all its associated constraints
AnsweredIs there a good way to remove a variable, and also remove all the constraints that contain this variable?
It looks like when the variable is removed, it just disappears from the constraints but these constraints are still there.
I'm actually thinking if we're able to query whether a variable exists in a constraint or not, so that I can just loop over all the constraints and delete what needs to be deleted. Otherwise I'll need to have some extra data structures to record this information.
-
There is no dedicated function or method to remove all constraints containing a particular variable.
One way to achieve this would be to get the GRBColumn object associated with a particular variable via the getCol() method. From the Column you can then deduce where the variable participates and remove the respective constraints from your model by looping over all Column entries and accessing the constraint objects via the GRBColumn::getConstr() method.
0 -
Hi Jaromił,
Thanks for your response. This solution works great with simple constraints.
However, I also have some general constraints that are created by addGenConstrIndicator(), and it looks like they are not captured in the GRBColumn object.
Is there a similar way to get those constraints?0 -
Hi Pai-Yu Chen,
Good point. General constraint are a special type of constraints and are handled differently. Unfortunately, the way of retrieving information about variables participating in general constraints is not as simple as for linear constraints.
You first have to get all general constraints via the getGenConstrs() method. You can then loop over all general constraints and check their GenConstrType attribute. Depending on the attribute, you then have to call the corresponding getGenConstrXxx() method which will then provide you pointers variables participating in a given general constraint.
For quadratic constraints, you would first have to get all quadratic constraints through the getQConstrs() method. You can then loop over all quadratic constraints and get the LHS of each constraints via the getQCRow() method. This will give you a GRBQuadExpr object which holds a linear part and a quadratic part and you would have to loop over both to check whether it holds the variable of interest.
Best regards,
Jaromił0 -
Thanks a lot!
0
Please sign in to leave a comment.
Comments
4 comments