2

Can anybody help me with coding a 2-norm constraint?

enter image description here

k=2
n=2
w = model.addMVar((k,n),lb = -1.0, ub = 1.0, vtype=gp.GRB.CONTINUOUS, name="w")

for i in range (k):
    sumw = 0
    for j in range(n):
        sumw += w[i,j] @ w[i,j]
    model.addConstr( sumw.__pow__(1/2) >= 1, name="nonconvex")
k=2
n=2
w = model.addMVar((k,n),lb = -1.0, ub = 1.0, vtype=gp.GRB.CONTINUOUS, name="w")
for i in range (k):
   wnorm = np.linalg.norm(w[i,:])
   model.addConstr(wnorm >= 1, name="nonconvex")

These two methods both are not feasible. I really dont know how to code this. Thanks!

1 Answer 1

1

There's no __pow__ method for an MQuadExpr object like w[i,j] @ w[i,j]. This is only available for LinExpr objects. Since Gurobis power function can't handle MVars yet, you need to do something like this:

from gurobipy import quicksum as qsum

k, n = 2, 2
w = model.addVars(k, n, vtype="C", lb=-1.0, ub=1.0, name="w")

# helper variables to store ||w_j||_2
helper1 = model.addVars(k, vtype="C")
helper2 = model.addVars(k, vtype="C")

for i in range(k):
    model.addConstr(helper1[i] == qsum(w[i, j]**2 for j in range(n)))

    # add helper2[i] == helper1[i]^0.5
    model.addGenConstrPow(helper2[i], helper1[i], 0.5)
    
    # add helper2[i] <= 1
    model.addConstr(helper2[i] <= 1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.