solution_140b.py

#!/usr/bin/python3
# ====================================================================
# Bus Capacity Problem
# ---- 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 cost
# --------------------------------------------------------------------
# Problem From : www.superprof.co.uk/resources/academic/maths/
#                linear-algebra/linear-programming/
#                linear-programming-problems-and-solutions.html
# ====================================================================

import pulp as pl

##print('PuLp installers:')
##print(pl.listSolvers())
##print('PuLp installers currently available:')
##print(pl.listSolvers(onlyAvailable=True))
##print()

# define variables

# ---- x = number of small busses
# ---- y = number of large busses

x = pl.LpVariable('x',0,8,cat='integer')

y = pl.LpVariable('y',0,10,cat='integer')

# ---- define problem

prob = pl.LpProblem('BusCapacityProblem', pl.LpMinimize)

# ---- add objective function (minimize cost)

prob += 600*x + 800*y

# ---- add constraints

prob += 40*x + 50*y >= 401

prob += x + y <= 9

# ---- solve problem (using default solver)

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}')