« lib/Algorithm/Evolutionary/Op/Replace_Different.pm
↑ Summary
lib/Algorithm/Evolutionary/Op/RouletteWheel.pm »
| 1 | 1 | ▶ | use strict; #-*-cperl-*- |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | 1 | ▶ | use lib qw( ../../.. ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Algorithm::Evolutionary::Op::Replace_Worst - Incorporate individuals into the population replacing the worst ones | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | my $op = new Algorithm::Evolutionary::Op::Replace_Worst; | ||
| 13 | $op->apply( $old_population_hashref, $new_population_hashref ); | ||
| 14 | |||
| 15 | =head1 Base Class | ||
| 16 | |||
| 17 | L<Algorithm::Evolutionary::Op::Base|Algorithm::Evolutionary::Op::Base> | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | Attempts all possible mutations in order, until a "novelty" individual | ||
| 22 | is found. Generated individuals are checked against the population | ||
| 23 | hash, and discarded if they are already in the population. | ||
| 24 | |||
| 25 | =head1 METHODS | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | package Algorithm::Evolutionary::Op::Replace_Worst; | ||
| 30 | |||
| 31 | our ($VERSION) = ( '$Revision: 3.2 $ ' =~ /(\d+\.\d+)/ ); | ||
| 32 | |||
| 33 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 34 | |||
| 35 | 1 | ▶ | use base 'Algorithm::Evolutionary::Op::Base'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 36 | |||
| 37 | #Class-wide constants | ||
| 38 | our $ARITY = 1; | ||
| 39 | |||
| 40 | =head2 new() | ||
| 41 | |||
| 42 | Does nothing, really | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | my $class = shift; | ||
| 48 | my $self = {}; # Create a reference | ||
| 49 | bless $self, $class; # And bless it | ||
| 50 | return $self; | ||
| 51 | } | ||
| 52 | |||
| 53 | =head2 apply( $population, $chromosome_list ) | ||
| 54 | |||
| 55 | Eliminates the worst individuals in the population, replacing them by the list of new chromosomes. The population must be evaluated, but there's no need to have it sorted in advance. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub apply ($;$){ | ||
| 60 | my $self = shift; | ||
| 61 | my $population = shift || croak "No population here!"; | ||
| 62 | my $chromosome_list = shift || croak "No new population here!"; | ||
| 63 | |||
| 64 | #Sort | ||
| 65 | my @sorted_population = sort { $b->Fitness() <=> $a->Fitness(); } | ||
| 66 | @$population ; | ||
| 67 | my $to_eliminate = scalar @$chromosome_list; | ||
| 68 | splice ( @sorted_population, -$to_eliminate ); | ||
| 69 | push @sorted_population, @$chromosome_list; | ||
| 70 | |||
| 71 | return \@sorted_population; | ||
| 72 | |||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | =head1 SEE ALSO | ||
| 77 | |||
| 78 | L<Algorithm::Evolutionary::Op::Generation_Skeleton>, where the | ||
| 79 | replacement policy is one of the parameters | ||
| 80 | |||
| 81 | It can also be used in L<POE::Component::Algorithm::Evolutionary> for | ||
| 82 | insertion of new individuals asynchronously. | ||
| 83 | |||
| 84 | =head1 Copyright | ||
| 85 | |||
| 86 | This file is released under the GPL. See the LICENSE file included in this distribution, | ||
| 87 | or go to http://www.fsf.org/licenses/gpl.txt | ||
| 88 | |||
| 89 | CVS Info: $Date: 2010/12/20 16:56:46 $ | ||
| 90 | $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/Replace_Worst.pm,v 3.2 2010/12/20 16:56:46 jmerelo Exp $ | ||
| 91 | $Author: jmerelo $ | ||
| 92 | $Revision: 3.2 $ | ||
| 93 | $Name $ | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||