| 1 | package AI::FANN::Evolving::Experiment; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use AI::FANN ':all'; | ||
| 5 | use AI::FANN::Evolving; | ||
| 6 | use File::Temp 'tempfile'; | ||
| 7 | use Algorithm::Genetic::Diploid; | ||
| 8 | use base 'Algorithm::Genetic::Diploid::Experiment'; | ||
| 9 | |||
| 10 | my $log = __PACKAGE__->logger; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | AI::FANN::Evolving::Experiment - an experiment in evolving artificial intelligence | ||
| 15 | |||
| 16 | =head1 METHODS | ||
| 17 | |||
| 18 | =over | ||
| 19 | |||
| 20 | =item new | ||
| 21 | |||
| 22 | Constructor takes named arguments, sets default factory to L<AI::FANN::Evolving::Factory> | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | sub new { shift->SUPER::new( 'factory' => AI::FANN::Evolving::Factory->new, @_ ) } | ||
| 27 | |||
| 28 | =item workdir | ||
| 29 | |||
| 30 | Getter/Setter for the workdir where L<AI::FANN> artificial neural networks will be | ||
| 31 | written during the experiment. The files will be named after the ANN's error, which | ||
| 32 | needs to be minimized. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | sub workdir { | ||
| 37 | my $self = shift; | ||
| 38 | if ( @_ ) { | ||
| 39 | my $value = shift; | ||
| 40 | $log->info("assigning new workdir $value"); | ||
| 41 | $self->{'workdir'} = $value; | ||
| 42 | } | ||
| 43 | else { | ||
| 44 | $log->debug("retrieving workdir"); | ||
| 45 | } | ||
| 46 | return $self->{'workdir'}; | ||
| 47 | } | ||
| 48 | |||
| 49 | =item traindata | ||
| 50 | |||
| 51 | Getter/setter for the L<AI::FANN::TrainData> object. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub traindata { | ||
| 56 | my $self = shift; | ||
| 57 | if ( @_ ) { | ||
| 58 | my $value = shift; | ||
| 59 | $log->info("assigning new traindata $value"); | ||
| 60 | $self->{'traindata'} = $value; | ||
| 61 | } | ||
| 62 | else { | ||
| 63 | $log->debug("retrieving traindata"); | ||
| 64 | } | ||
| 65 | return $self->{'traindata'}; | ||
| 66 | } | ||
| 67 | |||
| 68 | =item run | ||
| 69 | |||
| 70 | Runs the experiment! | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub run { | ||
| 75 | my $self = shift; | ||
| 76 | my $log = $self->logger; | ||
| 77 | |||
| 78 | $log->info("going to run experiment"); | ||
| 79 | my @results; | ||
| 80 | for my $i ( 1 .. $self->ngens ) { | ||
| 81 | |||
| 82 | # modify workdir | ||
| 83 | my $wd = $self->{'workdir'}; | ||
| 84 | $wd =~ s/\d+$/$i/; | ||
| 85 | $self->{'workdir'} = $wd; | ||
| 86 | mkdir $wd; | ||
| 87 | |||
| 88 | my $optimum = $self->optimum($i); | ||
| 89 | |||
| 90 | $log->debug("optimum at generation $i is $optimum"); | ||
| 91 | my ( $fittest, $fitness ) = $self->population->turnover($i,$self->env,$optimum); | ||
| 92 | push @results, [ $fittest, $fitness ]; | ||
| 93 | } | ||
| 94 | my ( $fittest, $fitness ) = map { @{ $_ } } sort { $a->[1] <=> $b->[1] } @results; | ||
| 95 | return $fittest, $fitness; | ||
| 96 | } | ||
| 97 | |||
| 98 | =item optimum | ||
| 99 | |||
| 100 | The optimal fitness is zero error in the ANN's classification. This method returns | ||
| 101 | that value: 0. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub optimum { 0 } | ||
| 106 | |||
| 107 | sub _sign { | ||
| 108 | my ( $obs, $exp ) = @_; | ||
| 109 | my $fitness = 0; | ||
| 110 | for my $i ( 0 .. $#{ $obs } ) { | ||
| 111 | $fitness += ( ( $obs->[$i] > 0 ) xor ( $exp->[$i] > 0 ) ); | ||
| 112 | } | ||
| 113 | return $fitness / scalar(@{$obs}); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub _mse { | ||
| 117 | my ( $obs, $exp ) = @_; | ||
| 118 | my $fitness = 0; | ||
| 119 | for my $i ( 0 .. $#{ $obs } ) { | ||
| 120 | $fitness += ( ( (1+$obs->[$i]) - (1+$exp->[$i]) ) ** 2 ); | ||
| 121 | } | ||
| 122 | return $fitness / scalar(@{$obs}); | ||
| 123 | } | ||
| 124 | |||
| 125 | =item error_func | ||
| 126 | |||
| 127 | Returns a function to compute the error. Given an argument, the following can happen: | ||
| 128 | 'sign' => error is the average number of times observed and expected have different signs | ||
| 129 | 'mse' => error is the mean squared difference between observed and expected | ||
| 130 | CODE => error function is the provided code reference | ||
| 131 | |||
| 132 | =back | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub error_func { | ||
| 137 | my $self = shift; | ||
| 138 | |||
| 139 | # process the argument | ||
| 140 | if ( @_ ) { | ||
| 141 | my $arg = shift; | ||
| 142 | if ( ref $arg eq 'CODE' ) { | ||
| 143 | $self->{'error_func'} = $arg; | ||
| 144 | $log->info("using custom error function"); | ||
| 145 | } | ||
| 146 | elsif ( $arg eq 'sign' ) { | ||
| 147 | $self->{'error_func'} = \&_sign; | ||
| 148 | $log->info("using sign test error function"); | ||
| 149 | } | ||
| 150 | elsif ( $arg eq 'mse' ) { | ||
| 151 | $self->{'error_func'} = \&_mse; | ||
| 152 | $log->info("using MSE error function"); | ||
| 153 | } | ||
| 154 | else { | ||
| 155 | $log->warn("don't understand error func '$arg'"); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | # map the constructor-supplied argument | ||
| 160 | if ( $self->{'error_func'} and $self->{'error_func'} eq 'sign' ) { | ||
| 161 | $self->{'error_func'} = \&_sign; | ||
| 162 | $log->info("using error function 'sign'"); | ||
| 163 | } | ||
| 164 | elsif ( $self->{'error_func'} and $self->{'error_func'} eq 'mse' ) { | ||
| 165 | $self->{'error_func'} = \&_mse; | ||
| 166 | $log->info("using error function 'mse'"); | ||
| 167 | } | ||
| 168 | |||
| 169 | return $self->{'error_func'} || \&_mse; | ||
| 170 | } | ||
| 171 | |||
| 172 | 1; |