File Coverage

blib/lib/AI/NeuralNet/SOM/Torus.pm
Criterion Covered Total %
statement 35 35 100.0
branch 10 10 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 52 52 100.0


line stmt bran cond sub pod time code
1             package AI::NeuralNet::SOM::Torus;
2              
3 1     1   22957 use strict;
  1         2  
  1         107  
4 1     1   5 use warnings;
  1         2  
  1         32  
5              
6 1     1   2216 use Data::Dumper;
  1         13241  
  1         85  
7 1     1   10 use base qw(AI::NeuralNet::SOM::Rect);
  1         2  
  1         648  
8 1     1   7 use AI::NeuralNet::SOM::Utils;
  1         1  
  1         366  
9              
10             =pod
11              
12             =head1 NAME
13              
14             AI::NeuralNet::SOM::Torus - Perl extension for Kohonen Maps (torus topology)
15              
16             =head1 SYNOPSIS
17              
18             use AI::NeuralNet::SOM::Torus;
19             my $nn = new AI::NeuralNet::SOM::Torus (output_dim => "5x6",
20             input_dim => 3);
21             $nn->initialize;
22             $nn->train (30,
23             [ 3, 2, 4 ],
24             [ -1, -1, -1 ],
25             [ 0, 4, -3]);
26              
27             print $nn->as_data;
28              
29             =head1 DESCRIPTION
30              
31             This SOM is very similar to that with a rectangular topology, except that the rectangle is connected
32             on the top edge and the bottom edge to first form a cylinder; and that cylinder is then formed into
33             a torus by connecting the rectangle's left and right border (L).
34              
35             =head1 INTERFACE
36              
37             It exposes the same interface as the base class.
38              
39             =cut
40              
41             sub neighbors { # http://www.ai-junkie.com/ann/som/som3.html
42 1202     1202 1 2810 my $self = shift;
43 1202         1402 my $sigma = shift;
44 1202         1548 my $sigma2 = $sigma * $sigma; # need the square more often
45 1202         1314 my $X = shift;
46 1202         1181 my $Y = shift;
47              
48 1202         2487 my ($_X, $_Y) = ($self->{_X}, $self->{_Y});
49              
50 1202         1200 my @neighbors;
51 1202         2492 for my $x (0 .. $self->{_X}-1) {
52 6010         11635 for my $y (0 .. $self->{_Y}-1){ # this is not overly elegant, or fast
53 36060         59025 my $distance2 = ($x - $X) * ($x - $X) + ($y - $Y) * ($y - $Y); # take the node with its x,y coords
54 36060 100       68752 push @neighbors, [ $x, $y, sqrt($distance2) ] if $distance2 <= $sigma2;
55              
56 36060         60341 $distance2 = ($x - $_X - $X) * ($x - $_X - $X) + ($y - $Y) * ($y - $Y); # take the node transposed to left by _X
57 36060 100       65654 push @neighbors, [ $x, $y, sqrt ($distance2) ] if $distance2 <= $sigma2;
58              
59 36060         53932 $distance2 = ($x + $_X - $X) * ($x + $_X - $X) + ($y - $Y) * ($y - $Y); # transposed by _X to right
60 36060 100       63867 push @neighbors, [ $x, $y, sqrt ($distance2) ] if $distance2 <= $sigma2;
61              
62 36060         61693 $distance2 = ($x - $X) * ($x - $X) + ($y - $_Y - $Y) * ($y - $_Y - $Y); # same with _Y up
63 36060 100       60218 push @neighbors, [ $x, $y, sqrt ($distance2) ] if $distance2 <= $sigma2;
64              
65 36060         54505 $distance2 = ($x - $X) * ($x - $X) + ($y + $_Y - $Y) * ($y + $_Y - $Y); # and down
66 36060 100       80109 push @neighbors, [ $x, $y, sqrt ($distance2) ] if $distance2 <= $sigma2;
67             }
68             }
69 1202         4519 return \@neighbors;
70             }
71              
72             =pod
73              
74             =head1 SEE ALSO
75              
76             L
77              
78             =head1 AUTHOR
79              
80             Robert Barta, Erho@devc.atE
81              
82             =head1 COPYRIGHT AND LICENSE
83              
84             Copyright (C) 2007 by Robert Barta
85              
86             This library is free software; you can redistribute it and/or modify
87             it under the same terms as Perl itself, either Perl version 5.8.8 or,
88             at your option, any later version of Perl 5 you may have available.
89              
90             =cut
91              
92             our $VERSION = '0.01';
93              
94             1;
95              
96             __END__