|
||||||
A Brief Introduction to Linear ProgrammingGetting Started with some Simple Linear Programming ExamplesLinear 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:
Some linear programming examples could be:
Having defined some problems, the next step is to identify the assumptions. Linear Programming AssumptionsIn order order for LP to work it must makes some basic assumptions:
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 ConstraintsAny 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:
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 SolutionsIn 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.
|
||||||
|
|
||||||
|
|
||||||