blib/lib/Math/ES.pm

filter: Statementstmt 96.6% 406 / 420 Branchbran 64.6% 97 / 150 Conditioncond 55.2% 21 / 38 MC/DCmcdc 25.0% 6 / 24 Subroutinesub 100% 31 / 31 Podpod 66.6% 6 / 9 totaltotal 84.3% 567 / 672 CC 115 SCAR 25.9
CC
115
Cov
86%
validate
42.0
run
13.1
do_mixing
10.8
CRAP 13.4
↑ Summary
1 package Math::ES;
2  
3 1 require 5.005_62;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Math::Random qw( random_permuted_index );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 require Exporter;
11 # use AutoLoader qw(AUTOLOAD);
12  
13 1 our @ISA = qw(Exporter);
14  
15 1 our %EXPORT_TAGS = ();
16 1 our @EXPORT_OK = ();
17 1 our @EXPORT = ();
18 1 our $VERSION = '0.08'; # Change version number in POD !
19  
20 # --------------------------------------------------------------------
21  
22 1 my $debug = 0;
23  
24 #
25 # Selection schemes:
26 # 1 : n best survive
27 # 2 : n-1 best survive, last choses randomly
28 # 3 : GA Roulette (not implemented, yet)
29 #
30  
31 # Package variable
32 1 my $count = 0;
33 1 my $file = 'es';
34 1 my $debug_suffix = '.dbg';
35 1 my $log_suffix = '.log';
36  
37  
38 # --------------------------------------------------------------------
39 #
40 # Constructor method
41 #
42 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
43  
44 3 my $obj = shift;
45  
46 3 $count++;
47  
48 # Preset with default values
49 3 my $eso = bless {
50 'populations' => 2,
51 'individuals' => 5,
52 'parents' => 2,
53 'children' => 10,
54 'elite' => 1,
55 'selection_scheme' => 1,
56  
57 'generations' => 50,
58 'stepwidth_const' => 1,
59 'stepwidth_var' => 1.5,
60 'variance_mutator' => 0.5,
61  
62 'isolation' => 25,
63 'migrators' => 1,
64  
65 'genes' => [],
66 'gene_deviations' => [],
67 'max_gene_values' => [],
68 'min_gene_values' => [],
69 'rating_function' => '',
70  
71 'log' => 1,
72 'debug' => 0,
73  
74 'log_handle' => FileHandle->new(),
75 'debug_handle' => FileHandle->new(),
76  
77 'log_file' => "$file-$count$log_suffix",
78 'debug_file' => "$file-$count$debug_suffix",
79 }, $obj;
80  
81 # Overwrite with user specific values
82 3 ▶ $eso->set_values(@_) if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 0 3
83 3 return $eso;
84 }
85  
86 # --------------------------------------------------------------------
87 #
88 # Add user specific values
89 #
90 3 ▶sub set_values {
Subroutine set_values: called CC=1 SCAR=0.0
Pod documented
91 3 my $obj = shift;
92 # Add or overwrite
93 3 %{$obj} = ((%{$obj}) ,@_);
94 3 return ($obj);
95 }
96  
97  
98 # --------------------------------------------------------------------
99 #
100 # Validate control parameters, array conformities etc.
101 #
102 12 ▶sub validate {
Subroutine validate: called CC=26 SCAR=37.4
Pod documented
103 12 my $obj = shift;
104  
105 12 my $msg = '';
106  
107 12 ▶ $msg .= "<!> Number of populations must be greater than zero\n" if ($obj->{'populations'} < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"populations"} < 1 0 12 12
108 12 ▶ $msg .= "<!> Number of individuals must be greater than zero\n" if ($obj->{'individuals'} < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"individuals"} < 1 0 12 12
109 12 ▶ $msg .= "<!> Number of parents must be greater than zero\n" if ($obj->{'parents'} < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"parents"} < 1 0 12 12
110 12 ▶ $msg .= "<!> Number of children must be greater than zero\n" if ($obj->{'children'} < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"children"} < 1 0 12 12
111 $msg .= "<!> Number of children must be greater than or equal to number of individuals\n"
112 12 ▶ if ($obj->{'children'} < $obj->{'individuals'});
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"children"} < $obj->{"individuals"} 0 12 12
113 $msg .= "<!> Number of elite must be less than number of individuals\n"
114 12 ▶ if ($obj->{'elite'} >= $obj->{'individuals'});
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"elite"} >= $obj->{"individuals"} 0 12 12
115 $msg .= "<!> Selection scheme must be 1 or 2\n" if ($obj->{'selection_scheme'} != 1 and
116 12 ▶ $obj->{'selection_scheme'} != 2);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"selection_scheme"} != 1 and $obj->{"selection_scheme"} != 2 0 12 12
Condition66%2/3
$obj->{"selection_scheme"} != 1 and $obj->{"selection_scheme"} != 2
!ll&&!rl&&r
1020
MC/DC0%0/2
$obj->{"selection_scheme"} != 1 and $obj->{"selection_scheme"} != 2
$obj->{"selection_scheme"} != 1 $obj->{"selection_scheme"} != 2
Truth table66%2/3
A and B
A= $obj->{"selection_scheme"} != 1B= $obj->{"selection_scheme"} != 2
ABresult
0X0
100
111
117 12 ▶ $msg .= "<!> Number of generations must be greater than zero\n" if ($obj->{'generations'} < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"generations"} < 1 0 12 12
118 12 ▶ $msg .= "<!> variance_mutator must be positive\n" if ($obj->{'variance_mutator'} < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"variance_mutator"} < 0 0 12 12
119 12 ▶ $msg .= "<!> Number of isolation cycles must not be negative\n" if ($obj->{'isolation'} < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"isolation"} < 0 0 12 12
120 12 ▶ $msg .= "<!> Number of migrators must not be negative\n" if ($obj->{'migrators'} < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"migrators"} < 0 0 12 12
121
122 12 my $ng = @{$obj->{'genes'}};
123 12 my $ngd = @{$obj->{'gene_deviations'}};
124 12 my $gmx = @{$obj->{'max_gene_values'}};
125 12 my $gmn = @{$obj->{'min_gene_values'}};
126  
127 12 ▶ $msg .= "<!> Number of gene_deviations ($ngd) must be equal to number of genes ($ng)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $ng == $ngd 0 12 12
128 unless ($ng == $ngd);
129 12 ▶ $msg .= "<!> Number of max_gene_values ($gmx) must be equal to number of genes ($ng)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $ng == $gmx 0 12 12
130 unless ($ng == $gmx);
131 12 ▶ $msg .= "<!> Number of min_gene_values ($gmn) must be equal to number of genes ($ng)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $ng == $gmn 0 12 12
132 unless ($ng == $gmn);
133
134 12 for my $i (1..$ng) {
135 72 my $g = $obj->{'genes'}[$i-1];
136 72 my $max = $obj->{'max_gene_values'}[$i-1];
137 72 my $min = $obj->{'min_gene_values'}[$i-1];
138 72 ▶ $msg .= "<!> max_gene_value $i ($max) is smaller than gene $i ($g)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $ng == $gmx and $max < $g 0 72 72
Condition33%1/3
$ng == $gmx and $max < $g
!ll&&!rl&&r
0720
MC/DC0%0/2
$ng == $gmx and $max < $g
$ng == $gmx $max < $g
Truth table33%1/3
A and B
A= $ng == $gmxB= $max < $g
ABresult
0X0
100
111
139 if ($ng == $gmx and $max < $g );
140 72 ▶ $msg .= "<!> min_gene_value $i ($min) is greater than gene $i ($g)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $ng == $gmn and $min > $g 0 72 72
Condition33%1/3
$ng == $gmn and $min > $g
!ll&&!rl&&r
0720
MC/DC0%0/2
$ng == $gmn and $min > $g
$ng == $gmn $min > $g
Truth table33%1/3
A and B
A= $ng == $gmnB= $min > $g
ABresult
0X0
100
111
141 if ($ng == $gmn and $min > $g );
142 }
143  
144 12 ▶ if ($obj->{'populations'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"populations"} == 1) 2 10 12
145 $msg .= "<!> Isolation feature cannot be used for a single population\n"
146 2 ▶ if ($obj->{'isolation'} > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"isolation"} > 0 0 2 2
147 $msg .= "<!> Migration feature cannot be used for a single population\n"
148 2 ▶ if ($obj->{'migrators'} > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"migrators"} > 0 0 2 2
149 }
150  
151 $msg .= "<!> Rating function is missing\n"
152 12 ▶ unless (ref($obj->{'rating_function'}) =~ /CODE/);
Branch: 1 branch
BranchTrueFalseTotal
unless ref($obj->{"rating_function"}) =~ /CODE/ 0 12 12
153  
154 12 ▶ print "Validated\n" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 12 12
155 12 return ($msg);
156 }
157  
158 # --------------------------------------------------------------------
159 #
160 # go Darwin go
161 #
162 3 ▶sub start {
Subroutine start: called CC=9 SCAR=23.0
Pod documented
163  
164 3 my $obj = shift;
165
166 3 my $debug = $obj->{'debug'};
167 3 my $log = $obj->{'log'};
168 3 $| = 1;
169  
170 # Validate
171 3 my $msg = $obj->validate();
172 3 ▶ return ($msg) if ($msg);
Branch: 1 branch
BranchTrueFalseTotal
if $msg 0 3 3
173  
174 # Files
175 3 my $dfh = $obj->{'debug_handle'};
176 3 my $lfh = $obj->{'log_handle'};
177 3 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 3 3
178 0 open ($dfh, ">".$obj->{'debug_file'});
179 }
180 3 ▶ if ($log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log) 3 0 3
181 3 open ($lfh, ">".$obj->{'log_file'});
182 }
183  
184 # Setup
185 3 my $npop = $obj->{'populations'};
186 3 my @populations = ();
187 3 ▶ for (my $i=1; $i<=$npop; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $npop) 8 3 11
188 8 ▶ print $dfh "Creating population number $i ...\n" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 8 8
189 my $pop = Math::ES::Population->new (
190 'individuals' => $obj->{'individuals'},
191 'parents' => $obj->{'parents'},
192 'children' => $obj->{'children'},
193 'elite' => $obj->{'elite'},
194 'selection_scheme' => $obj->{'selection_scheme'},
195 'migrators' => $obj->{'migrators'},
196
197 'stepwidth_const' => $obj->{'stepwidth_const'},
198 'stepwidth_var' => $obj->{'stepwidth_var'},
199 'variance_mutator' => $obj->{'variance_mutator'},
200
201 8 'genes' => [@{$obj->{'genes'}}],
202 8 'max_gene_values' => [@{$obj->{'max_gene_values'}}],
203 8 'min_gene_values' => [@{$obj->{'min_gene_values'}}],
204 8 ▶ 'gene_deviations' => [@{$obj->{'gene_deviations'}}],
Branch: 1 branch
BranchTrueFalseTotal
defined $obj->{'max_gene_deviations'} ? : 0 8 8
205 'max_gene_deviations' =>
206 0 ▶ ( defined($obj->{'max_gene_deviations'}) ? [@{$obj->{'max_gene_deviations'}}] : [ ] ),
Branch: 1 branch
BranchTrueFalseTotal
defined $obj->{'min_gene_deviations'} ? : 0 8 8
207 'min_gene_deviations' =>
208 0 ( defined($obj->{'min_gene_deviations'}) ? [@{$obj->{'min_gene_deviations'}}] : [ ] ),
209 'rating_function' => $obj->{'rating_function'},
210
211 'debug' => $obj->{'debug'},
212 8 'debug_handle' => $obj->{'debug_handle'},
213 );
214 8 push (@populations, $pop);
215 8 ▶ print $dfh "done\n" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 8 8
216 }
217  
218 3 $obj->{'populations_list'} = [@populations];
219  
220 3 $obj->run;
221 }
222  
223 # --------------------------------------------------------------------
224 #
225 # go Darwin go
226 #
227 9 ▶sub run {
Subroutine run: called CC=13 SCAR=25.7
Pod documented
228 9 my $obj = shift;
229
230 9 my $debug = $obj->{'debug'};
231 9 my $log = $obj->{'log'};
232 9 my $dfh = $obj->{'debug_handle'};
233 9 my $lfh = $obj->{'log_handle'};
234 # $| = 1;
235  
236 # 0, Validate
237 9 my $msg = $obj->validate();
238 9 ▶ return ($msg) if ($msg);
Branch: 1 branch
BranchTrueFalseTotal
if $msg 0 9 9
239  
240 # 1, Setup
241 9 my @populations = @{$obj->{'populations_list'}};
242 9 my $nmig = $obj->{'migrators'};
243 9 my $niso = $obj->{'isolation'};
244  
245 # 2, Evaluate first generation
246 9 my @pop_rate_list;
247 my @pop_rate_ranked;
248 9 foreach my $pop (@populations) {
249 # Evaluate function
250 32 push (@pop_rate_list, $pop->rate_individuals());
251  
252 # Sort individuals
253 32 push (@pop_rate_ranked, $pop->rank_individuals());
254 }
255
256  
257 # --- Loop
258 9 my $maxgn = $obj->{'generations'};
259 9 ▶ for (my $gn = 1; $gn <= $maxgn; $gn++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gn <= $maxgn) 480 9 489
260
261 # This should go to log file
262 480 ▶ if ($log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log) 480 0 480
263 480 print $lfh ">>","-"x80,"\n";
264 480 print $lfh ">>Generation $gn\n";
265 }
266  
267 # 3, Create children
268 480 foreach my $pop (@populations) {
269 1220 $pop->manage_children();
270
271 1220 $pop->do_selection();
272  
273  
274 1220 ▶ if ($log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log) 1220 0 1220
275 1220 my $ra_p= $pop->rank_individuals();
276 1220 print $lfh " Ranking list:\t";
277 1220 foreach my $p (@$ra_p) {
278 8600 printf $lfh " %10.5f", $p;
279 }
280 1220 print $lfh "\tBest genes: ",$pop->{'individuals_list'}[0]->pretty_genes;
281 1220 print $lfh "\n";
282 }
283  
284 }
285  
286 # 4, Do migration
287 480 ▶ if ($nmig > 0 and scalar(@populations) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nmig > 0 and scalar @populations > 1) 100 380 480
Condition66%2/3
$nmig > 0 and scalar @populations > 1
!ll&&!rl&&r
3800100
MC/DC50%1/2
$nmig > 0 and scalar @populations > 1
$nmig > 0 scalar @populations > 1
Truth table66%2/3
A and B
A= $nmig > 0B= scalar @populations > 1
ABresult
0X0
100
111
288 100 $obj->do_migration();
289 }
290  
291 # Do mixing
292 480 ▶ if ($niso > 0 and scalar(@populations) > 1 and ($gn % $niso) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($niso > 0 and scalar @populations > 1 and $gn % $niso == 0) 3 477 480
Condition83%5/6
$niso > 0 and scalar @populations > 1 and $gn % $niso == 0
!ll&&!rl&&r
2002773
$niso > 0 and scalar @populations > 1
!ll&&!rl&&r
2000280
MC/DC66%2/3
$niso > 0 and scalar @populations > 1 and $gn % $niso == 0
$niso > 0 scalar @populations > 1 $gn % $niso == 0
Truth table75%3/4
A and B and C
A= $niso > 0B= scalar @populations > 1C= $gn % $niso == 0
ABCresult
0XX0
10X0
1100
1111
293 3 $obj->do_mixing();
294 }
295  
296
297 }
298  
299 9 return ($obj->return_best_value(), [$obj->return_best_genes()]);
300 }
301  
302 # --------------------------------------------------------------------
303 #
304 # Do migration of n individuals
305 #
306 100 ▶sub do_migration {
Subroutine do_migration: called CC=5 SCAR=16.1
Pod undocumented
307 100 my $obj = shift;
308  
309 100 my $debug = $obj->{'debug'};
310 100 my $dfh = $obj->{'debug_handle'};
311 100 my $nmig = $obj->{'migrators'};
312 100 my @populations = @{$obj->{'populations_list'}};
313  
314 100 for my $i (1..$nmig) {
315
316 200 my @migrators = ();
317
318 # Fetch migrator
319 200 foreach my $pop (@populations) {
320 600 push (@migrators, $pop->withdraw_random_individual());
321 }
322
323 # Insert migrator (cyclic changed)
324 200 my $p = shift (@populations); push (@populations, $p);
325 200 foreach my $pop (@populations) {
326 600 $pop->integrate_individual( shift(@migrators) );
327 }
328 }
329  
330 # Debug
331 100 ▶ print $dfh "Migrated $nmig individual(s)\n" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 100 100
332  
333 100 return (1);
334  
335 }
336  
337 # --------------------------------------------------------------------
338 #
339 # Do mixing of all populations after n generations of isolation
340 #
341 3 ▶sub do_mixing {
Subroutine do_mixing: called CC=10 SCAR=23.8
Pod undocumented
342 3 my $obj = shift;
343  
344 3 my @all_indy = ();
345 3 my @idx = ();
346 3 my @nindy = ();
347
348 3 my $debug = $obj->{'debug'};
349 3 my $dfh = $obj->{'debug_handle'};
350 3 my $niso = $obj->{'isolation'};
351 3 my @populations = @{$obj->{'populations_list'}};
352  
353 # Empty all populations
354 3 foreach my $pop (@populations) {
355 10 my $n1 = $pop->{'individuals'};
356 10 ▶ print $dfh "\t$n1 individuals in current pop\n" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 10 10
357 10 push (@all_indy, $pop->withdraw_all_individual);
358 10 push (@nindy, $n1);
359 }
360  
361 3 ▶ print $dfh "\t",scalar(@all_indy)," individuals in total\n" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3 3
362
363 # Now fill again ...
364 3 my $n2 = scalar (@all_indy);
365 3 @idx = &random_permuted_index($n2);
366
367 3 ▶ print $dfh "<D> Indexvector : \n",join(":",@idx),"\n" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3 3
368  
369 # ... all populations ...
370 3 foreach my $pop (@populations) {
371 10 my $n1 = shift(@nindy);
372
373 # ... with randomly choosen individuals.
374 10 for my $i (1..$n1) {
375 80 my $idx = shift(@idx);
376 80 ▶ if (defined($idx) and defined($all_indy[$idx])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $idx and defined $all_indy[$idx]) { } 80 0 80
Condition33%1/3
defined $idx and defined $all_indy[$idx]
!ll&&!rl&&r
0080
MC/DC0%0/2
defined $idx and defined $all_indy[$idx]
defined $idx defined $all_indy[$idx]
Truth table33%1/3
A and B
A= defined $idxB= defined $all_indy[$idx]
ABresult
0X0
100
111
377 80 $pop->integrate_individual( $all_indy[$idx] );
378 }
379 else {
380 0 print $dfh "<!> Oops, we lost an individual: $i from $n1\n";
381 }
382 }
383 }
384  
385 # Debug
386 3 ▶ print $dfh "Mixing done\n" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3 3
387  
388 3 return (1);
389  
390 }
391  
392 # --------------------------------------------------------------------
393 #
394 # Retrieve best genes from all populations
395 #
396 17 ▶sub return_best_genes {
Subroutine return_best_genes: called CC=2 SCAR=6.9
Pod documented
397 17 my $obj = shift;
398  
399 17 my @populations = @{$obj->{'populations_list'}};
400
401 17 my @best_indys = ();
402 17 foreach my $pop (@populations) {
403  
404 # Be sure, that we have an ordered list.
405 60 $pop->rank_individuals();
406
407 60 push (@best_indys, $pop->{'individuals_list'}[0]);
408 }
409  
410 17 @best_indys = sort { $a->rate() <=> $b->rate() } (@best_indys);
411  
412 17 return (@{$best_indys[0]{'genes'}});
413 }
414  
415 # --------------------------------------------------------------------
416 #
417 # Retrieve best function value from all populations
418 #
419 17 ▶sub return_best_value {
Subroutine return_best_value: called CC=2 SCAR=6.9
Pod documented
420 17 my $obj = shift;
421  
422 17 my @populations = @{$obj->{'populations_list'}};
423
424 17 my @best_indys = ();
425 17 foreach my $pop (@populations) {
426  
427 # Be sure, that we have an ordered list.
428 60 $pop->rank_individuals();
429
430 60 push (@best_indys, $pop->{'individuals_list'}[0]);
431 }
432  
433 17 @best_indys = sort { $a->rate() <=> $b->rate() } (@best_indys);
434  
435 17 return ($best_indys[0]->rate);
436 }
437  
438 # --------------------------------------------------------------------
439 # --------------------------------------------------------------------
440  
441 package Math::ES::Population;
442 1 ▶use Math::Random qw( random_uniform_integer );
Subroutine BEGIN: called CC=1 SCAR=0.0
443  
444 8 ▶sub new {
Subroutine new: called CC=7 SCAR=19.6
445 8 my $name = shift;
446 8 my $obj = bless {@_}, $name;
447
448 8 my $debug = $obj->{'debug'};
449 8 my $dfh = $obj->{'debug_handle'};
450  
451 8 my $nindiv = $obj->{'individuals'};
452 8 $obj->{'pop_counter'} = 0;
453  
454 8 ▶ print $dfh " Creating population with $nindiv members\n" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 8 8
455
456 8 my @individuals = ();
457 8 ▶ for (my $j=1; $j <= $nindiv; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $nindiv) 60 8 68
458 60 ▶ print $dfh "\tCreating individuum $j out of $nindiv ... " if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 60 60
459 # Guarantee a individual with the input genes
460 60 my $do_mutate = 1;
461 60 ▶ $do_mutate = 0 if ($j == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $j == 1 8 52 60
462 my $indi = Math::ES::Individuum->new (
463 'pop_rate_individuals' => undef,
464 60 'genes' => [@{$obj->{'genes'}}],
465 60 'gene_deviations' => [@{$obj->{'gene_deviations'}}],
466 60 'max_gene_values' => [@{$obj->{'max_gene_values'}}],
467 60 'min_gene_values' => [@{$obj->{'min_gene_values'}}],
468 'rating_function' => $obj->{'rating_function'},
469  
470 'stepwidth_const' => $obj->{'stepwidth_const'},
471 'stepwidth_var' => $obj->{'stepwidth_var'},
472 'variance_mutator' => $obj->{'variance_mutator'},
473
474 'mutate' => $do_mutate,
475  
476 60 'debug' => $obj->{'debug'},
477 );
478 60 push (@individuals, $indi);
479 60 ▶ print $dfh " ok\n" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 60 60
480 }
481 8 $obj->{'individuals_list'} = [@individuals];
482 8 ▶ print $dfh " done\n" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 8 8
483  
484 # ---
485  
486 8 return $obj;
487 }
488  
489 # -------------
490  
491 # Create n children stemming from m parents, mutate them, rate them
492 1220 ▶sub manage_children {
Subroutine manage_children: called CC=8 SCAR=21.7
493 1220 my $obj = shift;
494
495 1220 my $debug = $obj->{'debug'};
496 1220 my $dfh = $obj->{'debug_handle'};
497  
498 1220 my $nchld = $obj->{'children'};
499 1220 my $nindy = $obj->{'individuals'};
500 1220 my $npar = $obj->{'parents'};
501  
502 1220 my @new_children = ();
503  
504 1220 $obj->{'children_list'} = [];
505  
506 1220 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 1220 1220
507 0 print $dfh "<D> Managing children\n";
508  
509 0 print $dfh "<D> Parents\n";
510 0 my $pp=0;
511 0 foreach my $p (@{$obj->{'individuals_list'}}) {
512 0 print $dfh "Parent $pp = ",$p->pretty_genes(),"\n";
513 0 $pp++;
514 }
515 }
516  
517 # Create children
518 1220 for my $nc (1..$nchld) {
519 23300 my $child = Math::ES::Individuum->new();
520  
521 # Determine parents
522 23300 my @parents_idx = ();
523 23300 my @parents_list = ();
524 23300 for my $np (1..$npar) {
525 50617 my $num = random_uniform_integer(1, 0,$nindy-1);
526 50617 ▶ if (grep(/^$num$/, @parents_idx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^$num$/, @parents_idx) { } 4017 46600 50617
527 4017 redo;
528 }
529 else {
530 46600 push (@parents_idx, $num) ;
531 46600 push (@parents_list, $obj->{'individuals_list'}[$num]);
532 }
533 }
534  
535 # Now do the origination (data copy and crossover)
536 23300 ▶ print $dfh "<D> Parents chosen for crossover ",join(' : ',@parents_idx),"\n" if($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 23300 23300
537 23300 $child->originate(@parents_list);
538  
539 # ... mutate it ...
540 23300 $child->mutate();
541  
542 # ... and rate it
543 23300 $child->rate();
544  
545 23300 push (@{$obj->{'children_list'}}, $child);
546  
547 23300 ▶ print $dfh "Child $nc = ",$child->pretty_genes()," >=> ",$child->rate(),"\n" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 23300 23300
548 }
549  
550 1220 $obj->rank_children();
551
552 1220 return(@{$obj->{'children_list'}});
553 }
554  
555 # -------------
556  
557 2684 ▶sub rate_individuals {
Subroutine rate_individuals: called CC=4 SCAR=13.9
558 2684 my $obj = shift;
559
560 2684 ▶ unless (exists($obj->{'pop_rate_individuals'}) or defined($obj->{'pop_rate_individuals'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $obj->{"pop_rate_individuals"} or defined $obj->{"pop_rate_individuals"}) 8 2676 2684
Condition66%2/3
exists $obj->{"pop_rate_individuals"} or defined $obj->{"pop_rate_individuals"}
l!l&&r!l&&!r
267608
MC/DC50%1/2
exists $obj->{"pop_rate_individuals"} or defined $obj->{"pop_rate_individuals"}
exists $obj->{"pop_rate_individuals"} defined $obj->{"pop_rate_individuals"}
Truth table66%2/3
A or B
A= exists $obj->{"pop_rate_individuals"}B= defined $obj->{"pop_rate_individuals"}
ABresult
1X1
011
000
561 8 $obj->{'pop_rate_individuals'} = 0;
562 8 foreach my $indy (@{$obj->{'individuals_list'}}) {
563 60 $obj->{'pop_rate_individuals'} += $indy->rate();
564 }
565 }
566  
567 2684 return($obj->{'pop_rate_individuals'});
568 }
569  
570 # -------------
571  
572 2652 ▶sub rank_individuals {
Subroutine rank_individuals: called CC=2 SCAR=6.9
573 2652 my $obj = shift;
574  
575 2652 $obj->rate_individuals();
576  
577 2652 my @temp = sort { $a->rate() <=> $b->rate() } (@{$obj->{'individuals_list'}});
578 2652 $obj->{'individuals_list'} = [@temp];
579  
580 2652 my @temp2;
581 2652 foreach my $indy (@{$obj->{'individuals_list'}}) {
582 21250 push (@temp2, $indy->rate());
583 }
584 2652 $obj->{'ranked_rates_individuals'} = [@temp2];
585 2652 return(\@temp2);
586 }
587  
588 # -------------
589  
590 1220 ▶sub rate_children {
Subroutine rate_children: called CC=4 SCAR=13.9
591 1220 my $obj = shift;
592
593 1220 ▶ unless (exists($obj->{'pop_rate_children'}) or defined($obj->{'pop_rate_children'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $obj->{"pop_rate_children"} or defined $obj->{"pop_rate_children"}) 8 1212 1220
Condition66%2/3
exists $obj->{"pop_rate_children"} or defined $obj->{"pop_rate_children"}
l!l&&r!l&&!r
121208
MC/DC50%1/2
exists $obj->{"pop_rate_children"} or defined $obj->{"pop_rate_children"}
exists $obj->{"pop_rate_children"} defined $obj->{"pop_rate_children"}
Truth table66%2/3
A or B
A= exists $obj->{"pop_rate_children"}B= defined $obj->{"pop_rate_children"}
ABresult
1X1
011
000
594 8 $obj->{'pop_rate_children'} = 0;
595 8 foreach my $indy (@{$obj->{'children_list'}}) {
596 160 $obj->{'pop_rate_children'} += $indy->rate();
597 }
598 }
599  
600 1220 return($obj->{'pop_rate_children'});
601 }
602  
603 # -------------
604  
605 1220 ▶sub rank_children {
Subroutine rank_children: called CC=2 SCAR=6.9
606 1220 my $obj = shift;
607  
608 1220 $obj->rate_children();
609  
610 1220 my @temp = sort { $a->rate() <=> $b->rate() } (@{$obj->{'children_list'}});
611 1220 $obj->{'children_list'} = [@temp];
612  
613 1220 my @temp2;
614 1220 foreach my $indy (@{$obj->{'children_list'}}) {
615 23300 push (@temp2, $indy->rate());
616 }
617 1220 $obj->{'ranked_rates_children'} = [@temp2];
618 1220 return(\@temp2);
619 }
620  
621 # -------------
622  
623 1220 ▶sub do_selection {
Subroutine do_selection: called CC=9 SCAR=22.0
624 1220 my $obj = shift;
625  
626 1220 my @new_indies = ();
627  
628 1220 my $nchld = $obj->{'children'};
629 1220 my $nindy = $obj->{'individuals'};
630 1220 my $elite = $obj->{'elite'};
631  
632 # Respect the elite
633 1220 ▶ if ($elite > 0 and $elite <= $nindy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elite > 0 and $elite <= $nindy) 920 300 1220
Condition66%2/3
$elite > 0 and $elite <= $nindy
!ll&&!rl&&r
3000920
MC/DC50%1/2
$elite > 0 and $elite <= $nindy
$elite > 0 $elite <= $nindy
Truth table66%2/3
A and B
A= $elite > 0B= $elite <= $nindy
ABresult
0X0
100
111
634 920 my @temp = sort { $a->rate() <=> $b->rate() } (@{$obj->{'children_list'}}, @{$obj->{'individuals_list'}});
635
636 920 for my $i (1..$elite) {
637 1120 push (@new_indies, $temp[$i-1]);
638 }
639 }
640  
641 # Deal with the rest
642 1220 my $nrest = $nindy - $elite;
643 1220 ▶ if ($nrest > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nrest > 0) 1220 0 1220
644  
645 # Selection according to scheme
646 1220 my $scheme = $obj->{'selection_scheme'};
647  
648 # 1 = Select n best
649 1220 ▶ if ($scheme == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scheme == 1) { } 1020 200 1220
elsif ($scheme == 2) { } 200 0 200
650 1020 foreach my $i (1..$nrest) {
651 5880 push (@new_indies, $obj->{'children_list'}[$i-1]);
652 }
653 }
654  
655 # 2 = Select n-1 best and one random other
656 elsif ($scheme == 2) {
657 200 foreach my $i (1..$nrest-1) {
658 1400 push (@new_indies, $obj->{'children_list'}[$i-1]);
659 }
660 200 my $lastone = random_uniform_integer(0, $nrest, $nchld);
661 200 push (@new_indies, $obj->{'children_list'}[$lastone-1]);
662 }
663
664 }
665  
666 # Move to next generation
667 1220 $obj->{'individuals_list'} = [@new_indies];
668 1220 $obj->{'pop_counter'}++;
669
670 }
671  
672 # -------------
673 # Withdraw a number of individuals from the population
674 # but spare the elite.
675 #
676 600 ▶sub withdraw_random_individual {
Subroutine withdraw_random_individual: called CC=5 SCAR=16.2
677 600 my $obj = shift;
678  
679 600 ▶ my $num = (shift || 1);
Condition50%1/2
shift() || 1
l!l
0600
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
680  
681 600 my ($nindy, $elite);
682 600 $elite = $obj->{'elite'};
683  
684 600 my @withdrawn = ();
685 600 for my $i (1..$num) {
686  
687 600 $nindy = $obj->{'individuals'};
688 600 ▶ last if ($nindy-$elite <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $nindy - $elite <= 0 0 600 600
689 600 ▶ last if ($nindy == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $nindy == 0 0 600 600
690  
691 600 my $num = random_uniform_integer(0, $elite+1, $nindy);
692  
693 600 $obj->{'individuals'}--;
694 600 push (@withdrawn, splice(@{$obj->{'individuals_list'}},$num-1,1));
695 }
696  
697 600 $obj->rank_individuals();
698  
699 600 return(@withdrawn);
700 }
701  
702 # -------------
703 # Withdraw all individuals
704 #
705 10 ▶sub withdraw_all_individual {
Subroutine withdraw_all_individual: called CC=1 SCAR=0.0
706 10 my $obj = shift;
707  
708  
709 10 my @withdrawn = @{$obj->{'individuals_list'}};
710 10 $obj->{'individuals_list'} = [];
711 10 $obj->{'individuals'} = 0;
712  
713 10 return(@withdrawn);
714 }
715  
716 # -------------
717 # Add a number of new individuals to the population
718 #
719 680 ▶sub integrate_individual {
Subroutine integrate_individual: called CC=2 SCAR=6.9
720 680 my $obj = shift;
721
722 680 foreach my $indy (@_) {
723 680 $obj->{'individuals'}++;
724 680 push (@{$obj->{'individuals_list'}}, $indy);
725 }
726  
727 680 $obj->rank_individuals();
728  
729 680 return($obj);
730 }
731  
732  
733 # --------------------------------------------------------------------
734 # --------------------------------------------------------------------
735 package Math::ES::Individuum;
736 1 ▶use Math::Random qw(random_normal random_uniform);
Subroutine BEGIN: called CC=1 SCAR=0.0
737  
738 # -----------
739 # Constructor of a new individuum
740 #
741 23360 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
742 23360 my $name = shift;
743 23360 my $obj = bless {@_}, $name;
744  
745 23360 $obj->{'indy_rate'} = undef;
746  
747 23360 ▶ if ($obj->{'mutate'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"mutate"}) 52 23308 23360
748 52 $obj->mutate;
749 }
750  
751 23360 return ($obj);
752 }
753  
754 # -----------
755 # Return the rating function value of the individuum
756 #
757 #
758 398347 ▶sub rate {
Subroutine rate: called CC=2 SCAR=6.9
759 398347 my $obj = shift;
760
761  
762 # Call the rating function (if no value is present)
763 #
764 # &function(@values) returns a result
765 398347 ▶ unless (defined $obj->{'indy_rate'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj->{"indy_rate"}) 23360 374987 398347
766 23360 $obj->{'indy_rate'} = &{$obj->{'rating_function'}}( @{$obj->{'genes'}} );
767 }
768 398347 return ($obj->{'indy_rate'});
769 }
770  
771  
772 # -----------
773 # Do mutation on individuum
774 #
775 # $obj->mutate();
776 #
777 23352 ▶sub mutate {
Subroutine mutate: called CC=10 SCAR=23.7
778  
779 23352 my $obj = shift;
780  
781 # Firstly mutate deviations
782 23352 my $i=-1;
783 23352 foreach my $gd (@{$obj->{'gene_deviations'}}) {
784 125856 my $rnn = random_normal(0,0, $obj->{'variance_mutator'});
785 125856 $i++;
786 125856 my $tmp = $gd * exp($rnn);
787 125856 ▶ if (defined($obj->{'max_gene_deviations'}[$i]) and
Branch: 2 branches
BranchTrueFalseTotal
if (defined $obj->{'max_gene_deviations'}[$i] and $tmp > $obj->{'max_gene_deviations'}[$i]) { } 0 125856 125856
elsif (defined $obj->{'min_gene_deviations'}[$i] and $tmp < $obj->{'min_gene_deviations'}[$i]) { } 0 125856 125856
Condition33%1/3
defined $obj->{"max_gene_deviations"}[$i] and $tmp > $obj->{"max_gene_deviations"}[$i]
!ll&&!rl&&r
12585600
MC/DC0%0/2
defined $obj->{"max_gene_deviations"}[$i] and $tmp > $obj->{"max_gene_deviations"}[$i]
defined $obj->{"max_gene_deviations"}[$i] $tmp > $obj->{"max_gene_deviations"}[$i]
Truth table33%1/3
A and B
A= defined $obj->{"max_gene_deviations"}[$i]B= $tmp > $obj->{"max_gene_deviations"}[$i]
ABresult
0X0
100
111
788 $tmp > $obj->{'max_gene_deviations'}[$i]) {
789 0 $gd = $obj->{'max_gene_deviations'}[$i];
790 }
791 ▶ elsif (defined($obj->{'min_gene_deviations'}[$i]) and
Condition33%1/3
defined $obj->{"min_gene_deviations"}[$i] and $tmp < $obj->{"min_gene_deviations"}[$i]
!ll&&!rl&&r
12585600
MC/DC0%0/2
defined $obj->{"min_gene_deviations"}[$i] and $tmp < $obj->{"min_gene_deviations"}[$i]
defined $obj->{"min_gene_deviations"}[$i] $tmp < $obj->{"min_gene_deviations"}[$i]
Truth table33%1/3
A and B
A= defined $obj->{"min_gene_deviations"}[$i]B= $tmp < $obj->{"min_gene_deviations"}[$i]
ABresult
0X0
100
111
792 $tmp < $obj->{'min_gene_deviations'}[$i]) {
793 0 $gd = $obj->{'min_gene_deviations'}[$i];
794 }
795 else {
796 125856 $gd = $tmp;
797 };
798 }
799  
800 # Secondly mutate genes
801 23352 my $n = @{$obj->{'genes'}};
802 23352 ▶ for (my $i=0; $i<$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 125856 23352 149208
803  
804 Try: {
805 125856 my $var = $obj->{'stepwidth_var'};
806 282732 ▶ my $factor = ( random_uniform() > 0.5 ? $var : 1/$var ) * $obj->{'stepwidth_const'};
Branch: 1 branch
BranchTrueFalseTotal
random_uniform() > 0.5 ? : 141227 141505 282732
807
808 282732 my $gd = $obj->{'gene_deviations'}[$i];
809 282732 my $rnn = random_normal(0,0,$gd);
810
811 282732 my $temp = $obj->{'genes'}[$i] + ($rnn * $factor);
812 282732 ▶ redo Try if ($temp > $obj->{'max_gene_values'}[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if $temp > $obj->{"max_gene_values"}[$i] 71751 210981 282732
813 210981 ▶ redo Try if ($temp < $obj->{'min_gene_values'}[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if $temp < $obj->{"min_gene_values"}[$i] 85125 125856 210981
814  
815 125856 $obj->{'genes'}[$i] = $temp;
816 }
817  
818 }
819
820 23352 return (1);
821 }
822  
823 # -----------
824 # Simulate the originating process of a new individuum.
825 #
826 # $child_obj->originate($parent1, $parent1, ...)
827 #
828 23300 ▶sub originate {
Subroutine originate: called CC=5 SCAR=16.1
829 23300 my $obj = shift;
830 23300 my @parents = @_; # Allow more than 1 or 2 cross over parents
831  
832 23300 my $np = @parents;
833  
834 # Copy all info from first parent
835 23300 $parents[0]->copy_to($obj);
836  
837 # ... but reset the value !!!
838 23300 $obj->{'indy_rate'} = undef;
839  
840 23300 my $n = @{$obj->{'genes'}};
841
842 # We have more than one parent, do the crossover
843 23300 ▶ unless ($np == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($np == 1) 23300 0 23300
844  
845 # Iterate over the genes
846 23300 ▶ for (my $i=0; $i<$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 125600 23300 148900
847 125600 my $rnu = random_uniform();
848 # print "Random Number: $rnu\n";
849  
850 # Find the appropriate parent
851 125600 ▶ Parent: for (my $p=0; $p<$np; $p++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $np) 188104 0 188104
852 188104 ▶ if ($rnu <= 1/$np*($p+1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rnu <= 1 / $np * ($p + 1)) 125600 62504 188104
853 125600 $obj->{'genes'}[$i] = $parents[$p]->{'genes'}[$i];
854 125600 $obj->{'gene_deviations'}[$i] = $parents[$p]->{'gene_deviations'}[$i];
855 125600 last Parent;
856 }
857 }
858 }
859
860 }
861 23300 return ($obj);
862 }
863  
864 # -----------
865 # Copy operator for an individuum
866 # $from_obj->copy_to($to_obj);
867  
868 23300 ▶sub copy_to {
Subroutine copy_to: called CC=4 SCAR=14.0
869 23300 my $obj = shift;
870 23300 my $new = shift;
871
872 23300 foreach (keys (%{$obj})) {
873 326156 my $temp = $obj->{$_};
874 326156 ▶ if (ref($temp) =~ 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref($temp) =~ /ARRAY/) { } 139756 186400 326156
elsif (ref($temp) =~ /HASH/) { } 0 186400 186400
875 139756 $new->{$_} = [@$temp];
876 }
877 elsif (ref($temp) =~ 'HASH') {
878 0 $new->{$_} = {%$temp};
879 }
880 else {
881 186400 $new->{$_} = $temp; # Scalars and programs go here
882 }
883 };
884 23300 return ($new);
885 }
886  
887 # -----------
888 # Return the genes and variances in a 'pretty' style
889 #
890 1220 ▶sub pretty_genes {
Subroutine pretty_genes: called CC=2 SCAR=6.9
891 1220 my $obj = shift;
892  
893 1220 my $n = @{$obj->{'genes'}};
894 1220 my $output;
895  
896 # Iterate over the genes
897 1220 ▶ for (my $i=0; $i<$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 7040 1220 8260
898 $output .= sprintf("%10.6f", $obj->{'genes'}[$i])
899 7040 . ' (' . sprintf("%10.6f", $obj->{'gene_deviations'}[$i]) . ')';
900 }
901 1220 return ($output);
902 }
903  
904 1 1;
905  
906 __END__
↑ Summary