How to find absolute difference of list and use those values in maximization optimization model guobipy?
回答済み
#Following is my code where I try absolute operations of two lists based on some conditions and then maximize the summation of those.
m=[5,3,2]
cm=[sum(m[0:x:1]) for x in range(1, len(m)+1)]
P=len(m)
p = range(len(m))
N=sum(m)
n = range(N)
sett=[0 for i in p]
for i in p:
if(i==0):
sett[i]=range(0,cm[i])
else:
sett[i]= range(cm[i-1],cm[i])
#model for optimization
with grb.Env() as env, grb.Model(env=env) as o:
o = grb.Model()
o.Params.LogToConsole =0
o.Params.OutputFlag =0
x = {}
for i in n:
for j in n:
x[i,j] = o.addVar(vtype=grb.GRB.BINARY, name='x'+str(i)+'_'+str(j))
c = {}
for j in n:
c[j] = o.addVar(vtype=grb.GRB.INTEGER, name='c'+str(j))
difc = {}
for j in n:
difc[j] = o.addVar(vtype=grb.GRB.INTEGER, name='difc'+str(j))
adifc = {}
for j in n:
adifc[j] = o.addVar(vtype=grb.GRB.INTEGER, name='adifc'+str(j))
sc = {}
for i in p:
sc[i]=o.addVar(vtype=grb.GRB.CONTINUOUS, name='sc'+str(i))
z=0
for i in p:
for j in range(0,m[i]):
o.addConstr((grb.quicksum(x[z,k] for k in range(int(((j)*N/m[i])),int(((j+1)*N/m[i])))) == 1))
z=z+1
for j in n:
o.addConstr((grb.quicksum(x[i,j] for i in n)) == 1)
z=0
for j in n:
o.addConstr(c[j]== grb.quicksum((j+1)* x[z,j] for j in range(0,N)))
z=z+1
z=0
for i in p:
for j in range(0,m[i]):
o.addConstr( difc[z]== c[z]-m[i] )
z=z+1
for j in n:
o.addConstr(adifc[j]== abs_(difc[j]))
for i in p:
o.addConstr((sc[i] == (sum((adifc[z]) for z in sett[i]))))
objective =(grb.quicksum(sc[i] for i in p))
o.ModelSense = grb.GRB.MAXIMIZE
o.setObjective(objective)
o.update()
o.write('mymodel.lp')
o.write('mymodel.mps')
t1=process_time()
o.optimize()
t2=process_time()
o.computeIIS()
o.write("infeasibility_.ilp")
print(o.objVal)
# I get that the model is infeasible. Can you please help where I did mistake?
0
-
正式なコメント
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?. -
From some suggestions, I made all variables as Continuous and then I checked it. It works for minimization objective but not for maximization.
0 -
Hi,
The default lower bounds for continuous and integer variables are set to 0 in Gurobi.
Adding
lb=-grb.GRB.INFINITY
to all integer and continuous variables in your model makes it feasible.
Best regards,
Jaromił0 -
Yes, I did worked. Thankyou.
0
投稿コメントは受け付けていません。
コメント
4件のコメント