The below code shows syntax error after for loop at this line
model += x[int((str((i*2)-1)+str(j)))] + x[int((str(i*2)+str(j)))]] <= 1
i want the variable to be declared like 2 D array x11, x12,x13 am i getting error because of this.
KeyError
Traceback (most recent call last)
<ipython-input-95-19b3a6e81910> in <module>()
19 for i in range (1, (Box//2)+1):
20 for j in range (1,Pallet+1):
---> 21 model += x[int((str((i*2)-1)+str(j)))] + x[int((str(i*2)+str(j)))] <= 1
22
23
KeyError: 11
i checked all possible option everything seems right someone please help.
'''
from pulp import *
Box=6
Pallet=3
Variable_range=Box*Pallet
from pulp import LpMaximize, LpProblem, LpStatus, lpSum, LpVariable
# Define the model
model = LpProblem(name="Container Loading", sense=LpMaximize)
# Define the decision variables
for i in range(1, Box+1):
for j in range (1,Pallet+1):
x = {int((str(i)+str(j))):LpVariable(name=f"x{i}_{j}",lowBound=0,upBound=1,cat='Integer')}
print(x)
# Add constraints
for i in range (1, (Box//2)+1):
for j in range (1,Pallet+1):
model += x[int((str((i*2)-1)+str(j)))] + x[int((str(i*2)+str(j)))]] <= 1 # error at this line
#Set the objective
model += lpSum(x.values())
# Solve the optimization problem
status = model.solve()
# Get the results
print(f"status: {model.status}, {LpStatus[model.status]}")
print(f"objective: {model.objective.value()}")
for var in x.values():
print(f"{var.name}: {var.value()}")
for name, constraint in model.constraints.items():
print(f"{name}: {constraint.value()}")
'''
This is about misunderstanding loops. In:
# Define the decision variables
for i in range(1, Box+1):
for j in range (1,Pallet+1):
x = {int((str(i)+str(j))):LpVariable(name=f"x{i}_{j}",lowBound=0,upBound=1,cat='Integer')}
print(x)
you each time overwrite x. So you end up x containing just one element. You can see this by moving the print statement to after the loop.
Better is:
# Define the decision variables
x = {int((str(i)+str(j))):LpVariable(name=f"x{i}_{j}",lowBound=0,upBound=1,cat='Integer')
for i in range(1, Box+1) for j in range (1,Pallet+1) }
print(x)
Try to use this code below. I have changed the style of defining of decision variables. It is not throwing error.
from pulp import *
Box=6
Pallet=3
Variable_range=Box*Pallet
x = {}
from pulp import LpMaximize, LpProblem, LpStatus, lpSum, LpVariable
# Define the model
model = LpProblem(name="Container Loading", sense=LpMaximize)
# Define the decision variables
for i in range(1, Box+1):
for j in range (1,Pallet+1):
x[(i,j)] = pulp.LpVariable('x' + str(i) + '_' + str(j), 0, 1, LpBinary)
print(x[(i,j)])
# Add constraints
for i in range (1, (Box//2)+1):
for j in range (1,Pallet+1):
model += x[(i*2-1,j)] + x[(i*2,j)] <= 1 # error at this line
#Set the objective
model += lpSum(x.values())
# Solve the optimization problem
status = model.solve()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.