About the gap does not decrease after optimization
AnsweredHi everyone,
I'm trying to solve a model. Max log2(1 + ∑ 1 / Xj), which Xj = ||q[j] - s[1]|| ** 2, (1<=j <=4)
log2(1 + 1 / x) is convex with x and I can get the optimal solution which about MAX(log2(1 + 1 / x)(1 <= x <=3) by using GUROBI. Then I use the above method to solve the initial problem.
This is my code.
s = np.array([[-250, -250, 0], [-250, 250, 0], [250, -250, 0], [250, 250, 0]])
q = model.addVars(4, 3, lb=-500, vtype=GRB.CONTINUOUS, name='q')
h = model.addVars(4, vtype=GRB.CONTINUOUS, name='h')
z = model.addVar(lb=-500, vtype=GRB.CONTINUOUS, name='z')
y = model.addVar(vtype=GRB.CONTINUOUS, name='y')
l = model.addVars(4, vtype=GRB.CONTINUOUS, name='l')
m = model.addVars(4, vtype=GRB.CONTINUOUS, name='m')
model.update()
model.setObjective(z, sense=GRB.MAXIMIZE)
for j in range(K):
rhs = QuadExpr()
rhs.addTerms(1, q[j, 0], q[j, 0])
rhs.addTerms(-2 * s[1][0], q[j, 0])
rhs.addTerms(1, q[j, 1], q[j, 1])
rhs.addTerms(-2 * s[1][1], q[j, 1])
rhs.addTerms(1, q[j, 2], q[j, 2])
rhs.addTerms(-2 * s[1][2], q[j, 2])
model.addConstr(h[j] == rhs + s[1][0] ** 2 + s[1][1] ** 2 + s[1][2] ** 2)
model.addConstr(l[j] * h[j] == 1)
model.addConstr(m[1] == quicksum(l[j] for j in range(K)))
model.addGenConstrLogA(m[1], z, 2, "log2")
for k in range(K):
model.addConstr(q[k, 2] == 100)
# model.addConstr(q[k, 2] <= 300)
model.addConstr(q[k, 0] <= 500)
model.addConstr(q[k, 0] >= -500)
model.addConstr(q[k, 1] <= 500)
model.addConstr(q[k, 1] >= -500)
model.setParam('NonConvex', 2)
model.optimize()
for v in model.getVars():
print('var name is {}, var value is {}'.format(v.VarName, v.X))
In the process of solving, the value of gap does not decrease, which is where the problem is. If you can help me, I will be very grateful to you.


-
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?. -
At the same time, I also feel that my code is complex. What can be simplified?
0 -
Hi Yanbo,
It is necessary to provide tight valid lower and upper bounds when working with nonlinear terms. In your particular case, it is enough to provide lower and upper bounds for the \(\texttt{l,h}\) variables, e.g.,
h = model.addVars(4, lb=1e-4, ub=10000, vtype=GRB.CONTINUOUS, name='h')
l = model.addVars(4, lb=1e-4, ub=10000, vtype=GRB.CONTINUOUS, name='l')This already solves the problem, however with rather large constraint violations. This may happen with highly nonlinear expression which are reformulated as piecewise linear by Gurobi. Lowering the FeasibilityTol
model.setParam("FeasibilityTol", 1e-7)to \(\texttt{1e-7}\) seems to be enough in this case.
Best regards,
Jaromił0 -
Hi,
Thank you for your reply, I was too anxious to cause my problem. My problem has now been successfully solved, the optimal solution is obtained after running for 600 seconds, and the source code can be successfully solved. Thanks to the powerful function of gurobi, we can solve this nonconvex problem.
Best regards,
Yanbo
0
Post is closed for comments.
Comments
4 comments