Knapsack problem/Continuous: Difference between revisions

m
→‎version 1: added/changed whitespace and comments, shorted the SORTD subroutine.
m (→‎version 1: added/changed whitespace and comments, shorted the SORTD subroutine.)
Line 2,248:
Originally used the Fortran program as a prototype.
<br>Some amount of code was added to make the output pretty.
<lang rexx>/*REXX program solves the (continuous) burglar's knapsack problem. */
@.= /*═══════ name weight value ══════*/
@.1 = 'flitch 4 30 '
@.2 = 'beef 3.8 36 '
@.3 = 'pork 5.4 43 '
@.4 = 'greaves 2.4 45 '
@.5 = 'brawn 2.5 56 '
@.6 = 'welt 3.7 67 '
@.7 = 'ham 3.6 90 '
@.8 = 'salami 3 95 '
@.9 = 'sausage 5.9 98 '
parse arg maxW d . /*get possible argsarguments from the C.L. */
if maxW=='' | maxW==',' then maxW=15 /*the burglar's knapsack maxmaximum weight. */
if d=='' | d==',' then d= 3 /*#number of decimal digits in FORMAT. */
wL=d+length('weight'); nL=d+length('total weight'); vL=d+length('value')
totW=0; totV=0
do #=1 while @.#\==''; parse var @.# n.# w.# v.# .
end /*#*/ /* [↑] assign item to separate lists. */
#=#-1 /*#: is the number of items in @ list.*/
call show 'unsorted item list' /*display the header and the @ list.*/
call sortD /*invoke usingsort a(which descendingsorts sortdescending).*/
call hdr "burglar's knapsack contents"
do j=1 for # while totW<maxW; f=1 /*grabprocess items. */
if totW+w.j>=maxW then f=(maxW-totW)/w.j /*calccalculate fract.*/
totW=totW+w.j*f; totV=totV+v.j*f /*add──►totsadd ───► totals.*/
call syf left(word('{all}',1+(f\==1)),5) n.j, w.j*f, v.j*f
end /*j*/ /*↑show [↑] show item.*/
call sep; say; t='t' /* [↓] $ supresses suppresses trailing Θszeroes.*/
call sy left('total weight',nL,'─'), $(format(totW,,d))
call sy left('total value',nL,'─'), , $(format(totV,,d))
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────one─liner subroutines──────────────────────subroutines─────────────────────*/
hdr: say; say; say center(arg(1),50,'─'); say; call title; call sep; return
sep: call sy copies('═',nL), copies("═",wL), copies('═',vL); return
show: call hdr arg(1); do j=1 for #; call syf n.j,w.j,v.j; end; return
sy: say left('',9) left(arg(1),nL) right(arg(2),wL) right(arg(3),vL); return
syf: call sy arg(1), $(format(arg(2),,d)), $(format(arg(3),,d)); return
title: call sy center('item',nL), center("weight",wL), center('value',vL); return
$: x=arg(1);if pos(.,x)>1 then x=left(strip(strip(x,'T't,0),,.),length(x));return x
/*──────────────────────────────────SORTD subroutine───────────────────────────subroutine──────────────────────────*/
sortD: do sorts=2 to #; _n a=n.sorts; _w!=w.sorts; u=v.s /* [↓] _v=v.sort this is a /*descending sort. */
do k=sorts-1 by -1 to 1 while v.k/w.k<_vu/_w /*order items!;?=k+1;n.*/?=n.k;w.?=w.k;v.?=v.k;end
p?=k+1; n.p?=n.ka; w.p?=w.k!; v.p?=v.k /*shuffle 'em.*/u
end /*k*/ /*[↓] last ones*/
return a=k+1; n.a=_n; w.a=_w;/* ↑↑↑ sort algorithm is OK v.a=_vfor small /*place itemarrays. */</lang>
'''output''' using the default inputs of: &nbsp; <tt> 15 &nbsp; 3 </tt>
end /*sort*/
return /* ↑ ↑ ↑ algorithm is OK for smallish arrays.*/</lang>
'''output''' using the default inputs of: &nbsp; <tt> 15 3 </tt>
<pre>
────────────────unsorted item list────────────────