Skip to main content

Norm 1 of a MVar as Constraint

Answered

Comments

4 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?.
  • Robert Luce
    • Gurobi Staff

    Hello José, unfortunately there isn't a shorthand for doing this in gurobipy yet, but I have taken note of your feature request in our development backlog.

     

    For now you will have to model the absolute values per variable.  For example, to compute a minimum 1-norm solution to an underdetermined linear system you could do:

    import numpy as np
    import gurobipy as gp

    A = np.random.rand(30, 100)
    x_true = np.random.rand(100)
    b = A @ x_true

    m = gp.Model()
    x = m.addMVar(100, lb=-np.inf)
    m.addConstr(A @ x == b)

    # 'absx' takes absolute value of 'x'
    absx = m.addMVar(100)
    for v, absv in zip(x.tolist(), absx.tolist()):
    m.addConstr(absv == gp.abs_(v))
    # minimize 1-norm of x
    m.setObjective(absx.sum())

    m.optimize()
    print("Solution 1-norm: {}".format(np.sum(np.abs(x.X))))
    0
  • José Ignacio Torralba
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you so much Robert!

     

    0
  • Maliheh Aramon
    • Gurobi Staff

    Hi José, 

    Gurobi 9.5 was recently released. Included in this release is the norm() general constraint helper function that can be used to set a decision variable equal to the norm of the other decision variables. The supported norms are 0-, 1-, 2-, and infinity.

    We hope this new feature works well for you. Please let us know if you find any issues using this.

    Best regards,

    Maliheh

    0

Post is closed for comments.