Skip to main content

How to add a constraint which is the absolute value is smaller than a constant?

Answered

Comments

2 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 Hanxiong,

    The abs_() function is used to set one variable equal to the absolute value of another. You should first define an auxiliary variable and set it equal to \( x - y \). Then, introduce another auxiliary variable and set it equal to \( | x - y| \) using \( \texttt{abs_()} \). E.g.:

    x = model.addVar(name="x")
    y = model.addVar(name="y")
    u = model.addVar(lb=-gp.GRB.INFINITY, name="u")
    v = model.addVar(ub=0.7, name="v")

    model.addConstr(u == x - y)
    model.addConstr(v == gp.abs_(u))

    This is discussed more in this forum post.

    Also, note that strict inequalities like \( < \) are not supported by Gurobi.

    Thanks,

    Eli

    0

Post is closed for comments.