Transportation problem: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Phix}}: syntax coloured, made p2js compatible) |
m (→{{header|Wren}}: Minor tidy) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 58:
=={{header|1C}}==
<syntaxhighlight lang="text">// based on the program of <romix>
перем m,n; // Table size
Line 445:
Процедура КомандаРассчитать(Команда)
РешениеТранспортнойЗадачи();
КонецПроцедуры</
=={{header|C sharp|C#}}==
{{trans|Java}}
<
using System.Collections.Generic;
using System.IO;
Line 695:
}
}
}</
{{out}}
<pre>Optimal solution input1.txt
Line 724:
=={{header|C++}}==
{{trans|Kotlin}}
<
#include <iomanip>
#include <iostream>
Line 731:
#include <string>
#include <vector>
#include <cfloat>
using namespace std;
Line 1,029 ⟶ 1,030:
return 0;
}</
{{out}}
<pre>input1.txt
Line 1,085 ⟶ 1,086:
=={{header|D}}==
{{trans|Java}}
<
final class Shipment {
Line 1,327 ⟶ 1,328:
printResult(fileName, demand, supply, costs, matrix);
}
}</
{{out}}
<pre>Optimal solution transportation_problem1.txt
Line 1,352 ⟶ 1,353:
=={{header|Glagol}}==
<syntaxhighlight lang="text">ОТДЕЛ Транспорт+;
ИСПОЛЬЗУЕТ
Вывод ИЗ "...\Отделы\Обмен\",
Line 1,801 ⟶ 1,802:
ВывестиПлан
КОН Транспорт.</
=== Input ===
Line 1,825 ⟶ 1,826:
=={{header|Go}}==
{{trans|Java}}
<
import (
Line 2,106 ⟶ 2,107:
t.printResult()
}
}</
{{out}}
Line 2,179 ⟶ 2,180:
In other words:
<
ndxasgn=: conjunction define
:
Line 2,199 ⟶ 2,200:
r=. n (<ndxs)} r
end.
)</
Task data:
<
supply=: 25 35
cost=:3 5 7,:3 2 5</
Task example:
<
20 0 5
0 30 5</
=={{header|Java}}==
{{works with|Java|8}}
<
import java.util.*;
import static java.util.Arrays.stream;
Line 2,438 ⟶ 2,439:
}
}
}</
<pre>input1.txt
Line 2,496 ⟶ 2,497:
=={{header|Julia}}==
Code taken from [https://github.com/dylanomics/transportation_problem here] using [https://jump.dev/JuMP.jl/stable/ JuMP].
<
# cost vector
Line 2,528 ⟶ 2,529:
λ = [JuMP.dual(C1[1]),JuMP.dual(C1[2])]
μ = [JuMP.dual(C2[1]),JuMP.dual(C2[2]),JuMP.dual(C2[3])]
</syntaxhighlight>
{{out}}
Line 2,536 ⟶ 2,537:
=={{header|Kotlin}}==
{{trans|Java}}
<
import java.io.File
Line 2,740 ⟶ 2,741:
}
}
}</
{{out}}
Line 2,749 ⟶ 2,750:
=={{header|Nim}}==
{{trans|Go}}
<
type
Line 2,943 ⟶ 2,944:
tr.northWestCornerRule()
tr.steppingStone()
tr.printResult()</
{{out}}
Line 2,999 ⟶ 3,000:
=={{header|Pascal}}==
<
{ based on the program of <Svetlana Belashova> }
Line 3,481 ⟶ 3,482:
GotoXY(40,1);
l1: d:=ReadKey;
END.</
=={{header|Perl}}==
Just re-using the code from [[Vogel's_approximation_method#Perl|Vogel's approximation method]], tweaked to handle specific input:
<
use warnings;
use feature 'say';
Line 3,521 ⟶ 3,522:
}
say my $result = "cost $cost\n\n" . $table =~ s/[A-Z]{2}/--/gr;</
{{out}}
<pre>cost 170
Line 3,532 ⟶ 3,533:
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.
<!--<
<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>
Line 3,565 ⟶ 3,566:
<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>
<!--</
{{out}}
<pre>
Line 3,574 ⟶ 3,575:
Obviously I did not really quite understand the problem when I rattled out the above... this does much better.
{{trans|Go}}
<!--<
<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>
Line 3,830 ⟶ 3,831:
<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>
<!--</
{{out}}
(Obviously the other eight tests all work fine and produce similar output.)
Line 3,868 ⟶ 3,869:
Using <code>lpSolve::lp.transport</code>.
<
# cost matrix
Line 3,887 ⟶ 3,888:
rownames(sol) <- c("Supplier 1", "Supplier 2")
colnames(sol) <- c("Customer 1", "Customer 2", "Customer 3")
sol </
{{out}}
<pre>Success: the objective function is 180
Line 3,901 ⟶ 3,902:
Using <code>typed/racket</code>, to keep track of Vectors of Vectors of data.
<
;; {{trans|Java}}
(define-type (V2 A) (Vectorof (Vectorof A)))
Line 4,127 ⟶ 4,128:
30 40 35 45
$
1000))</
{{out}}
Output of: <code>raco test Transportation-problem.rkt</code>:
Line 4,157 ⟶ 4,158:
{{works with|Rakudo|2019.03.1}}
Using [[Vogel's_approximation_method#Raku|Vogel's approximation method]]:
<syntaxhighlight lang="raku"
my %demand = :20C1, :30C2, :10C3;
my %supply = :25S1, :35S2;
Line 4,212 ⟶ 4,213:
print "\n";
}
say "\nTotal cost: $total";</
{{out}}
<pre> C1 C2 C3
Line 4,220 ⟶ 4,221:
=={{header|REXX}}==
{{trans|Java}}
<
* Solve the Transportation Problem using the Northwest Corner Method
Default Input
Line 4,598 ⟶ 4,599:
Nop
End
Exit 12</
{{out}}
<pre>F:\>rexx tpx2 input1.txt
Line 4,665 ⟶ 4,666:
Use network solver in SAS/OR:
<
data cost_data;
input from $ to $ cost;
Line 4,705 ⟶ 4,706:
print _OROPTMODEL_NUM_['OBJECTIVE'];
print flow;
quit;</
Output:
Line 4,719 ⟶ 4,720:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Class Shipment
Line 4,975 ⟶ 4,976:
End Sub
End Module</
{{out}}
<pre>Optimal solution input1.txt
Line 5,009 ⟶ 5,010:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<
import "./ioutil" for FileUtil, File
import "./math" for Nums
import "./seq" for Lst
import "./fmt" for Fmt
var Shipment = Struct.create("Shipment", ["quantity", "costPerUnit", "r", "c"])
Line 5,206 ⟶ 5,207:
t.steppingStone()
t.printResult()
}</
{{out}}
|