Linear Programming in APL
Linear Programming in APL
We assume the
Linear Programming in APL variab]es are in the vector X and there are several vectors and
-by c. matrices to be provided.
INF,AR PROGRAMMING HAS A HISTORY OF MANY YEARS w i t h 1. The objective vector is C with C+. xX maximized
Danzig's Simplex algorithm providing the first genera] 2. The array F and vector G such that G>F+. aX
solution. It is this algorithm that is implemented here. 3. The array H and vector U such that U<_H+.xX
The simplest description of the aim of linear programming is 4. The array 2? and vector S such that S=T+. xX
that we wish to maximize a linear function ofn variables,
Now create the vector R
i=1
R=G,U,S,C,(pU),(pS) where the two p's are crucial,
even if zero, and the array (origin 0)
subject to a number ofconstxaints, typically of the form:
M=F,[O]H,[O]2 andnow
~ a~i <b Z~-R LP M
i=I
Since these problems originated in the worm ofeconomics, the Provided there/s a solution, the final value of the objective is
variables x i are assumed to be positive, while all the other con- printed and a long vector ofresuhs is returned.
stants are not limited. The inequalities may be either direction or
equalities, with no upper limit to the number (other than The desired vector X is:
practicality). Use of any package requires care as to maximizing ( pX ) t Z the remaining numbers are described later.
or minimizing the objective, and a ~equent constraint is that all
the right sides (bj) must also be positive, and/or all inequalities The value of zero for the algorithm is used in fines 10 and 11, the
are only one direction. value 100000 in line 5 is, essentially, ® used to get started inside
The simplest description of the Simplex algorithm is that the the convex shape.
constraints determine a convex shape bounded by hyperplanes,
and the objective must be maximized on the boundary, and thus V X~B LP A;OIO;V;C;S;B;G;I;W;Z;H;Q
at one of the vertices. The algorithm first gets inside that shape [1] aio~o o A~(-I*O>(tpA)tR)*[O]A o V~-2~R
[2] G*(((tpA)-+/V)tR,-V[O]t(-+/V),(tpA)tR
(which is not intuitively easy to do in many cases), then gets to a [3] H*(2p~pA)pO o (0 0 ~H)~aG*(G+G=O),((tpA)-pG)pl
vertex and searches n.eighbouring vertices, always increasing the [4] A*A,H*H,(+NG)$(+/G)t[1],['']G*O>G
[5] C~C-(-pC~(l~pA)t(tpA)*-2*R)t(Q~V[l]++/G)plO0000
objective as it goes. Allowances must be made for adjacent vertex [6] 2~~S~(-l~pA)t-l^.<H
sets having the same objective values. The solution is found [7] R~(toA)t[R
[8] L P : ~ ( O ^ . > X ~ B ~ S / A ) / E B R
when the objective can increase no further, subject to the obvious
[9] G*2k(Vi[/V)=tpV*T/C-((S/C)[]~B)+..A
caveat of being within fuzz limits. There is also no guarantee of [10] ~(O.O0001zF/V)/DONE
a unique solution (again because offlat spots). [11] ~(O.O0001^.zZ~(,G/A)~B)/UNB
[12] H~(W/X)÷(W*Z>IE-13)/Z
[13] ~~S*(S^~SkWk(HiL/H)=ipB)vG
There are three possible outcomes: [14] ~LP
[15] DONE:t(Ov.<(-Q)tX~SkX)/'''Ihere are no feasible
solutions" o ~0'
1. There is no solution, i.e., the convex body is empty. [15] 'Done',~C+.xX o ~0
[17] ERR:~O,pD~'Error'
2. The solution is unbounded, i.e., the objective has no upper [18] UNB:~O,pD~'Unbounded'
V
limit.
3. A solution is found. T h e algorithm is implemented by adding or subtracting a
non-negative "alack variable" to each inequality, thus turning all
Note that since this is a purely numeric algorithm (contrast this inequalities into equalities. T h e solution vector Z now contains
with small problems in "Maple"), all coefficients must be present pX solutions followed by the final value of ( t pM)- t p2 slack
even if zero. All variables are also assumed to he non-negative, 1 variables. The remaining numbers in Z are the added variables
that allow for starting inside the convex shape. These slack and
added variables are also put into the objective function, and the
added variables must all become non-positive if there is to be a
t Ifyou need negatives, introduce one new variable, say3, and then replace
feasible solution.
all the x i that can be negative with x i - 3 " T h e r e is one extra term ill the
objective - ~ ' c i summed over those c i for for which x i can be negative, and Those of you interested in economic studies can use the
do the appropriate subtraction when the solution is found. "slack" variables for various forms of"marginal analysis."
SOLITON
ASSOCIATES
Your5of~,vare.SolutionsCompany
APL+WIN N o w Supporting W'mdows 95 Controls
in Windows 95, N T and Windows 3.1.
SHARP APt is superior in the rapid development of Same Workspace, Same Inlm'lxeter for
All Versions of Windows.
mission.critical applications which meet the ever- A ~ L ~ Your 32 Bit SQL / O D B C Connection.
changing demands of our customers. APL+UNIX New and Improved.
Includes Control Slmctures
APL+DOS Updated in 1996.
Soliton Associates provides SHARP APL for MVS and UNIX with: ConsulUng Solutions Mainframe & D O S to Windows
Application Design and Turnkey
• Superior productivity Development Sorviens.
• Powerful cooperative processing Out_Bourne Suppli~ for Legacy A P L
Servicns: Opm'atinn,Maintenance &
• Applications for end-users and programmers Incremental Development
• Responsive support services
Con~aut Pm~amminE