« blib/lib/Algorithm/Evolutionary/Op/GeneralGeneration.pm
↑ Summary
lib/Algorithm/Evolutionary/Fitness/ONEMAX.pm »
| 1 | package Algorithm::Evolutionary::Op::Animated_GIF_Output; | ||
| 2 | |||
| 3 | use lib qw( ../../../../lib | ||
| 4 | ../../../lib | ||
| 5 | ../../../../../../Algorithm-Evolutionary/lib ../Algorithm-Evolutionary/lib ); #For development and perl syntax mode | ||
| 6 | |||
| 7 | use warnings; | ||
| 8 | use strict; | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | our $VERSION = sprintf "%d.%03d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/g; | ||
| 12 | |||
| 13 | use base 'Algorithm::Evolutionary::Op::Base'; | ||
| 14 | |||
| 15 | use GD::Image; | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my $class = shift; | ||
| 19 | my $hash = shift || croak "No default values for length "; | ||
| 20 | my $self = Algorithm::Evolutionary::Op::Base::new( __PACKAGE__, 1, $hash ); | ||
| 21 | $hash->{'pixels_per_bit'} = $hash->{'pixels_per_bit'} || 1; | ||
| 22 | $self->{'_image'} = GD::Image->new($hash->{'length'}*$hash->{'pixels_per_bit'}, | ||
| 23 | $hash->{'number_of_strings'}*$hash->{'pixels_per_bit'}); | ||
| 24 | $self->{'_length'} = $hash->{'length'}; | ||
| 25 | $self->{'_pixels_per_bit'} = $hash->{'pixels_per_bit'}; | ||
| 26 | $self->{'_white'} = $self->{'_image'}->colorAllocate(0,0,0); #background color | ||
| 27 | $self->{'_black'} = $self->{'_image'}->colorAllocate(255,255,255); | ||
| 28 | $self->{'_gifdata'} = $self->{'_image'}->gifanimbegin; | ||
| 29 | $self->{'_gifdata'} .= $self->{'_image'}->gifanimadd; # first frame | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | sub apply { | ||
| 35 | my $self = shift; | ||
| 36 | my $population_hashref=shift; | ||
| 37 | my $frame = GD::Image->new($self->{'_image'}->getBounds); | ||
| 38 | my $ppb = $self->{'_pixels_per_bit'}; | ||
| 39 | my $l=0; | ||
| 40 | for my $i (@$population_hashref) { | ||
| 41 | my $bit_string = $i->{'_str'}; | ||
| 42 | for my $c ( 0..($self->{'_length'}-1) ) { | ||
| 43 | my $bit = substr( $bit_string, $c, 1 ); | ||
| 44 | if ( $bit ) { | ||
| 45 | for my $p ( 1..$ppb ) { | ||
| 46 | for my $q (1..$ppb ) { | ||
| 47 | $frame->setPixel($l*$ppb+$q, $c*$ppb+$p, | ||
| 48 | $self->{'_black'}) | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | $l++; | ||
| 54 | } | ||
| 55 | $self->{'_gifdata'} .= $frame->gifanimadd; # add frame | ||
| 56 | } | ||
| 57 | |||
| 58 | sub terminate { | ||
| 59 | my $self= shift; | ||
| 60 | $self->{'_gifdata'} .= $self->{'_image'}->gifanimend; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub output { | ||
| 64 | my $self = shift; | ||
| 65 | return $self->{'_gifdata'}; | ||
| 66 | } | ||
| 67 | |||
| 68 | "No man's land" ; # Magic true value required at end of module | ||
| 69 | |||
| 70 | __END__ |