Advertisement
Guest User

ml

a guest
Aug 29th, 2012
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 9.86 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use GD;
  3. #use strict;
  4. use warnings;
  5.  
  6. main();
  7. my $best;
  8. my $prevbest;
  9. my $numreps;
  10. my $generation;
  11. my $timage;
  12. my $width;
  13. my $height;
  14. my @im;
  15. my @score;
  16. my $savedIndex;
  17. my $population;
  18. my $polygons;
  19. my $points;
  20. my @touched;
  21. my $firstrun;
  22.  
  23. sub main {
  24.     $firstrun = 1;
  25.     get_dimensions();
  26.     initialise();
  27.     generate_genes();
  28.     while(1){
  29.         draw_images();
  30.         score_images();
  31.         $firstrun = 0;
  32.         if($best < $prevbest){
  33.             $prevbest = $best;
  34.             output_best();
  35.         }
  36.        
  37.         #soft mutations
  38.         $numreps = int(rand(20));
  39.         for(my $m = 0; $m <= $numreps; $m++){
  40.             if(int(rand(100)) < 15){ soft_mutate_x(); }
  41.             if(int(rand(100)) < 15){ soft_mutate_y(); }
  42.             if(int(rand(100)) < 15){ soft_mutate_r(); }
  43.             if(int(rand(100)) < 15){ soft_mutate_g(); }
  44.             if(int(rand(100)) < 15){ soft_mutate_b(); }
  45.             if(int(rand(100)) < 15){ soft_mutate_a(); }
  46.         }
  47.        
  48.         #hard mutations
  49.         $numreps = int(rand(20));
  50.         for(my $m = 0; $m <= $numreps; $m++){
  51.             if(int(rand(100)) < 5){ hard_mutate_x(); }
  52.             if(int(rand(100)) < 5){ hard_mutate_y(); }
  53.             if(int(rand(100)) < 5){ hard_mutate_r(); }
  54.             if(int(rand(100)) < 5){ hard_mutate_g(); }
  55.             if(int(rand(100)) < 5){ hard_mutate_b(); }
  56.             if(int(rand(100)) < 5){ hard_mutate_a(); }
  57.         }
  58.        
  59.         $numreps = int(rand($population/2));
  60.         for(my $m = 0; $m <= $numreps; $m++){
  61.         #if(int(rand(100)) < 100){
  62.             crossover();
  63.         #}
  64.         }
  65.         $generation++;
  66.     }
  67. }
  68.  
  69. sub generate_genes {
  70.     for(my $i = 1; $i <= $population; $i++){
  71.         for(my $d = 1; $d <= $polygons; $d++){
  72.             for(my $c = 1; $c <= $points; $c++){
  73.                 push(@{"xchrom".$i}, int(rand($width + 1)));
  74.                 push(@{"ychrom".$i}, int(rand($height + 1)));
  75.             }
  76.             for(my $c = 1; $c <= 3; $c++){
  77.                 push(@{"rchrom".$i}, int(rand(256)));
  78.                 push(@{"gchrom".$i}, int(rand(256)));
  79.                 push(@{"bchrom".$i}, int(rand(256)));
  80.             }
  81.             push(@{"achrom".$i}, int(rand(128)));
  82.         }
  83.     }
  84. }
  85.  
  86. sub get_dimensions {
  87.     $timage = GD::Image->newFromPng("ml.png");
  88.     ($width, $height) = $timage->getBounds;
  89. }
  90.  
  91. sub initialise {
  92.     $generation = 1;
  93.     $best = 195010 * $width * $height;
  94.     $prevbest = 195010 * $width * $height;
  95.     $population = 5;
  96.     $polygons = 20;
  97.     $points = 4;
  98. }
  99.  
  100. sub draw_images {
  101.     for(my $i = 1; $i <= $population; $i++){
  102.         $im[$i] = new GD::Image($width,$height,1);
  103.         for(my $z = 0; $z <= ($polygons * $points) - $points; $z+=$points){
  104.             my $poly = new GD::Polygon;
  105.             $poly->addPt(${"xchrom".$i}[$z], ${"ychrom".$i}[$z]);
  106.             $poly->addPt(${"xchrom".$i}[$z+1], ${"ychrom".$i}[$z+1]);
  107.             $poly->addPt(${"xchrom".$i}[$z+2], ${"ychrom".$i}[$z+2]);
  108.             $poly->addPt(${"xchrom".$i}[$z+3], ${"ychrom".$i}[$z+3]);
  109.             my $col = $im[$i]->colorAllocateAlpha(${"rchrom".$i}[$z/$points], ${"gchrom".$i}[$z/$points], ${"bchrom".$i}[$z/$points], ${"achrom".$i}[$z/$points]);
  110.             $im[$i]->filledPolygon($poly,$col);
  111.         }
  112.     }
  113. }
  114.  
  115. sub score_images {
  116.     for(my $i = 1; $i <= $population; $i++){
  117.         my $flag = 1;
  118.         for(my $l = 0; $l < @touched; $l++){
  119.             if($i == $touched[$l]) { $flag = 0; }
  120.         }
  121.         if($flag == 0 || $firstrun == 1){
  122.         $score[$i] = 0;
  123.         foreach my $x ( 0 .. $width ) {
  124.             foreach my $y ( 0 .. $height ) {
  125.                 my ($index1) = $timage->getPixel($x,$y);
  126.                 my ($index2) = $im[$i]->getPixel($x,$y);
  127.                 my ($r1,$g1,$b1) = $timage->rgb($index1);
  128.                 my ($r2,$g2,$b2) = $im[$i]->rgb($index2);
  129.                 $score[$i] += ($r1 - $r2)**2;
  130.                 $score[$i] += ($g1 - $g2)**2;
  131.                 $score[$i] += ($b1 - $b2)**2;
  132.             }
  133.         }
  134.         if($score[$i] < $best){
  135.             $best = $score[$i];
  136.             $savedIndex = $i;
  137.         }
  138.         }
  139.     }
  140.     @touched = ();
  141. }
  142.  
  143. sub output_best {
  144.     print $generation.",".$score[$savedIndex]."\n";
  145.     open FILE, ">".$generation."\.png" or die $!;
  146.     binmode FILE;
  147.     print FILE $im[$savedIndex]->png;
  148.     close FILE;
  149.     save_genes();
  150. }
  151.  
  152. sub soft_mutate_x {
  153.     my $randomchrom = int(rand($population)) + 1;
  154.     while($randomchrom == $savedIndex){
  155.         $randomchrom = int(rand($population)) + 1;
  156.     }
  157.     push(@touched, $randomchrom);
  158.     my $randlocus = int(rand($polygons * $points));
  159.     ${"xchrom".$randomchrom}[$randlocus] += int(rand($points));
  160.     ${"xchrom".$randomchrom}[$randlocus] = ${"xchrom".$randomchrom}[$randlocus] % ($width + 1);
  161. }
  162.  
  163. sub soft_mutate_y {
  164.     my $randomchrom = int(rand($population)) + 1;
  165.     while($randomchrom == $savedIndex){
  166.         $randomchrom = int(rand($population)) + 1;
  167.     }
  168.     push(@touched, $randomchrom);
  169.     my $randlocus = int(rand($polygons * $points));
  170.     ${"ychrom".$randomchrom}[$randlocus] += int(rand(4));
  171.     ${"ychrom".$randomchrom}[$randlocus] = ${"ychrom".$randomchrom}[$randlocus] % ($height + 1);
  172. }
  173.  
  174. sub soft_mutate_r {
  175.     my $randomchrom = int(rand($population)) + 1;
  176.     while($randomchrom == $savedIndex){
  177.         $randomchrom = int(rand($population)) + 1;
  178.     }
  179.     push(@touched, $randomchrom);
  180.     my $randlocus = int(rand($polygons));
  181.     if(${"rchrom".$randomchrom}[$randlocus] > 3){
  182.         ${"rchrom".$randomchrom}[$randlocus] += int(rand(7)) - 3;
  183.     }
  184.     else
  185.     {
  186.         ${"rchrom".$randomchrom}[$randlocus] += int(rand(4));
  187.     }
  188.     ${"rchrom".$randomchrom}[$randlocus] = ${"rchrom".$randomchrom}[$randlocus] % 256;
  189. }
  190.  
  191. sub soft_mutate_g {
  192.     my $randomchrom = int(rand($population)) + 1;
  193.     while($randomchrom == $savedIndex){
  194.         $randomchrom = int(rand($population)) + 1;
  195.     }
  196.     push(@touched, $randomchrom);
  197.     my $randlocus = int(rand($polygons));
  198.     if(${"gchrom".$randomchrom}[$randlocus] > 3){
  199.         ${"gchrom".$randomchrom}[$randlocus] += int(rand(7)) - 3;
  200.     }
  201.     else
  202.     {
  203.         ${"gchrom".$randomchrom}[$randlocus] += int(rand(4));
  204.     }
  205.     ${"gchrom".$randomchrom}[$randlocus] = ${"gchrom".$randomchrom}[$randlocus] % 256;
  206. }
  207.  
  208. sub soft_mutate_b {
  209.     my $randomchrom = int(rand($population)) + 1;
  210.     while($randomchrom == $savedIndex){
  211.         $randomchrom = int(rand($population)) + 1;
  212.     }
  213.     push(@touched, $randomchrom);
  214.     my $randlocus = int(rand($polygons));
  215.     if(${"bchrom".$randomchrom}[$randlocus] > 3){
  216.         ${"bchrom".$randomchrom}[$randlocus] += int(rand(7)) - 3;
  217.     }
  218.     else
  219.     {
  220.         ${"bchrom".$randomchrom}[$randlocus] += int(rand(4));
  221.     }
  222.     ${"bchrom".$randomchrom}[$randlocus] = ${"bchrom".$randomchrom}[$randlocus] % 256;
  223. }
  224.  
  225. sub soft_mutate_a {
  226.     my $randomchrom = int(rand($population)) + 1;
  227.     while($randomchrom == $savedIndex){
  228.         $randomchrom = int(rand($population)) + 1;
  229.     }
  230.     push(@touched, $randomchrom);
  231.     my $randlocus = int(rand($polygons));
  232.     if(${"achrom".$randomchrom}[$randlocus] > 3){
  233.         ${"achrom".$randomchrom}[$randlocus] += int(rand(7)) - 3;
  234.     }
  235.     else
  236.     {
  237.         ${"achrom".$randomchrom}[$randlocus] += int(rand(4));
  238.     }
  239.     ${"achrom".$randomchrom}[$randlocus] = ${"achrom".$randomchrom}[$randlocus] % 128;
  240. }
  241.  
  242. sub hard_mutate_x {
  243.     my $randomchrom = int(rand($population)) + 1;
  244.     while($randomchrom == $savedIndex){
  245.         $randomchrom = int(rand($population)) + 1;
  246.     }
  247.     push(@touched, $randomchrom);
  248.     my $randlocus = int(rand($polygons*$points));
  249.     ${"xchrom".$randomchrom}[$randlocus] = int(rand($width + 1));
  250. }
  251.  
  252. sub hard_mutate_y {
  253.     my $randomchrom = int(rand($population)) + 1;
  254.     while($randomchrom == $savedIndex){
  255.         $randomchrom = int(rand($population)) + 1;
  256.     }
  257.     push(@touched, $randomchrom);
  258.     my $randlocus = int(rand($polygons*$points));
  259.     ${"ychrom".$randomchrom}[$randlocus] = int(rand($height + 1));
  260. }
  261.  
  262. sub hard_mutate_r {
  263.     my $randomchrom = int(rand($population)) + 1;
  264.     while($randomchrom == $savedIndex){
  265.         $randomchrom = int(rand($population)) + 1;
  266.     }
  267.     push(@touched, $randomchrom);
  268.     my $randlocus = int(rand($polygons));
  269.     ${"rchrom".$randomchrom}[$randlocus] = int(rand(256));
  270. }
  271.  
  272. sub hard_mutate_g {
  273.     my $randomchrom = int(rand($population)) + 1;
  274.     while($randomchrom == $savedIndex){
  275.         $randomchrom = int(rand($population)) + 1;
  276.     }
  277.     push(@touched, $randomchrom);
  278.     my $randlocus = int(rand($polygons));
  279.     ${"gchrom".$randomchrom}[$randlocus] = int(rand(256));
  280. }
  281.  
  282. sub hard_mutate_b {
  283.     my $randomchrom = int(rand($population)) + 1;
  284.     while($randomchrom == $savedIndex){
  285.         $randomchrom = int(rand($population)) + 1;
  286.     }
  287.     push(@touched, $randomchrom);
  288.     my $randlocus = int(rand($polygons));
  289.     ${"bchrom".$randomchrom}[$randlocus] = int(rand(256));
  290. }
  291.  
  292. sub hard_mutate_a {
  293.     my $randomchrom = int(rand($population)) + 1;
  294.     while($randomchrom == $savedIndex){
  295.         $randomchrom = int(rand($population)) + 1;
  296.     }
  297.     push(@touched, $randomchrom);
  298.     my $randlocus = int(rand($polygons));
  299.     ${"achrom".$randomchrom}[$randlocus] = int(rand(128));
  300. }
  301.  
  302. sub crossover {
  303.     my $randomchrom = int(rand($population)) + 1;
  304.     while($randomchrom == $savedIndex){
  305.         $randomchrom = int(rand($population)) + 1;
  306.     }
  307.     push(@touched, $randomchrom);
  308.     my $randlocus = int(rand($polygons * $points));
  309.     for(my $i = 0; $i <= $randlocus; $i++){
  310.         ${"xchrom".$randomchrom}[$i] = ${"xchrom".$savedIndex}[$i];
  311.         ${"ychrom".$randomchrom}[$i] = ${"ychrom".$savedIndex}[$i];
  312.     }
  313.     $randlocus = int(rand($polygons));
  314.     for(my $i = 0; $i <= $randlocus; $i++){
  315.         ${"rchrom".$randomchrom}[$i] = ${"rchrom".$savedIndex}[$i];
  316.         ${"gchrom".$randomchrom}[$i] = ${"gchrom".$savedIndex}[$i];
  317.         ${"bchrom".$randomchrom}[$i] = ${"bchrom".$savedIndex}[$i];
  318.         ${"achrom".$randomchrom}[$i] = ${"achrom".$savedIndex}[$i];
  319.     }
  320. }
  321.  
  322. sub save_genes(){
  323.     system("del genes.txt");
  324.     open FILE, ">genes.txt";
  325.     print FILE "xchrom: ";
  326.     for(my $i = 0; $i < @{"xchrom".$savedIndex}; $i++){
  327.         print FILE ${"xchrom".$savedIndex}[$i].",";
  328.     }
  329.     print FILE "\n";
  330.     print FILE "ychrom: ";
  331.     for(my $i = 0; $i < @{"ychrom".$savedIndex}; $i++){
  332.         print FILE ${"ychrom".$savedIndex}[$i].",";
  333.     }
  334.     print FILE "\n";
  335.     print FILE "rchrom: ";
  336.     for(my $i = 0; $i < @{"rchrom".$savedIndex}; $i++){
  337.         print FILE ${"rchrom".$savedIndex}[$i].",";
  338.     }
  339.     print FILE "\n";
  340.     print FILE "gchrom: ";
  341.     for(my $i = 0; $i < @{"gchrom".$savedIndex}; $i++){
  342.         print FILE ${"gchrom".$savedIndex}[$i].",";
  343.     }
  344.     print FILE "\n";
  345.     print FILE "bchrom: ";
  346.     for(my $i = 0; $i < @{"bchrom".$savedIndex}; $i++){
  347.         print FILE ${"bchrom".$savedIndex}[$i].",";
  348.     }
  349.     print FILE "\n";
  350.     print FILE "achrom: ";
  351.     for(my $i = 0; $i < @{"achrom".$savedIndex}; $i++){
  352.         print FILE ${"achrom".$savedIndex}[$i].",";
  353.     }
  354.     print FILE "\n";
  355.     close FILE;
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement