I have the following code in Python and PuLP which uses static variables. I want to know how to solve the problem by converting all of the LpVariable parts into an array, as well as the constraints. The idea is to be able to add new elements to the LpVariable and constraints array and having the solver run through the arrays rather than having to manually code new lines if another type of bicycle needs to be produced or additional parts are added as constraints.
#Bicycle Manufacturing Schedule
from pulp import *
# The challenge is to maximise the profit by producing an optimum number of each type of bicycle
# A - "Mountain Bike", profit margin of $45 per unit
# B - "Street Bike", profit margin of $60 per unit
# C - "Racing Bike", profit margin of $55 per unit
# D - "Commuter bike", profit margin of $50 per unit
#
model = LpProblem("Profit Maximising", LpMaximize)
#We can't sell half a bike, so the category for each bicycle type is an integer.
#A >= 0, B >= 0, C >= 0, D >= 0 : Input (Changing) cells
A = LpVariable('A', lowBound=0, cat='Integer')
B = LpVariable('B', lowBound=0, cat='Integer')
C = LpVariable('C', lowBound=0, cat='Integer')
D = LpVariable('D', lowBound=0, cat='Integer')
#Our objective is to maximise the profit
model += 45 * A + 60 * B + 55 * C + 50 * D, "Total Profit"
#Setup the constraints (parts available in stock)
wheels = 2 * A + 2 * B + 2 * C + 2 * D
alloy_chassis = 1 * A + 1 * C
steel_chassis = 1 * B + 1 * D
hub_gears = 1 * A + 1 * D
derailleur_gears = 1 * B + 1 * C
#Add constraints to the model
model += wheels <=180
model += alloy_chassis <= 40
model += steel_chassis <= 60
model += hub_gears <= 50
model += derailleur_gears <= 40
#Print the problem
print (model)
#Solve the problem
model.solve()
print ("Status : ", LpStatus[model.status])
#Print our changing cells
print ("Units of Mountain Bike = ", A.varValue)
print ("Units of Street Bike = ", B.varValue)
print ("Units of Racing Bike = ", C.varValue)
print ("Units of Commuter Bike = ", D.varValue)
#Print our objective function value - Result (Target) cell
print ("Total Profit = ", value(model.objective))
EDIT 01-FEB-2021
I have converted the code to use arrays instead of fixed variables.
#Bicycle Manufacturing Schedule
from pulp import *
# The challenge is to maximise the profit by producing an optimum number of each type of bicycle
# A, profit margin of $45 per unit
# B, profit margin of $60 per unit
# C, profit margin of $55 per unit
# D, profit margin of $50 per unit
bike_types = ["A", "B", "C", "D"]
bike_profit = [45, 60, 55, 50]
parts_name = ["wheels", "alloy_chassis", "steel_chassis", "hub_gears", "derailleur_gears"]
parts_stock = [180, 40, 60, 50, 40]
bike_parts = [ #part_names
#"wheels", "alloy_chassis", "steel_chassis", "hub_gears", "derailleur_gears"
[2,1,0,1,0],#Bike type A
[2,0,1,0,1],#Bike type B
[2,1,0,0,1],#Bike type C
[2,0,1,1,0] #Bike type D
]
prob = LpProblem("Profit Maximising", LpMaximize)
n = len(bike_profit)
N = range(n)
#We can't sell half a bike, so the category for each bicycle type is an integer.
x = LpVariable.dicts("x", N, lowBound=0, cat='Integer')
#Our objective is to maximise the profit
prob += lpSum([bike_profit[i]*x[i] for i in N]), "Total Profit"
#Constraints on available bike parts
for p in range(len(parts_name)):
prob += lpSum(bike_parts[i][p]*x[i] for i in N) <= parts_stock[p], parts_name[p]
#Print the problem
print (prob)
#Solve the problem
prob.solve()
print ("Status : ", LpStatus[prob.status])
#Print our changing cells
for i in range(len(bike_types)):
print ("Units of ", x[i],"= ", x[i].varValue)
#Print our objective function value - Result (Target) cell
print ("Total Profit = ", value(prob.objective))