Objective Q not PSD
AnsweredHi Everyone, I was working on this binary non-linear programme and encountered this objective Q not PSD error, i've been looking through the code and couldn't figure out why.
here's my gurobi code
def offline(G, T, Demand, Pos, f, c, lp):
M = [i for i in range(len(list(G.nodes)))]
S = [i for i in range(service_num)]
T = [i+1 for i in range(T)]
print("Demand:", Demand)
print("M, S, T:", M, S, T)
print("location:", Pos)
print("f:", f)
print("c:", c)
print("last placement",lp)
m = Model()
x = m.addVars(M, S, T)
for s in S:
for t in range(len(T)+1):
for n in range(len(G.nodes)):
m.addVar(vtype=GRB.BINARY, name="x_%i,%i,%i"%(n, s, t))
for s in S:
for i in M:
if i == lp[s]:
x[i, s, 0] = 1
else:
x[i, s, 0] = 0
for t in T:
for s in S:
m.addConstr(quicksum(x[n,s,t] for n in M) == 1)
for t in T:
for n in M:
m.addConstr(quicksum(x[n,s,t] for s in S) <= 1)
#for t in T:
for i in M:
for j in M:
print(f[i][j])
for i in M:
print(c[i])
for i in M:
for s in S:
print(x[i, s, 0])
print(Pos[1])
print(Demand[1])
print(S[:-2])
m.setObjective(quicksum(quicksum((Demand[t][s]*(quicksum(x[i, s, t]* c[i] for i in M))) for s in S)
+ quicksum(quicksum(quicksum(f[i][j]*x[i, s, t]*x[j, s+1, t] for j in M) for i in M) for s in S[:-1])
+ quicksum(x[i, 0, t]*f[Pos[t]][i] for i in M) + quicksum(x[i, S[-1], t]*f[i][Pos[t]] for i in M)
+ quicksum(quicksum(quicksum(f[j][i]*x[j,s,t-1]*x[i,s,t] for i in M) for j in M) for s in S) for t in T),
GRB.MINIMIZE)
m.write("model.lp")
m.optimize()
return m.objVal
This is my lp file:
\ LP format - for model browsing. Use MPS format to capture full model detail.
Minimize
6.617182815859739 C0 + 7.424312815859739 C1 + 5.358055210573159 C2
+ 7.959418487442079 C3 + 6.30511848744208 C4 + 6.82478565829472 C5
+ 5.752237988597706 C6 + 5.849417988597706 C7 + 3.361525325731804 C8
+ 7.870029561214756 C9 + 5.545819561214756 C10 + 5.311473040809838 C11
+ 0 x_0,0,0 + 0 x_1,0,0 + 0 x_2,0,0 + 0 x_3,0,0 + 0 x_0,0,1 + 0 x_1,0,1
+ 0 x_2,0,1 + 0 x_3,0,1 + 0 x_0,1,0 + 0 x_1,1,0 + 0 x_2,1,0 + 0 x_3,1,0
+ 0 x_0,1,1 + 0 x_1,1,1 + 0 x_2,1,1 + 0 x_3,1,1 + 0 x_0,2,0 + 0 x_1,2,0
+ 0 x_2,2,0 + 0 x_3,2,0 + 0 x_0,2,1 + 0 x_1,2,1 + 0 x_2,2,1 + 0 x_3,2,1
+ [ 1.71572 C0 * C4 + 1.4199 C0 * C7 + 3.03416 C0 * C10
+ 1.71572 C1 * C3 + 1.4199 C1 * C6 + 3.03416 C1 * C9 + 1.71572 C1 * C5
+ 1.4199 C1 * C8 + 3.03416 C1 * C11 + 1.71572 C2 * C4 + 1.4199 C2 * C7
+ 3.03416 C2 * C10 + 3.13562 C3 * C7 + 1.54274 C3 * C10
+ 3.13562 C4 * C6 + 1.54274 C4 * C9 + 3.13562 C4 * C8 + 1.54274 C4 * C11
+ 3.13562 C5 * C7 + 1.54274 C5 * C10 + 1.61426 C6 * C10
+ 1.61426 C7 * C9 + 1.61426 C7 * C11 + 1.61426 C8 * C10 ] / 2
Subject To
R0: C0 + C3 + C6 + C9 = 1
R1: C1 + C4 + C7 + C10 = 1
R2: C2 + C5 + C8 + C11 = 1
R3: C0 + C1 + C2 <= 1
R4: C3 + C4 + C5 <= 1
R5: C6 + C7 + C8 <= 1
R6: C9 + C10 + C11 <= 1
Bounds
Binaries
x_0,0,0 x_1,0,0 x_2,0,0 x_3,0,0 x_0,0,1 x_1,0,1 x_2,0,1 x_3,0,1 x_0,1,0
x_1,1,0 x_2,1,0 x_3,1,0 x_0,1,1 x_1,1,1 x_2,1,1 x_3,1,1 x_0,2,0 x_1,2,0
x_2,2,0 x_3,2,0 x_0,2,1 x_1,2,1 x_2,2,1 x_3,2,1
End
0
-
Official comment
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?. -
Hi Lei,
This error means that your quadratic objective function is not convex. In other words, the \( Q \) matrix in your objective function \( x^\top Q x + c^\top x \) is not positive semi-definite (PSD).
Gurobi 9.0 is able to solve problems with non-convex quadratic constraints and objectives. To enable this, set the NonConvex parameter to 2.
Also, note that you can iterate over multiple indices within a single quicksum() function. E.g.:
m = Model()
x = m.addVars(5, 10, vtype=GRB.BINARY, name="x")
m.addConstr(quicksum(x[i,j] for i in range(5) for j in range(10)) >= 1)I hope this helps. Thanks!
Eli
1
Post is closed for comments.
Comments
2 comments