blib/lib/Optimization/NSGAII.pm

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