« blib/lib/Algorithm/Evolutionary/Fitness/Rastrigin.pm
↑ Summary
blib/lib/Algorithm/Evolutionary/Op/LinearFreezer.pm »
| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Algorithm::Evolutionary::Op::Creator - Operator that generates groups of individuals, of the intended class | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | my $op = new Algorithm::Evolutionary::Op::Creator; #Creates empty op, with rate | ||
| 11 | |||
| 12 | my $xmlStr=<<EOC; | ||
| 13 | <op name='Creator' type='nullary'> | ||
| 14 | <param name='number' value='20' /> | ||
| 15 | <param name='class' value='BitString' /> | ||
| 16 | <param name='options'> | ||
| 17 | <param name='length' value='320 /> | ||
| 18 | </param> | ||
| 19 | </op> | ||
| 20 | EOC | ||
| 21 | |||
| 22 | my $ref = XMLin($xmlStr); #This step is not really needed; only if it's going to be manipulated by another object | ||
| 23 | my $op = Algorithm::Evolutionary::Op::Base->fromXML( $ref ); #Takes a hash of parsed XML and turns it into an operator | ||
| 24 | |||
| 25 | print $op->asXML(); #print its back in XML shape | ||
| 26 | |||
| 27 | my $op2 = new Algorithm::Evolutionary::Op::Creator( 20, 'String', { chars => [a..j], length => '10' }); | ||
| 28 | |||
| 29 | my @pop; | ||
| 30 | $op2->apply( \@pop ); #Generates population | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | Base class for operators applied to Individuals and Populations and all the rest | ||
| 35 | |||
| 36 | =head1 METHODS | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | package Algorithm::Evolutionary::Op::Creator; | ||
| 41 | |||
| 42 | use lib qw( ../.. ../../.. ); | ||
| 43 | |||
| 44 | use base 'Algorithm::Evolutionary::Individual::Base'; | ||
| 45 | |||
| 46 | use Carp; | ||
| 47 | |||
| 48 | our ($VERSION) = ( '$Revision: 3.1 $ ' =~ / (\d+\.\d+)/ ) ; | ||
| 49 | |||
| 50 | =head2 new( $number_of_individuals, $class_to_generate, $options_hash ) | ||
| 51 | |||
| 52 | Takes a hash with specific parameters for each subclass, creates the | ||
| 53 | object, and leaves subclass-specific assignments to subclasses | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub new { | ||
| 58 | my $class = shift; | ||
| 59 | my $number = shift || croak "What?? No number??\n"; | ||
| 60 | my $classToGenerate = shift || croak "Need a class to generate, man!\n"; | ||
| 61 | my $hash = shift; #No carp here, some operators do not need specific stuff | ||
| 62 | my $self = { _number => $number, | ||
| 63 | _class => $classToGenerate, | ||
| 64 | _hash => $hash }; | ||
| 65 | bless $self, $class; # And bless it | ||
| 66 | return $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | =head2 apply( $population_hash ) | ||
| 70 | |||
| 71 | Generates the population according to the parameters passed in the ctor | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub apply ($) { | ||
| 76 | my $self = shift; | ||
| 77 | my $popRef = shift || croak "Don't have a pop here\n"; | ||
| 78 | |||
| 79 | for ( my $i = 0; $i < $self->{_number}; $i ++ ) { | ||
| 80 | my $indi = Algorithm::Evolutionary::Individual::Base::create( $self->{_class}, $self->{_hash}); | ||
| 81 | push( @{$popRef}, $indi ); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | =head2 asXML() | ||
| 86 | |||
| 87 | Serializes the object as an XML nodeset | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub asXML { | ||
| 92 | my $self = shift; | ||
| 93 | |||
| 94 | my $str=<<EOC; | ||
| 95 | <op name='Creator'> | ||
| 96 | <param name='number' value='$self->{_number}' /> | ||
| 97 | <param name='class' value='$self->{_class}' /> | ||
| 98 | <param name='options'> | ||
| 99 | EOC | ||
| 100 | for ( keys %{$self->{_hash}} ){ | ||
| 101 | $str.="\t\t<param name='$_' value='$self->{_hash}->{$_}' />\n"; | ||
| 102 | } | ||
| 103 | $str.= "\t</param>\n</op>\n"; | ||
| 104 | return $str; | ||
| 105 | } | ||
| 106 | |||
| 107 | =head2 set( $params_hash ) | ||
| 108 | |||
| 109 | Sets the instance variables of the object, which, so far, should be a | ||
| 110 | bit "raw". Usually called from the base class | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub set { | ||
| 115 | my $self = shift; | ||
| 116 | my $hash = shift || croak "No params!"; | ||
| 117 | |||
| 118 | $self->{_number} = $hash->{number}; | ||
| 119 | $self->{_class} = $hash->{class}; | ||
| 120 | $self->{_hash} = {}; | ||
| 121 | for my $k ( keys %{$hash->{options}} ) { | ||
| 122 | $self->{_hash}->{$k} = $hash->{'options'}->{$k}; | ||
| 123 | } | ||
| 124 | |||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | =head1 Copyright | ||
| 129 | |||
| 130 | This file is released under the GPL. See the LICENSE file included in this distribution, | ||
| 131 | or go to http://www.fsf.org/licenses/gpl.txt | ||
| 132 | |||
| 133 | CVS Info: $Date: 2009/09/14 16:36:38 $ | ||
| 134 | $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/Creator.pm,v 3.1 2009/09/14 16:36:38 jmerelo Exp $ | ||
| 135 | $Author: jmerelo $ | ||
| 136 | $Revision: 3.1 $ | ||
| 137 | $Name $ | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | "What???"; |