Transportation problem: Difference between revisions
→{{header|REXX}}: cleanup & remove debug code
Walterpachl (talk | contribs) m (→{{header|REXX}}: remove one debug line) |
Walterpachl (talk | contribs) (→{{header|REXX}}: cleanup & remove debug code) |
||
Line 3,922:
* Note: correctnes of input is not checked
* 20201226 add optimization
* 20210103 remove debug code
**********************************************************************/
Signal On Halt
Signal On Novalue
Signal On Syntax
Parse Arg fid
If fid='' Then
fid='input1.txt'
Call init
matrix.=0
ms=0
Do r=1 To rr
Line 3,949 ⟶ 3,950:
End
Call show_alloc 'after NWC application'
Call steppingstone
Exit
/**********************************************************************
* Subroutines for NWC Algorithm
**********************************************************************/
init:
Line 4,131 ⟶ 4,045:
Return
Parse Arg header
If header='' Then
Return
Say ''
Say header
total=0
ol=' '
Do c=1 to cc
ol=ol format(demand_in.c,3)
End
Do r=1 to rr
ol=format(source_in.r,4)
a=word(matrix.r.1,4)
If a>0 Then
ol=format(a,4)
Else
ol=' - '
total=total+word(matrix.r.1,4)*word(matrix.r.1,3)
Do c=2 To cc
a=word(matrix.r.c,4)
If a>0 Then
ol=ol format(a,4)
Else
ol=ol ' - '
total=total+word(matrix.r.c,4)*word(matrix.r.c,3)
End
Say ol
End
Say 'Total costs:' format(total,4,1)
Return
/**********************************************************************
* Subroutines for Optimization
**********************************************************************/
steppingstone: Procedure Expose matrix. cost. rr cc demand_in.,
source_in. fid
maxReduction=0
move=''
Call fixDegenerateCase
Do r=1 To rr
Do c=1 To cc
Parse Var matrix.r.c r c cost qrc
If qrc=0 Then Do
path=getclosedpath(r,c)
reduction = 0
lowestQuantity = 1e10
leavingCandidate = ''
plus=1
pathx=path
Do While pathx<>''
Parse Var pathx s '|' pathx
If plus Then
reduction=reduction+word(s,3)
Else Do
reduction=reduction-word(s,3)
If word(s,4)<lowestQuantity Then Do
leavingCandidate = s
lowestQuantity = word(s,4)
End
End
plus=\plus
End
If reduction < maxreduction Then Do
move=path
leaving=leavingCandidate
maxReduction = reduction
End
End
End
End
if move <> '' Then Do
quant=word(leaving,4)
plus=1
Do While move<>''
Parse Var move m '|' move
Parse Var m r c cpu qrc
Parse Var matrix.r.c vr vc vcost vquant
If plus Then
nquant=vquant+quant
Else
nquant=vquant-quant
matrix.r.c = vr vc vcost nquant
plus=\plus
End
move=''
Call steppingStone
End
Else Do
Call show_alloc 'Optimal Solution' fid
End
Return
getclosedpath: Procedure Expose matrix. cost. rr cc
Parse Arg rd,cd
path=rd cd cost.rd.cd word(matrix.rd.cd,4)
do r=1 To rr
Do c=1 To cc
If word(matrix.r.c,4)>0 Then Do
path=path'|'r c cost.r.c word(matrix.r.c,4)
End
End
End
path=magic(path)
Return stones(path)
magic: Procedure
Parse Arg list
Do Forever
list_1=remove_1(list)
If list_1=list Then Leave
list=list_1
End
Return list_1
remove_1: Procedure
Parse Arg list
cntr.=0
cntc.=0
Line 4,153 ⟶ 4,170:
parse Var list e.i '|' list
Parse Var e.i r c .
cntr.r=cntr.r+1
cntc.c=cntc.c+1
End
n=i-1
Line 4,162 ⟶ 4,179:
If cntr.r<2 |,
cntc.c<2 Then Do
keep.i=0
End
Line 4,171 ⟶ 4,187:
list=list'|'e.i
End
Return list
Parse Arg lst
tstc=lst
Line 4,214 ⟶ 4,195:
Parse Var tstc o.i '|' tstc
end
stones=lst
o.0=i-1
prev=o.1
Line 4,243 ⟶ 4,212:
stones=stones'|'st.i
End
Return stones
getNeighbors: Procedure
parse Arg s, lst
Do i=1
Parse Var lst o.i '|' lst
End
o.0=i-1
nbrs.=''
sr=word(s,1)
Line 4,276 ⟶ 4,236:
End
return nbrs.0'|'nbrs.1
pelems: Procedure
Line 4,321 ⟶ 4,244:
Return i
Call matrixtolist
If (rr+cc-1)<>ms Then Do
Do r=1 To rr
Line 4,360 ⟶ 4,271:
Return strip(list,,'|')
Novalue:
Say 'Novalue raised in line' sigl
Say sourceline(sigl)
Say 'Variable' condition('D')
Signal lookaround
Syntax:
Say 'Syntax raised in line' sigl
Say sourceline(sigl)
Say 'rc='rc '('errortext(rc)')'
halt:
lookaround:
If fore() Then Do
Say 'You can look around now.'
Trace ?R
Nop
End
Exit 12</lang>
{{out}}
<pre>F:\>rexx tpx2 input1.txt
|