Transportation problem: Difference between revisions

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

edits