blib/lib/Optimization/NSGAII.pm

filter: Statementstmt 78.8% 321 / 407 Branchbran 78.2% 61 / 78 Conditioncond 45.0% 18 / 40 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 90.4% 19 / 21 Podpod 8.3% 1 / 12 totaltotal 73.5% 425 / 578 SCAR 52.1
CC
85
Cov
76%
f_Optim_NSGAII
72.7
f_plot
72.0
f_fast_non_dominated_sort
12.0
CRAP 182.5
↑ Summary
1 package Optimization::NSGAII;
2  
3 23 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 23 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 our @ISA = ("Exporter");
10 23 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use List::Util qw/min max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =pod
15
16 =head1 NAME
17  
18 Optimization::NSGAII - non dominant sorting genetic algorithm for multi-objective optimization (also known as NSGA2)
19  
20 =head1 VERSION
21  
22 Version 0.02
23  
24 =cut
25  
26 23 our $VERSION = "0.02";
27  
28 =head1 SYNOPSIS
29  
30 use Optimization::NSGAII qw/ f_Optim_NSGAII /;
31 use Data::Dumper;
32  
33 # D E F I N E O B J E C T I V E S T O O P T I M I Z E
34 ###########################################################
35  
36 sub f_to_optimize {
37
38 my $x = shift; # load input parameters (genes constituting a single individual)
39  
40 # ... # do your things using these inputs $x->[0], $x->[1] ...
41 # and produce the outputs to be minimized $f1, $f2 ...
42  
43 # examples of things you can do include:
44 # - mathematical formulas in perl to define $f1, $f2, ...
45 # - computations with commercial software and so:
46 # - write input file using $x->[0] ...
47 # - run the computation, for example with perl system() function
48 # - locally or
49 # - on a server for example with 'qsub... '
50 # - wait simulation to end
51 # - postprocess its output and define $f1, $f2 ...
52 # - ...
53
54 my $out = [$f1,$f2,$f3]; # and finally return the set of these outputs for
55 return $out; # this single individual of the population
56
57 }
58  
59 # D E F I N E B O U N D S [ A N D I N E Q U A L I T I E S ]
60 ###################################################################
61
62 # define min and max bounds for $x->[0], $x->[1], ...
63 my $bounds = [[0,1],[0,1],[0,1]]; # example with 3 input parameters (genes) with min = 0 and max = 1:
64
65 sub f_inequality { # optional inequality constraints set
66
67 my $x =shift;
68
69 my @ineq =
70 (
71 $x->[1] + 1 , # equations >= 0
72 $x->[0] + $x->[1] - 9,
73 ...
74 ...
75
76 );
77
78 return \@ineq;
79 }
80
81 # R U N O P T I M I Z A T I O N
82 #################################
83  
84 # execute NSGA-II algorithm
85  
86 my $ref_input_output = f_Optim_NSGAII(
87 {
88
89 'nPop' => 50, # population size
90 'nGen' => 250, # final generation number
91 'bounds' => $bounds, # loads the previously defined bounds
92 'function' => \&f_to_optimize, # loads the subroutine to optimize (minimize)
93 'nProc' => 8, # how many individuals to evaluate in parallel as separate processes
94 'filesDir' => '/tmp', # work directory
95
96
97 # optional parameters:
98
99 'verboseFinal' => 1, # 0|1: input and output values print at final generation, for each individual of the population
100 # default: print is made ( = 1)
101 'f_ineq' => \&f_inequality, # subroutine describing the constraining inequality set
102 # default: no constraint function
103
104 # parameters for mutation
105
106 'distrib' => [-1,0,1], # distribution of values (for example a Gaussian distribution), used to perturb individuals
107 # default: [-1,-0.5,0,0.5,1]
108 'scaleDistrib' => 0.05, # scaling of the distribution array
109 # default: 0 (no perturbation will be done)
110 'percentMut' => 5, # percentage of individual that are randomly perturbated (in all their genes)
111 # and also percentage of input parameters (genes) that are randomly mutated in each individual
112 # default: 5%
113 'startPop' => [[0.3,0.18,-0.1],# initial population
114 [-0.38,0.5,0.1], # default: random population satisfying the bounds
115 ...,
116 ]
117  
118 },
119  
120 # the following is the optional set of parameters for 'Pareto front' 2D live plot
121 # if the part below is not present, no plot will be made
122  
123 {
124  
125 'dx' => 200, # characters width and height of the plot
126 'dy' => 40,
127 'xlabel' => 'stiffness [N/mm]', # horizontal and vertical axis labels
128 'ylabel' => 'mass [kg]',
129 'xlim' => [0,1], # horizontal and vertical axis limits
130 'ylim' => [0,1],
131 'nfun' => [0,2], # which function to plot from return value by f_to_optimize ($out) ; 0=f1, 1=f2 ...
132 }
133 );
134  
135 # U S E S O L U T I O N S
136 ############################
137  
138 # for example print of the input parameters and
139 # corresponding output functions' values of the final found Pareto front
140
141 my @ref_input = @{$ref_input_output->[0]};
142 my @ref_output = @{$ref_input_output->[1]};
143  
144 print Dumper(\@ref_input);
145 print Dumper(\@ref_output);
146  
147  
148  
149  
150 =head1 EXPORT
151  
152 =over
153  
154 =item * f_Optim_NSGAII
155  
156 =back
157  
158 =cut
159  
160 23 our @EXPORT_OK = qw/ f_Optim_NSGAII /;
161  
162 =head1 DESCRIPTION
163  
164  
165 =head2 Reference
166  
167 NSGAII.pm apply (with some variations) the NSGA-II algorithm described in the paper:
168  
169 A Fast and Elitist Multiobjective Genetic Algorithm:NSGA-II
170  
171 =over
172  
173 Kalyanmoy Deb, Associate Member, IEEE, Amrit Pratap, Sameer Agarwal, and T. Meyarivan
174  
175 =back
176  
177  
178 =head2 Objective
179  
180 C<NSGAII.pm> performs multi-objective optimization using a genetic algorithm approach: it searches the input parameters (genes) which minimize a set of output functions and with some luck a Pareto front is produced.
181 In the Pareto front no solution is better than the others because each solution is a trade-off.
182  
183 =head2 Function to optimize
184  
185 This module requires to define a perl subroutine (C<f_to_optimize> in the code above) which can take the input parameters and gives the corresponding outputs (in other words, it requires a subroutine to evaluate an individual of this population)
186  
187  
188 =head2 Features
189  
190 The optimization is done:
191  
192 =over 3
193  
194 =item * considering allowed B<boundary for each input parameter (min e max)>
195  
196 =item * considering optional B<set of inequality equations containing input parameters> (x1^2 + sqrt(x2) -x3 >= 0 , ...)
197  
198 =item * with a B<parallel evaluation> of the subroutine to optimize (and so of individuals) in each generation, by using perl fork() function
199  
200 =back
201  
202 The inequalities must be given by a subroutine which calculate the error, look below in the example: basically all the LHS of the inequalities in the form "... >=0" are put in an array.
203  
204 The number of B<parallel evaluation> of C<f_to_optimize>, and so the value of C<nProc>, can be for example the max number of parallel C<f_to_optimize> computation that you want and can:
205  
206 =over
207  
208 =item * run on your pc if you run the computation locally (e.g. 4)
209  
210 =item * run on a remote server if you run (inside the C<f_to_optimize>) the computation there (e.g. 32)
211  
212 =back
213  
214 C<nPop> should be multiple of C<nProc>, to optimize resources use, but it is not necessary.
215  
216 Problems with this modules are expected on systems not supporting fork() perl function.
217  
218 A B<2D plot> can be activated to control in real time the convergence of the algorithm on two chosen output functions (to assist at the formation of the Pareto front, generation after generation).
219  
220 Each time a new generation finish, all information of the population are written in the C<filesDir> directory:
221  
222 =over
223  
224 =item * F<VPt_genXXXXX.txt>: input (parameters values)
225  
226 =item * F<Pt_genXXXXX.txt>: output (corresponding functions values)
227  
228 =back
229  
230 The algorithm can start by default with a random initial population (satisfying the bounds) or the B<start population> can be assigned by assigning it to the C<startPop> option.
231  
232 Assigning the population at the start can be useful for example if:
233  
234 =over
235  
236 =item * there was an unexpected termination of the program during the optimization, so that one can restart by using the content of one of the last saved F<VPt_genXXXXX.txt>
237  
238 =item * there is the interest in continuing the optimization with different parameters
239  
240 =item * there is already an idea of some input parameters which could give a good output
241  
242 =back
243  
244 For an example of use see F<NSGAII_startPop_example.pl>.
245  
246 =head2 Mutation
247  
248 The implementation of the B<mutation algorithm> part has been done in a B<different way if compared to that described in the paper>.
249  
250 In particular B<two mutation> are applied in sequence:
251  
252 =over
253  
254 =item 1) mutation of all the input parameters (the genes), but only on a certain percentage C<percentMut> of the population:
255  
256 -> Small perturbation of the each gene by adding a number chosen randomly from the given C<distrib> (scaled with both C<scaleDistrib> and the difference between the two bounds).
257  
258 =item 2) mutation of all the individuals of the population, but only on a certain percentage C<percentMut> of the input parameters (the genes)
259  
260 -> Random change (inside the permitted bounds)
261  
262 =back
263  
264  
265 =head2 Verification
266  
267 This module has been tested, successfully, on many of the test problems defined in the paper described in the Reference section (see EXAMPLE section)
268  
269 The performance (convergence for same population number and max generation number) seems to be comparable to that described in that paper.
270  
271  
272  
273  
274 =head1 EXAMPLE
275  
276 B<More examples> (the same test problems contained in the paper described in the Reference section) are available in the test folder (F<NSGAII_all_examples.pl> containing ZDT1, ZDT2, TNK, ...).
277  
278 Here you see the B<CONSTR> problem with:
279  
280 =over
281  
282 =item * two input parameters $x->[0] and $x->[1]
283  
284 =item * two output functions to optimize f1 and f2
285  
286 =item * two constraining equations between the input parameters
287  
288 =item * 8 process in parallel (8 subroutine f_CONTRS are evaluated in parallel as indipendent processes)
289  
290 =back
291  
292 use Optimization::NSGAII qw/ f_Optim_NSGAII /;
293
294 # function to minimize
295 sub f_CONSTR {
296
297 my $x = shift;
298
299 my $n = scalar(@$x);
300
301 my $f1 = $x->[0];
302 my $f2 = (1 + $x->[1])/$x->[0];
303
304 my $out = [$f1,$f2];
305
306 return $out;
307 }
308  
309 # inequality constraints set
310 sub f_inequality {
311 my $x =shift;
312
313 # equation >= 0
314 my @ineq =
315 (
316 $x->[1] + 9*$x->[0] - 6 ,
317 -$x->[1] + 9*$x->[0] -1
318 );
319
320 return \@ineq;
321 }
322  
323 my $bounds = [[0.1,1],[0,5]];
324  
325 my $ref_input_output = f_Optim_NSGAII(
326 {
327 'nPop' => 50,
328 'nGen' => 100,
329 'bounds' => $bounds,
330 'function' => \&f_CONSTR,
331 'f_ineq' => \&f_inequality,
332 'nProc' => 8,
333 'filesDir' => '/tmp',
334 'verboseFinal' => 1,
335 'distrib' => [-1,-0.9,-0.8,-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9],
336 'scaleDistrib' => 0.05,
337 'percentMut' => 5,
338 },
339 {
340 'dx' => 100,
341 'dy' => 40,
342 'xlabel' => 'stiffness [N/mm]',
343 'ylabel' => 'mass [kg]',
344 'xlim' => [0.1,1],
345 'ylim' => [0,10],
346 'nfun' => [0,1],
347 }
348 );
349  
350  
351  
352  
353 =head1 OUTPUT PREVIEW
354  
355 This below is a typical output of the final Pareto front (problem TNK).
356  
357 The numbers represent the rank of the points: in the initial generations you can see points of rank 1,2,3... where points with rank 1 dominate points of rank 2 and so on.
358  
359 Generation after generation all the points go on the Pareto front, so all the points become rank 1 (not dominated, nothing best is present in this population)
360  
361 The points will also expand to occupy the entire front.
362  
363 GENERATION 250
364 m|
365 a|
366 s|
367 s|
368 |
369 [|
370 k|
371 g| 1
372 ]| 11
373 | 11
374 | 1 1
375 | 11
376 | 1
377 | 11 1 1 1 1
378 | 1
379 | 1
380 |
381 | 1
382 | 1 1
383 | 1111
384 | 1
385 |
386 |
387 |
388 |
389 | 1
390 | 11
391 | 1
392 | 1
393 |
394 |______________________________________________________________________
395 stiffness [N/mm]
396  
397  
398  
399  
400 =head1 INSTALLATION
401  
402 Following the instruction in perlmodinstall:
403  
404 =over
405  
406 =item * download the F<Optimization-NSGAII-X.XX.tar.gz>
407  
408 =item * decompress and unpack
409  
410 =over
411  
412 =item * C<gzip -d Optimization-NSGAII-X.XX.tar.gz>
413  
414 =item * C<tar -xof Optimization-NSGAII-X.XX.tar>
415  
416 =back
417  
418 =item * C<cd Optimization-NSGAII-X.XX>
419  
420 =item * C<perl Makefile.PL>
421  
422 =item * C<make>
423
424 =item * C<make test>
425  
426 =item * C<make install>
427  
428 to install it locally use this instead of C<perl Makefile.PL>:
429  
430 C<perl Makefile.PL PREFIX=/my/folder> if you want to install it in /my/folder
431 then you will have to use in your script: C<use lib "path/before(Optimization/NSGAII.pm);"> before using C<use Optimization::NSGAII qw/ f_Optim_NSGAII /;>
432  
433 =back
434  
435  
436  
437  
438  
439 =head1 AUTHOR
440  
441 Dario Rubino, C<< <drubino at cpan.org> >>
442  
443  
444  
445  
446 =head1 BUGS
447  
448 Solutions (input-output pairs) often contain duplicates, this would require some investigation.
449  
450 Please report any bugs to C<bug-optimization-nsgaii at rt.cpan.org>, or through
451 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Optimization-NSGAII>. I will be notified, and then you'll
452 automatically be notified of progress on your bug as I make changes.
453  
454  
455  
456  
457 =head1 SUPPORT
458  
459 You can find documentation for this module with the perldoc command.
460  
461 perldoc Optimization::NSGAII
462  
463  
464 You can also look for information at:
465  
466 =over 4
467  
468 =item * RT: CPAN's request tracker (report bugs here)
469  
470 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Optimization-NSGAII>
471  
472 =item * CPAN Ratings
473  
474 L<https://cpanratings.perl.org/d/Optimization-NSGAII>
475  
476 =item * Search CPAN
477  
478 L<https://metacpan.org/release/Optimization-NSGAII>
479  
480 =back
481  
482 =head1 LICENSE AND COPYRIGHT
483  
484 This software is Copyright (c) 2022 by Dario Rubino.
485  
486 This is free software, licensed under:
487  
488 The Artistic License 2.0 (GPL Compatible)
489  
490 =cut
491  
492 # A point in the population is defined by:
493 # - input values for the objective functions (input parameters) -> contained in the reference VP
494 # - correspondent output population (objective function values) -> contained in the reference P
495 #
496 # P is the most important set of values used in this package, because the objective functions' values lead the algorithm
497 # VP elements then are postprocessed accordingly so that P and VP maintain always a one to one index correspondence
498 #
499 # VARIABLES
500 #
501 # VP: input values for each population's point
502 # : [[x11,x12,...x1D],...]
503 # P : population (objective values for each point)
504 # : [[fun11,fun12,..fun1M],..]
505  
506 # D : number of dimension of the problem (how many input values for a point)
507 # N : size of population P
508  
509 # p,q : index of a single solution of the population
510 # : id_sol_ith
511 # Sp: set of solution dominated by p (worse than p) for each point
512 # : [[id_sol_i,id_sol_j,...],..]
513  
514 # np: number of solutions which dominate p (better than p), np = zero for first front solutions
515 # : [np1,np2,..]
516 # F: set of solution in front ith
517 # : [[id_sol_k,id_sol_t,...],...]
518 # rank : rank of each solution = front number , rank = 1 for first front solutions
519 # : [rank1,rank2...rankN];
520  
521 ########################################################################################################
522  
523 23 my @out_print;
524 23 my $inf = 1e9;
525  
526 sub f_ineq2maxerr {
527 # max error calculation in inequalities
528 # input: lhs inequalities ref (errors)
529 # output: max error
530 4346688 ▶ my $ref_ineq = shift;
Subroutine f_ineq2maxerr: called CC=2 SCAR=6.9
Pod undocumented
531
532 4346688 my @ineq = @{$ref_ineq};
533
534 4346688 my $err = 0;
535 4346688 foreach my $el(@ineq){
536 4346688 $err = max( $err, -$el);
537 }
538 4346688 return $err;
539 }
540  
541  
542 sub f_fast_non_dominated_sort {
543 # reference to population
544 121 ▶ my $P = shift;
Subroutine f_fast_non_dominated_sort: called CC=12 SCAR=24.8
Pod undocumented
545 121 my $VP = shift;
546 121 my $f_ineq = shift;
547
548
549 121 my $Sp;
550 my $np;
551 121 my $F;
552 121 my $rank;
553
554 121 my $N = scalar(@{$P});
555  
556 121 foreach my $p(0..$N-1){
557 12100 $np->[$p] = 0;
558 12100 foreach my $q(0..$N-1){
559 1210000 ▶ next if $p == $q; # max error <-- inequalities <-- input pars
Branch: 1 branch
BranchTrueFalseTotal
if $p == $q 12100 1197900 1210000
560 1197900 ▶ if ( f_dominates($P->[$p],$P->[$q], f_ineq2maxerr( &{$f_ineq}($VP->[$p]) ), f_ineq2maxerr( &{$f_ineq}($VP->[$q])) ) ){
Branch: 2 branches
BranchTrueFalseTotal
if (f_dominates($P->[$p], $P->[$q], f_ineq2maxerr(&{$f_ineq;}($VP->[$p])), f_ineq2maxerr(&{$f_ineq;}($VP->[$q])))) { } 222456 975444 1197900
elsif (f_dominates($P->[$q], $P->[$p], f_ineq2maxerr(&{$f_ineq;}($VP->[$q])), f_ineq2maxerr(&{$f_ineq;}($VP->[$p])))) { } 218508 756936 975444
561 222456 push @{$Sp->[$p]}, $q;
562 }
563 975444 elsif ( f_dominates($P->[$q],$P->[$p], f_ineq2maxerr( &{$f_ineq}($VP->[$q]) ), f_ineq2maxerr( &{$f_ineq}($VP->[$p])) ) ){
564 218508 $np->[$p] += 1;
565 }
566 }
567 12100 ▶ if ($np->[$p] == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($np->[$p] == 0) 1821 10279 12100
568 1821 $rank->[$p]=1; # front number, 1 = best
569 1821 push @{$F->[0]}, $p;
570 }
571 }
572
573 # all other fronts
574 121 my $i = 0;
575
576 121 ▶ while (scalar(@{$F->[$i]})){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$F->[$i];}) 1558 121 1679
577 1558 my @Q;# members of next front
578
579 1558 foreach my $p(@{$F->[$i]}){
580 12100 foreach my $q(@{$Sp->[$p]}){
581 222456 $np->[$q] -=1;
582 222456 ▶ if ($np->[$q] == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($np->[$q] == 0) 10279 212177 222456
583 10279 $rank->[$q] = $i + 1 + 1;
584 10279 push @Q, $q;
585 }
586 }
587 }
588 1558 $i++;
589 1558 $F->[$i] = [@Q];
590
591 }
592  
593 121 for my $p(0..$N-1){
594 12100 push @out_print, join(' ',($rank->[$p],@{$P->[$p]}));
595 }
596  
597 121 return ($rank,$F); # rank for each point, points in each front
598
599 }
600  
601 ########################################################################################################
602  
603 sub f_dominates {
604 # input: two elements of P
605 # output: 1 if P1 dominate P2, 0 otherwise
606
607 2173344 ▶ my $P1 = shift;
Subroutine f_dominates: called CC=6 SCAR=17.9
Pod undocumented
608 2173344 my $P2 = shift;
609
610 # constraints errors
611 2173344 my $err1 = shift;
612 2173344 my $err2 = shift;
613
614  
615 # number of objective (dimensions)
616 2173344 my $M = scalar(@{$P1});
617
618 2173344 my $P1_dominate_P2_count = 0;
619
620 2173344 for my $kM(0..$M-1){
621 4346688 ▶ if ($P1->[$kM] <= $P2->[$kM]){
Branch: 1 branch
BranchTrueFalseTotal
if ($P1->[$kM] <= $P2->[$kM]) 2395800 1950888 4346688
622 2395800 $P1_dominate_P2_count++;
623 }
624 }
625  
626 2173344 my $P1_dominate_P2_p;
627 # if 1 has lower constraints errors, then 1 dominate 2, else if the error is the same (e.g 0) then the dominance is decided on objective value
628 # else simply 1 doesn't dominate 2
629 2173344 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($err1 < $err2 or $err1 == $err2 and $P1_dominate_P2_count == $M) { } 440964 1732380 2173344
Condition66%4/6
$err1 < $err2 or $err1 == $err2 and $P1_dominate_P2_count == $M
l!l&&r!l&&!r
04409641732380
$err1 == $err2 and $P1_dominate_P2_count == $M
!ll&&!rl&&r
01732380440964
MC/DC33%1/3
$err1 < $err2 or $err1 == $err2 and $P1_dominate_P2_count == $M
$err1 < $err2 $err1 == $err2 $P1_dominate_P2_count == $M
Truth table50%2/4
A or B and C
A= $err1 < $err2B= $err1 == $err2C= $P1_dominate_P2_count == $M
ABCresult
1XX1
0111
00X0
0100
630 $err1 < $err2
631 ||
632 ($err1 == $err2 && $P1_dominate_P2_count == $M)
633 ){
634 440964 $P1_dominate_P2_p = 1;
635 }
636 1732380 else { $P1_dominate_P2_p = 0}
637
638 2173344 return $P1_dominate_P2_p;
639
640 }
641  
642 ########################################################################################################
643  
644 sub f_crowding_distance_assignment{
645 # input: ref of array of a subset of population P, population P
646 # output: distance value for each point of this set
647  
648 # global ids of the set of non dominated points of interest
649 596 ▶ my $ids = shift;
Subroutine f_crowding_distance_assignment: called CC=4 SCAR=13.9
Pod undocumented
650 # objectives of all points
651 596 my $P = shift;
652
653 # build the set of objectives for these global ids (I is a subset of P)
654 596 my $I = [@{$P}[@{$ids}]];
655
656 # initialize distance
657 596 my $Dist;
658
659 # number of objective (dimensions)
660 596 my $M = scalar(@{$P->[0]});
661 # number of points in I
662 596 my $n = scalar(@{$ids});
663
664 # for each objective
665 596 for my $kM(0..$M-1){
666  
667 # local id of the points, sorted by objective
668 1192 my @index_sort = sort{$I->[$a][$kM] <=> $I->[$b][$kM]} 0..($n-1);
669  
670 # min & max for this objective
671 1192 my $fmin = $I->[$index_sort[0]][$kM];
672 1192 my $fmax = $I->[$index_sort[-1]][$kM];
673
674 1192 ▶ if ($fmax-$fmin<0.00001){$fmax += 0.00001}
Branch: 1 branch
BranchTrueFalseTotal
if ($fmax - $fmin < "1e-05") 0 1192 1192
675  
676 # build the distance
677 1192 $Dist->[$index_sort[0]] = $inf;
678 1192 $Dist->[$index_sort[-1]] = $inf;
679 # and for all other intermediate point
680 1192 for my $i(1..($n-2)){
681 11646 $Dist->[$index_sort[$i]] += ($I->[$index_sort[$i+1]][$kM] - $I->[$index_sort[$i-1]][$kM])/($fmax-$fmin);
682 }
683
684 }
685 # return distances for each point, $Dist->[ith] is distance for point $ids->[ith]
686 596 return $Dist;
687 }
688  
689 ########################################################################################################
690  
691 sub f_crowding_distance_operator {
692 # input: rank and distance of two element of the population (selected by GA)
693 # output: 1 if first dominate second, else 0
694
695 12100 ▶ my $rank = shift;
Subroutine f_crowding_distance_operator: called CC=4 SCAR=13.9
Pod undocumented
696 12100 my $Dist = shift;
697
698 12100 my $P1_dominate_P2_p;
699
700 12100 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($rank->[0] < $rank->[1] or $rank->[0] == $rank->[1] and $Dist->[0] > $Dist->[1]) { } 5902 6198 12100
Condition100%6/6
$rank->[0] < $rank->[1] or $rank->[0] == $rank->[1] and $Dist->[0] > $Dist->[1]
l!l&&r!l&&!r
419417086198
$rank->[0] == $rank->[1] and $Dist->[0] > $Dist->[1]
!ll&&!rl&&r
412320751708
MC/DC100%3/3
$rank->[0] < $rank->[1] or $rank->[0] == $rank->[1] and $Dist->[0] > $Dist->[1]
$rank->[0] < $rank->[1] $rank->[0] == $rank->[1] $Dist->[0] > $Dist->[1]
Truth table100%4/4
A or B and C
A= $rank->[0] < $rank->[1]B= $rank->[0] == $rank->[1]C= $Dist->[0] > $Dist->[1]
ABCresult
1XX1
0111
00X0
0100
701 $rank->[0] < $rank->[1]
702 ||
703 ($rank->[0] == $rank->[1]) && ($Dist->[0] > $Dist->[1])
704 ){
705 5902 $P1_dominate_P2_p = 1
706 }
707 6198 else {$P1_dominate_P2_p = 0}
708 12100 return $P1_dominate_P2_p;
709 }
710  
711 ########################################################################################################
712  
713 sub f_NSGAII {
714 # input: current function input values VP(t) & VQ(t) ( VQ is obtained by GA)
715 # current population Pt & Qt (obtained by evaluating objective function using VPt & VQt)
716 # output: VP(t+1), P(t+1), rank & distance for each point of this new population
717
718 # input variables' values
719 121 ▶ my $VPt = shift;
Subroutine f_NSGAII: called CC=2 SCAR=6.9
Pod undocumented
720 121 my $VQt = shift;
721
722 # population (objective function values)
723 121 my $Pt = shift;
724 121 my $Qt = shift;
725
726 # constraints function
727 121 my $f_ineq = shift;
728
729 121 my $Rt = [(@$Pt,@$Qt)];
730 121 my $VRt = [(@$VPt,@$VQt)];
731 121 my $N = scalar(@$Pt);
732
733 121 my ($temp,$F) = f_fast_non_dominated_sort($Rt,$VRt,$f_ineq);
734
735 # input variables for the new population P_t+1
736 121 my $VPtp1=[];
737 # new population
738 121 my $Ptp1=[];
739 121 my $Dist=[];
740 121 my $rank=[];
741
742 121 my $i=0;
743 # push the best fronts in final population & store crowding distance
744 121 ▶ while ( scalar(@{$Ptp1}) + scalar(@{$F->[$i]}) <= $N ){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@{$Ptp1;}) + scalar(@{$F->[$i];}) <= $N) 475 121 596
745 475 push @$Ptp1, @$Rt[@{$F->[$i]}];
746 475 push @$VPtp1,@$VRt[@{$F->[$i]}];
747 475 my $Dist_ = f_crowding_distance_assignment($F->[$i],$Rt);
748 475 push @$rank, ($i+1) x @$Dist_;
749 475 push @$Dist, @$Dist_;
750 475 $i++;
751 }
752  
753 # only part of the following front will be pushed in final population, sorting by crowding
754 # here rank is the same for all points, so the crowded-comparison operators reduce to a comparison of crowding distance
755 121 my $Dist_ = f_crowding_distance_assignment($F->[$i],$Rt);
756  
757 121 my $nf = scalar(@{$F->[$i]});
758
759 121 my @index_sort = sort{$Dist_->[$b] <=> $Dist_->[$a]} 0..($nf-1);
760
761 # cut to fill only the remaining part of Ptp1
762 121 @index_sort = @index_sort[0..(($N-scalar(@$Ptp1))-1)];
763  
764 121 push @$Ptp1, @$Rt[@{$F->[$i]}[@index_sort]];
765 121 push @$VPtp1, @$VRt[@{$F->[$i]}[@index_sort]];
766 121 push @$rank, ($i+1) x @index_sort;
767 121 push @$Dist, @$Dist_[@index_sort];
768  
769  
770 121 return $VPtp1,$Ptp1,$rank,$Dist;
771  
772 }
773  
774 ########################################################################################################
775  
776 sub f_SBX {
777 6050 ▶ my $contiguity = shift; # >0 (usually between 2 and 5), greater value produces child values similar to those of the parents
Subroutine f_SBX: called CC=6 SCAR=17.9
Pod undocumented
778 6050 my $VP1_ = shift; # input values of parent 1 (ref)
779 6050 my $VP2_ = shift; # input values of parent 2 (ref)
780
781 # array of equal length
782 6050 my @VP1 = @$VP1_;
783 6050 my @VP2 = @$VP2_;
784 6050 my @out;
785
786 6050 for my $kp(0..$#VP1){
787 # for array of length=1 or rand<0.5 the cross is made
788 18150 ▶ if ( $#VP1 == 0 || rand(1)<0.5){
Branch: 1 branch
BranchTrueFalseTotal
if ($#VP1 == 0 or rand 1 < 0.5) { } 9108 9042 18150
Condition66%2/3
$#VP1 == 0 or rand 1 < "0.5"
l!l&&r!l&&!r
091089042
MC/DC50%1/2
$#VP1 == 0 or rand 1 < "0.5"
$#VP1 == 0 rand 1 < "0.5"
Truth table66%2/3
A or B
A= $#VP1 == 0B= rand 1 < "0.5"
ABresult
1X1
011
000
789
790 9108 my $u = rand(1);
791
792 9108 my $exponent = (1/($contiguity+1));
793 9108 ▶ my $beta = ($u < 0.5)? (2*$u)**$exponent : (0.5/(1-$u))**$exponent;
Branch: 1 branch
BranchTrueFalseTotal
$u < 0.5 ? : 4645 4463 9108
794
795 9108 ▶ my $sign = (rand(1) < 0.5)? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
rand 1 < 0.5 ? : 4516 4592 9108
796 9108 $out[$kp] = (($VP1[$kp] + $VP2[$kp])/2) + $sign * $beta*0.5*abs($VP1[$kp] - $VP2[$kp])
797
798 }
799 else {
800 9042 $out[$kp] = $VP1[$kp]
801 }
802 }
803 6050 return \@out;
804 }
805  
806 ########################################################################################################
807  
808 sub f_GA {
809 # input: input values, rank and Dist of the population P(t+1)
810 # output: input value of the population Q(t+1)
811
812 121 ▶ my $VPtp1 = shift;
Subroutine f_GA: called CC=10 SCAR=23.0
Pod undocumented
813 121 my $rank = shift;
814 121 my $Dist = shift;
815 121 my $contiguity = shift;
816
817 121 my $bounds = shift; # for mutation
818
819 # optional paramters for mutation:
820 121 my $distrib = shift;
821 121 my $scaleDistrib = shift;
822 121 my $percentMut =shift;
823
824 121 my $VQtp1 = [];
825
826 # binary tournament
827 # two random element are compared
828 # the best according crowding distance operator is selected as parent 1
829 # the same for selecting parent 2
830 # parent 1 and 2 are crossed with SBX to give a child
831 # the procedure is repeated to fill Q(t+1)
832
833 121 my $N = scalar(@$VPtp1);
834
835 121 for my $kt(0..$N-1){
836  
837 # selection of the two parent
838 6050 my @index_parent;
839 6050 for (1..2){
840 12100 my ($r1,$r2) = (int(rand($N)),int(rand($N)));
841 12100 my $P1_dominate_P2_p = f_crowding_distance_operator( [$rank->[$r1],$rank->[$r2]] , [$Dist->[$r1],$Dist->[$r2]] );
842
843 12100 ▶ if ($P1_dominate_P2_p == 1){push @index_parent, $r1}
Branch: 1 branch
BranchTrueFalseTotal
if ($P1_dominate_P2_p == 1) { } 5902 6198 12100
844 6198 else {push @index_parent, $r2}
845 }
846 # crossover of the two parent
847 6050 my $out = f_SBX( $contiguity , $VPtp1->[$index_parent[0]] , $VPtp1->[$index_parent[1]] );
848 6050 push @$VQtp1, $out;
849 }
850
851 # mutation 1
852 # perturbation using distribution array
853 121 for my $k(0..(scalar(@$VQtp1) - 1)){
854 # $percentMut percentage of individuals are changed in all their elements (all input parameter will be perturbated)
855 6050 ▶ if (rand(1)> 1-$percentMut/100){
Branch: 1 branch
BranchTrueFalseTotal
if (rand 1 > 1 - $percentMut / 100) 228 5822 6050
856 228 for my $d(0..(scalar(@{$VQtp1->[0]}) - 1)){
857 # increment the current value by a random value chosen from the distribution (many time it will be near to zero for a gaussian distribution) scaled with the delta_bounds
858 684 $VQtp1->[$k][$d] += ($bounds->[$d][1] - $bounds->[$d][0]) * $scaleDistrib * $distrib->[int(rand(scalar(@$distrib)))];
859 }
860 }
861 }
862
863 # mutation 2
864 # 100% probability of mutation inside VQ(t+1), so the intention is to act on all individual of the population
865 121 for my $k(0..int((scalar(@$VQtp1) - 1) * 100/100)){
866 6050 for my $d(0..(scalar(@{$VQtp1->[0]}) - 1)){
867 # $percentMut percentage of values are changed inside each single individual, value is inside bounds
868 18150 ▶ if (rand(1)> 1-$percentMut/100){
Branch: 1 branch
BranchTrueFalseTotal
if (rand 1 > 1 - $percentMut / 100) 1030 17120 18150
869 1030 $VQtp1->[$k][$d] = rand(1) * ($bounds->[$d][1] - $bounds->[$d][0]) + $bounds->[$d][0];
870 }
871 }
872 }
873  
874 121 return $VQtp1;
875  
876 }
877  
878  
879  
880 sub f_Optim_NSGAII {
881  
882 23 ▶ my $par = shift;
Subroutine f_Optim_NSGAII: called CC=38 SCAR=42.9
Pod documented
883
884 23 my $par_plot = shift;
885
886
887
888 23 my %par = %{$par};
889  
890 23 my $nPop = $par{'nPop'};
891 23 my $nGen = $par{'nGen'};
892  
893 23 my $bounds = $par{'bounds'};
894 23 my $n = scalar @$bounds;
895
896 23 my $fun = $par{'function'};
897
898 # optional paramters:
899
900 23 ▶ my $f_ineq = $par{'f_ineq'} // sub {return [0]};
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$par{"f_ineq"} // sub { return [0]; }
l!l&&r!l&&!r
0230
MC/DC0%0/2
$par{"f_ineq"} // sub { return [0]; }
$par{"f_ineq"} sub { return [0]; }
Truth table33%1/3
A // B
A= $par{"f_ineq"}B= sub { return [0]; }
ABresult
1X1
011
000
901  
902 23 ▶ my $verboseFinal = $par{'verboseFinal'} // 1;
Condition50%1/2
$par{"verboseFinal"} // 1
l!l
230
MC/DC0%0/1
$par{"verboseFinal"} // 1
$par{"verboseFinal"}
Truth table50%1/2
A
A= $par{"verboseFinal"}
Aresult
11
00
903  
904 23 ▶ my $distrib = $par{'distrib'} // [-1,-0.5,0,0.5,1]; # for mutation, default [-1,-0.5,0,0.5,1]
Condition50%1/2
$par{"distrib"} // [-1, "-0.5", 0, "0.5", 1]
l!l
230
MC/DC0%0/1
$par{"distrib"} // [-1, "-0.5", 0, "0.5", 1]
$par{"distrib"}
Truth table50%1/2
A
A= $par{"distrib"}
Aresult
11
00
905 23 ▶ my $scaleDistrib = $par{'scaleDistrib'} // 0; # for mutation, default = 0, no perturbation
Condition50%1/2
$par{"scaleDistrib"} // 0
l!l
230
MC/DC0%0/1
$par{"scaleDistrib"} // 0
$par{"scaleDistrib"}
Truth table50%1/2
A
A= $par{"scaleDistrib"}
Aresult
11
00
906 23 ▶ my $percentMut = $par{'percentMut'} // 5; # for mutation, default = 5%
Condition50%1/2
$par{"percentMut"} // 5
l!l
023
MC/DC0%0/1
$par{"percentMut"} // 5
$par{"percentMut"}
Truth table50%1/2
A
A= $par{"percentMut"}
Aresult
11
00
907
908 23 ▶ my $startPop = $par{'startPop'} // 'nostartPop';
Condition50%1/2
$par{"startPop"} // "nostartPop"
l!l
023
MC/DC0%0/1
$par{"startPop"} // "nostartPop"
$par{"startPop"}
Truth table50%1/2
A
A= $par{"startPop"}
Aresult
11
00
909  
910 # control for no wrong use of keys
911 23 my @keys_ok = ('nPop','nGen','bounds','function','nProc','filesDir','verboseFinal','f_ineq','distrib','scaleDistrib','percentMut','startPop');
912 23 for my $key(keys %par){
913 207 ▶ unless ( grep( /^$key$/, @keys_ok ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^$key$/, @keys_ok) 0 207 207
914 0 die 'E R R O R : the use of "'.$key.'" in the function to optimize is not defined! Compare with documentation.';
915 }
916 }
917
918 23 my $VPt;
919 my $VQt;
920  
921  
922 23 for my $gen(0..$nGen){
923 143 ▶ if ($gen == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($gen == 0) 23 120 143
924 # input
925 23 for (0..$nPop-1){
926 1150 ▶ if ($startPop eq 'nostartPop'){
Branch: 1 branch
BranchTrueFalseTotal
if ($startPop eq 'nostartPop') { } 1150 0 1150
927 1150 $VPt->[$_]=[map { rand(1) * ($bounds->[$_-1][1] - $bounds->[$_-1][0]) + $bounds->[$_-1][0] } 1..$n];
928 }
929 else {
930 0 $VPt = $startPop;
931 }
932 1150 $VQt->[$_]=[map { rand(1) * ($bounds->[$_-1][1] - $bounds->[$_-1][0]) + $bounds->[$_-1][0] } 1..$n];
933 }
934 }
935  
936  
937  
938  
939  
940  
941  
942  
943 143 my $nProc = $par{'nProc'};
944 143 my $filesDir = $par{'filesDir'};
945  
946 # if ($nPop%$nProc != 0){warn "\n nPop should be divisible by nProc!\n\n"};
947  
948 143 my $fork = 0;
949  
950 143 for (1..$nProc){
951  
952 275 my $pid = fork;
953 275 ▶ die $! unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 275 275
954 275 $fork++;
955
956 # say "in PID = $$ with child PID = $pid fork# = $fork";
957 # parent process stop here, child processes go on
958 275 ▶ next if $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pid 253 22 275
959
960 22 my $r = 0;
961
962 22 my $nameFileP = $filesDir.'/Pt_'.$fork.'.txt';
963 22 my $nameFileQ = $filesDir.'/Qt_'.$fork.'.txt';
964
965 # remove existing input file for this process number
966 22 system ('rm -f '.$nameFileP);
967 22 system ('rm -f '.$nameFileQ);
968
969
970 22 my $id_from = ($fork-1)*int($nPop/$nProc);
971 22 my $id_to = $fork *int($nPop/$nProc)-1;
972 22 ▶ if ($fork == $nProc){$id_to = $nPop - 1};
Branch: 1 branch
BranchTrueFalseTotal
if ($fork == $nProc) 11 11 22
973
974 # output
975 22 ▶ open my $fileoP, '>>', $nameFileP or croak "E R R O R : problem in writing the file ".$nameFileP.' -> "filesDir" path not reachable? -- ';
Branch: 1 branch
BranchTrueFalseTotal
open my $fileoP, ">>", $nameFileP or croak('E R R O R : problem in writing the file ' . $nameFileP . ' -> "filesDir" path not reachable? -- ') 0 22 22
976 22 ▶ open my $fileoQ, '>>', $nameFileQ or croak "E R R O R : problem in writing the file ".$nameFileQ.' -> "filesDir" path not reachable? -- ';
Branch: 1 branch
BranchTrueFalseTotal
open my $fileoQ, ">>", $nameFileQ or croak('E R R O R : problem in writing the file ' . $nameFileQ . ' -> "filesDir" path not reachable? -- ') 0 22 22
977 22 for ($id_from .. $id_to){
978 550 my $Pt_ = &{$fun}($VPt->[$_]);
979 550 my $Qt_ = &{$fun}($VQt->[$_]);
980 550 say $fileoP join ',',@{$Pt_};
981 550 say $fileoQ join ',',@{$Qt_};
982 }
983 22 close $fileoP;
984 22 close $fileoQ;
985 22 exit;
986 }
987  
988 # wait for the processes to finish
989 121 my $kid;
990 121 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$kid > 0 and do { do { $kid = waitpid(-1, 0) } } 242 121 363
991 363 $kid = waitpid -1, 0;
992 } while ($kid>0);
993  
994 121 my $Pt;
995 my $Qt;
996
997 # collect data together
998 121 for (1..$nProc){
999
1000 242 my $nameFileP = $filesDir.'/Pt_'.$_.'.txt';
1001 242 my $nameFileQ = $filesDir.'/Qt_'.$_.'.txt';
1002 242 ▶ open my $fileiP, '<', $nameFileP or croak "E R R O R : problem in reading the file ".$nameFileP.' -> "filesDir" path not reachable? ';
Branch: 1 branch
BranchTrueFalseTotal
open my $fileiP, "<", $nameFileP or croak('E R R O R : problem in reading the file ' . $nameFileP . ' -> "filesDir" path not reachable? ') 0 242 242
1003 242 ▶ open my $fileiQ, '<', $nameFileQ or croak "E R R O R : problem in reading the file ".$nameFileQ.' -> "filesDir" path not reachable? ';
Branch: 1 branch
BranchTrueFalseTotal
open my $fileiQ, "<", $nameFileQ or croak('E R R O R : problem in reading the file ' . $nameFileQ . ' -> "filesDir" path not reachable? ') 0 242 242
1004
1005 242 ▶ while (my $line = <$fileiP>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fileiP)) 6050 242 6292
1006 6050 chomp $line;
1007 6050 my @vals = split ',', $line;
1008 6050 push @$Pt, \@vals;
1009 }
1010 242 ▶ while (my $line = <$fileiQ>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fileiQ)) 6050 242 6292
1011 6050 chomp $line;
1012 6050 my @vals = split ',', $line;
1013 6050 push @$Qt, \@vals;
1014 }
1015 242 close $fileiP;
1016 242 close $fileiQ;
1017 }
1018  
1019
1020
1021 # new input
1022 121 my ($VPtp1,$Ptp1,$rank,$Dist)=f_NSGAII($VPt,$VQt,$Pt,$Qt,$f_ineq);
1023  
1024 # save inputs and corresponding outputs at this generation
1025 121 my $to_print;
1026 my $FILEO;
1027 # inputs (genes for each individual)
1028 121 ▶ open $FILEO, '>', $filesDir.'/VPt_gen'.sprintf('%05d',$gen).'.txt' or croak 'E R R O R : problem in writing the generation file -> "filesDir" path not reachable? ';
Branch: 1 branch
BranchTrueFalseTotal
open $FILEO, ">", $filesDir . '/VPt_gen' . sprintf("%05d", $gen) . '.txt' or croak("E R R O R : problem in writing the generation file -> \"filesDir\" path not reachable? ") 0 121 121
1029 121 $to_print = f_print_columns($VPt,'%25.15f');
1030 121 print $FILEO (join "\n", @$to_print);
1031 121 close $FILEO;
1032 # outputs (f1, f2 ... for each individual)
1033 121 ▶ open $FILEO, '>', $filesDir.'/Pt_gen'.sprintf('%05d',$gen).'.txt' or croak 'E R R O R : problem in writing the generation file -> "filesDir" path not reachable? ';
Branch: 1 branch
BranchTrueFalseTotal
open $FILEO, ">", $filesDir . '/Pt_gen' . sprintf("%05d", $gen) . '.txt' or croak("E R R O R : problem in writing the generation file -> \"filesDir\" path not reachable? ") 0 121 121
1034 121 $to_print = f_print_columns($Pt,'%25.15f');
1035 121 print $FILEO (join "\n", @$to_print);
1036 121 close $FILEO;
1037  
1038
1039 #print " example input values: " ;
1040 #say join ' ', @{$VPtp1->[0]};
1041
1042 121 ▶ if (defined $par_plot){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $par_plot) 0 121 121
1043 0 f_plot($par_plot,$gen,$Ptp1,$rank);
1044 0 say '';
1045  
1046 0 my $max = -$inf;
1047 0 my $min = $inf;
1048
1049 0 for (0..$nPop-1){
1050 0 $max = max(@{$Pt->[$_]},@{$Qt->[$_]},$max);
1051 0 $min = min(@{$Pt->[$_]},@{$Qt->[$_]},$min);
1052 }
1053 0 say 'max output = '.$max;
1054 0 say 'min output = '.$min;
1055  
1056  
1057 0 my $maxV = -$inf;
1058 0 my $minV= $inf;
1059
1060 0 my $minD = min(@$Dist);
1061
1062  
1063 0 for (0..$nPop-1){
1064
1065 0 $maxV = max(@{$VPt->[$_]},@{$VQt->[$_]},$maxV);
1066 0 $minV = min(@{$VPt->[$_]},@{$VQt->[$_]},$minV);
1067
1068 }
1069 0 say 'max input = '.$maxV;
1070 0 say 'min input = '.$minV;
1071
1072 0 say 'min Dist = '.$minD;
1073 }
1074  
1075  
1076 121 my ($VQtp1) = f_GA($VPtp1,$rank,$Dist,2.0,$bounds,$distrib,$scaleDistrib,$percentMut);
1077  
1078 # correction of input values produced by GA to respect bounds
1079 121 for my $p(0..$nPop-1){
1080 6050 for my $d(0..$n-1){
1081 18150 ▶ if($VQtp1->[$p][$d] < $bounds->[$d][0]){$VQtp1->[$p][$d] = $bounds->[$d][0]};
Branch: 1 branch
BranchTrueFalseTotal
if ($VQtp1->[$p][$d] < $bounds->[$d][0]) 0 18150 18150
1082 18150 ▶ if($VQtp1->[$p][$d] > $bounds->[$d][1]){$VQtp1->[$p][$d] = $bounds->[$d][1]};
Branch: 1 branch
BranchTrueFalseTotal
if ($VQtp1->[$p][$d] > $bounds->[$d][1]) 21 18129 18150
1083 }
1084 }
1085  
1086 # new output
1087 121 my $Qtp1;
1088 121 for (0..$nPop-1){
1089 6050 $Qtp1->[$_]=&{$fun}($VQtp1->[$_]);
1090 }
1091
1092 # new became old
1093 121 $VPt = [@$VPtp1];
1094 121 $VQt = [@$VQtp1];
1095
1096
1097 # output final
1098 121 ▶ if($gen == $nGen){
Branch: 1 branch
BranchTrueFalseTotal
if ($gen == $nGen) 1 120 121
1099 1 ▶ if ($verboseFinal == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($verboseFinal == 1) 0 1 1
1100 0 say '------------------------- I N P U T -------------------------:';
1101 0 map {say join ' ',@{$_}} @$VPt;
1102 0 say '------------------------- O U T P U T -------------------------';
1103 0 map {say join ' ',@{$_}} @$Pt;
1104 }
1105
1106 1 return [$VPt,$Pt];
1107
1108 }
1109
1110 }
1111
1112 }
1113  
1114  
1115  
1116 sub f_print_columns {
1117 # to print in columns format the content of $P, $Ptp1 ...
1118 242 ▶ my $P = shift;
Subroutine f_print_columns: called CC=3 SCAR=11.0
Pod undocumented
1119 242 my $formato = shift; # e.g. '%25.15f'
1120
1121 # how many input parameters (genes)
1122 242 my $n = scalar @{$P->[0]};
1123 # population number
1124 242 my $nPop = scalar @$P;
1125
1126 # print, each line contains the genes of an individual
1127 242 my @to_print;
1128 242 for my $kl(0..($nPop-1)){
1129 12100 my $line;
1130 12100 for my $kx(0..($n-1)){
1131 30250 $line.= sprintf($formato,$P->[$kl][$kx]) . ' ';
1132 }
1133 12100 push @to_print, $line;
1134 }
1135
1136 242 return \@to_print;
1137 }
1138  
1139 ########################################################################################################
1140  
1141 sub f_plot {
1142 0 ▶ my $par_ref = shift;
Subroutine f_plot: not called CC=8 SCAR=42.8
Pod undocumented
1143
1144 0 my $gen = shift;
1145  
1146 0 my $P = shift;
1147 0 my $rank = shift;
1148
1149
1150
1151
1152 0 my %par = %{$par_ref};
1153
1154 # nfun: what objective to plot from all (x=f1 y=f4 -> [0,3])
1155 0 my $nfun = $par{'nfun'};
1156  
1157
1158 0 my $title = 'GENERATION '.$gen;
1159  
1160 # inizialization of all lines of output
1161 0 my @output = map{ ' ' x $par{'dx'} } 1..$par{'dy'};
1162
1163 0 my @nameFront =(0..9,"a".."z","A".."Z");
1164
1165 # print points of the front
1166 0 my $xASCIImin = 0;
1167 0 my $xASCIImax = $par{'dx'}-1;
1168 0 my $yASCIImin = $par{'dy'}-1;
1169 0 my $yASCIImax = 0;
1170 0 my $n_outliers = 0;
1171
1172 0 for my $kp(1..scalar(@{$P})-1){
1173 # conversion x,y -> xASCII,yASCII
1174 0 my $x = $P->[$kp][$nfun->[0]];
1175 0 my $y = $P->[$kp][$nfun->[1]];
1176 0 my $xmin = $par{'xlim'}->[0];
1177 0 my $xmax = $par{'xlim'}->[1];
1178 0 my $ymin = $par{'ylim'}->[0];
1179 0 my $ymax = $par{'ylim'}->[1];
1180
1181 0 my $xASCII;
1182 my $yASCII;
1183  
1184
1185 0 ▶ if ($x < $xmax && $x > $xmin && $y < $ymax && $y > $ymin && $rank->[$kp] < $#nameFront){
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $xmax and $x > $xmin and $y < $ymax and $y > $ymin and $rank->[$kp] < $#nameFront) { } 0 0 0
Condition0%0/12
$x < $xmax and $x > $xmin and $y < $ymax and $y > $ymin and $rank->[$kp] < $#nameFront
!ll&&!rl&&r
000
$x < $xmax and $x > $xmin and $y < $ymax and $y > $ymin
!ll&&!rl&&r
000
$x < $xmax and $x > $xmin and $y < $ymax
!ll&&!rl&&r
000
$x < $xmax and $x > $xmin
!ll&&!rl&&r
000
MC/DC0%0/5
$x < $xmax and $x > $xmin and $y < $ymax and $y > $ymin and $rank->[$kp] < $#nameFront
$x < $xmax $x > $xmin $y < $ymax $y > $ymin $rank->[$kp] < $#nameFront
Truth table0%0/6
A and B and C and D and E
A= $x < $xmaxB= $x > $xminC= $y < $ymaxD= $y > $yminE= $rank->[$kp] < $#nameFront
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
1186 0 $xASCII = f_interp($xmin,$xmax,$xASCIImin,$xASCIImax,$x);
1187 0 $yASCII = f_interp($ymin,$ymax,$yASCIImin,$yASCIImax,$y);
1188  
1189 0 substr($output[$yASCII],$xASCII,1,$nameFront[$rank->[$kp]]);
1190
1191 }
1192 0 else {$n_outliers++};
1193 }
1194
1195 # add axis
1196 0 push @output, '_' x $par{'dx'};
1197 0 @output = map{'|'.$_} @output;
1198 # add axis labels
1199 0 push @output, sprintf("%$par{'dx'}s",$par{'xlabel'});
1200 0 my @ylabelv = split '',$par{'ylabel'};
1201 0 ▶ @output = map{ defined $ylabelv[$_] ? $ylabelv[$_].$output[$_] : ' '.$output[$_]} 0..$#output;
Branch: 1 branch
BranchTrueFalseTotal
defined $ylabelv[$_] ? : 0 0 0
1202 # add statistics
1203 0 unshift @output, sprintf("%".int(($par{'dx'}+length($title))/2)."s",$title);
1204 0 push @output, "xlim: ".(join ' ',@{$par{'xlim'}});
1205 0 push @output, "ylim: ".(join ' ',@{$par{'ylim'}});
1206 0 push @output, "#outliers: ".$n_outliers;
1207
1208 0 print join "\n", @output;
1209
1210 }
1211  
1212  
1213 sub f_interp{
1214 0 ▶ my ($xmin,$xmax,$xASCIImin,$xASCIImax,$x) = @_;
Subroutine f_interp: not called CC=1 SCAR=6.9
Pod undocumented
1215 0 my $xASCII = ($xASCIImax-$xASCIImin)/($xmax-$xmin)*($x-$xmin)+$xASCIImin;
1216 0 $xASCII = sprintf("%.0f",$xASCII);
1217 0 return $xASCII
1218 }
1219  
1220  
1221  
1222  
1223  
1224  
1225 23 1;
↑ Summary