Skip to main content

Norm 1 of a MVar as Constraint

Answered

Comments

3 comments

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

Please sign in to leave a comment.