solve an optimization problem n times with a contraint that varies for a constant (eps)
回答済みfrom gurobipy import GRB
from gurobipy import Model
import math
m = Model('pyOA_e2')
UB = 2
LB = -3
x = m.addVar(name = 'x', lb = -2, ub= 2)
y = m.addVar(name = 'y', lb=-2, ub = 2)
u = m.addVar(name = 'u', lb = -2, ub = 2)
z = m.addVar(name = 'z')
m.update()
n=11
eps_min = -math.sqrt(3) + math.exp(-2)
eps_max = math.exp(2)
eps =[eps_min]
for i in range(n-1):
eps+= [eps[i] + (eps_max-eps_min)/(n-1)]
i= 1+ i
print(eps)
e=0
while e <= len(eps)-1:
m.addGenConstrExp(u, z)
m.addConstr(y + z - eps[e]<=0)
m.addConstr(x**2 + (1/3)*(y**2) - 1 <=0)
m.setObjective(x-u)
m.optimize()
if m.Status == GRB.OPTIMAL:
LB = m.ObjVal
m.printAttr('x')
print(m.ObjVal)
print(eps[e]) e= e+1
print(e)
Can you help me understand why if I compute this code, I always get the same solution (n times) instead of having different solution for each value of eps?
Thank you in advance
-
正式なコメント
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Paola,
You should write out the models in every iteration and then check whether they actually represent what you intend to solve.
Just add this line to the loop:
m.write(f"model_{e}.lp")Cheers,
Matthias0 -
Hi Matthias,
Thank you for your fast reply. Unfortunately I added that line inside the loop but still have the same result of before (n time the same solution, instead of n different solutions as I vary the value of eps) The code inside the while loop works: if I change manually the eps value and compute the code for each value I obtain what I want. But I need to automatise it in a while or for loop. Can you help me with that?
Thank you. Best,
Paola
0 -
Hi Paola,
You actually need to investigate and compare those generated LP files to see whether they do what you want them to do.
Just adding a write statement to the code is of course not fixing the issue.
Cheers,
Matthias0 -
For example this is the code for a fixed value of eps (in particular for eps equal to the value in the second position of the array eps[e] and it works fine:
from gurobipy import GRB
from gurobipy import Model
import math
m = Model('pyOA_e2')
UB = 2
LB = -3
eps = -math.sqrt(3) + math.exp(-2) + (math.exp(2) +math.sqrt(3) - math.exp(-2))/(n-1)
print(eps)
x = m.addVar(name = 'x', lb = -2, ub= 2)
y = m.addVar(name = 'y', lb=-2, ub = 2)
u = m.addVar(name = 'u', lb = -2, ub = 2)
z = m.addVar(name = 'z')
m.update()
def g(y,z):
return y + z - eps
m.setObjective(x-u)
m.addGenConstrExp(u, z)
m.addConstr(g(y,z)<=0)
m.addConstr(x**2 + (1/3)*(y**2) - 1 <=0)
m.optimize()
if m.Status == GRB.OPTIMAL:
LB = m.ObjVal
m.printAttr('x')
print(m.ObjVal)
elif m.Status == GRB.INFEASIBLE:
print('Model MINLP is infeasible')I need to do it in a loop for all the values of the array eps[e]. Is my issue clear? Because I'm not understanding the reply.
Thanks. Best, Paola
0 -
Hi Paola,
There is only one constraint that is affected by eps. Instead of adding this modified constraint to your problem in every iteration of the loop, you should just change the right-hand side and then optimize again:
[...]
m.addGenConstrExp(u, z)
m.addConstr(x**2 + (1/3)*(y**2) - 1 <=0)
epsconstr = m.addConstr(y + z <=0)
m.setObjective(x-u)
m.setParam("OutputFlag", 0)
for e in eps:
epsconstr.RHS = e
m.optimize()
if m.Status == GRB.OPTIMAL:
LB = m.ObjVal
print(e)
print(m.ObjVal)I hope that helps.
Cheers,
Matthias1
投稿コメントは受け付けていません。
コメント
6件のコメント