Algorithm-Evolutionary

 view release on metacpan or  search on metacpan

lib/Algorithm/Evolutionary/Op/Animated_GIF_Output.pm  view on Meta::CPAN

  my $hash = shift || croak "No default values for length ";
  my $self = Algorithm::Evolutionary::Op::Base::new( __PACKAGE__, 1, $hash );
  $hash->{'pixels_per_bit'} = $hash->{'pixels_per_bit'} || 1;
  $self->{'_image'} = GD::Image->new($hash->{'length'}*$hash->{'pixels_per_bit'},
				     $hash->{'number_of_strings'}*$hash->{'pixels_per_bit'});
  $self->{'_length'} = $hash->{'length'};
  $self->{'_pixels_per_bit'} = $hash->{'pixels_per_bit'};
  $self->{'_white'} = $self->{'_image'}->colorAllocate(0,0,0); #background color
  $self->{'_black'} = $self->{'_image'}->colorAllocate(255,255,255);
  $self->{'_gifdata'} = $self->{'_image'}->gifanimbegin;
  $self->{'_gifdata'}   .= $self->{'_image'}->gifanimadd;    # first frame
  return $self;
}


sub apply {
    my $self = shift;
    my $population_hashref=shift;
    my $frame  = GD::Image->new($self->{'_image'}->getBounds);
    my $ppb = $self->{'_pixels_per_bit'};
    my $l=0;
    for my $i (@$population_hashref) {
      my $bit_string = $i->{'_str'};
      for my $c ( 0..($self->{'_length'}-1) ) {
	my $bit = substr( $bit_string, $c, 1 );
	if ( $bit ) {
	  for my $p ( 1..$ppb ) {
	    for my $q (1..$ppb ) {
	      $frame->setPixel($l*$ppb+$q, $c*$ppb+$p,
			       $self->{'_black'})
	    }
	  }
	}
      }
      $l++;
    }
    $self->{'_gifdata'}   .= $frame->gifanimadd;     # add frame
}

sub terminate {
  my $self= shift;
  $self->{'_gifdata'}   .= $self->{'_image'}->gifanimend;
}

sub output {
  my $self = shift;
  return $self->{'_gifdata'};
}

"No man's land" ; # Magic true value required at end of module

__END__

=head1 NAME

Algorithm::Evolutionary::Op::Animated_GIF_Output - Creates an animated GIF, a frame per generation. Useful for binary strings.


=head1 SYNOPSIS

  my $pp = new Algorithm::Evolutionary::Op::Animated_GIF_Output; 

  my @pop;
  my $length = 8;
  my $number_of_strings = 10;
  for ( 1..$number_of_strings ) {

lib/Algorithm/Evolutionary/Op/Animated_GIF_Output.pm  view on Meta::CPAN

                  number_of_strings => $number_of_strings };

  $pp = new Algorithm::Evolutionary::Op::Animated_GIF_Output $options

  $pp->apply( \@pop );
  $pp->terminate();
  my $output_gif = $pp->output(); # Prints final results

=head1 DESCRIPTION

Saves each generation as a frame in an animated GIF. Every individual
gets a line of the number of pixels specified, and bits set to "1" are
represented via black pixels or fat pixels. By default, a bit takes a
single pixel. 

=head1 INTERFACE 

=head2 new( [$hash_ref] )

C<$hash_ref> is a hashref with 3 options: C<pixels_per_bit>, which
defaults to 1, and C<length> and C<number_of_strings> which have no

scripts/rectangle-coverage.pl  view on Meta::CPAN

	   )->pack( -side => 'left',
		    -expand => 1 );

$mw->eventAdd('<<Gen>>' => '<Control-Shift-G>'); # Improbable combination
$mw->eventAdd('<<Fin>>' => '<Control-C>');
$mw->bind('<<Gen>>' => \&generation);
$mw->bind('<<Fin>>' => \&finished );


sub create_and_pack {
  my $frame = shift;
  my $var = shift;
  my $f = $frame->Frame();
  my $label = $f->Label(-text => $var )->pack(-side => 'left');
  my $entry = $f->Entry( -textvariable => eval '\$'.$var )->pack(-side => 'right' );
  $f->pack();
}

sub start {
  #Generate random rectangles
  for my $i (0 .. $num_rects) {
    
    my $x_0 = rand( $arena_side );



( run in 1.175 second using v1.01-cache-2.11-cpan-df04353d9ac )