Problem Description
This problem is from
Linear Programming Problems and Solutions
.
Please read the information
on the web page before proceeding.
A school is preparing a trip for 400 students.
- The transportation company has 10 buses of 50 seats each and 8
buses of 40 seats.
- Only 9 drivers are available.
- The rental cost for a large bus is 800 and 600 for a small bus.
Calculate how many buses of each type that minimizes
the cost of the trip.
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.)
- number of drivers
- bus costs
- bus capacity
#!/usr/bin/python3
# ====================================================================
# Bus Capacity Problem using PuLp
#---------------------------------------------------------------------
# problem description
#
# ---- x = number of small buses
# ---- number of buses is 8
# ---- bus capacity is 40
# ---- cost per bus 600
# ---- y = number of large buses
# ---- number of buses is 10
# ---- bus capacity is 50
# ---- cost per bus 800
# ----
# ---- number of students 400
# ---- number of drivers 9
#
# minimize bus rental cost
# --------------------------------------------------------------------
# To see information on Pulp classes go to
# https://www.coin-or.org/PuLP/pulp.html
# ====================================================================
import pulp as pl
# ---- define LP variables
x = pl.LpVariable('x',0,8,cat='integer')
y = pl.LpVariable('y',0,10,cat='integer')
# ---- define LP problem
prob = pl.LpProblem('BusCapacityProblem', pl.LpMinimize)
# ---- add objective function
prob += 600*x + 800*y
# ---- add constraints
prob += 40*x + 50*y >= 400
prob += x + y <= 9
# ---- 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} small buses')
print(f'{y} large buses')
print(f'total cost = {600*x + 800*y}')