Transportation problem: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible)
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:
Процедура КомандаРассчитать(Команда)
Процедура КомандаРассчитать(Команда)
РешениеТранспортнойЗадачи();
РешениеТранспортнойЗадачи();
КонецПроцедуры</lang>
КонецПроцедуры</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|Java}}
{{trans|Java}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.IO;
Line 695: Line 695:
}
}
}
}
}</lang>
}</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}}
<lang cpp>#include <algorithm>
<syntaxhighlight lang="cpp">#include <algorithm>
#include <iomanip>
#include <iomanip>
#include <iostream>
#include <iostream>
Line 1,029: Line 1,029:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>input1.txt
<pre>input1.txt
Line 1,085: Line 1,085:
=={{header|D}}==
=={{header|D}}==
{{trans|Java}}
{{trans|Java}}
<lang d>import std.stdio, std.range, std.algorithm, std.conv, std.math, std.traits;
<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);
}
}
}</lang>
}</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:
ВывестиПлан
ВывестиПлан


КОН Транспорт.</lang>
КОН Транспорт.</syntaxhighlight>


=== Input ===
=== Input ===
Line 1,825: Line 1,825:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Java}}
{{trans|Java}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 2,106: Line 2,106:
t.printResult()
t.printResult()
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,179: Line 2,179:
In other words:
In other words:


<lang J>NB. C's y[m] v= x implemented as x m ndxasgn v y
<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.
)</lang>
)</syntaxhighlight>


Task data:
Task data:


<lang J>need=: 20 30 10
<syntaxhighlight lang="j">need=: 20 30 10
supply=: 25 35
supply=: 25 35
cost=:3 5 7,:3 2 5</lang>
cost=:3 5 7,:3 2 5</syntaxhighlight>


Task example:
Task example:


<lang J> need cost trans supply
<syntaxhighlight lang="j"> need cost trans supply
20 0 5
20 0 5
0 30 5</lang>
0 30 5</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|8}}
{{works with|Java|8}}
<lang java>import java.io.File;
<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:
}
}
}
}
}</lang>
}</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].
<lang julia>using JuMP, GLPK
<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}}
<lang scala>// version 1.1.51
<syntaxhighlight lang="scala">// version 1.1.51


import java.io.File
import java.io.File
Line 2,740: Line 2,740:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,749: Line 2,749:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Go}}
{{trans|Go}}
<lang Nim>import fenv, lists, math, sequtils, strformat, strutils
<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()</lang>
tr.printResult()</syntaxhighlight>


{{out}}
{{out}}
Line 2,999: Line 2,999:


=={{header|Pascal}}==
=={{header|Pascal}}==
<lang pascal>Program transport;
<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.</lang>
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:
<lang perl>use strict;
<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;</lang>
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.
<!--<lang Phix>(phixonline)-->
<!--<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>
<!--</lang>-->
<!--</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}}
<!--<lang Phix>(phixonline)-->
<!--<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>
<!--</lang>-->
<!--</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>.


<lang R>library(lpSolve)
<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 </lang>
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.


<lang racket>#lang typed/racket
<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))</lang>
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 perl6>my %costs = :S1{:3C1, :5C2, :7C3}, :S2{:3C1, :2C2, :5C3};
<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";</lang>
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}}
<lang rexx>/* REXX ***************************************************************
<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</lang>
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:


<lang sas>/* create SAS data sets */
<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;</lang>
quit;</syntaxhighlight>


Output:
Output:
Line 4,719: Line 4,719:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1


Class Shipment
Class Shipment
Line 4,975: Line 4,975:
End Sub
End Sub


End Module</lang>
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}}
<lang ecmascript>import "/dynamic" for Struct
<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()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}