-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tsp.pl
More file actions
199 lines (164 loc) · 7.33 KB
/
run_tsp.pl
File metadata and controls
199 lines (164 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/perl
use strict;
use warnings;
my $out_file_name = "tsp_experiments.dat";
# clean previous output file
# system("rm -f $out_file_name") >= 0
# or die "\nAborting execution (impossible to write/remove file: $?)";
my @config_files = create_config_files();
my $count = 1;
foreach my $config (@config_files)
{
# skip completed experiments
if($count < 37)
{
$count++;
next;
}
# copy config file
system("cp $config Config.java") >= 0
or die "\nAborting execution due to config file copy error: $?";
# compile
system("rm -f *.class ; javac *.java") >= 0
or die "\nAborting execution due to compile error: $?";
# status message
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
print sprintf("[%02d:%02d:%02d] Experiment \# %3d/%d: %s ", $hour, $min, $sec, ($count++), scalar(@config_files), $config =~ m/config\/(.+)\.java/);
# run experiments
my @exectimes;
for (my $exec = 0; $exec < 10 ; $exec++)
{
# gnu time utility (/usr/bin/time) for "wall/real time" measuring
# (since time uses STDERR, we must redirect it to STDERR
my $run = "time -f \"\%e\" java myTSPDriver" . " 2>&1";
$exectimes[$exec] = `$run`;
}
# mean and standard deviation from 30 executions
my @stats = stats(@exectimes);
print sprintf("(time: %.4fs +/- %.4fs)\n", $stats[0], $stats[1]);
# current configuration (for output file)
$config =~ m/config\/in(\d)_it(\d)_ant(\d)_thr(\d)\.java/;
my ($in) = $1;
my ($it) = $2;
my ($ant) = $3;
my ($thr) = $4;
# user-friendly configuration (for output file)
if ($in == 1) { $in = 42; }
elsif ($in == 2) { $in = 96; }
elsif ($in == 3) { $in = 229; }
elsif ($in == 4) { $in = 431; }
elsif ($in == 5) { $in = 666; }
if ($it == 1) { $it = 50; }
elsif ($it == 2) { $it = 150; }
elsif ($it == 3) { $it = 300; }
if ($ant == 1) { $ant = 20; }
elsif ($ant == 2) { $ant = 50; }
elsif ($ant == 3) { $ant = 150; }
# write output file
open my $out_file, ">>", $out_file_name;
print $out_file sprintf("$in $it $ant $thr %.4f %.4f\n", $stats[0], $stats[1]);
close $out_file;
}
# clean *.class files
system("rm *.class");
exit(0);
sub stats
{
## execution statistics function, returning [mean, stddev] for the sample in
## the given array
my(@sample) = @_;
my $len = (scalar @sample);
# prevent division by 0 error
return undef unless($len);
# direct and squared element sum
my $total1 = 0;
my $total2 = 0;
foreach my $num (@sample) {
$total1 += $num;
$total2 += $num**2;
}
# mean / average
my $mean = $total1 / $len;
# standard deviation
my $diff = $total2/$len - $mean**2;
my $std_dev = sqrt($diff);
my @stat = ($mean, $std_dev);
return @stat;
}
sub create_config_files
{
## create experiments config directory and files (framework java Config
## class) returns an array containing Config files path
my @config_iterations =
( " public static final int max_iterations = 50; // 50 , 150 , 300\n"
, " public static final int max_iterations = 150; // 50 , 150 , 300\n"
, " public static final int max_iterations = 300; // 50 , 150 , 300\n"
);
my @config_ants =
( " public static final int num_ants = 20; // 20 , 50 , 150\n"
, " public static final int num_ants = 50; // 20 , 50 , 150\n"
, " public static final int num_ants = 150; // 20 , 50 , 150\n"
);
my @config_threads =
# ( " public static final int num_threads = 1; // 1 , 2 , 3 , 4 , 5 , 6\n"
# , " public static final int num_threads = 2; // 1 , 2 , 3 , 4 , 5 , 6\n"
( " public static final int num_threads = 3; // 1 , 2 , 3 , 4 , 5 , 6\n"
# , " public static final int num_threads = 4; // 1 , 2 , 3 , 4 , 5 , 6\n"
# , " public static final int num_threads = 5; // 1 , 2 , 3 , 4 , 5 , 6\n"
# , " public static final int num_threads = 6; // 1 , 2 , 3 , 4 , 5 , 6\n"
);
my @config_input =
( "\n public static final String input_file = \"input/dantzig42_opt699_disttable.txt\";\n public static final boolean computed_dist_table = true;\n public static final int num_cities = 42;\n public static final long known_optimum = 699;\n"
, "\n public static final String input_file = \"input/gr96_opt55209.txt\";\n public static final boolean computed_dist_table = false;\n public static final int num_cities = 96;\n public static final long known_optimum = 55209;\n"
, "\n public static final String input_file = \"input/gr229_opt134602.txt\";\n public static final boolean computed_dist_table = false;\n public static final int num_cities = 229;\n public static final long known_optimum = 134602;\n"
, "\n public static final String input_file = \"input/gr431_opt171414.txt\";\n public static final boolean computed_dist_table = false;\n public static final int num_cities = 431;\n public static final long known_optimum = 171414;\n"
, "\n public static final String input_file = \"input/gr666_opt294358.txt\";\n public static final boolean computed_dist_table = false;\n public static final int num_cities = 666;\n public static final long known_optimum = 294358;\n"
);
my @config_base_prefix =
( "public class Config"
, "{"
, " public static final double initial_pheromone = 0.33, evaporation_rate = 0.5;"
, " public static final double pheromone_weight = 1, heuristic_weight = 2;"
, " public static final int num_exec = 1;"
, " public static long rand_seed = 1234567;"
, ""
, " ////////////////////////////////////////////////////////////////////////////"
, ""
);
my @config_base_sufix =
( " // dantzig42_opt699_disttable.txt"
, " // gr96_opt55209.txt"
, " // gr229_opt134602.txt"
, " // gr431_opt171414.txt"
, " // gr666_opt294358.txt"
, " ////////////////////////////////////////////////////////////////////////////"
, "}"
, "\n"
);
# create experiments config directory and files
mkdir("config") if !(-e "config");
my @config_files;
for (my $input = 1; $input <= @config_input ; $input++)
{
for (my $iter = 1; $iter <= @config_iterations ; $iter++)
{
for (my $ants = 1; $ants <= @config_ants ; $ants++)
{
for (my $threads = 1; $threads <= @config_threads; $threads++)
{
my $file_name = "config/in$input\_it$iter\_ant$ants\_thr$threads.java";
push(@config_files, $file_name);
open my $config_file, ">", $file_name;
print $config_file join("\n", @config_base_prefix);
print $config_file $config_iterations[$iter-1];
print $config_file $config_ants[$ants-1];
print $config_file $config_threads[$threads-1];
print $config_file $config_input[$input-1];
print $config_file join("\n", @config_base_sufix);
close $config_file;
}
}
}
}
return @config_files;
}