0

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?

Deepan
  • 9
  • 4
  • And adding to that, for minimization objective, it works fine. But my objective is maximization. So I need to know what should be done for my objective – Deepan Sep 22 '21 at 17:13

1 Answers1

2
difc[j] = o.addVar(vtype=grb.GRB.INTEGER, name='difc'+str(j))

By default, this is a non-negative variable (see https://www.gurobi.com/documentation/9.1/refman/py_model_addvar.html).

This is likely not correct, as you use it to model a difference and take the absolute value.

 o.addConstr( difc[z]== c[z]-m[i] )
 o.addConstr(adifc[j]== abs_(difc[j]))

You probably should make difc a free variable. And there is no reason to make it discrete.

You need to be very careful implementing models and understand fully everything. Remember that we solve systems of equations. Any mistake somewhere can create havoc. Much more so than in normal sequential programming. So extra care is warranted. Sloppy programming will be punished.

Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39
  • Ok, I didnt know that it will considered as non-negative variable. I made it as free variable. Even I did get the infeasible error. So, can you help on how to solve the issue? – Deepan Sep 22 '21 at 16:55
  • And adding to that, for minimization objective, it works fine. But my objective is maximization. So what changes to be done for my objective – Deepan Sep 22 '21 at 17:13
  • Mathematically, the objective should not impact feasibility. – Erwin Kalvelagen Sep 22 '21 at 17:43
  • In some cases Gurobi cannot make a distinction between infeasible and unbounded. May be your maximization model is unbounded. That is easily fixed by adding a bound on the objective. – Erwin Kalvelagen Sep 23 '21 at 03:37