A Brief Introduction to Linear Programming

Getting Started with some Simple Linear Programming Examples

© Mark Alexander Bain

Aug 13, 2009
A Brief Introduction to Linear Programming, Mark Alexander Bain
Linear programming is a mathematical technique well suited to any programming language, and the best way to understand it is to look at some linear programming examples

Linear programming (or LP) is a mathematical problem solving technique that:

  • makes various assumptions about a set of data
  • works within well defined constraints
  • produces a solution by modelling these assumptions and constraints

Some linear programming examples could be:

  • a shop has a set budget of to fill a set of shelves and two products with different cost prices. How should the shop distribute the products to maximise their profits?
  • a coal fired power station can use two different types of coal; one is cheaper but more polluting; the other is more expensive but burns cleaner. How can the power station produce the most electricity for the least cost and with the minimum pollution?
  • a taxi driver can take a longer route with faster flowing traffic or a shorter route with slower traffic. Which one is most cost effective?

Having defined some problems, the next step is to identify the assumptions.

Linear Programming Assumptions

In order order for LP to work it must makes some basic assumptions:

  • divisibility - all variables must be real
  • non-negativity - a variables must be positive
  • linearity - the relationships between any variables must be linear

If the linear programming examples fulfil these criteria then the next step is to understand any constraints imposed on the variables (often referred to as the decision variables).

Linear Programming Constraints

Any decision variables will have constraints imposed upon them by the LP assumptions, for example:

x > 0
y > 0

Other constraints will be imposed by the problem itself. For instance, the constraints for the shop in the first of the above linear programming examples could be:

  • a budget of $2,500 for filling the shelves:
  • item x costs the shop $20 and item y costs $35
  • item x gives a profit of $8 and y $12
  • the shelves can contain a maximum of 100 items

So, the budget constraint is:

20x + 35y = 2500

And if the shelves can only fit 100 items then:

x + y <= 100

And, of course, there is a constrain due to the profit to be made. So if the shop makes $8 on item x and $12 on item y then:

z = 8x + 12y

where z is the maximum profit.

Linear Programming Solutions

In order to solve these linear programming examples the programmer needs a generic model that takes account of the assumptions and constraints but without any specific values. For example it could be some simple VBScript:

sub lp (max_items, cost_x, profit_x, profit_y, budget)
lp_z = 0
lp_x = 0
lp_y = 0
for x = 1 to max_items - 1
y = max_items - x
z = profit_x * x + profit_y * y
t = cost_x * x + cost_y * y
if t <= budget then
if z > lp_z then
lp_z = z
lp_y = y
lp_x = x
end if
end if
next
wscript.echo "z = $" & lp_z
wscript.echo "x = " & lp_x
wscript.echo "y = " & lp_y
end sub

It's then just a matter running the subroutine with the specific values for the linear programming example:

max_items = 100
profit_x = 8
profit_y = 12
cost_x = 20
cost_y = 35
budget = 2500
lp max_items, cost_x, profit_x, profit_y, budget

In this linear programming example the optimal values of x an y will be calculated to be 67 and 33 (as shown in figure 1) and the profit (if everything is sold) will be $932. This generic model can then be used to see the effect of, for example, raising the cost of item y to $40. This time x will be 75, y will be 25 and the profit $900.

The linear programming examples in this article have, of course, been very simple. In the real world there will be several, if not hundreds, of decision variables and constraints. However, the examples do show how easy it is to build a generic model from the constraints and the assumptions, and that this model can then be applied to any set of similar values.


The copyright of the article A Brief Introduction to Linear Programming in Computer Programming is owned by Mark Alexander Bain. Permission to republish A Brief Introduction to Linear Programming in print or online must be granted by the author in writing.


A Brief Introduction to Linear Programming, Mark Alexander Bain
Figure 1: A Linear Programming Example, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo