blib/lib/AI/NeuralNet/BackProp.pm

filter: Statementstmt 64.3% 408 / 634 Branchbran 41.5% 79 / 190 Conditioncond 41.0% 23 / 56 MC/DCmcdc 15.1% 5 / 33 Subroutinesub 58.5% 41 / 70 Podpod 75.0% 24 / 32 totaltotal 57.1% 580 / 1015 CC 155 SCAR 28.6
CC
155
Cov
58%
range
110.0
load_pcx
56.0
join_cols
31.3
CRAP 17.5
↑ Summary
1 #!/usr/bin/perl
2
3 # $Id: BackProp.pm,v 0.89 2000/08/12 01:05:27 josiah Exp $
4 #
5 # Copyright (c) 2000 Josiah Bryan USA
6 #
7 # See AUTHOR section in pod text below for usage and distribution rights.
8 # See UPDATES section in pod text below for info on what has changed in this release.
9 #
10
11 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 $AI::NeuralNet::BackProp::VERSION = "0.89";
13 }
14
15 #
16 # name: AI::NeuralNet::BackProp
17 #
18 # author: Josiah Bryan
19 # date: Tuesday August 15 2000
20 # desc: A simple back-propagation, feed-foward neural network with
21 # learning implemented via a generalization of Dobbs rule and
22 # several principals of Hoppfield networks.
23 # online: http://www.josiah.countystart.com/modules/AI/cgi-bin/rec.pl
24 #
25
26 package AI::NeuralNet::BackProp::neuron;
27
28 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29
30 # Dummy constructor
31 12 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
32 12 bless {}, shift
33 }
34
35 # Rounds floats to ints
36 1730 ▶ sub intr {
Subroutine intr: called CC=2 SCAR=7.6
37 1730 ▶ shift if(substr($_[0],0,4) eq 'AI::');
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[0], 0, 4) eq "AI::" 0 1730 1730
38 1730 try { return int(sprintf("%.0f",shift)) }
39 0 catch { return 0 }
40 }
41
42
43 # Receives input from other neurons. They must
44 # be registered as a synapse of this neuron to effectively
45 # input.
46 1038 ▶ sub input {
Subroutine input: called CC=3 SCAR=11.0
47 1038 my $self = shift;
48 1038 my $sid = shift;
49 1038 my $value = shift;
50
51 # We simply weight the value sent by the neuron. The neuron identifies itself to us
52 # using the code we gave it when it registered itself with us. The code is in $sid,
53 # (synapse ID) and we use that to track the weight of the connection.
54 # This line simply multiplies the value by its weight and gets the integer from it.
55 1038 $self->{SYNAPSES}->{LIST}->[$sid]->{VALUE} = intr($value * $self->{SYNAPSES}->{LIST}->[$sid]->{WEIGHT});
56 1038 $self->{SYNAPSES}->{LIST}->[$sid]->{FIRED} = 1;
57 1038 $self->{SYNAPSES}->{LIST}->[$sid]->{INPUT} = $value;
58
59 # Debugger
60 1038 AI::NeuralNet::BackProp::out1("\nRecieved input of $value, weighted to $self->{SYNAPSES}->{LIST}->[$sid]->{VALUE}, synapse weight is $self->{SYNAPSES}->{LIST}->[$sid]->{WEIGHT} (sid is $sid for $self).\n");
61 1038 ▶ AI::NeuralNet::BackProp::out1((($self->input_complete())?"All synapses have fired":"Not all synapses have fired"));
Branch: 1 branch
BranchTrueFalseTotal
$self->input_complete ? : 692 346 1038
62 1038 AI::NeuralNet::BackProp::out1(" for $self.\n");
63
64 # Check and see if all synapses have fired that are connected to this one.
65 # If they have, then generate the output value for this synapse.
66 1038 ▶ $self->output() if($self->input_complete());
Branch: 1 branch
BranchTrueFalseTotal
if $self->input_complete 692 346 1038
67 }
68
69 # Loops thru and outputs to every neuron that this
70 # neuron is registered as synapse of.
71 692 ▶ sub output {
Subroutine output: called CC=3 SCAR=11.0
72 692 my $self = shift;
73 692 ▶ my $size = $self->{OUTPUTS}->{SIZE} || 0;
Condition50%1/2
$self->{'OUTPUTS'}{'SIZE'} || 0
l!l
6920
MC/DC0%0/1
$self->{'OUTPUTS'}{'SIZE'} || 0
$self->{'OUTPUTS'}{'SIZE'}
Truth table50%1/2
A
A= $self->{'OUTPUTS'}{'SIZE'}
Aresult
11
00
74 692 my $value = $self->get_output();
75 692 for (0..$size-1) {
76 1038 AI::NeuralNet::BackProp::out1("Outputing to $self->{OUTPUTS}->{LIST}->[$_]->{PKG}, index $_, a value of $value with ID $self->{OUTPUTS}->{LIST}->[$_]->{ID}.\n");
77 1038 $self->{OUTPUTS}->{LIST}->[$_]->{PKG}->input($self->{OUTPUTS}->{LIST}->[$_]->{ID},$value);
78 }
79 }
80
81 # Used internally by output().
82 692 ▶ sub get_output {
Subroutine get_output: called CC=5 SCAR=16.4
83 692 my $self = shift;
84 692 ▶ my $size = $self->{SYNAPSES}->{SIZE} || 0;
Condition50%1/2
$self->{'SYNAPSES'}{'SIZE'} || 0
l!l
6920
MC/DC0%0/1
$self->{'SYNAPSES'}{'SIZE'} || 0
$self->{'SYNAPSES'}{'SIZE'}
Truth table50%1/2
A
A= $self->{'SYNAPSES'}{'SIZE'}
Aresult
11
00
85 692 my $value = 0;
86 692 my $state = 0;
87 692 my (@map,@weight);
88
89 # We loop through all the syanpses connected to this one and add the weighted
90 # valyes together, saving in a debugging list.
91 692 for (0..$size-1) {
92 1038 $value += $self->{SYNAPSES}->{LIST}->[$_]->{VALUE};
93 1038 $self->{SYNAPSES}->{LIST}->[$_]->{FIRED} = 0;
94
95 1038 $map[$_]=$self->{SYNAPSES}->{LIST}->[$_]->{VALUE};
96 1038 $weight[$_]=$self->{SYNAPSES}->{LIST}->[$_]->{WEIGHT};
97 }
98
99 # Debugger
100 692 ▶ AI::NeuralNet::BackProp::join_cols(\@map,5) if(($AI::NeuralNet::BackProp::DEBUG eq 3) || ($AI::NeuralNet::BackProp::DEBUG eq 2));
Branch: 1 branch
BranchTrueFalseTotal
if $AI::NeuralNet::BackProp::DEBUG eq 3 or $AI::NeuralNet::BackProp::DEBUG eq 2 0 692 692
Condition33%1/3
$AI::NeuralNet::BackProp::DEBUG eq 3 or $AI::NeuralNet::BackProp::DEBUG eq 2
l!l&&r!l&&!r
00692
MC/DC0%0/2
$AI::NeuralNet::BackProp::DEBUG eq 3 or $AI::NeuralNet::BackProp::DEBUG eq 2
$AI::NeuralNet::BackProp::DEBUG eq 3 $AI::NeuralNet::BackProp::DEBUG eq 2
Truth table33%1/3
A or B
A= $AI::NeuralNet::BackProp::DEBUG eq 3B= $AI::NeuralNet::BackProp::DEBUG eq 2
ABresult
1X1
011
000
101 692 AI::NeuralNet::BackProp::out2("Weights: ".join(" ",@weight)."\n");
102
103 # Simply average the values and get the integer of the average.
104 692 $state = intr($value/$size);
105
106 # Debugger
107 692 AI::NeuralNet::BackProp::out1("From get_output, value is $value, so state is $state.\n");
108
109 # Possible future exapnsion for self excitation. Not currently used.
110 692 $self->{LAST_VALUE} = $value;
111
112 # Just return the $state
113 692 return $state;
114 }
115
116 # Used by input() to check if all registered synapses have fired.
117 2076 ▶ sub input_complete {
Subroutine input_complete: called CC=4 SCAR=13.9
118 2076 my $self = shift;
119 2076 ▶ my $size = $self->{SYNAPSES}->{SIZE} || 0;
Condition50%1/2
$self->{'SYNAPSES'}{'SIZE'} || 0
l!l
20760
MC/DC0%0/1
$self->{'SYNAPSES'}{'SIZE'} || 0
$self->{'SYNAPSES'}{'SIZE'}
Truth table50%1/2
A
A= $self->{'SYNAPSES'}{'SIZE'}
Aresult
11
00
120 2076 my $retvalue = 1;
121
122 # Very simple loop. Doesn't need explaning.
123 2076 for (0..$size-1) {
124 3460 ▶ $retvalue = 0 if(!$self->{SYNAPSES}->{LIST}->[$_]->{FIRED});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"SYNAPSES"}{"LIST"}[$_]{"FIRED"} 692 2768 3460
125 }
126 2076 return $retvalue;
127 }
128
129 # Used to recursively adjust the weights of synapse input channeles
130 # to give a desired value. Designed to be called via
131 # AI::NeuralNet::BackProp::NeuralNetwork::learn().
132 984 ▶ sub weight {
Subroutine weight: called CC=4 SCAR=13.9
133 984 my $self = shift;
134 984 my $ammount = shift;
135 984 my $what = shift;
136 984 ▶ my $size = $self->{SYNAPSES}->{SIZE} || 0;
Condition50%1/2
$self->{'SYNAPSES'}{'SIZE'} || 0
l!l
9840
MC/DC0%0/1
$self->{'SYNAPSES'}{'SIZE'} || 0
$self->{'SYNAPSES'}{'SIZE'}
Truth table50%1/2
A
A= $self->{'SYNAPSES'}{'SIZE'}
Aresult
11
00
137 984 my $value;
138 984 AI::NeuralNet::BackProp::out1("Weight: ammount is $ammount, what is $what with size at $size.\n");
139
140 # Now this sub is the main cog in the learning wheel. It is called recursively on
141 # each neuron that has been bad (given incorrect output.)
142 984 for my $i (0..$size-1) {
143 1312 $value = $self->{SYNAPSES}->{LIST}->[$i]->{VALUE};
144
145 1312 if(0) {
146
147 # Formula by Steve Purkis
148 # Converges very fast for low-value inputs. Has trouble converging on high-value
149 # inputs. Feel free to play and try to get to work for high values.
150 my $delta = $ammount * ($what - $value) * $self->{SYNAPSES}->{LIST}->[$i]->{INPUT};
151 $self->{SYNAPSES}->{LIST}->[$i]->{WEIGHT} += $delta;
152 $self->{SYNAPSES}->{LIST}->[$i]->{PKG}->weight($ammount,$what);
153 }
154
155 # This formula in use by default is original by me (Josiah Bryan) as far as I know.
156
157 # If it is equal, then don't adjust
158 #
159 ### Disabled because this soemtimes causes
160 ### infinte loops when learning with range limits enabled
161 #
162 #next if($value eq $what);
163
164 # Adjust increment by the weight of the synapse of
165 # this neuron & apply direction delta
166 my $delta =
167 $ammount *
168 ($value<$what?1:-1) *
169 1312 ▶ $self->{SYNAPSES}->{LIST}->[$i]->{WEIGHT};
Branch: 1 branch
BranchTrueFalseTotal
$value < $what ? : 700 612 1312
170
171 #print "($value,$what) delta:$delta\n";
172
173 # Recursivly apply
174 1312 $self->{SYNAPSES}->{LIST}->[$i]->{WEIGHT} += $delta;
175 1312 $self->{SYNAPSES}->{LIST}->[$i]->{PKG}->weight($ammount,$what);
176
177 }
178 }
179
180 # Registers some neuron as a synapse of this neuron.
181 # This is called exclusively by connect(), except for
182 # in initalize_group() to connect the _map() package.
183 18 ▶ sub register_synapse {
Subroutine register_synapse: called CC=3 SCAR=11.0
184 18 my $self = shift;
185 18 my $synapse = shift;
186 18 ▶ my $sid = $self->{SYNAPSES}->{SIZE} || 0;
Condition100%2/2
$self->{'SYNAPSES'}{'SIZE'} || 0
l!l
612
MC/DC100%1/1
$self->{'SYNAPSES'}{'SIZE'} || 0
$self->{'SYNAPSES'}{'SIZE'}
Truth table100%2/2
A
A= $self->{'SYNAPSES'}{'SIZE'}
Aresult
11
00
187 18 $self->{SYNAPSES}->{LIST}->[$sid]->{PKG} = $synapse;
188 18 ▶ $self->{SYNAPSES}->{LIST}->[$sid]->{WEIGHT} = 1.00 if(!$self->{SYNAPSES}->{LIST}->[$sid]->{WEIGHT});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"SYNAPSES"}{"LIST"}[$sid]{"WEIGHT"} 18 0 18
189 18 $self->{SYNAPSES}->{LIST}->[$sid]->{FIRED} = 0;
190 18 AI::NeuralNet::BackProp::out1("$self: Registering sid $sid with weight $self->{SYNAPSES}->{LIST}->[$sid]->{WEIGHT}, package $self->{SYNAPSES}->{LIST}->[$sid]->{PKG}.\n");
191 18 $self->{SYNAPSES}->{SIZE} = ++$sid;
192 18 return ($sid-1);
193 }
194
195 # Called via AI::NeuralNet::BackProp::NeuralNetwork::initialize_group() to
196 # form the neuron grids.
197 # This just registers another synapes as a synapse to output to from this one, and
198 # then we ask that synapse to let us register as an input connection and we
199 # save the sid that the ouput synapse returns.
200 18 ▶ sub connect {
Subroutine connect: called CC=2 SCAR=6.9
201 18 my $self = shift;
202 18 my $to = shift;
203 18 ▶ my $oid = $self->{OUTPUTS}->{SIZE} || 0;
Condition100%2/2
$self->{'OUTPUTS'}{'SIZE'} || 0
l!l
612
MC/DC100%1/1
$self->{'OUTPUTS'}{'SIZE'} || 0
$self->{'OUTPUTS'}{'SIZE'}
Truth table100%2/2
A
A= $self->{'OUTPUTS'}{'SIZE'}
Aresult
11
00
204 18 AI::NeuralNet::BackProp::out1("Connecting $self to $to at $oid...\n");
205 18 $self->{OUTPUTS}->{LIST}->[$oid]->{PKG} = $to;
206 18 $self->{OUTPUTS}->{LIST}->[$oid]->{ID} = $to->register_synapse($self);
207 18 $self->{OUTPUTS}->{SIZE} = ++$oid;
208 18 return $self->{OUTPUTS}->{LIST}->[$oid]->{ID};
209 }
210 1 1;
211
212 package AI::NeuralNet::BackProp;
213
214 1 ▶ use Benchmark;
Subroutine BEGIN: called CC=1 SCAR=0.0
215 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
216
217 # Returns the number of elements in an array ref, undef on error
218 0 ▶ sub _FETCHSIZE {
Subroutine _FETCHSIZE: not called CC=3 SCAR=24.8
219 0 my $a=$_[0];
220 0 my ($b,$x);
221 0 ▶ return undef if(substr($a,0,5) ne "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if substr($a, 0, 5) ne "ARRAY" 0 0 0
222 0 foreach $b (@{$a}) { $x++ };
223 0 return $x;
224 }
225
226 # Debugging subs
227 1 $AI::NeuralNet::BackProp::DEBUG = 0;
228 0 ▶ sub whowasi { (caller(1))[3] . '()' }
Subroutine whowasi: not called CC=1 SCAR=6.9
Pod undocumented
229 0 ▶ sub debug { shift; $AI::NeuralNet::BackProp::DEBUG = shift || 0; }
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
230 7925 ▶ sub out1 { print shift() if ($AI::NeuralNet::BackProp::DEBUG eq 1) }
Subroutine out1: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $AI::NeuralNet::BackProp::DEBUG eq 1 0 7925 7925
231 714 ▶ sub out2 { print shift() if (($AI::NeuralNet::BackProp::DEBUG eq 1) || ($AI::NeuralNet::BackProp::DEBUG eq 2)) }
Subroutine out2: called CC=3 SCAR=14.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $AI::NeuralNet::BackProp::DEBUG eq 1 or $AI::NeuralNet::BackProp::DEBUG eq 2 0 714 714
Condition33%1/3
$AI::NeuralNet::BackProp::DEBUG eq 1 or $AI::NeuralNet::BackProp::DEBUG eq 2
l!l&&r!l&&!r
00714
MC/DC0%0/2
$AI::NeuralNet::BackProp::DEBUG eq 1 or $AI::NeuralNet::BackProp::DEBUG eq 2
$AI::NeuralNet::BackProp::DEBUG eq 1 $AI::NeuralNet::BackProp::DEBUG eq 2
Truth table33%1/3
A or B
A= $AI::NeuralNet::BackProp::DEBUG eq 1B= $AI::NeuralNet::BackProp::DEBUG eq 2
ABresult
1X1
011
000
232 0 ▶ sub out3 { print shift() if ($AI::NeuralNet::BackProp::DEBUG) }
Subroutine out3: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $AI::NeuralNet::BackProp::DEBUG 0 0 0
233 680 ▶ sub out4 { print shift() if ($AI::NeuralNet::BackProp::DEBUG eq 4) }
Subroutine out4: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $AI::NeuralNet::BackProp::DEBUG eq 4 0 680 680
234
235 # Rounds a floating-point to an integer with int() and sprintf()
236 708 ▶ sub intr {
Subroutine intr: called CC=2 SCAR=7.0
Pod documented
237 708 ▶ shift if(substr($_[0],0,4) eq 'AI::');
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[0], 0, 4) eq "AI::" 701 7 708
238 708 try { return int(sprintf("%.0f",shift)) }
239 0 catch { return 0 }
240 }
241
242 # Used to format array ref into columns
243 # Usage:
244 # join_cols(\@array,$row_length_in_elements,$high_state_character,$low_state_character);
245 # Can also be called as method of your neural net.
246 # If $high_state_character is null, prints actual numerical values of each element.
247 0 ▶ sub join_cols {
Subroutine join_cols: not called CC=6 SCAR=34.4
Pod documented
248 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
249 0 ▶ shift if(substr($_[0],0,4) eq 'AI::');
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[0], 0, 4) eq "AI::" 0 0 0
250 0 my $map = shift;
251 0 my $break = shift;
252 0 my $a = shift;
253 0 my $b = shift;
254 0 my $x;
255 0 foreach my $el (@{$map}) {
256 0 ▶ my $str = ((int($el))?$a:$b);
Branch: 1 branch
BranchTrueFalseTotal
int $el ? : 0 0 0
257 0 ▶ $str=$el."\0" if(!$a);
Branch: 1 branch
BranchTrueFalseTotal
unless $a 0 0 0
258 0 print $str;
259 0 $x++;
260 0 ▶ if($x>$break-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > $break - 1) 0 0 0
261 0 print "\n";
262 0 $x=0;
263 }
264 }
265 0 print "\n";
266 }
267
268 # Returns percentage difference between all elements of two
269 # array refs of exact same length (in elements).
270 # Now calculates actual difference in numerical value.
271 171 ▶ sub pdiff {
Subroutine pdiff: called CC=7 SCAR=19.5
Pod documented
272 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
273 171 ▶ shift if(substr($_[0],0,4) eq 'AI::');
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[0], 0, 4) eq "AI::" 0 171 171
274 171 my $a1 = shift;
275 171 my $a2 = shift;
276 171 my $a1s = $#{$a1}; #AI::NeuralNet::BackProp::_FETCHSIZE($a1);
277 171 my $a2s = $#{$a2}; #AI::NeuralNet::BackProp::_FETCHSIZE($a2);
278 171 my ($a,$b,$diff,$t);
279 171 $diff=0;
280 #return undef if($a1s ne $a2s); # must be same length
281 171 for my $x (0..$a1s) {
282 171 $a = $a1->[$x];
283 171 $b = $a2->[$x];
284 171 ▶ if($a!=$b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a != $b) 164 7 171
285 164 ▶ if($a<$b){$t=$a;$a=$b;$b=$t;}
Branch: 1 branch
BranchTrueFalseTotal
if ($a < $b) 87 77 164
286 164 ▶ $a=1 if(!$a);
Branch: 1 branch
BranchTrueFalseTotal
unless $a 0 164 164
287 164 $diff+=(($a-$b)/$a)*100;
288 }
289 }
290 171 ▶ $a1s = 1 if(!$a1s);
Branch: 1 branch
BranchTrueFalseTotal
unless $a1s 171 0 171
291 171 return sprintf("%.10f",($diff/$a1s));
292 }
293
294 # Returns $fa as a percentage of $fb
295 0 ▶ sub p {
Subroutine p: not called CC=3 SCAR=24.8
Pod documented
296 0 ▶ shift if(substr($_[0],0,4) eq 'AI::');
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[0], 0, 4) eq "AI::" 0 0 0
297 0 my ($fa,$fb)=(shift,shift);
298 0 ▶ sprintf("%.3f",((($fb-$fa)*((($fb-$fa)<0)?-1:1))/$fa)*100);
Branch: 1 branch
BranchTrueFalseTotal
$fb - $fa < 0 ? : 0 0 0
299 }
300
301 # This sub will take an array ref of a data set, which it expects in this format:
302 # my @data_set = ( [ ...inputs... ], [ ...outputs ... ],
303 # ... rows ...
304 # );
305 #
306 # This wil sub returns the percentage of 'forgetfullness' when the net learns all the
307 # data in the set in order. Usage:
308 #
309 # learn_set(\@data,[ options ]);
310 #
311 # Options are options in hash form. They can be of any form that $net->learn takes.
312 #
313 # It returns a percentage string.
314 #
315 1 ▶ sub learn_set {
Subroutine learn_set: called CC=9 SCAR=23.0
Pod documented
316 1 ▶ my $self = shift if(substr($_[0],0,4) eq 'AI::');
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[0], 0, 4) eq "AI::" 1 0 1
317 1 my $data = shift;
318 1 my %args = @_;
319 1 my $len = $#{$data}/2-1;
320 1 my $inc = $args{inc};
321 1 my $max = $args{max};
322 1 my $error = $args{error};
323 1 ▶ my $p = (defined $args{flag}) ?$args{flag} :1;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'flag'} ? : 0 1 1
324 1 ▶ my $row = (defined $args{pattern})?$args{pattern}*2+1:1;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'pattern'} ? : 0 1 1
325 1 my ($fa,$fb);
326 1 for my $x (0..$len) {
327 6 ▶ print "\nLearning index $x...\n" if($AI::NeuralNet::BackProp::DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $AI::NeuralNet::BackProp::DEBUG 0 6 6
328 6 my $str = $self->learn( $data->[$x*2], # The list of data to input to the net
329 $data->[$x*2+1], # The output desired
330 inc=>$inc, # The starting learning gradient
331 max=>$max, # The maximum num of loops allowed
332 error=>$error); # The maximum (%) error allowed
333 6 ▶ print $str if($AI::NeuralNet::BackProp::DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $AI::NeuralNet::BackProp::DEBUG 0 6 6
334 }
335
336
337 1 my $res;
338 1 ▶ $data->[$row] = $self->crunch($data->[$row]) if($data->[$row] == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $data->[$row] == 0 0 1 1
339
340 1 ▶ if ($p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) { } 1 0 1
341 1 $res=pdiff($data->[$row],$self->run($data->[$row-1]));
342 } else {
343 0 $res=$data->[$row]->[0]-$self->run($data->[$row-1])->[0];
344 }
345 1 return $res;
346 }
347
348 # This sub will take an array ref of a data set, which it expects in this format:
349 # my @data_set = ( [ ...inputs... ], [ ...outputs ... ],
350 # ... rows ...
351 # );
352 #
353 # This wil sub returns the percentage of 'forgetfullness' when the net learns all the
354 # data in the set in RANDOM order. Usage:
355 #
356 # learn_set_rand(\@data,[ options ]);
357 #
358 # Options are options in hash form. They can be of any form that $net->learn takes.
359 #
360 # It returns a true value.
361 #
362 0 ▶ sub learn_set_rand {
Subroutine learn_set_rand: not called CC=5 SCAR=34.0
Pod documented
363 0 ▶ my $self = shift if(substr($_[0],0,4) eq 'AI::');
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[0], 0, 4) eq "AI::" 0 0 0
364 0 my $data = shift;
365 0 my %args = @_;
366 0 my $len = $#{$data}/2-1;
367 0 my $inc = $args{inc};
368 0 my $max = $args{max};
369 0 my $error = $args{error};
370 0 my @learned;
371 0 while(1) {
372 0 _GET_X:
373 my $x=$self->intr(rand()*$len);
374 0 ▶ goto _GET_X if($learned[$x]);
Branch: 1 branch
BranchTrueFalseTotal
if $learned[$x] 0 0 0
375 0 $learned[$x]=1;
376 0 ▶ print "\nLearning index $x...\n" if($AI::NeuralNet::BackProp::DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $AI::NeuralNet::BackProp::DEBUG 0 0 0
377 0 my $str = $self->learn($data->[$x*2], # The list of data to input to the net
378 $data->[$x*2+1], # The output desired
379 inc=>$inc, # The starting learning gradient
380 max=>$max, # The maximum num of loops allowed
381 error=>$error); # The maximum (%) error allowed
382 0 ▶ print $str if($AI::NeuralNet::BackProp::DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $AI::NeuralNet::BackProp::DEBUG 0 0 0
383 }
384
385
386 0 return 1;
387 }
388
389 # Returns the index of the element in array REF passed with the highest comparative value
390 0 ▶ sub high {
Subroutine high: not called CC=5 SCAR=34.0
Pod documented
391 0 ▶ shift if(substr($_[0],0,4) eq 'AI::');
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[0], 0, 4) eq "AI::" 0 0 0
392 0 my $ref1 = shift;
393
394 0 my ($el,$len,$tmp);
395 0 foreach $el (@{$ref1}) {
396 0 $len++;
397 }
398 0 $tmp=0;
399 0 for my $x (0..$len-1) {
400 0 ▶ $tmp = $x if((@{$ref1})[$x] > (@{$ref1})[$tmp]);
Branch: 1 branch
BranchTrueFalseTotal
if (@{$ref1;})[$x] > (@{$ref1;})[$tmp] 0 0 0
401 }
402 0 return $tmp;
403 }
404
405 # Returns the index of the element in array REF passed with the lowest comparative value
406 0 ▶ sub low {
Subroutine low: not called CC=5 SCAR=34.0
Pod documented
407 0 ▶ shift if(substr($_[0],0,4) eq 'AI::');
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[0], 0, 4) eq "AI::" 0 0 0
408 0 my $ref1 = shift;
409
410 0 my ($el,$len,$tmp);
411 0 foreach $el (@{$ref1}) {
412 0 $len++;
413 }
414 0 $tmp=0;
415 0 for my $x (0..$len-1) {
416 0 ▶ $tmp = $x if((@{$ref1})[$x] < (@{$ref1})[$tmp]);
Branch: 1 branch
BranchTrueFalseTotal
if (@{$ref1;})[$x] < (@{$ref1;})[$tmp] 0 0 0
417 }
418 0 return $tmp;
419 }
420
421 # Returns a pcx object
422 0 ▶ sub load_pcx {
Subroutine load_pcx: not called CC=1 SCAR=6.9
Pod documented
423 0 my $self = shift;
424 0 return AI::NeuralNet::BackProp::PCX->new($self,shift);
425 }
426
427 # Crunch a string of words into a map
428 0 ▶ sub crunch {
Subroutine crunch: not called CC=3 SCAR=24.8
Pod documented
429 0 my $self = shift;
430 0 my (@map,$ic);
431 0 my @ws = split(/[\s\t]/,shift);
432 0 for my $a (0..$#ws) {
433 0 $ic=$self->crunched($ws[$a]);
434 0 ▶ if(!defined $ic) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ic) { } 0 0 0
435 0 $self->{_CRUNCHED}->{LIST}->[$self->{_CRUNCHED}->{_LENGTH}++]=$ws[$a];
436 0 @map[$a]=$self->{_CRUNCHED}->{_LENGTH};
437 } else {
438 0 @map[$a]=$ic;
439 }
440 }
441 0 return \@map;
442 }
443
444 # Finds if a word has been crunched.
445 # Returns undef on failure, word index for success.
446 0 ▶ sub crunched {
Subroutine crunched: not called CC=3 SCAR=24.8
Pod documented
447 0 my $self = shift;
448 0 for my $a (0..$self->{_CRUNCHED}->{_LENGTH}-1) {
449 0 ▶ return $a+1 if($self->{_CRUNCHED}->{LIST}->[$a] eq $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_CRUNCHED"}{"LIST"}[$a] eq $_[0] 0 0 0
450 }
451 0 return undef;
452 }
453
454 # Alias for crunched(), above
455 0 ▶ sub word { crunched(@_) }
Subroutine word: not called CC=1 SCAR=6.9
Pod undocumented
456
457 # Uncrunches a map (array ref) into an array of words (not an array ref) and returns array
458 0 ▶ sub uncrunch {
Subroutine uncrunch: not called CC=2 SCAR=17.9
Pod documented
459 0 my $self = shift;
460 0 my $map = shift;
461 0 my ($c,$el,$x);
462 0 foreach $el (@{$map}) {
463 0 $c .= $self->{_CRUNCHED}->{LIST}->[$el-1].' ';
464 }
465 0 return $c;
466 }
467
468 # Sets/gets randomness facter in the network. Setting a value of 0 disables random factors.
469 0 ▶ sub random {
Subroutine random: not called CC=2 SCAR=17.9
Pod documented
470 0 my $self = shift;
471 0 my $rand = shift;
472 0 ▶ return $self->{random} if(!(defined $rand));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rand 0 0 0
473 0 $self->{random} = $rand;
474 }
475
476 # Sets/gets column width for printing lists in debug modes 1,3, and 4.
477 0 ▶ sub col_width {
Subroutine col_width: not called CC=2 SCAR=17.9
Pod documented
478 0 my $self = shift;
479 0 my $width = shift;
480 0 ▶ return $self->{col_width} if(!$width);
Branch: 1 branch
BranchTrueFalseTotal
unless $width 0 0 0
481 0 $self->{col_width} = $width;
482 }
483
484 # Sets/Removes value ranging
485 0 ▶ sub range {
Subroutine range: not called CC=10 SCAR=47.0
Pod documented
486 0 my $self = shift;
487 0 my $ref = shift;
488 0 my $b = shift;
489 0 ▶ if(substr($ref,0,5) ne "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($ref, 0, 5) ne "ARRAY") 0 0 0
490 0 ▶ if(($ref == 0) && (!defined $b)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref == 0 and not defined $b) { } 0 0 0
Condition0%0/3
$ref == 0 and not defined $b
!ll&&!rl&&r
000
MC/DC0%0/2
$ref == 0 and not defined $b
$ref == 0 not defined $b
Truth table0%0/3
A and not(B)
A= $ref == 0B= not defined $b
ABresult
0X0
100
111
491 0 $ref = $self->crunch($ref);
492 #print "\$ref is a string, crunching to ",join(',',@{$ref}),"\n";
493 } else {
494 0 my $a = $ref;
495 0 ▶ $a = $self->crunch($a)->[0] if($a == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $a == 0 0 0 0
496 0 ▶ $b = $self->crunch($b)->[0] if($b == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $b == 0 0 0 0
497 0 $_[++$#_] = $a;
498 0 $_[++$#_] = $b;
499 0 $ref = \@_;
500 #print "Found ranged definition, joined to ",join(',',@{$ref}),"\n";
501 }
502 }
503 0 my $rA = 0;
504 0 my $rB = $#{$ref};
505 0 my $rS = 0; #shift;
506 0 ▶ if(!$rA && !$rB) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rA or $rB) 0 0 0
Condition0%0/3
$rA or $rB
l!l&&r!l&&!r
000
MC/DC0%0/2
$rA or $rB
$rA $rB
Truth table0%0/3
A or B
A= $rAB= $rB
ABresult
1X1
011
000
507 0 $self->{rA}=$self->{rB}=-1;
508 0 return undef;
509 }
510 0 ▶ if($rB<$rA){my $t=$rA;$rA=$rB;$rB=$t};
Branch: 1 branch
BranchTrueFalseTotal
if ($rB < $rA) 0 0 0
511 0 $self->{rA}=$rA;
512 0 $self->{rB}=$rB;
513 0 ▶ $self->{rS}=$rS if($rS);
Branch: 1 branch
BranchTrueFalseTotal
if $rS 0 0 0
514 0 $self->{rRef} = $ref;
515 0 return $ref;
516 }
517
518 # Used internally to scale outputs to fit range
519 173 ▶ sub _range {
Subroutine _range: called CC=7 SCAR=30.3
520 173 my $self = shift;
521 173 my $in = shift;
522 173 my $rA = $self->{rA};
523 173 my $rB = $self->{rB};
524 173 my $rS = $self->{rS};
525 173 my $r = $rB;#-$rA+1;
526 173 ▶ return $in if(!$rA && !$rB);
Branch: 1 branch
BranchTrueFalseTotal
unless $rA or $rB 173 0 173
Condition33%1/3
$rA or $rB
l!l&&r!l&&!r
00173
MC/DC0%0/2
$rA or $rB
$rA $rB
Truth table33%1/3
A or B
A= $rAB= $rB
ABresult
1X1
011
000
527 0 my $l = $self->{OUT}-1;
528 0 my $out = [];
529 # Adjust for a maximum outside what we have seen so far
530 0 for my $i (0..$l) {
531 0 ▶ $rS=$in->[$i] if($in->[$i]>$rS);
Branch: 1 branch
BranchTrueFalseTotal
if $in->[$i] > $rS 0 0 0
532 }
533 #print "\$l:$l,\$rA:$rA,\$rB:$rB,\$rS:$rS,\$r:$r\n";
534 # Loop through, convert values to percentage of maximum, then multiply
535 # percentage by range and add to base of range to get finaly value
536 0 for my $i (0..$l) {
537 #print "\$i:$i,\$in:$in->[$i]\n";
538 0 ▶ $rS=1 if(!$rS);
Branch: 1 branch
BranchTrueFalseTotal
unless $rS 0 0 0
539 0 my $t=intr((($rS-$in->[$i])/$rS)*$r+$rA);
540 #print "t:$t,$self->{rRef}->[$t],i:$i\n";
541 0 $out->[$i] = $self->{rRef}->[$t];
542 }
543 0 $self->{rS}=$rS;
544 0 return $out;
545 }
546
547
548 # Initialzes the base for a new neural network.
549 # It is recomended that you call learn() before run()ing a pattern.
550 # See documentation above for usage.
551 3 ▶ sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
552 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
553 3 my $type = shift;
554 3 my $self = {};
555 3 my $layers = shift;
556 3 my $size = shift;
557 3 ▶ my $out = shift || $size;
Condition66%2/3
shift() || $size
l!l&&r!l&&!r
201
MC/DC50%1/2
shift() || $size
shift() $size
Truth table66%2/3
A || B
A= shift()B= $size
ABresult
1X1
011
000
558 3 ▶ my $flag = shift || 0;
Condition50%1/2
shift() || 0
l!l
03
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
559
560 3 bless $self, $type;
561
562 # If $layers is a string, then it will be nummerically equal to 0, so try to load it
563 # as a network file.
564 3 ▶ if($layers == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layers == 0) 1 2 3
565 # We use a "1" flag as the second argument to indicate that we want load()
566 # to call the new constructor to make a network the same size as in the file
567 # and return a refrence to the network, instead of just creating the network from
568 # pre-exisiting refrence
569 1 return $self->load($layers,1);
570 }
571
572
573 #print "Creating $size neurons in each layer for $layers layer(s)...\n";
574
575 2 AI::NeuralNet::BackProp::out2 "Creating $size neurons in each layer for $layers layer(s)...\n";
576
577 # Error checking
578 2 ▶ return undef if($out>$size);
Branch: 1 branch
BranchTrueFalseTotal
if $out > $size 0 2 2
579
580 # When this is called, they tell us howmany layers and neurons in each layer.
581 # But really what we store is a long line of neurons that are only divided in theory
582 # when connecting the outputs and inputs.
583 2 my $div = $size;
584 2 my $size = $layers * $size;
585
586 2 AI::NeuralNet::BackProp::out2 "Creating RUN and MAP systems for network...\n";
587 #print "Creating RUN and MAP systems for network...\n";
588
589 # Create a new runner and mapper for the network.
590 2 $self->{RUN} = new AI::NeuralNet::BackProp::_run($self);
591 2 $self->{MAP} = new AI::NeuralNet::BackProp::_map($self);
592
593 2 $self->{SIZE} = $size;
594 2 $self->{DIV} = $div;
595 2 $self->{OUT} = $out;
596 2 $self->{FLAG} = $flag;
597 2 $self->{col_width}= 5;
598 2 $self->{random} = 0.001;
599
600 2 $self->initialize_group();
601
602 2 return $self;
603 }
604
605 # Save entire network state to disk.
606 2 ▶ sub save {
Subroutine save: called CC=5 SCAR=16.1
Pod documented
607 2 my $self = shift;
608 2 my $file = shift;
609 2 my $size = $self->{SIZE};
610 2 my $div = $self->{DIV};
611 2 my $out = $self->{OUT};
612 2 my $flag = $self->{FLAG};
613
614 2 open(FILE,">$file");
615
616 2 print FILE "size=$size\n";
617 2 print FILE "div=$div\n";
618 2 print FILE "out=$out\n";
619 2 print FILE "flag=$flag\n";
620 2 print FILE "rand=$self->{random}\n";
621 2 print FILE "cw=$self->{col_width}\n";
622 2 print FILE "crunch=$self->{_CRUNCHED}->{_LENGTH}\n";
623 2 print FILE "rA=$self->{rA}\n";
624 2 print FILE "rB=$self->{rB}\n";
625 2 print FILE "rS=$self->{rS}\n";
626 2 ▶ print FILE "rRef=",(($self->{rRef})?join(',',@{$self->{rRef}}):''),"\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'rRef'} ? : 1 1 2
627
628 2 for my $a (0..$self->{_CRUNCHED}->{_LENGTH}-1) {
629 0 print FILE "c$a=$self->{_CRUNCHED}->{LIST}->[$a]\n";
630 }
631
632 2 my $w;
633 2 for my $a (0..$self->{SIZE}-1) {
634 8 $w="";
635 8 for my $b (0..$self->{DIV}-1) {
636 16 $w .= "$self->{NET}->[$a]->{SYNAPSES}->{LIST}->[$b]->{WEIGHT},";
637 }
638 8 chop($w);
639 8 print FILE "n$a=$w\n";
640 }
641
642 2 close(FILE);
643
644 2 return $self;
645 }
646
647 # Load entire network state from disk.
648 1 ▶ sub load {
Subroutine load: called CC=9 SCAR=22.6
Pod documented
649 1 my $self = shift;
650 1 my $file = shift;
651 1 ▶ my $load_flag = shift || 0;
Condition50%1/2
shift() || 0
l!l
10
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
652
653 1 ▶ return undef if(!(-f $file));
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 1 1
654
655 1 open(FILE,"$file");
656 1 my @lines=<FILE>;
657 1 close(FILE);
658
659 1 my %db;
660 1 for my $line (@lines) {
661 15 chomp($line);
662 15 my ($a,$b) = split /=/, $line;
663 15 $db{$a}=$b;
664 }
665
666 1 ▶ return undef if(!$db{"size"});
Branch: 1 branch
BranchTrueFalseTotal
unless $db{"size"} 0 1 1
667
668 1 ▶ if($load_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($load_flag) { } 1 0 1
669 1 undef $self;
670
671 # Create new network
672 $self = AI::NeuralNet::BackProp->new(intr($db{"size"}/$db{"div"}),
673 $db{"div"},
674 $db{"out"},
675 1 $db{"flag"});
676 } else {
677 0 $self->{DIV} = $db{"div"};
678 0 $self->{SIZE} = $db{"size"};
679 0 $self->{OUT} = $db{"out"};
680 0 $self->{FLAG} = $db{"flag"};
681 }
682
683 # Load variables
684 1 $self->{col_width} = $db{"cw"};
685 1 $self->{random} = $db{"rand"};
686 1 $self->{rA} = $db{"rA"};
687 1 $self->{rB} = $db{"rB"};
688 1 $self->{rS} = $db{"rS"};
689 1 my @tmp = split /\,/, $db{"rRef"};
690 1 $self->{rRef} = \@tmp;
691
692 1 $self->{_CRUNCHED}->{_LENGTH} = $db{"crunch"};
693
694 1 for my $a (0..$self->{_CRUNCHED}->{_LENGTH}-1) {
695 0 $self->{_CRUNCHED}->{LIST}->[$a] = $db{"c$a"};
696 }
697
698 1 $self->initialize_group();
699
700 1 my ($w,@l);
701 1 for my $a (0..$self->{SIZE}-1) {
702 4 $w=$db{"n$a"};
703 4 @l=split /\,/, $w;
704 4 for my $b (0..$self->{DIV}-1) {
705 8 $self->{NET}->[$a]->{SYNAPSES}->{LIST}->[$b]->{WEIGHT}=$l[$b];
706 }
707 }
708
709 1 return $self;
710 }
711
712 # Dumps the complete weight matrix of the network to STDIO
713 0 ▶ sub show {
Subroutine show: not called CC=3 SCAR=24.8
Pod documented
714 0 my $self = shift;
715 0 for my $a (0..$self->{SIZE}-1) {
716 0 print "Neuron $a: ";
717 0 for my $b (0..$self->{DIV}-1) {
718 0 print $self->{NET}->[$a]->{SYNAPSES}->{LIST}->[$b]->{WEIGHT},"\t";
719 }
720 0 print "\n";
721 }
722 }
723
724 # Used internally by new() and learn().
725 # This is the sub block that actually creats
726 # the connections between the synapse chains and
727 # also connects the run packages and the map packages
728 # to the appropiate ends of the neuron grids.
729 3 ▶ sub initialize_group() {
Subroutine initialize_group: called CC=14 SCAR=27.7
Pod undocumented
730 3 my $self = shift;
731 3 my $size = $self->{SIZE};
732 3 my $div = $self->{DIV};
733 3 my $out = $self->{OUT};
734 3 my $flag = $self->{FLAG};
735 3 my $x = 0;
736 3 my $y = 0;
737
738 # Reset map and run synapse counters.
739 3 $self->{RUN}->{REGISTRATION} = $self->{MAP}->{REGISTRATION} = 0;
740
741 3 AI::NeuralNet::BackProp::out2 "There will be $size neurons in this network group, with a divison value of $div.\n";
742 #print "There will be $size neurons in this network group, with a divison value of $div.\n";
743
744 # Create initial neuron packages in one long array for the entire group
745 3 ▶ for($y=0; $y<$size; $y++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $size) 12 3 15
746 #print "Initalizing neuron $y... \r";
747 12 $self->{NET}->[$y]=new AI::NeuralNet::BackProp::neuron();
748 }
749
750 3 AI::NeuralNet::BackProp::out2 "Creating synapse grid...\n";
751
752 3 my $z = 0;
753 3 my $aa = 0;
754 3 my ($n0,$n1,$n2);
755
756 # Outer loop loops over every neuron in group, incrementing by the number
757 # of neurons supposed to be in each layer
758
759 3 ▶ for($y=0; $y<$size; $y+=$div) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $size) 6 0 6
760 6 ▶ if($y+$div>=$size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y + $div >= $size) 3 3 6
761 3 last;
762 }
763
764 # Inner loop connects every neuron in this 'layer' to one input of every neuron in
765 # the next 'layer'. Remeber, layers only exist in terms of where the connections
766 # are divided. For example, if a person requested 2 layers and 3 neurons per layer,
767 # then there would be 6 neurons in the {NET}->[] list, and $div would be set to
768 # 3. So we would loop over and every 3 neurons we would connect each of those 3
769 # neurons to one input of every neuron in the next set of 3 neurons. Of course, this
770 # is an example. 3 and 2 are set by the new() constructor.
771
772 # Flag values:
773 # 0 - (default) -
774 # My feed-foward style: Each neuron in layer X is connected to one input of every
775 # neuron in layer Y. The best and most proven flag style.
776 #
777 # ^ ^ ^
778 # O\ O\ /O Layer Y
779 # ^\\/^/\/^
780 # | //|\/\|
781 # |/ \|/ \|
782 # O O O Layer X
783 # ^ ^ ^
784 #
785 # 1 -
786 # In addition to flag 0, each neuron in layer X is connected to every input of
787 # the neurons ahead of itself in layer X.
788 # 2 - ("L-U Style") -
789 # No, its not "Learning-Unit" style. It gets its name from this: In a 2 layer, 3
790 # neuron network, the connections form a L-U pair, or a W, however you want to look
791 # at it.
792 #
793 # ^ ^ ^
794 # | | |
795 # O-->O-->O
796 # ^ ^ ^
797 # | | |
798 # | | |
799 # O-->O-->O
800 # ^ ^ ^
801 # | | |
802 #
803 # As you can see, each neuron is connected to the next one in its layer, as well
804 # as the neuron directly above itself.
805
806 3 ▶ for ($z=0; $z<$div; $z++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z < $div) 6 3 9
807 6 ▶ if((!$flag) || ($flag == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $flag or $flag == 1) 6 0 6
Condition33%1/3
not $flag or $flag == 1
l!l&&r!l&&!r
600
MC/DC0%0/2
not $flag or $flag == 1
not $flag $flag == 1
Truth table33%1/3
not(A) or B
A= not $flagB= $flag == 1
ABresult
1X1
011
000
808 6 ▶ for ($aa=0; $aa<$div; $aa++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aa < $div) 12 6 18
809 12 $self->{NET}->[$y+$z]->connect($self->{NET}->[$y+$div+$aa]);
810 }
811 }
812 6 ▶ if($flag == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag == 1) 0 6 6
813 0 ▶ for ($aa=$z+1; $aa<$div; $aa++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aa < $div) 0 0 0
814 0 $self->{NET}->[$y+$z]->connect($self->{NET}->[$y+$aa]);
815 }
816 }
817 6 ▶ if($flag == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag == 2) 0 6 6
818 0 $self->{NET}->[$y+$z]->connect($self->{NET}->[$y+$div+$z]);
819 0 ▶ $self->{NET}->[$y+$z]->connect($self->{NET}->[$y+$z+1]) if($z<$div-1);
Branch: 1 branch
BranchTrueFalseTotal
if $z < $div - 1 0 0 0
820 }
821 6 AI::NeuralNet::BackProp::out1 "\n";
822 }
823 3 AI::NeuralNet::BackProp::out1 "\n";
824 }
825
826 # These next two loops connect the _run and _map packages (the IO interface) to
827 # the start and end 'layers', respectively. These are how we insert data into
828 # the network and how we get data from the network. The _run and _map packages
829 # are connected to the neurons so that the neurons think that the IO packages are
830 # just another neuron, sending data on. But the IO packs. are special packages designed
831 # with the same methods as neurons, just meant for specific IO purposes. You will
832 # never need to call any of the IO packs. directly. Instead, they are called whenever
833 # you use the run(), map(), or learn() methods of your network.
834
835 3 AI::NeuralNet::BackProp::out2 "\nMapping I (_run package) connections to network...\n";
836
837 3 ▶ for($y=0; $y<$div; $y++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $div) 6 3 9
838 6 $self->{_tmp_synapse} = $y;
839 6 $self->{NET}->[$y]->register_synapse($self->{RUN});
840 #$self->{NET}->[$y]->connect($self->{RUN});
841 }
842
843 3 AI::NeuralNet::BackProp::out2 "Mapping O (_map package) connections to network...\n\n";
844
845 3 ▶ for($y=$size-$div; $y<$size; $y++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $size) 6 3 9
846 6 $self->{_tmp_synapse} = $y;
847 6 $self->{NET}->[$y]->connect($self->{MAP});
848 }
849
850 # And the group is done!
851 }
852
853
854 # When called with an array refrence to a pattern, returns a refrence
855 # to an array associated with that pattern. See usage in documentation.
856 3 ▶ sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod documented
857 3 my $self = shift;
858 3 my $map = shift;
859 3 my $t0 = new Benchmark;
860 3 $self->{RUN}->run($map);
861 3 $self->{LAST_TIME}=timestr(timediff(new Benchmark, $t0));
862 3 return $self->map();
863 }
864
865 # This automatically uncrunches a response after running it
866 0 ▶ sub run_uc {
Subroutine run_uc: not called CC=1 SCAR=6.9
Pod documented
867 0 $_[0]->uncrunch(run(@_));
868 }
869
870 # Returns benchmark and loop's ran or learned
871 # for last run(), or learn()
872 # operation preformed.
873 #
874 0 ▶ sub benchmarked {
Subroutine benchmarked: not called CC=1 SCAR=6.9
Pod documented
875 0 my $self = shift;
876 0 return $self->{LAST_TIME};
877 }
878
879 # Used to retrieve map from last internal run operation.
880 173 ▶ sub map {
Subroutine map: called CC=1 SCAR=0.0
Pod undocumented
881 173 my $self = shift;
882 173 $self->{MAP}->map();
883 }
884
885 # Forces network to learn pattern passed and give desired
886 # results. See usage in POD.
887 6 ▶ sub learn {
Subroutine learn: called CC=24 SCAR=32.7
Pod documented
888 6 my $self = shift;
889 6 my $omap = shift;
890 6 my $res = shift;
891 6 my %args = @_;
892 6 ▶ my $inc = $args{inc} || 0.20;
Condition50%1/2
$args{'inc'} || 0.2
l!l
06
MC/DC0%0/1
$args{'inc'} || 0.2
$args{'inc'}
Truth table50%1/2
A
A= $args{'inc'}
Aresult
11
00
893 6 ▶ my $max = $args{max} || 1024;
Condition50%1/2
$args{'max'} || 1024
l!l
06
MC/DC0%0/1
$args{'max'} || 1024
$args{'max'}
Truth table50%1/2
A
A= $args{'max'}
Aresult
11
00
894 6 my $_mx = intr($max/10);
895 6 my $_mi = 0;
896 6 ▶ my $error = ($args{error}>-1 && defined $args{error}) ? $args{error} : -1;
Branch: 1 branch
BranchTrueFalseTotal
$args{'error'} > -1 && defined $args{'error'} ? : 0 6 6
Condition33%1/3
$args{"error"} > -1 and defined $args{"error"}
!ll&&!rl&&r
060
MC/DC0%0/2
$args{"error"} > -1 and defined $args{"error"}
$args{"error"} > -1 defined $args{"error"}
Truth table33%1/3
A and B
A= $args{"error"} > -1B= defined $args{"error"}
ABresult
0X0
100
111
897 6 my $div = $self->{DIV};
898 6 my $size = $self->{SIZE};
899 6 my $out = $self->{OUT};
900 6 my $divide = AI::NeuralNet::BackProp->intr($div/$out);
901 6 my ($a,$b,$y,$flag,$map,$loop,$diff,$pattern,$value);
902 6 my ($t0,$it0);
903 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
904
905 # Take care of crunching strings passed
906 6 ▶ $omap = $self->crunch($omap) if($omap == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $omap == 0 0 6 6
907 6 ▶ $res = $self->crunch($res) if($res == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $res == 0 0 6 6
908
909 # Fill in empty spaces at end of results matrix with a 0
910 6 ▶ if($#{$res}<$out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$res;} < $out) 6 0 6
911 6 for my $x ($#{$res}+1..$out) {
912 #$res->[$x] = 0;
913 }
914 }
915
916 # Debug
917 6 AI::NeuralNet::BackProp::out1 "Num output neurons: $out, Input neurons: $size, Division: $divide\n";
918
919 # Start benchmark timer and initalize a few variables
920 6 $t0 = new Benchmark;
921 6 $flag = 0;
922 6 $loop = 0;
923 6 my $ldiff = 0;
924 6 my $dinc = 0.0001;
925 6 my $cdiff = 0;
926 6 $diff = 100;
927 6 ▶ $error = ($error>-1)?$error:-1;
Branch: 1 branch
BranchTrueFalseTotal
$error > -1 ? : 0 6 6
928
929 # $flag only goes high when all neurons in output map compare exactly with
930 # desired result map or $max loops is reached
931 #
932 6 ▶ while(!$flag && ($max ? $loop<$max : 1)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $flag and $max ? $loop < $max : 1) 170 6 176
$max ? : 170 0 170
Condition66%2/3
not $flag and $max ? $loop < $max : 1
!ll&&!rl&&r
60170
MC/DC50%1/2
not $flag and $max ? $loop < $max : 1
not $flag $max ? $loop < $max : 1
Truth table66%2/3
not(A) and B
A= not $flagB= $max ? $loop < $max : 1
ABresult
0X0
100
111
933 170 $it0 = new Benchmark;
934
935 # Run the map
936 170 $self->{RUN}->run($omap);
937
938 # Retrieve last mapping and initialize a few variables.
939 170 $map = $self->map();
940 170 $y = $size-$div;
941 170 $flag = 1;
942
943 # Compare the result map we just ran with the desired result map.
944 170 $diff = pdiff($map,$res);
945
946 # This adjusts the increment multiplier to decrease as the loops increase
947 170 ▶ if($_mi > $_mx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_mi > $_mx) 1 169 170
948 1 $dinc *= 0.1;
949 1 $_mi = 0;
950 }
951
952 170 $_mi++;
953
954
955 # We de-increment the loop ammount to prevent infinite learning loops.
956 # In old versions of this module, if you used too high of an initial input
957 # $inc, then the network would keep jumping back and forth over your desired
958 # results because the increment was too high...it would try to push close to
959 # the desired result, only to fly over the other edge too far, therby trying
960 # to come back, over shooting again.
961 # This simply adjusts the learning gradient proportionally to the ammount of
962 # convergance left as the difference decreases.
963 170 $inc -= ($dinc*$diff);
964 170 ▶ $inc = 0.0000000001 if($inc < 0.0000000001);
Branch: 1 branch
BranchTrueFalseTotal
if $inc < "1e-10" 0 170 170
965
966 # This prevents it from seeming to get stuck in one location
967 # by attempting to boost the values out of the hole they seem to be in.
968 170 ▶ if($diff eq $ldiff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff eq $ldiff) { } 2 168 170
969 2 $cdiff++;
970 2 $inc += ($dinc*$diff)+($dinc*$cdiff*10);
971 } else {
972 168 $cdiff=0;
973 }
974
975 # Save last $diff
976 170 $ldiff = $diff;
977
978 # This catches a max error argument and handles it
979 170 ▶ if(!($error>-1 ? $diff>$error : 1)) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($error > -1 ? $diff > $error : 1) 0 170 170
$error > -1 ? : 0 170 170
980 0 $flag=1;
981 0 last;
982 }
983
984 # Debugging
985 170 AI::NeuralNet::BackProp::out4 "Difference: $diff\%\t Increment: $inc\tMax Error: $error\%\n";
986 170 AI::NeuralNet::BackProp::out1 "\n\nMapping results from $map:\n";
987
988 # This loop compares each element of the output map with the desired result map.
989 # If they don't match exactly, we call weight() on the offending output neuron
990 # and tell it what it should be aiming for, and then the offending neuron will
991 # try to adjust the weights of its synapses to get closer to the desired output.
992 # See comments in the weight() method of AI::NeuralNet::BackProp for how this works.
993 170 my $l=$self->{NET};
994 170 for my $i (0..$out-1) {
995 170 $a = $map->[$i];
996 170 $b = $res->[$i];
997
998 170 AI::NeuralNet::BackProp::out1 "\nmap[$i] is $a\n";
999 170 AI::NeuralNet::BackProp::out1 "res[$i] is $b\n";
1000
1001 170 for my $j (0..$divide-1) {
1002 340 ▶ if($a!=$b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a != $b) 328 12 340
1003 328 AI::NeuralNet::BackProp::out1 "Punishing $self->{NET}->[($i*$divide)+$j] at ",(($i*$divide)+$j)," ($i with $a) by $inc.\n";
1004 328 ▶ $l->[$y+($i*$divide)+$j]->weight($inc,$b) if($l->[$y+($i*$divide)+$j]);
Branch: 1 branch
BranchTrueFalseTotal
if $$l[$y + $i * $divide + $j] 328 0 328
1005 328 $flag = 0;
1006 }
1007 }
1008 }
1009
1010 # This counter is just used in the benchmarking operations.
1011 170 $loop++;
1012
1013 170 AI::NeuralNet::BackProp::out1 "\n\n";
1014
1015 # Benchmark this loop.
1016 170 AI::NeuralNet::BackProp::out4 "Learning itetration $loop complete, timed at".timestr(timediff(new Benchmark, $it0),'noc','5.3f')."\n";
1017
1018 # Map the results from this loop.
1019 170 AI::NeuralNet::BackProp::out4 "Map: \n";
1020 170 ▶ AI::NeuralNet::BackProp::join_cols($map,$self->{col_width}) if ($AI::NeuralNet::BackProp::DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $AI::NeuralNet::BackProp::DEBUG 0 170 170
1021 170 AI::NeuralNet::BackProp::out4 "Res: \n";
1022 170 ▶ AI::NeuralNet::BackProp::join_cols($res,$self->{col_width}) if ($AI::NeuralNet::BackProp::DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $AI::NeuralNet::BackProp::DEBUG 0 170 170
1023 }
1024
1025 # Compile benchmarking info for entire learn() process and return it, save it, and
1026 # display it.
1027 6 $self->{LAST_TIME}="$loop loops and ".timestr(timediff(new Benchmark, $t0));
1028 6 my $str = "Learning took $loop loops and ".timestr(timediff(new Benchmark, $t0),'noc','5.3f');
1029 6 AI::NeuralNet::BackProp::out2 $str;
1030 6 return $str;
1031 }
1032
1033 1 1;
1034
1035 # Internal input class. Not to be used directly.
1036 package AI::NeuralNet::BackProp::_run;
1037
1038 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1039
1040 # Dummy constructor.
1041 2 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
1042 2 bless { PARENT => $_[1] }, $_[0]
1043 }
1044
1045 # This is so we comply with the neuron interface.
1046 656 ▶ sub weight {}
Subroutine weight: called CC=1 SCAR=0.0
1047 0 ▶ sub input {}
Subroutine input: not called CC=1 SCAR=0.0
1048
1049 # Again, compliance with neuron interface.
1050 0 ▶ sub register_synapse {
Subroutine register_synapse: not called CC=2 SCAR=17.9
1051 0 my $self = shift;
1052 0 ▶ my $sid = $self->{REGISTRATION} || 0;
Condition0%0/2
$self->{'REGISTRATION'} || 0
l!l
00
MC/DC0%0/1
$self->{'REGISTRATION'} || 0
$self->{'REGISTRATION'}
Truth table0%0/2
A
A= $self->{'REGISTRATION'}
Aresult
11
00
1053 0 $self->{REGISTRATION} = ++$sid;
1054 0 $self->{RMAP}->{$sid-1} = $self->{PARENT}->{_tmp_synapse};
1055 0 return $sid-1;
1056 }
1057
1058 # Here is the real meat of this package.
1059 # run() does one thing: It fires values
1060 # into the first layer of the network.
1061 173 ▶ sub run {
Subroutine run: called CC=7 SCAR=20.8
1062 173 my $self = shift;
1063 173 my $map = shift;
1064 173 my $x = 0;
1065 173 ▶ $map = $self->{PARENT}->crunch($map) if($map == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $map == 0 0 173 173
1066 173 ▶ return undef if(substr($map,0,5) ne "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if substr($map, 0, 5) ne "ARRAY" 0 173 173
1067 173 foreach my $el (@{$map}) {
1068 # Catch ourself if we try to run more inputs than neurons
1069 346 ▶ return $x if($x>$self->{PARENT}->{DIV}-1);
Branch: 1 branch
BranchTrueFalseTotal
if $x > $self->{"PARENT"}{"DIV"} - 1 0 346 346
1070
1071 # Here we add a small ammount of randomness to the network.
1072 # This is to keep the network from getting stuck on a 0 value internally.
1073 346 $self->{PARENT}->{NET}->[$x]->input(0,$el+(rand()*$self->{ramdom}));
1074 346 $x++;
1075 };
1076 # Incase we tried to run less inputs than neurons, run const 1 in extra neurons
1077 173 ▶ if($x<$self->{PARENT}->{DIV}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"PARENT"}{"DIV"}) 0 173 173
1078 0 for my $y ($x..$self->{PARENT}->{DIV}-1) {
1079 0 $self->{PARENT}->{NET}->[$y]->input(0,1);
1080 }
1081 }
1082 173 return $x;
1083 }
1084
1085
1086 1 1;
1087
1088 # Internal output class. Not to be used directly.
1089 package AI::NeuralNet::BackProp::_map;
1090
1091 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1092
1093 # Dummy constructor.
1094 2 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
1095 2 bless { PARENT => $_[1] }, $_[0]
1096 }
1097
1098 # Compliance with neuron interface
1099 0 ▶ sub weight {}
Subroutine weight: not called CC=1 SCAR=0.0
1100
1101 # Compliance with neuron interface
1102 6 ▶ sub register_synapse {
Subroutine register_synapse: called CC=2 SCAR=6.9
1103 6 my $self = shift;
1104 6 ▶ my $sid = $self->{REGISTRATION} || 0;
Condition100%2/2
$self->{'REGISTRATION'} || 0
l!l
33
MC/DC100%1/1
$self->{'REGISTRATION'} || 0
$self->{'REGISTRATION'}
Truth table100%2/2
A
A= $self->{'REGISTRATION'}
Aresult
11
00
1105 6 $self->{REGISTRATION} = ++$sid;
1106 6 $self->{RMAP}->{$sid-1} = $self->{PARENT}->{_tmp_synapse};
1107 6 return $sid-1;
1108 }
1109
1110 # This acts just like a regular neuron by receiving
1111 # values from input synapes. Yet, unlike a regularr
1112 # neuron, it doesnt weight the values, just stores
1113 # them to be retrieved by a call to map().
1114 346 ▶ sub input {
Subroutine input: called CC=1 SCAR=0.0
1115 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1116 346 my $self = shift;
1117 346 my $sid = shift;
1118 346 my $value = shift;
1119 346 my $size = $self->{PARENT}->{DIV};
1120 346 my $flag = 1;
1121 346 $self->{OUTPUT}->[$sid]->{VALUE} = $self->{PARENT}->intr($value);
1122 346 $self->{OUTPUT}->[$sid]->{FIRED} = 1;
1123
1124 346 AI::NeuralNet::BackProp::out1 "Received value $self->{OUTPUT}->[$sid]->{VALUE} and sid $sid, self $self.\n";
1125 }
1126
1127 # Here we simply collect the value of every neuron connected to this
1128 # one from the layer below us and return an array ref to the final map..
1129 173 ▶ sub map {
Subroutine map: called CC=3 SCAR=11.0
1130 173 my $self = shift;
1131 173 my $size = $self->{PARENT}->{DIV};
1132 173 my $out = $self->{PARENT}->{OUT};
1133 173 my $divide = AI::NeuralNet::BackProp->intr($size/$out);
1134 173 my @map = ();
1135 173 my $value;
1136 173 AI::NeuralNet::BackProp::out1 "Num output neurons: $out, Input neurons: $size, Division: $divide\n";
1137 173 for(0..$out-1) {
1138 173 $value=0;
1139 173 for my $a (0..$divide-1) {
1140 346 $value += $self->{OUTPUT}->[($_*$divide)+$a]->{VALUE};
1141 346 AI::NeuralNet::BackProp::out1 "\$a is $a, index is ".(($_*$divide)+$a).", value is $self->{OUTPUT}->[($_*$divide)+$a]->{VALUE}\n";
1142 }
1143 173 $map[$_] = AI::NeuralNet::BackProp->intr($value/$divide);
1144 173 AI::NeuralNet::BackProp::out1 "Map position $_ is $map[$_] in @{[\@map]} with self set to $self.\n";
1145 173 $self->{OUTPUT}->[$_]->{FIRED} = 0;
1146 }
1147 173 my $ret=\@map;
1148 173 return $self->{PARENT}->_range($ret);
1149 }
1150 1 1;
1151
1152 # load_pcx() wrapper package
1153 package AI::NeuralNet::BackProp::PCX;
1154
1155 # Called by load_pcx in AI::NeuralNet::BackProp;
1156 0 ▶ sub new {
Subroutine new: not called CC=1 SCAR=6.9
1157 0 my $type = shift;
1158 0 my $self = {
1159 parent => $_[0],
1160 file => $_[1]
1161 };
1162 0 my (@a,@b)=load_pcx($_[1]);
1163 0 $self->{image}=\@a;
1164 0 $self->{palette}=\@b;
1165 0 bless \%{$self}, $type;
1166 }
1167
1168 # Returns a rectangular block defined by an array ref in the form of
1169 # [$x1,$y1,$x2,$y2]
1170 # Return value is an array ref
1171 0 ▶ sub get_block {
Subroutine get_block: not called CC=3 SCAR=24.8
1172 0 my $self = shift;
1173 0 my $ref = shift;
1174 0 my ($x1,$y1,$x2,$y2) = @{$ref};
1175 0 my @block = ();
1176 0 my $count = 0;
1177 0 for my $x ($x1..$x2-1) {
1178 0 for my $y ($y1..$y2-1) {
1179 0 $block[$count++] = $self->get($x,$y);
1180 }
1181 }
1182 0 return \@block;
1183 }
1184
1185 # Returns pixel at $x,$y
1186 0 ▶ sub get {
Subroutine get: not called CC=1 SCAR=6.9
1187 0 my $self = shift;
1188 0 my ($x,$y) = (shift,shift);
1189 0 return $self->{image}->[$y*320+$x];
1190 }
1191
1192 # Returns array of (r,g,b) value from palette index passed
1193 0 ▶ sub rgb {
Subroutine rgb: not called CC=1 SCAR=6.9
1194 0 my $self = shift;
1195 0 my $color = shift;
1196 0 return ($self->{palette}->[$color]->{red},$self->{palette}->[$color]->{green},$self->{palette}->[$color]->{blue});
1197 }
1198
1199 # Returns mean of (rgb) value of palette index passed
1200 0 ▶ sub avg {
Subroutine avg: not called CC=1 SCAR=6.9
1201 0 my $self = shift;
1202 0 my $color = shift;
1203 0 return $self->{parent}->intr(($self->{palette}->[$color]->{red}+$self->{palette}->[$color]->{green}+$self->{palette}->[$color]->{blue})/3);
1204 }
1205
1206 # Loads and decompresses a PCX-format 320x200, 8-bit image file and returns
1207 # two arrays, first is a 64000-byte long array, each element contains a palette
1208 # index, and the second array is a 255-byte long array, each element is a hash
1209 # ref with the keys 'red', 'green', and 'blue', each key contains the respective color
1210 # component for that color index in the palette.
1211 0 ▶ sub load_pcx {
Subroutine load_pcx: not called CC=7 SCAR=40.3
1212 0 ▶ shift if(substr($_[0],0,4) eq 'AI::');
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[0], 0, 4) eq "AI::" 0 0 0
1213
1214 # open the file
1215 0 open(FILE, "$_[0]");
1216 0 binmode(FILE);
1217
1218 0 my $tmp;
1219 my @image;
1220 0 my @palette;
1221 0 my $data;
1222
1223 # Read header
1224 0 read(FILE,$tmp,128);
1225
1226 # load the data and decompress into buffer
1227 0 my $count=0;
1228
1229 0 ▶ while($count<320*200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 64000) 0 0 0
1230 # get the first piece of data
1231 0 read(FILE,$data,1);
1232 0 $data=ord($data);
1233
1234 # is this a rle?
1235 0 ▶ if ($data>=192 && $data<=255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data >= 192 and $data <= 255) { } 0 0 0
Condition0%0/3
$data >= 192 and $data <= 255
!ll&&!rl&&r
000
MC/DC0%0/2
$data >= 192 and $data <= 255
$data >= 192 $data <= 255
Truth table0%0/3
A and B
A= $data >= 192B= $data <= 255
ABresult
0X0
100
111
1236 # how many bytes in run?
1237 0 my $num_bytes = $data-192;
1238
1239 # get the actual $data for the run
1240 0 read(FILE, $data, 1);
1241 0 $data=ord($data);
1242 # replicate $data in buffer num_bytes times
1243 0 ▶ while($num_bytes-->0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_bytes-- > 0) 0 0 0
1244 0 $image[$count++] = $data;
1245 } # end while
1246 } else {
1247 # actual $data, just copy it into buffer at next location
1248 0 $image[$count++] = $data;
1249 } # end else not rle
1250 }
1251
1252 # move to end of file then back up 768 bytes i.e. to begining of palette
1253 0 seek(FILE,-768,2);
1254
1255 # load the pallete into the palette
1256 0 for my $index (0..255) {
1257 # get the red component
1258 0 read(FILE,$tmp,1);
1259 0 $palette[$index]->{red} = ($tmp>>2);
1260
1261 # get the green component
1262 0 read(FILE,$tmp,1);
1263 0 $palette[$index]->{green} = ($tmp>>2);
1264
1265 # get the blue component
1266 0 read(FILE,$tmp,1);
1267 0 $palette[$index]->{blue} = ($tmp>>2);
1268
1269 }
1270
1271 0 close(FILE);
1272
1273 0 return @image,@palette;
1274 }
1275
1276 1 1;
1277
1278
1279 __END__
↑ Summary