AI-NeuralNet-Kohonen-Demo-RGB
view release on metacpan or search on metacpan
package AI::NeuralNet::Kohonen::Demo::RGB;
use vars qw/$VERSION/;
$VERSION = 0.123; # 13 March 2003; using smoothing
=head1 NAME
AI::NeuralNet::Kohonen::Demo::RGB - Colour-based demo
=head1 SYNOPSIS
use AI::NeuralNet::Kohonen::Demo::RGB;
$_ = AI::NeuralNet::Kohonen::Demo::RGB->new(
display_scale => 20,
display => 'hex',
map_dim => 39,
epochs => 9,
table => "R G B"
."1 0 0"
."0 1 0"
."0 0 1",
);
$_->train;
exit;
=head1 DESCRIPTION
A sub-class of C<AI::NeuralNet::Kohonen>
that impliments extra methods to make use of TK
in a very slow demonstration of how a SOM can collapse
a three dimensional space (RGB colour values) into a
two dimensional space (the display). See L<SYNOPSIS>.
The only things added are two new fields to supply to the
constructor - set C<display> to C<hex> for display as
a unified distance matrix, rather than plain grid; set
C<display_scale> for the size of the display.
=cut
use strict;
use warnings;
use Carp qw/cluck carp confess croak/;
use base "AI::NeuralNet::Kohonen";
use Tk;
use Tk::Canvas;
use Tk::Label;
use Tk qw/DoOneEvent DONT_WAIT/;
#
# Used only by &tk_train
#
sub tk_show { my $self=shift;
for my $x (0..$self->{map_dim_x}){
for my $y (0..$self->{map_dim_y}){
my $colour = sprintf("#%02x%02x%02x",
(int (255 * $self->{map}->[$x]->[$y]->{weight}->[0])),
(int (255 * $self->{map}->[$x]->[$y]->{weight}->[1])),
(int (255 * $self->{map}->[$x]->[$y]->{weight}->[2])),
);
if ($self->{display} and $self->{display} eq 'hex'){
my $xo = ($y % 2) * ($self->{display_scale}/2);
my $yo = 0;
$self->{c}->create(
polygon => [
$xo + ((1+$x)*$self->{display_scale} ),
$yo + ((1+$y)*$self->{display_scale} ),
# polygon only:
$xo + ((1+$x)*($self->{display_scale})+($self->{display_scale}/2) ),
$yo + ((1+$y)*($self->{display_scale})-($self->{display_scale}/2) ),
#
$xo + ((1+$x)*($self->{display_scale})+$self->{display_scale} ),
$yo + ((1+$y)*$self->{display_scale} ),
$xo + ((1+$x)*($self->{display_scale})+$self->{display_scale} ),
$yo + ((1+$y)*($self->{display_scale})+($self->{display_scale}/2) ),
# Polygon only:
$xo + ((1+$x)*($self->{display_scale})+($self->{display_scale}/2) ),
$yo + ((1+$y)*($self->{display_scale})+($self->{display_scale}) ),
#
$xo + ((1+$x)*$self->{display_scale} ),
$yo + ((1+$y)*($self->{display_scale})+($self->{display_scale}/2) ),
],
-outline => "black",
-fill => $colour,
);
}
else {
( run in 1.994 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )