Weather routing: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible)
m (syntax highlighting fixup automation)
Line 15: Line 15:
{{trans|Julia}}
{{trans|Julia}}
This runs in only 37 seconds which is surprisingly quick compared to Julia. However, I've just noticed that I'm using an out of date version of Julia (1.0.4) so hopefully the latest version will be able to close the gap.
This runs in only 37 seconds which is surprisingly quick compared to Julia. However, I've just noticed that I'm using an out of date version of Julia (1.0.4) so hopefully the latest version will be able to close the gap.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 467: Line 467:
fmt.Println("The route taking the least time found was:\n", tp.path, "\nwhich has duration",
fmt.Println("The route taking the least time found was:\n", tp.path, "\nwhich has duration",
int(tp.duration/60), "hours,", int(math.Round(math.Mod(tp.duration, 60))), "minutes.")
int(tp.duration/60), "hours,", int(math.Round(math.Mod(tp.duration, 60))), "minutes.")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 478: Line 478:
=={{header|Julia}}==
=={{header|Julia}}==
Brute force optimization search, practical for shorter path lengths, but would require a better algorithm for paths over twice this size.
Brute force optimization search, practical for shorter path lengths, but would require a better algorithm for paths over twice this size.
<lang julia>module SailingPolars
<syntaxhighlight lang="julia">module SailingPolars


using DelimitedFiles
using DelimitedFiles
Line 861: Line 861:
println("The route taking the least time found was:\n ", tp.path,
println("The route taking the least time found was:\n ", tp.path,
"\nwhich has duration $(div(tp.duration, 60)) hours, $(rem(tp.duration, 60)) minutes.")
"\nwhich has duration $(div(tp.duration, 60)) hours, $(rem(tp.duration, 60)) minutes.")
</syntaxhighlight>
</lang>
The polar CSV file used for this solution, named polar.csv, is as follows. Note that this is a very detailed
The polar CSV file used for this solution, named polar.csv, is as follows. Note that this is a very detailed
polar, chosen to stress the testing of the code. Most polar files are far smaller,
polar, chosen to stress the testing of the code. Most polar files are far smaller,
Line 921: Line 921:
The Go version runs in about 44 seconds on my computer. This Nim version, compiled in release mode (which includes runtime checks), runs in 22 seconds (18 seconds if link time optimization is activated). When compiled without checks (“danger” mode), it runs in 15.5 seconds.
The Go version runs in about 44 seconds on my computer. This Nim version, compiled in release mode (which includes runtime checks), runs in 22 seconds (18 seconds if link time optimization is activated). When compiled without checks (“danger” mode), it runs in 15.5 seconds.


<lang Nim>import hashes, math, parsecsv, sequtils, sets, strutils, sugar
<syntaxhighlight lang="nim">import hashes, math, parsecsv, sequtils, sets, strutils, sugar


type
type
Line 1,237: Line 1,237:
echo "The route taking the least time found was:"
echo "The route taking the least time found was:"
echo tp.path
echo tp.path
echo "which has duration ", int(tp.duration / 60), " hours ", toInt(tp.duration mod 60), " minutes."</lang>
echo "which has duration ", int(tp.duration / 60), " hours ", toInt(tp.duration mod 60), " minutes."</syntaxhighlight>


{{out}}
{{out}}
Line 1,246: Line 1,246:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|Go}}
{{trans|Go}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Weather_Routing.exw
-- demo\rosetta\Weather_Routing.exw
Line 1,590: Line 1,590:
<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}}
<pre>
<pre>
Line 1,605: Line 1,605:


As you'd expect, this takes many times longer than Julia to run (about 24.5 minutes versus 3 minutes 20 seconds) but gets there in the end :)
As you'd expect, this takes many times longer than Julia to run (about 24.5 minutes versus 3 minutes 20 seconds) but gets there in the end :)
<lang ecmascript>import "io" for File
<syntaxhighlight lang="ecmascript">import "io" for File


/*
/*
Line 2,026: Line 2,026:
var tp = minimumTimeRoute.call(routeProb, sp, false)
var tp = minimumTimeRoute.call(routeProb, sp, false)
System.print("The route taking the least time found was:\n %(tp.path) \nwhich has duration " +
System.print("The route taking the least time found was:\n %(tp.path) \nwhich has duration " +
"%((tp.duration/60).truncate) hours, %((tp.duration%60).round) minutes.")</lang>
"%((tp.duration/60).truncate) hours, %((tp.duration%60).round) minutes.")</syntaxhighlight>


{{out}}
{{out}}