File Coverage

lib/Algorithm/Evolutionary/Experiment.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1 1     1   16490 use strict; #-*-CPerl-*- -*- hi-lock -*-
  1         2  
  1         30  
2 1     1   4 use warnings;
  1         1  
  1         34  
3              
4 1     1   3 use lib qw( ../../../lib );
  1         1  
  1         3  
5              
6             =head1 NAME
7              
8             Algorithm::Evolutionary::Experiment - Class for setting up an
9             experiment with algorithms and population
10              
11             =head1 SYNOPSIS
12            
13             use Algorithm::Evolutionary::Experiment;
14             my $popSize = 20;
15             my $indiType = 'BitString';
16             my $indiSize = 64;
17            
18             #Algorithm might be anything of type Op
19             my $ex = new Algorithm::Evolutionary::Experiment $popSize, $indiType, $indiSize, $algorithm;
20              
21              
22             =head1 DESCRIPTION
23              
24             This class contains (as instance variables) an algorithm and a population, and applies one to
25             the other. Can be serialized
26             using XML, and can read an XML description of the experiment.
27              
28             =head1 METHODS
29              
30             =cut
31              
32             package Algorithm::Evolutionary::Experiment;
33              
34 1     1   411 use Algorithm::Evolutionary::Utils qw(parse_xml);
  0            
  0            
35             use Algorithm::Evolutionary qw(Individual::Base
36             Op::Base
37             Op::Creator );
38              
39             our $VERSION = sprintf "3.4";
40              
41             use Carp;
42              
43             =head2 new( $pop_size, $type_of_individual, $individual_size )
44              
45             Creates a new experiment. An C has two parts: the
46             population and the algorithm. The population is created from a set
47             of parameters: popSize, indiType and indiSize, and an array of
48             algorithms that will be applied sequentially. Alternatively, if
49             only operators are passed as an argument, it is understood as an
50             array of algorithms (including, probably, initialization of the
51             population).
52              
53             =cut
54              
55             sub new ($$$$;$) {
56             my $class = shift;
57             my $self = { _pop => [] };
58             if ( index ( ref $_[0], 'Algorithm::Evolutionary') == -1 ) {
59             #If the first arg is not an algorithm, create one
60             my $popSize = shift || carp "Pop size = 0, can't create\n";
61             my $indiType = shift || carp "Empty individual class, can't create\n";
62             my $indiSize = shift || carp "Empty individual size, no reasonable default, can't create\n";
63             for ( my $i = 0; $i < $popSize; $i ++ ) {
64             my $indi = Algorithm::Evolutionary::Individual::Base::new( $indiType,
65             { length => $indiSize } );
66             $indi->randomize();
67             push @{$self->{_pop}}, $indi;
68             }
69             };
70             @_ || croak "Can't find an algorithm";
71             push @{$self->{_algo}}, @_;
72             bless $self, $class;
73             return $self
74            
75             }
76              
77             =head2 go
78              
79             Applies the different operators in the order that they appear; returns the population
80             as a ref-to-array.
81              
82             =cut
83              
84             sub go {
85             my $self = shift;
86             for ( @{$self->{_algo}} ) {
87             $_->apply( $self->{_pop} );
88             }
89             return $self->{_pop}
90             }
91              
92             =head2 SEE ALSO
93              
94             L , another option for setting up
95             experiments, which is the one you should rather use, as XML support is
96             going to be deprecated (some day).
97              
98             =head1 Copyright
99            
100             This file is released under the GPL. See the LICENSE file included in this distribution,
101             or go to http://www.fsf.org/licenses/gpl.txt
102              
103             =cut
104              
105             "What???";