Vogel's approximation method: Difference between revisions
m
→{{header|Wren}}: Minor tidy
Walterpachl (talk | contribs) (→{{header|REXX}}: Added Low Cost Algorithm) |
m (→{{header|Wren}}: Minor tidy) |
||
(8 intermediate revisions by 7 users not shown) | |||
Line 73:
;Cf.
* [[Transportation_problem|Transportation problem]]
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V costs = [‘W’ = [‘A’ = 16, ‘B’ = 16, ‘C’ = 13, ‘D’ = 22, ‘E’ = 17],
‘X’ = [‘A’ = 14, ‘B’ = 14, ‘C’ = 13, ‘D’ = 19, ‘E’ = 15],
‘Y’ = [‘A’ = 19, ‘B’ = 19, ‘C’ = 20, ‘D’ = 23, ‘E’ = 50],
‘Z’ = [‘A’ = 50, ‘B’ = 12, ‘C’ = 50, ‘D’ = 15, ‘E’ = 11]]
V demand = [‘A’ = 30, ‘B’ = 20, ‘C’ = 70, ‘D’ = 30, ‘E’ = 60]
V cols = sorted(demand.keys())
V supply = [‘W’ = 50, ‘X’ = 60, ‘Y’ = 50, ‘Z’ = 50]
V res = Dict(costs.keys().map(k -> (k, DefaultDict[Char, Int]())))
[Char = [Char]] g
L(x) supply.keys()
g[x] = sorted(costs[x].keys(), key' g -> :costs[@x][g])
L(x) demand.keys()
g[x] = sorted(costs.keys(), key' g -> :costs[g][@x])
L !g.empty
[Char = Int] d
L(x) demand.keys()
d[x] = I g[x].len > 1 {(costs[g[x][1]][x] - costs[g[x][0]][x])} E costs[g[x][0]][x]
[Char = Int] s
L(x) supply.keys()
s[x] = I g[x].len > 1 {(costs[x][g[x][1]] - costs[x][g[x][0]])} E costs[x][g[x][0]]
V f = max(d.keys(), key' n -> @d[n])
V t = max(s.keys(), key' n -> @s[n])
(t, f) = I d[f] > s[t] {(f, g[f][0])} E (g[t][0], t)
V v = min(supply[f], demand[t])
res[f][t] += v
demand[t] -= v
I demand[t] == 0
L(k, n) supply
I n != 0
g[k].remove(t)
g.pop(t)
demand.pop(t)
supply[f] -= v
I supply[f] == 0
L(k, n) demand
I n != 0
g[k].remove(f)
g.pop(f)
supply.pop(f)
L(n) cols
print("\t "n, end' ‘ ’)
print()
V cost = 0
L(g) sorted(costs.keys())
print(g" \t", end' ‘ ’)
L(n) cols
V y = res[g][n]
I y != 0
print(y, end' ‘ ’)
cost += y * costs[g][n]
print("\t", end' ‘ ’)
print()
print("\n\nTotal Cost = "cost)</syntaxhighlight>
{{out}}
<pre>
A B C D E
W 50
X 20 40
Y 30 20
Z 30 20
Total Cost = 3130
</pre>
=={{header|C}}==
{{trans|Kotlin}}
<
#include <limits.h>
Line 183 ⟶ 254:
printf("\nTotal cost = %d\n", total_cost);
return 0;
}</
{{output}}
Line 197 ⟶ 268:
If the program is changed to this (to accomodate the second Ruby example):
<
#include <limits.h>
Line 231 ⟶ 302:
printf("\nTotal cost = %d\n", total_cost);
return 0;
}</
then the output, which agrees with the Phix output but not with the Ruby output itself is:
Line 247 ⟶ 318:
=={{header|C++}}==
{{trans|Java}}
<
#include <numeric>
#include <vector>
Line 377 ⟶ 448:
return 0;
}</
{{out}}
<pre>[0, 0, 50, 0, 0]
Line 388 ⟶ 459:
Strongly typed version (but K is not divided in Task and Contractor types to keep code simpler).
{{trans|Python}}
<
import std.stdio, std.string, std.algorithm, std.range;
Line 467 ⟶ 538:
}
writeln("\nTotal Cost = ", cost);
}</
{{out}}
<pre>
Line 481 ⟶ 552:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 619 ⟶ 690:
}
fmt.Println("\nTotal cost =", totalCost)
}</
{{out}}
Line 633 ⟶ 704:
If the program is changed as follows to accomodate the second Ruby example:
<
import (
Line 678 ⟶ 749:
}
fmt.Println("\nTotal cost =", totalCost)
}</
then the output, which agrees with the C and Phix output but not with the Ruby output itself, is:
Line 696 ⟶ 767:
Implementation:
<
:
exceeding=. 0 <. -&(+/)
Line 723 ⟶ 794:
end.
_1 _1 }. R
)</
Note that for our penalty we are using the difference between the two smallest relevant costs multiplied by 1 larger than the highest represented cost and we subtract from that multiple the smallest relevant cost. This gives us the tiebreaker mechanism currently specified for this task.
Line 729 ⟶ 800:
Task example:
<
src=: 50 60 50 50
cost=: 16 16 13 22 17,14 14 13 19 15,19 19 20 23 50,:50 12 50 15 11
Line 737 ⟶ 808:
30 0 20 0 10
0 20 0 30 0
0 0 0 0 50</
=={{header|Java}}==
{{works with|Java|8}}
<
import static java.util.Arrays.stream;
import java.util.concurrent.*;
Line 837 ⟶ 908:
return isRow ? new int[]{pm, pc, mc, md} : new int[]{pc, pm, mc, md};
}
}</
<pre>[0, 0, 50, 0, 0]
Line 855 ⟶ 926:
<code>Resource</code> stores the currently available quantity of a given supply or demand as well as its penalty, cost, and some meta-data. <code>isavailable</code> indicates whether any of the given resource remains. <code>isless</code> is designed to make the currently most usable resource appear as a maximum compared to other resources.
<syntaxhighlight lang="julia">
immutable TProblem{T<:Integer,U<:String}
sd::Array{Array{T,1},1}
Line 904 ⟶ 975:
isavailable(r::Resource) = 0 < r.quant
Base.isless(a::Resource, b::Resource) = a.p < b.p || (a.p == b.p && b.q < a.q)
</syntaxhighlight>
'''Functions'''
Line 911 ⟶ 982:
<code>vogel</code> implements Vogel's approximation method on a <code>TProblem</code>. It is somewhat straightforward, given the types and <code>penalize!</code>.
<syntaxhighlight lang="julia">
function penalize!{T<:Integer,U<:String}(sd::Array{Array{Resource{T},1},1},
tp::TProblem{T,U})
Line 959 ⟶ 1,030:
return sol
end
</syntaxhighlight>
'''Main'''
<
sup = [50, 60, 50, 50]
Line 991 ⟶ 1,062:
end
println("The total cost is: ", cost)
</syntaxhighlight>
{{out}}
Line 1,006 ⟶ 1,077:
=={{header|Kotlin}}==
{{trans|Java}}
<
val supply = intArrayOf(50, 60, 50, 50)
Line 1,095 ⟶ 1,166:
}
println("\nTotal Cost = $totalCost")
}</
{{out}}
Line 1,110 ⟶ 1,181:
=={{header|Lua}}==
{{trans|Kotlin}}
<
local tbl = {}
for i=1,n do
Line 1,257 ⟶ 1,328:
end
main()</
{{out}}
<pre> A B C D E
Line 1,265 ⟶ 1,336:
Z 0 0 0 0 50
Total Cost = 3100</pre>
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang="nim">import math, sequtils, strutils
var
supply = [50, 60, 50, 50]
demand = [30, 20, 70, 30, 60]
let
costs = [[16, 16, 13, 22, 17],
[14, 14, 13, 19, 15],
[19, 19, 20, 23, 50],
[50, 12, 50, 15, 11]]
nRows = supply.len
nCols = demand.len
var
rowDone = newSeq[bool](nRows)
colDone = newSeq[bool](nCols)
results = newSeqWith(nRows, newSeq[int](nCols))
proc diff(j, len: int; isRow: bool): array[3, int] =
var min1, min2 = int.high
var minP = -1
for i in 0..<len:
let done = if isRow: colDone[i] else: rowDone[i]
if done: continue
let c = if isRow: costs[j][i] else: costs[i][j]
if c < min1:
min2 = min1
min1 = c
minP = i
elif c < min2:
min2 = c
result = [min2 - min1, min1, minP]
proc maxPenalty(len1, len2: int; isRow: bool): array[4, int] =
var md = int.low
var pc, pm, mc = -1
for i in 0..<len1:
let done = if isRow: rowDone[i] else: colDone[i]
if done: continue
let res = diff(i, len2, isRow)
if res[0] > md:
md = res[0] # max diff
pm = i # pos of max diff
mc = res[1] # min cost
pc = res[2] # pos of min cost
result = if isRow: [pm, pc, mc, md] else: [pc, pm, mc, md]
proc nextCell(): array[4, int] =
let res1 = maxPenalty(nRows, nCols, true)
let res2 = maxPenalty(nCols, nRows, false)
if res1[3] == res2[3]:
return if res1[2] < res2[2]: res1 else: res2
result = if res1[3] > res2[3]: res2 else: res1
when isMainModule:
var supplyLeft = sum(supply)
var totalCost = 0
while supplyLeft > 0:
let cell = nextCell()
let r = cell[0]
let c = cell[1]
let q = min(demand[c], supply[r])
dec demand[c], q
if demand[c] == 0: colDone[c] = true
dec supply[r], q
if supply[r] == 0: rowDone[r] = true
results[r][c] = q
dec supplyLeft, q
inc totalCost, q * costs[r][c]
echo " A B C D E"
for i, result in results:
stdout.write chr(i + ord('W'))
for item in result:
stdout.write " ", ($item).align(2)
echo()
echo "\nTotal cost = ", totalCost</syntaxhighlight>
{{out}}
<pre> A B C D E
W 0 0 50 0 0
X 30 0 20 0 10
Y 0 20 0 30 0
Z 0 0 0 0 50
Total cost = 3100</pre>
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Vogel%27s_approximation_method
use warnings;
use List::AllUtils qw( max_by nsort_by min );
my $data = <<END;
A=30 B=20 C=70 D=30 E=60
W=50 X=60 Y=50 Z=50
AW=16 BW=16 CW=13 DW=22 EW=17
AX=14 BX=14 CX=13 DX=19 EX=15
AY=19 BY=19 CY=20 DY=23 EY=50
AZ=50 BZ=12 CZ=50 DZ=15 EZ=11
END
my $table = sprintf +('%4s' x 6 . "\n") x 5,
map {my $t = $_; map "$_$t", '', 'A' .. 'E' } '' , 'W' .. 'Z';
my ($cost, %assign) = (0);
while( $data =~ /\b\w=\d/ )
{
my @penalty;
for ( $data =~ /\b(\w)=\d/g )
{
my @all = map /(\d+)/, nsort_by { /\d+/ && $& }
grep { my ($t, $c) = /(.)(.)=/; $data =~ /\b$c=\d/ and $data =~ /\b$t=\d/ }
$data =~ /$_\w=\d+|\w$_=\d+/g;
push @penalty, [ $_, ($all[1] // 0) - $all[0] ];
}
my $rc = (max_by { $_->[1] } nsort_by
{ my $x = $_->[0]; $data =~ /(?:$x\w|\w$x)=(\d+)/ && $1 } @penalty)->[0];
my @lowest = nsort_by { /\d+/ && $& }
grep { my ($t, $c) = /(.)(.)=/; $data =~ /\b$c=\d/ and $data =~ /\b$t=\d/ }
$data =~ /$rc\w=\d+|\w$rc=\d+/g;
my ($t, $c) = $lowest[0] =~ /(.)(.)/;
my $allocate = min $data =~ /\b[$t$c]=(\d+)/g;
$table =~ s/$t$c/ sprintf "%2d", $allocate/e;
$cost += $data =~ /$t$c=(\d+)/ && $1 * $allocate;
$data =~ s/\b$_=\K\d+/ $& - $allocate || '' /e for $t, $c;
}
print "cost $cost\n\n", $table =~ s/[A-Z]{2}/--/gr;</syntaxhighlight>
{{out}}
<pre>
cost 3100
A B C D E
W -- -- 50 -- --
X 30 -- 20 -- 10
Y -- 20 -- 30 --
Z -- -- -- -- 50
</pre>
=={{header|Phix}}==
Line 1,270 ⟶ 1,490:
{{trans|YaBasic}}
{{trans|Go}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">supply</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">50</span><span style="color: #0000FF;">,</span><span style="color: #000000;">60</span><span style="color: #0000FF;">,</span><span style="color: #000000;">50</span><span style="color: #0000FF;">,</span><span style="color: #000000;">50</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">demand</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">30</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #000000;">70</span><span style="color: #0000FF;">,</span><span style="color: #000000;">30</span><span style="color: #0000FF;">,</span><span style="color: #000000;">60</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">costs</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">,</span><span style="color: #000000;">22</span><span style="color: #0000FF;">,</span><span style="color: #000000;">17</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">14</span><span style="color: #0000FF;">,</span><span style="color: #000000;">14</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">,</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #000000;">23</span><span style="color: #0000FF;">,</span><span style="color: #000000;">50</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">50</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">50</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">}}</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">row_done</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">supply</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">col_done</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">demand</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">diff</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">leng</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">is_row</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">min1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#3FFFFFFF</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">min2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">min1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">min_p</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">leng</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">is_row</span><span style="color: #0000FF;">?</span><span style="color: #000000;">col_done</span><span style="color: #0000FF;">:</span><span style="color: #000000;">row_done</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">is_row</span><span style="color: #0000FF;">?</span><span style="color: #000000;">costs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]:</span><span style="color: #000000;">costs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;"><</span><span style="color: #000000;">min1</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">min2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">min1</span>
<span style="color: #000000;">min1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span>
<span style="color: #000000;">min_p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">c</span><span style="color: #0000FF;"><</span><span style="color: #000000;">min2</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">min2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">min2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">min1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">min1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">min_p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">max_penalty</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">len1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">len2</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">is_row</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">pc</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pm</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">mc</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">md</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">#3FFFFFFF</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">len1</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">is_row</span><span style="color: #0000FF;">?</span><span style="color: #000000;">row_done</span><span style="color: #0000FF;">:</span><span style="color: #000000;">col_done</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">diff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">len2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">is_row</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">res2</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]></span><span style="color: #000000;">md</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">md</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pm</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">md</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mc</span><span style="color: #0000FF;">}&</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">is_row</span><span style="color: #0000FF;">?{</span><span style="color: #000000;">pm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pc</span><span style="color: #0000FF;">}:{</span><span style="color: #000000;">pc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pm</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">supply_left</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">supply</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">total_cost</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">demand</span><span style="color: #0000FF;">)),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">supply</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">supply_left</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cell</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">max_penalty</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">supply</span><span style="color: #0000FF;">),</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">demand</span><span style="color: #0000FF;">),</span> <span style="color: #004600;">true</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">max_penalty</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">demand</span><span style="color: #0000FF;">),</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">supply</span><span style="color: #0000FF;">),</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{{},{},</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cell</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">demand</span><span style="color: #0000FF;">[</span><span style="color: #000000;">c</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">supply</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">demand</span><span style="color: #0000FF;">[</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">q</span>
<span style="color: #000000;">col_done</span><span style="color: #0000FF;">[</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">demand</span><span style="color: #0000FF;">[</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]==</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">supply</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">q</span>
<span style="color: #000000;">row_done</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">supply</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">]==</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">q</span>
<span style="color: #000000;">supply_left</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">q</span>
<span style="color: #000000;">total_cost</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">q</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">costs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" A B C D E\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">supply</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%c "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'Z'</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">supply</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">demand</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%4d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nTotal cost = %d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">total_cost</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,349 ⟶ 1,572:
</pre>
Using the sample from Ruby:
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">supply</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">461</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">277</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">356</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">488</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">393</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">demand</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">278</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">60</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">461</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">116</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1060</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">costs</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">46</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">74</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">28</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">99</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">75</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">36</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">48</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">35</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">199</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">71</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">61</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">81</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">44</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">88</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">85</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">60</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">14</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">79</span><span style="color: #0000FF;">}}</span>
<!--</syntaxhighlight>-->
{{Out}}
Note this agrees with C and Go but not Ruby
Line 1,371 ⟶ 1,596:
=={{header|Python}}==
{{trans|Ruby}}
<
costs = {'W': {'A': 16, 'B': 16, 'C': 13, 'D': 22, 'E': 17},
Line 1,427 ⟶ 1,652:
print "\t",
print
print "\n\nTotal Cost = ", cost</
{{out}}
<pre> A B C D E
Line 1,445 ⟶ 1,670:
somehow at the same total cost!
<
(define-values (1st 2nd 3rd) (values first second third))
Line 1,547 ⟶ 1,772:
(DEMAND (hash 'A 30 'B 20 'C 70 'D 30 'E 60))
(SUPPLY (hash 'W 50 'X 60 'Y 50 'Z 50)))
(displayln (describe-VAM-solution COSTS DEMAND (VAM COSTS SUPPLY DEMAND))))</
{{out}}
Line 1,564 ⟶ 1,789:
{{trans|Sidef}}
<syntaxhighlight lang="raku"
:W{:16A, :16B, :13C, :22D, :17E},
:X{:14A, :14B, :13C, :19D, :15E},
Line 1,624 ⟶ 1,849:
print "\n";
}
say "\nTotal cost: $total";</
{{out}}
<pre> A B C D E
Line 1,637 ⟶ 1,862:
{{trans|java}}
===Vogel's Approximation===
<
* Solve the Transportation Problem using Vogel's Approximation
Default Input
Line 1,648 ⟶ 1,873:
* Note: correctness of input is not checked
* 20210102 restored Vogel's Approximation and added Optimization
* 20210103 eliminated debug code
**********************************************************************/
Signal On Halt
Signal On Novalue
Signal On Syntax
Parse Arg fid
If fid='' Then
Line 1,723 ⟶ 1,953:
End
Call steppingstone
Exit
Line 1,751 ⟶ 1,976:
End
Parse Var in.1 numSources numDestinations . 1 rr cc .
source.=0
demand.=0
source_sum=0
Do i=1 To numSources
Line 1,878 ⟶ 2,105:
Return strip(swl)
show_alloc: Procedure Expose matrix. rr cc demand_in. source_in
Parse Arg header
If header='' Then
Line 1,921 ⟶ 2,148:
steppingstone: Procedure Expose matrix. cost. rr cc matrix. demand_in.,
source_in.
maxReduction=0
move=''
Line 1,930 ⟶ 2,156:
Parse Var matrix.r.c r c cost qrc
If qrc=0 Then Do
path=getclosedpath(r,c)
If pelems(path)<4 Then Do
Line 1,954 ⟶ 2,179:
End
If reduction < maxreduction Then Do
move=path
leaving=leavingCandidate
maxReduction = reduction
Line 1,965 ⟶ 2,187:
End
if move<>'' Then Do
quant=word(leaving,4)
If quant=0 Then Do
Line 1,982 ⟶ 2,197:
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
Call show_alloc 'Optimal Solution' fid
Return
getclosedpath: Procedure Expose matrix. cost. rr cc matrix
Parse Arg rd,cd
path=rd cd cost.rd.cd word(matrix.rd.cd,4)
Line 2,014 ⟶ 2,224:
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
Line 2,033 ⟶ 2,240:
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 2,052 ⟶ 2,259:
Return list
stones: Procedure
Parse Arg lst
tstc=lst
Line 2,092 ⟶ 2,265:
Parse Var tstc o.i '|' tstc
end
stones=lst
o.0=i-1
prev=o.1
Line 2,111 ⟶ 2,271:
st.i=prev
k=i//2
nbrs=getNeighbors(prev,
Parse Var nbrs n.1 '|' n.2
If k=0 Then
Line 2,122 ⟶ 2,282:
stones=stones'|'st.i
End
Return stones
getNeighbors: Procedure Expose o
parse Arg s, lst
Do i=1 To 4
Line 2,169 ⟶ 2,320:
Return n
Call matrixtolist
If (rr+cc-1)<>ms Then Do
Do r=1 To rr
Line 2,195 ⟶ 2,334:
Return
matrixtolist: Procedure Expose matrix. rr cc ms
ms=0
list=''
Line 2,207 ⟶ 2,346:
End
Return strip(list,,'|')
Novalue:
Line 2,229 ⟶ 2,365:
Nop
End
Exit 12</
{{out}}
<pre>F:\>regina tpv vv.txt
Line 2,256 ⟶ 2,392:
===Low Cost Algorithm===
<
* Solve the Transportation Problem using the Least Cost Method
Default Input
Line 2,266 ⟶ 2,402:
* 20201228 corresponds to NWC above
* Note: correctness of input is not checked
* 20210102
* 20210103 remove debug code
**********************************************************************/
Signal On Halt
Line 2,274 ⟶ 2,411:
Parse Arg fid
If fid='' Then
fid='
Call init
Do r=1 To rr
Do c=1 To cc
Line 2,283 ⟶ 2,418:
End
End
Do
mincost=1e10
Do r=1 To rr
Line 2,311 ⟶ 2,445:
End
Call show_alloc 'Low Cost Algorithm'
Call steppingstone
Exit
Line 2,405 ⟶ 2,534:
Return
show_alloc: Procedure Expose matrix. rr cc demand_in. source_in.
Parse Arg header
If header='' Then
Line 2,449 ⟶ 2,577:
steppingstone: Procedure Expose matrix. cost. rr cc matrix. demand_in.,
maxReduction=0
move=''
Line 2,458 ⟶ 2,585:
Parse Var matrix.r.c r c cost qrc
If qrc=0 Then Do
path=getclosedpath(r,c)
If pelems(path)<4 then
Iterate
reduction = 0
lowestQuantity = 1e10
Line 2,483 ⟶ 2,607:
End
If reduction < maxreduction Then Do
move=path
leaving=leavingCandidate
maxReduction = reduction
Line 2,494 ⟶ 2,615:
End
if move<>'' Then Do
quant=word(leaving,4)
If quant=0 Then Do
Line 2,511 ⟶ 2,625:
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
Call show_alloc 'Optimal Solution' fid
Return
getclosedpath: Procedure Expose matrix. cost. rr cc
Parse Arg rd,cd
path=rd cd cost.rd.cd word(matrix.rd.cd,4)
Line 2,543 ⟶ 2,652:
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
Line 2,562 ⟶ 2,668:
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 2,581 ⟶ 2,687:
Return list
stones: Procedure
Parse Arg lst
stones=lst
tstc=lst
Do i=1 By 1 While tstc<>''
Parse Var tstc o.i '|' tstc
End
o.0=i-1
prev=o.1
Line 2,653 ⟶ 2,712:
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 2,688 ⟶ 2,748:
End
Return n
fixDegenerateCase: Procedure Expose matrix. rr cc ms ms demand_in. source_in. move cnt.
If (rr+cc-1)<>ms Then Do
Do r=1 To rr
Line 2,715 ⟶ 2,763:
Return
matrixtolist: Procedure Expose matrix. rr cc ms
ms=0
list=''
Line 2,727 ⟶ 2,775:
End
Return strip(list,,'|')
Novalue:
Line 2,749 ⟶ 2,794:
Nop
End
Exit 12
</syntaxhighlight>
{{out}}
<pre>F:\>rexx tpl vv.txt
Line 2,778 ⟶ 2,824:
Breaks ties using lowest cost cell.
===Task Example===
<
#
# Nigel_Galloway
Line 2,831 ⟶ 2,877:
puts
end
print "\n\nTotal Cost = ", cost</
{{out}}
<pre>
Line 2,846 ⟶ 2,892:
===Reference Example===
Replacing the data in the Task Example with:
<
S2: {D1: 12, D2: 75, D3: 6, D4: 36, D5: 48},
S3: {D1: 35, D2: 199, D3: 4, D4: 5, D5: 71},
Line 2,852 ⟶ 2,898:
S5: {D1: 85, D2: 60, D3: 14, D4: 25, D5: 79}}
demand = {D1: 278, D2: 60, D3: 461, D4: 116, D5: 1060}
supply = {S1: 461, S2: 277, S3: 356, S4: 488, S5: 393}</
Produces:
<pre>
Line 2,868 ⟶ 2,914:
=={{header|Sidef}}==
{{trans|Ruby}}
<
W => :(A => 16, B => 16, C => 13, D => 22, E => 17),
X => :(A => 14, B => 14, C => 13, D => 19, E => 15),
Line 2,934 ⟶ 2,980:
}
say "\n\nTotal Cost = #{cost}"</
{{out}}
<pre>
Line 2,949 ⟶ 2,995:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<
# A sort that works by sorting by an auxiliary key computed by a lambda term
Line 3,052 ⟶ 3,098:
}
return $res
}</
Demonstration:
<
W {A 16 B 16 C 13 D 22 E 17}
X {A 14 B 14 C 13 D 19 E 15}
Line 3,074 ⟶ 3,120:
}] \t]
}
puts "\nTotal Cost = $cost"</
{{out}}
<pre>
Line 3,090 ⟶ 3,136:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
var supply = [50, 60, 50, 50]
Line 3,112 ⟶ 3,158:
var diff = Fn.new { |j, len, isRow|
var min1 =
var min2 = min1
var minP = -1
Line 3,130 ⟶ 3,176:
var maxPenalty = Fn.new { |len1, len2, isRow|
var md =
var pc = -1
var pm = -1
Line 3,162 ⟶ 3,208:
var r = cell[0]
var c = cell[1]
var q =
demand[c] = demand[c] - q
if (demand[c] == 0) colDone[c] = true
Line 3,180 ⟶ 3,226:
i = i + 1
}
System.print("\nTotal Cost = %(totalCost)")</
{{out}}
Line 3,195 ⟶ 3,241:
=={{header|Yabasic}}==
{{trans|C}}
<syntaxhighlight lang="yabasic">
N_ROWS = 4 : N_COLS = 5
Line 3,343 ⟶ 3,389:
print
next i
print "\nTotal cost = ", total_cost</
=={{header|zkl}}==
{{trans|Python}}{{trans|Ruby}}
<
"W",Dictionary("A",16, "B",16, "C",13, "D",22, "E",17),
"X",Dictionary("A",14, "B",14, "C",13, "D",19, "E",15),
Line 3,353 ⟶ 3,399:
"Z",Dictionary("A",50, "B",12, "C",50, "D",15, "E",11)).makeReadOnly();
demand:=Dictionary("A",30, "B",20, "C",70, "D",30, "E",60); // gonna be modified
supply:=Dictionary("W",50, "X",60, "Y",50, "Z",50); // gonna be modified</
<
res :=vogel(costs,supply,demand);
cost:=0;
Line 3,367 ⟶ 3,413:
println();
}
println("\nTotal Cost = ",cost);</
<
// a Dictionary can be created via a list of (k,v) pairs
res:= Dictionary(costs.pump(List,fcn([(k,_)]){ return(k,D()) }));
Line 3,397 ⟶ 3,443:
}//while
res
}</
{{out}}
<pre>
|