Transportation problem: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: syntax coloured, made p2js compatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 58: | Line 58: | ||
=={{header|1C}}== |
=={{header|1C}}== |
||
<lang>// based on the program of <romix> |
<syntaxhighlight lang="text">// based on the program of <romix> |
||
перем m,n; // Table size |
перем m,n; // Table size |
||
Line 445: | Line 445: | ||
Процедура КомандаРассчитать(Команда) |
Процедура КомандаРассчитать(Команда) |
||
РешениеТранспортнойЗадачи(); |
РешениеТранспортнойЗадачи(); |
||
КонецПроцедуры</ |
КонецПроцедуры</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.IO; |
using System.IO; |
||
Line 695: | Line 695: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Optimal solution input1.txt |
<pre>Optimal solution input1.txt |
||
Line 724: | Line 724: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
#include <iomanip> |
#include <iomanip> |
||
#include <iostream> |
#include <iostream> |
||
Line 1,029: | Line 1,029: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>input1.txt |
<pre>input1.txt |
||
Line 1,085: | Line 1,085: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.range, std.algorithm, std.conv, std.math, std.traits; |
||
final class Shipment { |
final class Shipment { |
||
Line 1,327: | Line 1,327: | ||
printResult(fileName, demand, supply, costs, matrix); |
printResult(fileName, demand, supply, costs, matrix); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Optimal solution transportation_problem1.txt |
<pre>Optimal solution transportation_problem1.txt |
||
Line 1,352: | Line 1,352: | ||
=={{header|Glagol}}== |
=={{header|Glagol}}== |
||
<lang>ОТДЕЛ Транспорт+; |
<syntaxhighlight lang="text">ОТДЕЛ Транспорт+; |
||
ИСПОЛЬЗУЕТ |
ИСПОЛЬЗУЕТ |
||
Вывод ИЗ "...\Отделы\Обмен\", |
Вывод ИЗ "...\Отделы\Обмен\", |
||
Line 1,801: | Line 1,801: | ||
ВывестиПлан |
ВывестиПлан |
||
КОН Транспорт.</ |
КОН Транспорт.</syntaxhighlight> |
||
=== Input === |
=== Input === |
||
Line 1,825: | Line 1,825: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 2,106: | Line 2,106: | ||
t.printResult() |
t.printResult() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,179: | Line 2,179: | ||
In other words: |
In other words: |
||
< |
<syntaxhighlight lang="j">NB. C's y[m] v= x implemented as x m ndxasgn v y |
||
ndxasgn=: conjunction define |
ndxasgn=: conjunction define |
||
: |
: |
||
Line 2,199: | Line 2,199: | ||
r=. n (<ndxs)} r |
r=. n (<ndxs)} r |
||
end. |
end. |
||
)</ |
)</syntaxhighlight> |
||
Task data: |
Task data: |
||
< |
<syntaxhighlight lang="j">need=: 20 30 10 |
||
supply=: 25 35 |
supply=: 25 35 |
||
cost=:3 5 7,:3 2 5</ |
cost=:3 5 7,:3 2 5</syntaxhighlight> |
||
Task example: |
Task example: |
||
< |
<syntaxhighlight lang="j"> need cost trans supply |
||
20 0 5 |
20 0 5 |
||
0 30 5</ |
0 30 5</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|8}} |
{{works with|Java|8}} |
||
< |
<syntaxhighlight lang="java">import java.io.File; |
||
import java.util.*; |
import java.util.*; |
||
import static java.util.Arrays.stream; |
import static java.util.Arrays.stream; |
||
Line 2,438: | Line 2,438: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre>input1.txt |
<pre>input1.txt |
||
Line 2,496: | Line 2,496: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Code taken from [https://github.com/dylanomics/transportation_problem here] using [https://jump.dev/JuMP.jl/stable/ JuMP]. |
Code taken from [https://github.com/dylanomics/transportation_problem here] using [https://jump.dev/JuMP.jl/stable/ JuMP]. |
||
< |
<syntaxhighlight lang="julia">using JuMP, GLPK |
||
# cost vector |
# cost vector |
||
Line 2,528: | Line 2,528: | ||
λ = [JuMP.dual(C1[1]),JuMP.dual(C1[2])] |
λ = [JuMP.dual(C1[1]),JuMP.dual(C1[2])] |
||
μ = [JuMP.dual(C2[1]),JuMP.dual(C2[2]),JuMP.dual(C2[3])] |
μ = [JuMP.dual(C2[1]),JuMP.dual(C2[2]),JuMP.dual(C2[3])] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,536: | Line 2,536: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.51 |
||
import java.io.File |
import java.io.File |
||
Line 2,740: | Line 2,740: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,749: | Line 2,749: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="nim">import fenv, lists, math, sequtils, strformat, strutils |
||
type |
type |
||
Line 2,943: | Line 2,943: | ||
tr.northWestCornerRule() |
tr.northWestCornerRule() |
||
tr.steppingStone() |
tr.steppingStone() |
||
tr.printResult()</ |
tr.printResult()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,999: | Line 2,999: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">Program transport; |
||
{ based on the program of <Svetlana Belashova> } |
{ based on the program of <Svetlana Belashova> } |
||
Line 3,481: | Line 3,481: | ||
GotoXY(40,1); |
GotoXY(40,1); |
||
l1: d:=ReadKey; |
l1: d:=ReadKey; |
||
END.</ |
END.</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Just re-using the code from [[Vogel's_approximation_method#Perl|Vogel's approximation method]], tweaked to handle specific input: |
Just re-using the code from [[Vogel's_approximation_method#Perl|Vogel's approximation method]], tweaked to handle specific input: |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 3,521: | Line 3,521: | ||
} |
} |
||
say my $result = "cost $cost\n\n" . $table =~ s/[A-Z]{2}/--/gr;</ |
say my $result = "cost $cost\n\n" . $table =~ s/[A-Z]{2}/--/gr;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>cost 170 |
<pre>cost 170 |
||
Line 3,532: | Line 3,532: | ||
The simplest solution I could think of.<br> |
The simplest solution I could think of.<br> |
||
Assumes 0 cost is not allowed, but using say -1 as the "done" cost instead should be fine. |
Assumes 0 cost is not allowed, but using say -1 as the "done" cost instead should be fine. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">needs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">avail</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">costs</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">procedure</span> <span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">needs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">avail</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">costs</span><span style="color: #0000FF;">)</span> |
||
Line 3,565: | Line 3,565: | ||
<span style="color: #000000;">solve</span><span style="color: #0000FF;">({</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #000000;">30</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">25</span><span style="color: #0000FF;">,</span><span style="color: #000000;">35</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">}})</span> |
<span style="color: #000000;">solve</span><span style="color: #0000FF;">({</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #000000;">30</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">25</span><span style="color: #0000FF;">,</span><span style="color: #000000;">35</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">}})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,574: | Line 3,574: | ||
Obviously I did not really quite understand the problem when I rattled out the above... this does much better. |
Obviously I did not really quite understand the problem when I rattled out the above... this does much better. |
||
{{trans|Go}} |
{{trans|Go}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Transportation_problem.exw</span> |
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Transportation_problem.exw</span> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
Line 3,830: | Line 3,830: | ||
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span> |
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span> |
||
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span> |
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
(Obviously the other eight tests all work fine and produce similar output.) |
(Obviously the other eight tests all work fine and produce similar output.) |
||
Line 3,868: | Line 3,868: | ||
Using <code>lpSolve::lp.transport</code>. |
Using <code>lpSolve::lp.transport</code>. |
||
< |
<syntaxhighlight lang="r">library(lpSolve) |
||
# cost matrix |
# cost matrix |
||
Line 3,887: | Line 3,887: | ||
rownames(sol) <- c("Supplier 1", "Supplier 2") |
rownames(sol) <- c("Supplier 1", "Supplier 2") |
||
colnames(sol) <- c("Customer 1", "Customer 2", "Customer 3") |
colnames(sol) <- c("Customer 1", "Customer 2", "Customer 3") |
||
sol </ |
sol </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Success: the objective function is 180 |
<pre>Success: the objective function is 180 |
||
Line 3,901: | Line 3,901: | ||
Using <code>typed/racket</code>, to keep track of Vectors of Vectors of data. |
Using <code>typed/racket</code>, to keep track of Vectors of Vectors of data. |
||
< |
<syntaxhighlight lang="racket">#lang typed/racket |
||
;; {{trans|Java}} |
;; {{trans|Java}} |
||
(define-type (V2 A) (Vectorof (Vectorof A))) |
(define-type (V2 A) (Vectorof (Vectorof A))) |
||
Line 4,127: | Line 4,127: | ||
30 40 35 45 |
30 40 35 45 |
||
$ |
$ |
||
1000))</ |
1000))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Output of: <code>raco test Transportation-problem.rkt</code>: |
Output of: <code>raco test Transportation-problem.rkt</code>: |
||
Line 4,157: | Line 4,157: | ||
{{works with|Rakudo|2019.03.1}} |
{{works with|Rakudo|2019.03.1}} |
||
Using [[Vogel's_approximation_method#Raku|Vogel's approximation method]]: |
Using [[Vogel's_approximation_method#Raku|Vogel's approximation method]]: |
||
<lang |
<syntaxhighlight lang="raku" line>my %costs = :S1{:3C1, :5C2, :7C3}, :S2{:3C1, :2C2, :5C3}; |
||
my %demand = :20C1, :30C2, :10C3; |
my %demand = :20C1, :30C2, :10C3; |
||
my %supply = :25S1, :35S2; |
my %supply = :25S1, :35S2; |
||
Line 4,212: | Line 4,212: | ||
print "\n"; |
print "\n"; |
||
} |
} |
||
say "\nTotal cost: $total";</ |
say "\nTotal cost: $total";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> C1 C2 C3 |
<pre> C1 C2 C3 |
||
Line 4,220: | Line 4,220: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="rexx">/* REXX *************************************************************** |
||
* Solve the Transportation Problem using the Northwest Corner Method |
* Solve the Transportation Problem using the Northwest Corner Method |
||
Default Input |
Default Input |
||
Line 4,598: | Line 4,598: | ||
Nop |
Nop |
||
End |
End |
||
Exit 12</ |
Exit 12</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>F:\>rexx tpx2 input1.txt |
<pre>F:\>rexx tpx2 input1.txt |
||
Line 4,665: | Line 4,665: | ||
Use network solver in SAS/OR: |
Use network solver in SAS/OR: |
||
< |
<syntaxhighlight lang="sas">/* create SAS data sets */ |
||
data cost_data; |
data cost_data; |
||
input from $ to $ cost; |
input from $ to $ cost; |
||
Line 4,705: | Line 4,705: | ||
print _OROPTMODEL_NUM_['OBJECTIVE']; |
print _OROPTMODEL_NUM_['OBJECTIVE']; |
||
print flow; |
print flow; |
||
quit;</ |
quit;</syntaxhighlight> |
||
Output: |
Output: |
||
Line 4,719: | Line 4,719: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="vbnet">Module Module1 |
||
Class Shipment |
Class Shipment |
||
Line 4,975: | Line 4,975: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Optimal solution input1.txt |
<pre>Optimal solution input1.txt |
||
Line 5,009: | Line 5,009: | ||
{{libheader|Wren-seq}} |
{{libheader|Wren-seq}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/dynamic" for Struct |
||
import "/ioutil" for FileUtil, File |
import "/ioutil" for FileUtil, File |
||
import "/math" for Nums |
import "/math" for Nums |
||
Line 5,206: | Line 5,206: | ||
t.steppingStone() |
t.steppingStone() |
||
t.printResult() |
t.printResult() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |