-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze-convergence.pl
246 lines (202 loc) · 6.46 KB
/
analyze-convergence.pl
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/perl
use warnings;
use strict;
my @filenames;
my %times;
my %convergenceGenerations;
my $dir = $ARGV[0];
my $timeFile = $ARGV[1];
my $optimum = $ARGV[2];
open(my $fh, "<", $timeFile) or die "Could not open experiment output file";
while(<$fh>) {
chomp;
/^(.+) took (\d+)m (\d+)s$/;
$times{$1} = $2*60 + $3;
}
close $fh;
opendir(DIR, $dir) or die "Could not open results directory $dir";
while ($_ = readdir(DIR)) {
push @filenames, "$_" if (/^run-\d+\.txt$/);
}
sub sortFiles {
$a =~ /run-(\d+)\.txt/; my $num1 = $1;
$b =~ /run-(\d+)\.txt/; my $num2 = $1;
$num1 <=> $num2;
}
open(my $csv, '>', "$dir/summary.csv");
print $csv "Evolutionary System,Problem,Hierarchy Type,Structure,Run,Generations to Convergence,Highest Achieved Fitness,First Occurence Generation of Highest Fitness,Individuals Meeting Convergence Criteria\n" if (-z $csv);
my $experimentName = $dir;
$experimentName =~ s/^experiment-results\///;
my $experimentRegex = '(.+)\/(1max|LongFrag|AverageFrag)_(?:\d(Coev|Hier))?(.+)';
print "Analyzing $experimentName\n";
open(my $gens, '>>', "generations.csv");
if (-z $gens) {
print $gens "Evolutionary System,Problem,Hierarchy Type,Structure,Run,Generation,";
for (my $i = 0; $i < 32; $i++) {
print $gens "# With Fitness $i,";
}
print $gens "# With Fitness 32,";
for (my $i = 0; $i < 32; $i++) {
print $gens "# With HD $i,";
}
print $gens "# With HD 32\n";
}
foreach (sort sortFiles @filenames) {
my $converged = 0;
my $populationSize = 0;
my $generation = 0;
my @lines;
my @members;
my $bestFitness = 0;
my $bestGeneration = 0;
my $convergedMembers = 0;
my @allFitnesses;
my @allDistances;
my @hammingDistances = (0) x 33;
open($fh, "<", "$dir/$_") or die "Could not open results file $_";
/^run-(\d+)\.txt$/;
my $run = $1;
while(<$fh>) {
chomp;
push @lines, $_;
}
close $fh;
foreach (@lines) {
$populationSize = $1 if (!$populationSize and /^Population size: (\d+)$/);
if (/^Before generation (\d+):$/) {
if (@members) {
my $optimal = 0;
my $bestMember = 0;
my @fitnesses = (0) x 33;
foreach my $fitness (@members) {
$optimal++ if ($fitness >= $optimum);
if ($fitness > $bestFitness) {
$bestFitness = $fitness;
$bestGeneration = $generation;
}
$fitnesses[$fitness] += 1;
}
my @temp = @hammingDistances;
push @allFitnesses, \@fitnesses;
push @allDistances, \@temp;
if ($optimal > 0 && !$converged) {
$convergenceGenerations{"Run $run"} = $generation;
$convergedMembers = $optimal;
$converged = 1;
last;
}
}
# Reset for the next generation
$generation = $1-1;
undef(@members);
@hammingDistances = (0) x 33;
}
if (/^Member (\d+): ([01]+) Fitness: (\d+)$/ && $converged == 0) {
push @members, $3;
my $distance = ($2 =~ tr/0//);
$hammingDistances[$distance]++;
}
}
if (!$converged) {
$convergenceGenerations{"Run $run"} = "Failed to converge"
}
$experimentName =~ /$experimentRegex/;
print $csv "$1,$2,".($3 || "N/A").",$4,"; #Info about the experiment
print $csv "$run,".($converged ? $convergenceGenerations{"Run $run"} : "Failed").",$bestFitness,$bestGeneration,$convergedMembers\n";
for (my $i=0; $i <= $bestGeneration; $i++) {
print $gens "$1,$2,".($3 || "N/A").",$4,$run,$i,";
for (my $k=0; $k < 32; $k++) {
print $gens "$allFitnesses[$i][$k],";
}
print $gens "$allFitnesses[$i][32],";
for (my $k=0; $k < 32; $k++) {
print $gens "$allDistances[$i][$k],";
}
print $gens "$allDistances[$i][32]\n";
}
}
close ($gens);
close ($csv);
my $mean = 0;
my $slowest = 0;
my $sum = 0;
my $fastest = 100;
my $runCount = 0;#keys %convergenceGenerations;
foreach my $run (keys %convergenceGenerations) {
my $gen = $convergenceGenerations{$run};
if ($gen ne "Failed to converge") {
$runCount++;
$sum += $gen;
$slowest = $gen if $gen > $slowest;
$fastest = $gen if $gen < $fastest;
}
}
$mean = $sum/$runCount if $runCount;
open($fh, '>', "$dir/results.txt");
sub sortRuns {
$a =~ /^Run (\d+)$/;
my $num1 = $1;
$b =~ /^Run (\d+)$/;
my $num2 = $1;
$num1 <=> $num2;
}
print $fh "Experiment: $experimentName\n";
print $fh "================================================\n";
open($csv, '>>', "results.csv");
print $csv "Evolutionary System, Problem, Hierarchy Type, Structure, Run Time (seconds), Success, Mean, Fastest, Slowest, 25th %tile, Med, 75th %tile, IQR, Variance, StdDev, StdErr\n" if (-z $csv);
$experimentName =~ /$experimentRegex/;
print $csv "$1,$2,".($3 || "N/A").",$4,"; #Information about the experiment
print $csv $times{$experimentName}.",";
if ($runCount) {
print $fh "Successful runs: $runCount\n";
print $fh "Run time: $times{$experimentName} seconds\n";
print $fh "Mean convergence time: $mean generations\n";
print $fh "Fastest convergence time: $fastest generations\n";
print $fh "Slowest convergence time: $slowest generations\n";
my ($median, $twentyFifth, $seventyFifth);
my @convergences;
foreach my $run (keys %convergenceGenerations) {
my $value = $convergenceGenerations{$run};
push @convergences, $value if $value ne "Failed to converge";
}
@convergences = sort {$a <=> $b} @convergences;
if ($runCount >= 50) {
$median = $convergences[49];
} else {
$median = 'Inf';
}
if ($runCount >= 25) {
$twentyFifth = $convergences[24];
} else {
$twentyFifth = 'Inf';
}
if ($runCount >= 75) {
$seventyFifth = $convergences[74];
} else {
$seventyFifth = 'Inf';
}
print $fh "Twenty-fifth percentile: $twentyFifth generations\n";
print $fh "Median: $median generations\n";
print $fh "Seventy-fifth percentile: $seventyFifth generations\n";
print $fh "Interquartile range: ".(my $iqRange = $seventyFifth - $twentyFifth)." generations\n";
#Calculate the variance and standard deviation
my $variance=0;
$variance += ($_-$mean)**2 foreach (@convergences);
$variance = $variance/$runCount;
my $stdDev = sqrt($variance);
my $stdError = $stdDev/sqrt($runCount);
print $fh "Variance: $variance\n";
print $fh "Standard deviation: $stdDev\n";
print $fh "Standard error: $stdError\n";
print $csv "$runCount,$mean,$fastest,$slowest,$twentyFifth,$median,$seventyFifth,$iqRange, $variance, $stdDev, $stdError\n";
} else {
print $fh "All runs failed to converge in 100 generations.\n";
print $csv "All runs failed to converge\n";
}
close $csv;
print $fh "================================================\n";
print $fh "Convergence generations by run:\n";
foreach my $run (sort sortRuns keys %convergenceGenerations) {
printf $fh "%-8s: %s\n", $run, $convergenceGenerations{$run};
}
close $fh;