Evolving/Gene.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 100 CC 2 SCAR 9.0
CC
2
Cov
0%
ann
6.0
new
2.0
make_function
2.0
CRAP 2.5
1 package AI::FANN::Evolving::Gene;
2 use strict;
3 use warnings;
4 use List::Util 'shuffle';
5 use File::Temp 'tempfile';
6 use Scalar::Util 'refaddr';
7 use AI::FANN::Evolving;
8 use Algorithm::Genetic::Diploid::Gene;
9 use base 'Algorithm::Genetic::Diploid::Gene';
10 use Data::Dumper;
11  
12 my $log = __PACKAGE__->logger;
13  
14 =head1 NAME
15  
16 AI::FANN::Evolving::Gene - gene that codes for an artificial neural network (ANN)
17  
18 =head1 METHODS
19  
20 =over
21  
22 =item new
23  
24 Constructor is passed named arguments. Instantiates a trained L<AI::FANN::Evolving> ANN
25  
26 =cut
27  
28 sub new {
29  
30 # initialize self up the inheritance tree
31 my $self = shift->SUPER::new(@_);
32
33 # instantiate and train the FANN object
34 my $traindata = $self->experiment->traindata;
35 $self->ann( AI::FANN::Evolving->new( 'data' => $traindata ) );
36 return $self;
37 }
38  
39 =item ann
40  
41 Getter/setter for an L<AI::FANN::Evolving> ANN
42  
43 =cut
44  
45 sub ann {
46 my $self = shift;
47 if ( @_ ) {
48 my $ann = shift;
49 $log->debug("setting ANN $ann");
50 return $self->{'ann'} = $ann;
51 }
52 else {
53 $log->debug("getting ANN");
54 return $self->{'ann'};
55 }
56 }
57  
58 =item make_function
59  
60 Returns a code reference to the fitness function, which when executed returns a fitness
61 value and writes the corresponding ANN to file
62  
63 =cut
64  
65 sub make_function {
66 my $self = shift;
67 my $ann = $self->ann;
68 my $error_func = $self->experiment->error_func;
69 $log->debug("making fitness function");
70
71 # build the fitness function
72 return sub {
73
74 # train the AI
75 $ann->train( $self->experiment->traindata );
76
77 # isa TrainingData object, this is what we need to use
78 # to make our prognostications. It is a different data
79 # set (out of sample) than the TrainingData object that
80 # the AI was trained on.
81 my $env = shift;
82
83 # this is a number which we try to keep as near to zero
84 # as possible
85 my $fitness = 0;
86
87 # iterate over the list of input/output pairs
88 for my $i ( 0 .. ( $env->length - 1 ) ) {
89 my ( $input, $expected ) = $env->data($i);
90 my $observed = $ann->run($input);
91
92 use Data::Dumper;
93 $log->debug("Observed: ".Dumper($observed));
94 $log->debug("Expected: ".Dumper($expected));
95
96 # invoke the error_func provided by the experiment
97 $fitness += $error_func->($observed,$expected);
98 }
99 $fitness /= $env->length;
100
101 # store result
102 $self->{'fitness'} = $fitness;
103  
104 # store the AI
105 my $outfile = $self->experiment->workdir . "/${fitness}.ann";
106 $self->ann->save($outfile);
107 return $self->{'fitness'};
108 }
109 }
110  
111 =item fitness
112  
113 Stores the fitness value after expressing the fitness function
114  
115 =cut
116  
117 sub fitness { shift->{'fitness'} }
118  
119 =item clone
120  
121 Clones the object
122  
123 =cut
124  
125 sub clone {
126 my $self = shift;
127 my $ann = delete $self->{'ann'};
128 my $ann_clone = $ann->clone;
129 my $self_clone = $self->SUPER::clone;
130 $self_clone->ann( $ann_clone );
131 $self->ann( $ann );
132 return $self_clone;
133 }
134  
135 =item mutate
136  
137 Mutates the ANN by stochastically altering its properties in proportion to
138 the mutation_rate
139  
140 =back
141  
142 =cut
143  
144 sub mutate {
145 my $self = shift;
146
147 # probably 0.05
148 my $mu = $self->experiment->mutation_rate;
149  
150 # make a clone, whose untrained ANN properties are mutated
151 my $self_clone = $self->clone;
152 my $ann = AI::FANN::Evolving->new( 'ann' => $self->ann );
153 $ann->mutate($mu);
154 $self_clone->ann($ann);
155
156 return $self_clone;
157 }
158  
159 1;