aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-07-14 05:15:32 +0100
committerGitHub <noreply@github.com>2021-07-14 05:15:32 +0100
commit6c47b5e1dd9503743d02114cf788c2536380a734 (patch)
treeeeb2ceca21c4d016803a0507eb014c7da3487de4
parentba46beae958e10782919af2d5fe3b0d8d7c55a60 (diff)
parent97f02a20b90a89b6e1a257ac93e80144766b31ae (diff)
downloadperlweeklychallenge-club-6c47b5e1dd9503743d02114cf788c2536380a734.tar.gz
perlweeklychallenge-club-6c47b5e1dd9503743d02114cf788c2536380a734.tar.bz2
perlweeklychallenge-club-6c47b5e1dd9503743d02114cf788c2536380a734.zip
Merge pull request #4514 from wlmb/challenges
Correct mistake in annealing programs
-rwxr-xr-xchallenge-121/wlmb/perl/ch-2a.pl10
-rwxr-xr-xchallenge-121/wlmb/perl/ch-2b.pl10
2 files changed, 10 insertions, 10 deletions
diff --git a/challenge-121/wlmb/perl/ch-2a.pl b/challenge-121/wlmb/perl/ch-2a.pl
index 20945defc6..26013d3c32 100755
--- a/challenge-121/wlmb/perl/ch-2a.pl
+++ b/challenge-121/wlmb/perl/ch-2a.pl
@@ -8,9 +8,9 @@ use warnings;
use v5.12;
use PDL;
-die "Usage: ./ch-2a.pl cities steps high low data" unless @ARGV==5;
-my ($cities, $steps, $high, $low, $data)=@ARGV;
-open(my $fh, '>', $data) or die "Couldn't open $data: $!";
+die "Usage: ./ch-2a.pl cities steps high low output" unless @ARGV==5;
+my ($cities, $steps, $high, $low, $output)=@ARGV;
+open(my $fh, '>', $output) or die "Couldn't open $output: $!";
srand(0); #seed, for tests
my $M=random($cities, $cities); # generate distances matrix
$M->diagonal(0,1).=0; # zero the diagonal
@@ -22,13 +22,13 @@ my $route=pdl(0..$cities-1); #initial route
my $L=distance($route);
while($L0>$L_stop){
my $new_route=step($route);
- my $new_L=distance($route);
+ my $new_L=distance($new_route);
my $dL=$new_L-$L;
if($dL<=0 || random(1)<exp(-$dL/$L0)){
$route=$new_route; # accept
$L=$new_L;
}
- say $fh $L;
+ say $fh $L; # for plotting later
$L0*=$factor;
}
my $best_route=append($route, 0);
diff --git a/challenge-121/wlmb/perl/ch-2b.pl b/challenge-121/wlmb/perl/ch-2b.pl
index c4a4c29070..caa3647fcc 100755
--- a/challenge-121/wlmb/perl/ch-2b.pl
+++ b/challenge-121/wlmb/perl/ch-2b.pl
@@ -1,6 +1,6 @@
#!/usr/bin/env perl
# Perl weekly challenge 121
-# Task 2: The travelling salesman. Simulated Annealing
+# Task 2: The travelling salesman. Simulated Annealing. Obeying triangle inequality.
#
# See https://wlmb.github.io/2021/07/12/PWC121/#task-2-the-travelling-salesman
use strict;
@@ -8,9 +8,9 @@ use warnings;
use v5.12;
use PDL;
-die "Usage: ./ch-2a.pl cities steps high low data" unless @ARGV==5;
-my ($cities, $steps, $high, $low, $data)=@ARGV;
-open(my $fh, '>', $data) or die "Couldn't open $data: $!";
+die "Usage: ./ch-2a.pl cities steps high low output" unless @ARGV==5;
+my ($cities, $steps, $high, $low, $output)=@ARGV;
+open(my $fh, '>', $output) or die "Couldn't open $output: $!";
srand(0); #seed, for tests
my $locations=random(2,$cities); #positions of cities in a plane
my $M=(($locations->dummy(2)-$locations->dummy(1))**2)->sumover->sqrt; # euclidean distances
@@ -21,7 +21,7 @@ my $route=pdl(0..$cities-1); #initial route
my $L=distance($route);
while($L0>$L_stop){
my $new_route=step($route);
- my $new_L=distance($route);
+ my $new_L=distance($new_route);
my $dL=$new_L-$L;
if($dL<=0 || random(1)<exp(-$dL/$L0)){
$route=$new_route; # accept