Skip to main content

Trouble using gp.abs_()

Answered

Comments

3 comments

  • Ahmed Azab
    Gurobi Staff Gurobi Staff

    Hi,

    The Model.getConstrByName method is designed to retrieve a linear constraint by its name. However, you can define your absolute constraint using the special method Model.addGenConstrAbs, and then you can use Model.getGenConstrAbs method to retrieve the data associated with a general constraint of type ABS.

    import gurobipy as gp
    from gurobipy import*

    model=gp.Model()
    a = model.addVar(name="a")
    b = model.addVar(lb=-1, name="b")
    absconstr = model.addGenConstrAbs(a, b, "absconstr")
    model.update()
    (resvar, argvar) = model.getGenConstrAbs(absconstr)

    where 

    • resvar – (Var) The variable whose value will be to equal the absolute value of the argument variable. ( in your case: <gurobi.Var a> )

    • argvar – (Var) The variable for which the absolute value will be taken ( in your case: <gurobi.Var b> ).

    I hope this helps.

    -Ahmed

    0
  • Ruixuan Liu
    First Comment
    First Question

    Thanks! This is really helpful! 

    Is there a way I can retrieve the constr obj (ie absconstr)? Cuz I'm trying to update the abs constraint on the fly, it would be nice if I can retrieve the abs constraint by its name so I can remove and add an updated abs constraint.

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Ruixuan,

    You could accomplish this with a dictionary and getGenConstrs(), i.e.

    my_gen_cons = {gc.GenConstrName:gc for gc in m.getGenConstrs()}

    - Riley

    0

Please sign in to leave a comment.