Problem Description
This problem is from
Linear Programming Problems and Solutions
.
Please read the information
on the web page before proceeding.
A transport company has two types of trucks, Type A and Type B.
- Type A has a refrigerated capacity of 20 m3 and a non-refrigerated
capacity of 40 m3.
- Type B has the same overall volume with equal refrigerated and non-refrigerated
stock sections.
- The cost per kilometer of Type A is 30, and 40 for Type B.
A grocer must hire trucks to transport 3000 m
3
of refrigerated stock and 4000 m
3 of non-refrigerated stock.
How many trucks of each type should the grocer rent to achieve the minimum
total cost?
Problem #0
Install the Python PuLp library. Use a virtual environment for the project?
pip install pulp
or
python -m pip install pulp
Problem #1
This code uses the Python PuLp library to solves the problem in the
"Problem Description".
First, test the code to verify it works.
Second, create an interactive program that allow the user to modify
the input parameters and calculate different solutions.
(Set some reasonable limits. No zero or negative values.)
- truck refrigerated and non-refrigerated volume
- truck cost per kilometer
- add a third type of truck
#!/usr/bin/python3
# ====================================================================
# Truck transportation problem using PuLp
# --------------------------------------------------------------------
# problem description
#
# ---- x = number of type A trucks
# ---- refrigerated capacity of 20 cubic meters
# ---- non-refrigerated capacity of 40 cubic meters
# ---- cost per kilometer is 30
# ---- y = number of type B trucks
# ---- refrigerated capacity of 30 cubic meters
# ---- non-refrigerated capacity of 30 cubic meters
# ---- cost per kilometer is 40
# ----
# ---- ship refrigerated 3000 cubic meters
# ---- ship non-refrigerated 4000 cubic meters
#
# minimize transportation cost
# --------------------------------------------------------------------
# To see information on Pulp classes go to
# https://www.coin-or.org/PuLP/pulp.html
# ====================================================================
import pulp as pl
# ---- define LP variable
x = pl.LpVariable('x',0,None,cat='integer')
y = pl.LpVariable('y',0,None,cat='integer')
# ---- define LP problem
prob = pl.LpProblem('TransportationCompanyProblem', pl.LpMinimize)
# ---- add objective function
prob += 30*x + 40*y
# ---- add constraint refrigerated capacity required
prob += 20*x + 30*y >= 3000
# ---- add constraint non-refrigerated capacity required
prob += 40*x + 30*y >= 4000
# ---- solve the problem
status = prob.solve()
print(f'status = {pl.LpStatus[status]}')
# ---- describe solution
x = pl.value(x)
y = pl.value(y)
print(f'{x} type A trucks')
print(f'{y} type B trucks')
print(f'min cost = {30*x + 40*y} per kilometer')