| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | tide_float.pl - Optimization of the tide function using A::E | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | prompt% ./tide_float.pl <population> <number of generations> | ||
| 10 | |||
| 11 | or | ||
| 12 | |||
| 13 | prompt% perl tide_float.pl <population> <number of generations> | ||
| 14 | |||
| 15 | will show 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 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | use warnings; | ||
| 31 | use strict; | ||
| 32 | |||
| 33 | use Time::HiRes qw( gettimeofday tv_interval); | ||
| 34 | |||
| 35 | use lib qw(lib ../lib); | ||
| 36 | use Algorithm::Evolutionary qw( Individual::Vector Op::Easy | ||
| 37 | Op::GaussianMutation Op::VectorCrossover); | ||
| 38 | |||
| 39 | #----------------------------------------------------------# | ||
| 40 | my $popSize = shift || 100; #Population size | ||
| 41 | my $numGens = shift || 100; #Max number of generations | ||
| 42 | my $dimension = shift || 2; #Vector dimension | ||
| 43 | |||
| 44 | #----------------------------------------------------------# | ||
| 45 | #Fitness function will be Tide | ||
| 46 | my $funcionMarea = sub { | ||
| 47 | my $indi = shift; | ||
| 48 | my ( $x, $y ) = @{$indi->{_array}}; | ||
| 49 | my $sqrt = sqrt( $x*$x+$y*$y); | ||
| 50 | |||
| 51 | if( !$sqrt ){ return 1; } | ||
| 52 | return sin( $sqrt )/$sqrt; | ||
| 53 | }; | ||
| 54 | |||
| 55 | #----------------------------------------------------------# | ||
| 56 | #Initial population | ||
| 57 | my @pop; | ||
| 58 | #Creamos $popSize individuos | ||
| 59 | for ( 0..$popSize ) { | ||
| 60 | my $indi = Algorithm::Evolutionary::Individual::Vector->new( $dimension ); | ||
| 61 | push( @pop, $indi ); | ||
| 62 | } | ||
| 63 | |||
| 64 | #----------------------------------------------------------# | ||
| 65 | # Variation operators | ||
| 66 | my $m = Algorithm::Evolutionary::Op::GaussianMutation->new( 0, 0.1 ); | ||
| 67 | my $c = Algorithm::Evolutionary::Op::VectorCrossover->new( $dimension ); | ||
| 68 | |||
| 69 | #----------------------------------------------------------# | ||
| 70 | #Usamos estos operadores para definir una generación del algoritmo. Lo cual | ||
| 71 | # no es realmente necesario ya que este algoritmo define ambos operadores por | ||
| 72 | # defecto. Los parámetros son la función de fitness, la tasa de selección y los | ||
| 73 | # operadores de variación. | ||
| 74 | my $generation = Algorithm::Evolutionary::Op::Easy->new( $funcionMarea , 0.2 , [$m, $c] ) ; | ||
| 75 | |||
| 76 | #Time | ||
| 77 | my $inicioTiempo = [gettimeofday()]; | ||
| 78 | |||
| 79 | #----------------------------------------------------------# | ||
| 80 | for ( @pop ) { | ||
| 81 | if ( !defined $_->Fitness() ) { | ||
| 82 | my $fitness = $funcionMarea->($_); | ||
| 83 | $_->Fitness( $fitness ); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | my $contador=0; | ||
| 88 | do { | ||
| 89 | $generation->apply( \@pop ); | ||
| 90 | |||
| 91 | print "$contador : ", $pop[0]->asString(), "\n" ; | ||
| 92 | |||
| 93 | $contador++; | ||
| 94 | } while( ( $contador < $numGens ) && ( $pop[0]->Fitness() < 1 ) ); | ||
| 95 | |||
| 96 | |||
| 97 | #----------------------------------------------------------# | ||
| 98 | #leemos el mejor resultado | ||
| 99 | my ( $x, $y ) = @{$pop[0]->{'_array'}}; | ||
| 100 | |||
| 101 | #Mostramos los resultados obtenidos | ||
| 102 | print "El mejor es:\n\t ",$pop[0]->asString(),"\n\t x=$x \n\t y=$y \n\t 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: 2012/12/08 10:12:37 $ | ||
| 118 | $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/scripts/tide_float.pl,v 3.1 2012/12/08 10:12:37 jmerelo Exp $ | ||
| 119 | $Author: jmerelo $ | ||
| 120 | $Revision: 3.1 $ | ||
| 121 | $Name $ | ||
| 122 | |||
| 123 | =cut |