Algorithm-Evolutionary
view release on metacpan or search on metacpan
lib/Algorithm/Evolutionary/Op/VectorCrossover.pm view on Meta::CPAN
=head2 new( [$number_of_crossing_points = 2], [$priority_rate = 1] )
Creates a new 1 or 2 point crossover operator. But this is just to have a non-empty chromosome
Defaults to 2 point crossover
=cut
sub new {
my $class = shift;
my $hash = { numPoints => shift || 2 };
my $rate = shift || 1;
my $self = Algorithm::Evolutionary::Op::Base::new( 'Algorithm::Evolutionary::Op::VectorCrossover', $rate, $hash );
return $self;
}
=head2 create( [$number_of_crossing_points = 2] )
Creates a new 1 or 2 point crossover operator. But this is just to have a non-empty chromosome
Defaults to 2 point.
=cut
sub create {
my $class = shift;
my $self;
$self->{_numPoints} = shift || 2;
bless $self, $class;
return $self;
}
=head2 apply( $chromosome_1, $chromosome_2 )
Applies xover operator to a "Chromosome", a vector of stuff,
really. Can be applied only to I<victims> with the C<_array> instance
variable; but it checks before application that both operands are of
type L<Algorithm::Evolutionary::Individual::Vector|Algorithm::Evolutionary::Individual::Vector>.
=cut
sub apply ($$;$){
my $self = shift;
my $arg = shift || croak "No victim here!";
my $victim = clone($arg);
my $victim2 = shift || croak "No victim here!";
croak "Incorrect type ".(ref $victim) if !$victim->{'_array'};
croak "Incorrect type ".(ref $victim2) if !$victim2->{'_array'};
if ( (scalar @{$victim->{'_array'}} == 2) || (scalar @{$victim2->{'_array'}} == 2 ) ) {
#Too small, don't pay attention to number of cutting points
my $i = (rand() > 0.5 )? 0:1;
$victim->{'_array'}[$i] = $victim2->{'_array'}[$i];
} else {
my $pt1 = int( rand( @{$victim->{'_array'}} - 1 ) ) ; #in int env; contains $# +1
my $possibleRange = @{$victim->{'_array'}} - $pt1 - 1;
my $range;
if ( $self->{'_numPoints'} > 1 ) {
$range = 1+ int ( rand( $possibleRange ) );
} else {
$range = $possibleRange + 1;
}
#Check length to avoid unwanted lengthening
return $victim if ( ( $pt1+$range >= @{$victim->{'_array'}} ) || ( $pt1+$range >= @{$victim2->{'_array'}} ));
@{$victim->{'_array'}}[$pt1..($pt1+$range)] =
@{$victim2->{'_array'}}[$pt1..($pt1+$range)];
$victim->Fitness( undef ); #It's been changed, so fitness is invalid
}
return $victim;
}
=head1 Copyright
This file is released under the GPL. See the LICENSE file included in this distribution,
or go to http://www.fsf.org/licenses/gpl.txt
CVS Info: $Date: 2012/12/08 10:06:23 $
$Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/VectorCrossover.pm,v 3.1 2012/12/08 10:06:23 jmerelo Exp $
$Author: jmerelo $
$Revision: 3.1 $
$Name $
=cut
"Sad, but true";
( run in 1.462 second using v1.01-cache-2.11-cpan-5a3173703d6 )