solution_140f.py

#!/usr/bin/python3
# ====================================================================
# Truck transportation problem
# ---- x = number of type A trucks
# ----     refrigerated capacity     of 20 cubic meters
# ----     non-refrigerated capacity of 30 cubic meters
# ----     cost per kilometer is 30
# ----     cost per kilometer (original problem is 40)  ***********
# ---- y = number of type B trucks
# ----     refrigerated capacity     of 30 cubic meters
# ----     non-refrigerated capacity of 30 cubic meters
# ----     cost per kilometer is 40
# ---- z = number of type C trucks
# ----     refrigerated capacity     of 20 cubic meters
# ----     non-refrigerated capacity of 55 cubic meters
# ----     cost per kilometer is 20
# ----
# ---- ship refrigerated     3000 cubic meters
# ---- ship non-refrigerated 4000 cubic meters
# 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
import math

# ---- define variables

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

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

z = pl.LpVariable('z',0,None,cat='integer')

# ---- define problem

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

# ---- add objective function (minimum cost per kilometer)

##prob += 30*x + 40*y + 20*z
prob += 30*x + 30*y + 20*z

# ---- add constraint refrigerated capacity required

prob += 20*x + 30*y + 20*z >= 3000

# ---- add constraint non-refrigerated capacity required

prob += 40*x + 30*y + z*55 >= 4000

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

status = prob.solve()

print(f'status = {pl.LpStatus[status]}')

# ---- describe solution

x = pl.value(x)
y = pl.value(y)
z = pl.value(z)

print()
print('---- solution from web site --------------------')
print('  original problem only had x,y variables')
print('type A   = 51 trucks')
print('type B   = 66 trucks')
print('min cost = 4170 per kilometer')
print('---- solution calculated -----------------------')
print(f'{math.ceil(x)} type A trucks')
print(f'{math.ceil(y)} type B trucks')
print(f'{math.ceil(z)} type Z trucks')
print(f'min cost = {round(30*x + 40*y + z*20)} per kilometer')
print('------------------------------------------------')