blib/script/tide_bitstring.pl untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 80 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 =head1 NAME
4  
5 tide_bitstring.pl - Implementation of the Tide optimization using A::E
6  
7 =head1 SYNOPSIS
8  
9 prompt% ./tide_bitstring.pl <population> <number of generations>
10  
11 or
12  
13 prompt% perl tide_bitstring.pl <population> <number of generations>
14  
15 # Shows the values of the two floating-point components of the
16 # chromosome and finally the best value and fitness reached, which
17 # should be as close to 1 as possible.
18
19  
20 =head1 DESCRIPTION
21  
22 A simple example of how to run an Evolutionary algorithm based on
23 Algorithm::Evolutionary. Tries to find the max of the bidimensional
24 Tide , and outputs the x and y coordinates, along with fitness. Best
25 fitness is close to 1. Around 50 generations should be enough, but
26 default is population and number of generations equal to 100.
27 This one uses a bitstring, as opposed to floating point, representation.
28  
29 =cut
30  
31 use warnings;
32 use strict;
33  
34 use Time::HiRes qw( gettimeofday tv_interval);
35  
36 use lib qw(lib ../lib);
37 use Algorithm::Evolutionary::Individual::BitString;
38 use Algorithm::Evolutionary::Op::Easy;
39 use Algorithm::Evolutionary::Op::Bitflip;
40 use Algorithm::Evolutionary::Op::Crossover;
41 use Algorithm::Evolutionary::Op::Creator;
42  
43 #----------------------------------------------------------#
44 my $popSize = shift || 100; #Population size
45 my $numGens = shift || 100; #Max number of generations
46 my $precision = shift || 20;
47  
48 my $max = 2 << $precision -1;
49 #----------------------------------------------------------#
50 #Fitness function will be Tide
51 my $funcionMarea = sub {
52 my $indi = shift;
53 my $str = $indi->Chrom();
54  
55  
56 #extraemos los dos números reales de la cadena binaria
57 my $l2=length($str)/2;
58 my $x=eval("0b".substr ($str, 0, $l2));
59 $x = $x/$max*2 -1;
60 my $y=eval("0b".substr ($str, $l2));
61 $y = $y/$max*2 -1;
62 my $sqrt = sqrt( $x*$x+$y*$y);
63  
64 if( !$sqrt ){ return 1; }
65 return sin( $sqrt )/$sqrt;
66 };
67  
68 #----------------------------------------------------------#
69 # Variation operators
70 my $m = Algorithm::Evolutionary::Op::Bitflip->new();
71 my $c = Algorithm::Evolutionary::Op::Crossover->new(2);
72  
73  
74 #----------------------------------------------------------#
75 #Usamos estos operadores para definir una generación del algoritmo. Lo cual
76 # no es realmente necesario ya que este algoritmo define ambos operadores por
77 # defecto. Los parámetros son la función de fitness, la tasa de selección y los
78 # operadores de variación.
79 my $generation = Algorithm::Evolutionary::Op::Easy->new( $funcionMarea , 0.2 , [$m, $c] ) ;
80  
81 #Time
82 my $inicioTiempo = [gettimeofday()];
83 #----------------------------------------------------------#
84 #Initial population
85 my @pop;
86 my $creator = new Algorithm::Evolutionary::Op::Creator( 20, 'BitString', { length => $precision*2 } );
87 $creator->apply( \@pop );
88 map( $_->evaluate($funcionMarea), @pop );
89  
90 my $contador=0;
91 do {
92 $generation->apply( \@pop );
93  
94 print "$contador : ", $pop[0]->asString(), "\n" ;
95  
96 $contador++;
97 } while( $contador < $numGens );
98  
99  
100 #----------------------------------------------------------#
101 # Show the best
102 print "El mejor es:\n\t ",$pop[0]->asString()," Fitness: ",$pop[0]->Fitness(),"\n";
103  
104 print "\n\nTime: ". tv_interval( $inicioTiempo ) . "\n";
105  
106 =head1 AUTHOR
107  
108 Contributed by Pedro Castillo Valdivieso, modified by J. J. Merelo
109  
110 =cut
111  
112 =head1 Copyright
113
114 This file is released under the GPL. See the LICENSE file included in this distribution,
115 or go to http://www.fsf.org/licenses/gpl.txt
116  
117 CVS Info: $Date: 2009/07/24 08:46:59 $
118 $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/scripts/tide_bitstring.pl,v 3.0 2009/07/24 08:46:59 jmerelo Exp $
119 $Author: jmerelo $
120 $Revision: 3.0 $
121 $Name $
122  
123 =cut