Advertisement
Guest User

Untitled

a guest
Aug 28th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.38 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use GD;
  3.  
  4. main();
  5.  
  6. sub main {
  7.     get_dimensions();
  8.     initialise();
  9.     generate_genes();
  10.     while(1){
  11.         draw_images();
  12.         score_images();
  13.         if($best < $prevbest){
  14.             $prevbest = $best;
  15.             output_best();
  16.         }
  17.         $numreps = int(rand(50));
  18.         for($m = 0; $m <= $numreps; $m++){
  19.         if(int(rand(100)) < 100){ soft_mutate_x(); }
  20.         if(int(rand(100)) < 100){ soft_mutate_y(); }
  21.         if(int(rand(100)) < 100){ soft_mutate_r(); }
  22.         if(int(rand(100)) < 100){ soft_mutate_g(); }
  23.         if(int(rand(100)) < 100){ soft_mutate_b(); }
  24.         if(int(rand(100)) < 100){ soft_mutate_a(); }
  25.         }
  26.         $generation++;
  27.     }
  28. }
  29.  
  30. sub generate_genes {
  31.     for($i = 1; $i <= 10; $i++){
  32.         for($d = 1; $d <= 50; $d++){
  33.             for($c = 1; $c <= 4; $c++){
  34.                 push(@{"xchrom".$i}, int(rand($width + 1)));
  35.                 push(@{"ychrom".$i}, int(rand($height + 1)));
  36.             }
  37.             for($c = 1; $c <= 3; $c++){
  38.                 push(@{"rchrom".$i}, int(rand(256)));
  39.                 push(@{"gchrom".$i}, int(rand(256)));
  40.                 push(@{"bchrom".$i}, int(rand(256)));
  41.             }
  42.             push(@{"achrom".$i}, int(rand(128)));
  43.         }
  44.     }
  45. }
  46.  
  47. sub get_dimensions {
  48.     $timage = GD::Image->newFromPng("ml.png");
  49.     ($width, $height) = $timage->getBounds;
  50. }
  51.  
  52. sub initialise {
  53.     $generation = 1;
  54.     $best = 195010 * $width * $height;
  55.     $prevbest = 195010 * $width * $height;
  56. }
  57.  
  58. sub draw_images {
  59.     for($i = 1; $i <= 10; $i++){
  60.         $im[$i] = new GD::Image($width,$height,1);
  61.         for($z = 0; $z <= 196; $z+=4){
  62.             $poly = new GD::Polygon;
  63.             $poly->addPt(${"xchrom".$i}[$z], ${"ychrom".$i}[$z]);
  64.             $poly->addPt(${"xchrom".$i}[$z+1], ${"ychrom".$i}[$z+1]);
  65.             $poly->addPt(${"xchrom".$i}[$z+2], ${"ychrom".$i}[$z+2]);
  66.             $poly->addPt(${"xchrom".$i}[$z+3], ${"ychrom".$i}[$z+3]);
  67.             $col = $im[$i]->colorAllocateAlpha(${"rchrom".$i}[$z/4], ${"gchrom".$i}[$z/4], ${"bchrom".$i}[$z/4], ${"achrom".$i}[$z/4]);
  68.             $im[$i]->filledPolygon($poly,$col);
  69.         }
  70.     }
  71. }
  72.  
  73. sub score_images {
  74.     for($i = 1; $i <= 10; $i++){
  75.         $score[$i] = 0;
  76.         foreach $x ( 0 .. $width ) {
  77.             foreach $y ( 0 .. $height ) {
  78.                 ($index1) = $timage->getPixel($x,$y);
  79.                 ($index2) = $im[$i]->getPixel($x,$y);
  80.                 ($r1,$g1,$b1) = $timage->rgb($index1);
  81.                 ($r2,$g2,$b2) = $im[$i]->rgb($index2);
  82.                 $score[$i] += ($r1 - $r2)**2;
  83.                 $score[$i] += ($g1 - $g2)**2;
  84.                 $score[$i] += ($b1 - $b2)**2;
  85.             }
  86.         }
  87.         if($score[$i] < $best){
  88.             $best = $score[$i];
  89.             $savedIndex = $i;
  90.         }
  91.     }
  92. }
  93.  
  94. sub output_best {
  95.     print $generation.",".$score[$savedIndex]."\n";
  96.     open FILE, ">".$generation."\.png" or die $!;
  97.     binmode FILE;
  98.     print FILE $im[$savedIndex]->png;
  99.     close FILE;
  100. }
  101.  
  102. sub soft_mutate_x {
  103.     $randomchrom = int(rand(10)) + 1;
  104.     while($randomchrom == $savedIndex){
  105.         $randomchrom = int(rand(10)) + 1;
  106.     }
  107.     $randlocus = int(rand(200));
  108.     ${"xchrom".$randomchrom}[$randlocus] += int(rand(35));
  109.     ${"xchrom".$randomchrom}[$randlocus] = ${"xchrom".$randomchrom}[$randlocus] % ($width + 1);
  110. }
  111.  
  112. sub soft_mutate_y {
  113.     $randomchrom = int(rand(10)) + 1;
  114.     while($randomchrom == $savedIndex){
  115.         $randomchrom = int(rand(10)) + 1;
  116.     }
  117.     $randlocus = int(rand(200));
  118.     ${"ychrom".$randomchrom}[$randlocus] += int(rand(35));
  119.     ${"ychrom".$randomchrom}[$randlocus] = ${"ychrom".$randomchrom}[$randlocus] % ($height + 1);
  120. }
  121.  
  122. sub soft_mutate_r {
  123.     $randomchrom = int(rand(10)) + 1;
  124.     while($randomchrom == $savedIndex){
  125.         $randomchrom = int(rand(10)) + 1;
  126.     }
  127.     $randlocus = int(rand(50));
  128.     ${"rchrom".$randomchrom}[$randlocus] += int(rand(35));
  129.     ${"rchrom".$randomchrom}[$randlocus] = ${"rchrom".$randomchrom}[$randlocus] % 256;
  130. }
  131.  
  132. sub soft_mutate_g {
  133.     $randomchrom = int(rand(10)) + 1;
  134.     while($randomchrom == $savedIndex){
  135.         $randomchrom = int(rand(10)) + 1;
  136.     }
  137.     $randlocus = int(rand(50));
  138.     ${"gchrom".$randomchrom}[$randlocus] += int(rand(35));
  139.     ${"gchrom".$randomchrom}[$randlocus] = ${"gchrom".$randomchrom}[$randlocus] % 256;
  140. }
  141.  
  142. sub soft_mutate_b {
  143.     $randomchrom = int(rand(10)) + 1;
  144.     while($randomchrom == $savedIndex){
  145.         $randomchrom = int(rand(10)) + 1;
  146.     }
  147.     $randlocus = int(rand(50));
  148.     ${"bchrom".$randomchrom}[$randlocus] += int(rand(35));
  149.     ${"bchrom".$randomchrom}[$randlocus] = ${"bchrom".$randomchrom}[$randlocus] % 256;
  150. }
  151.  
  152. sub soft_mutate_a {
  153.     $randomchrom = int(rand(10)) + 1;
  154.     while($randomchrom == $savedIndex){
  155.         $randomchrom = int(rand(10)) + 1;
  156.     }
  157.     $randlocus = int(rand(50));
  158.     ${"achrom".$randomchrom}[$randlocus] += int(rand(35));
  159.     ${"achrom".$randomchrom}[$randlocus] = ${"achrom".$randomchrom}[$randlocus] % 128;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement