Skip to main content

Remove a variable and all its associated constraints

Answered

Comments

4 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    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
  • Pai-Yu Chen
    Gurobi-versary
    First Comment
    First Question

    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
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    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
  • Pai-Yu Chen
    Gurobi-versary
    First Comment
    First Question

    Thanks a lot!

    0

Please sign in to leave a comment.