Skip to main content

Create a constraints in C#

Comments

3 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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?.
  • Eli Towle
    • Gurobi Staff

    Hi Giang,

    Can you clarify: are you looking for a way to model the logical condition that either \( x \geq a \) or \( x \leq b \)? This can be done if bounds on \( x \) are known. Let \( \ell \) and \( u \) be lower and upper bounds on \( x \), respectively (such that \( \ell < a \) and \( u > b \)). We can model this by introducing a binary variable \( z \), and adding the following constraints:

    \( (\ell - a)z + a \leq x \leq u + (b-u)z \)

    If \(z=0\), we have \( a \leq x \leq u \). The \( x \leq u \) part does not affect the problem, because \( u \) is known to be an upper bound on \( x \). Similarly, if \( z = 1 \), we have \( \ell \leq x \leq b \). Here, only the constraint \( x \leq b \) affects the problem.

    In C#, you can use the GRBModel.AddGenConstrIndicator() method to do this more succinctly:

    GRBVar z = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "z");
    model.AddGenConstrIndicator(z, 0, x >= a, "logicLB");
    model.AddGenConstrIndicator(z, 1, x <= b, "logicUB");

    Does this help? Thanks!

    Eli

    0
  • Giang Le
    • Gurobi-versary
    • First Question
    • First Comment

    Hi Eli,

    It really helps my problem. Can I ask how to create a object function to minimize like this: |x - a| + |y - b| + |z-c| ? x, y,z are variables, and a,b,c are constants. I dont know how to describe a abs() in C#. Thank you in advance!

    0

Post is closed for comments.