Untitled

 avatar
unknown
python
2 years ago
1.8 kB
9
Indexable
import pulp as p

model = p.LpProblem("p_m", p.LpMinimize)

L = [1,2,3,4,5]
C = [1,2,3,4,5]
P=2

scost = []
line = [1,2,1,2,2]
scost.append(line)
line = [2,1,3,1,2]
scost.append(line)

line = [2,2,2,1,2]
scost.append(line)

line = [1,1,1,2,1]
scost.append(line)

line = [1,2,3,1,3]
scost.append(line)

actcost = [3,2,3,3,2]

y = []
for i in range(0,len(L)):
    y_ = p.LpVariable(name="y_" + str(i), lowBound=0)
    y.append(y_)
x=[]
for i in range(0,len(L)):
    x_=[]
    for j in range(0, len(C)):
        x_i_j = p.LpVariable(name="x_" + str(i) + "_" + str(j),  lowBound=0)
        x_.append(x_i_j)
    x.append(x_)

print(str(x))

#constraint 1
for j in range(0, len(C)):
    constraint = p.LpAffineExpression()
    for i in range(0, len(L)):
        constraint +=x[i][j]
    model += constraint == 1, "C_"+str(j)

for i in range(0,len(L)):
    for j in range(0, len(C)):
        constraint = p.LpAffineExpression()
        constraint += x[i][j]
        model += constraint <= y[i], "L_" + str(i)+"_"+str(j)

constraint = p.LpAffineExpression()
for i in range(0, len(L)):
    constraint += y[i]
model += constraint == P, "P"

#objective function
obj = p.LpAffineExpression()
for i in range(0, len(L)):
    obj += actcost[i] * y[i]
for i in range(0, len(L)):
    for j in range(0, len(C)):
        obj += scost[i][j] * x[i][j]
model+= obj

print(model)

status = model.solve()
print("Status",p.LpStatus[status])

obj = p.value(model.objective)
print("Objective value : ",obj)

for i in range(0, len(L)):
    vy1 = p.value(y[i])
    if vy1>0:
        print("Location :",str(i+1)," Open ")

for i in range(0, len(L)):
    for j in range(0,len(C)):
        vxij = p.value(x[i][j])
        if vxij>0:
            print("Location :",str(i+1),"\t Customer : "+str(j+1))

Editor is loading...