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)
All the above constraints except the difference of list and absolute of the difference constraint works fine.
The model becomes infeasible only when I add the difference and absolute constraints to the model.
Infeasiblity occurs at some point which shouldn't occur. What I did wrong?