Linear Equations using the Simplex Method

Version/Date: Oct 3, 2017

Exercise

PREDICT_400-DL_SEC56 Week3 Module

File(s)

Linear Equations using Simplex.ipynb

Instructions

Present a model involving minimization that contains three or more equations (ideally a system that you have come across professionally or personally) and solve the system using the Simplex Method and again using Python. Be sure to share your Python code and output.

Reference

I will be using a modification of my example from Week2. See details here: Wk2 Linear Eq Example

Description

The code below shows an example of three product models and an attempt to minimize the cost.

Goal: Cost minimization of the objective function: C = 18y1 + 20y2 + 25y3

This function represents the total cost for three different products manufactured and sold by our company.

The constraints for this linear program example:

120y1 + 160y2 + 200y3 >= 75000

yy1 + y2 + y3 >= 450

y1 + 2y2 >= 300

y1, y2, y3 >=0

The first constraint function gives the total revenue target (at least $75,000 USD). The second limiting function gives the minimum total number of units we want to produce with this batch (at least 450). Also given demand for the first two models is high, we must make at least 300 between y1 and y2 units. Based on past sales, we expect we'll need about twice as many y2 as y1 models. Obviously, y1, y2, y3 all must be >= 0.

How many of each units should we produce to limit minimize our costs?


In [69]:
# Cost objective function is:
# C = 18y1 + 20y2 + 25y3
# Rewrite Constraints:
# Revenue: 120x1 + 160x2 + 200x3 >=75000
# Numbers: x1 + x2 + x3 >= 450
# Condition: x1 + 2x2 >= 300
# x1,x2,x3 >= 0

# coefficients of the objective function
z = [18,20,25]

# left-hand coefficients - tableau matrix
A = [[-120,-160,-200],[-1,-1,-1],[-1,-2,0]]
print(A)

# right-hand coefficients - 
b = [-75000,-450,-300]
print(b)

from scipy.optimize import linprog as lp

x1_bounds = (0,None)
x2_bounds = (0,None)
x3_bounds = (0,None)

res = lp(z, A_ub=A, b_ub=b, bounds=(x1_bounds, x2_bounds, x3_bounds), method='simplex', options={"disp": True})
print(res)


[[-120, -160, -200], [-1, -1, -1], [-1, -2, 0]]
[-75000, -450, -300]
Optimization terminated successfully.
         Current function value: 9375.000000 
         Iterations: 4
     fun: 9375.0
 message: 'Optimization terminated successfully.'
     nit: 4
   slack: array([   0.,    0.,  450.])
  status: 0
 success: True
       x: array([   0.,  375.,   75.])

In [70]:
# Based on these calculations, if we want to hit our total revenue target of $75,000, meeting the other constraints and 
# minimizing cost, we should produce zero y1 units, 375 y2 units and 75 y3 units. The min cost would be approximately $9,375.