How to resolve the algorithm Evolutionary algorithm step by step in the Perl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Evolutionary algorithm step by step in the Perl programming language

Table of Contents

Problem Statement

Starting with:

Note: to aid comparison, try and ensure the variables and functions mentioned in the task description appear in solutions

A cursory examination of a few of the solutions reveals that the instructions have not been followed rigorously in some solutions. Specifically, Note that some of the the solutions given retain characters in the mutated string that are correct in the target string. However, the instruction above does not state to retain any of the characters while performing the mutation. Although some may believe to do so is implied from the use of "converges" Strictly speaking, the new parent should be selected from the new pool of mutations, and then the new parent used to generate the next set of mutations with parent characters getting retained only by not being mutated. It then becomes possible that the new set of mutations has no member that is fitter than the parent! As illustration of this error, the code for 8th has the following remark. NOTE: this has been changed, the 8th version is completely random now Clearly, this algo will be applying the mutation function only to the parent characters that don't match to the target characters! To ensure that the new parent is never less fit than the prior parent, both the parent and all of the latest mutations are subjected to the fitness test to select the next parent.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Evolutionary algorithm step by step in the Perl programming language

Source code in the perl programming language

use List::Util 'reduce';
use List::MoreUtils 'false';

### Generally useful declarations

sub randElm
 {$_[int rand @_]}

sub minBy (&@)
 {my $f = shift;
  reduce {$f->($b) < $f->($a) ? $b : $a} @_;}

sub zip
 {@_ or return ();
  for (my ($n, @a) = 0 ;; ++$n)
    {my @row;
     foreach (@_)
        {$n < @$_ or return @a;
         push @row, $_->[$n];}
     push @a, \@row;}}

### Task-specific declarations

my $C = 100;
my $mutation_rate = .05;
my @target = split '', 'METHINKS IT IS LIKE A WEASEL';
my @valid_chars = (' ', 'A' .. 'Z');

sub fitness
 {false {$_->[0] eq $_->[1]} zip shift, \@target;}

sub mutate
 {my $rate = shift;
  return [map {rand() < $rate ? randElm @valid_chars : $_} @{shift()}];}

### Main loop

my $parent = [map {randElm @valid_chars} @target];

while (fitness $parent)
   {$parent =
       minBy \&fitness,
       map {mutate $mutation_rate, $parent}
       1 .. $C;
    print @$parent, "\n";}


  

You may also check:How to resolve the algorithm 100 prisoners step by step in the C# programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the Haskell programming language
You may also check:How to resolve the algorithm Show ASCII table step by step in the AWK programming language
You may also check:How to resolve the algorithm Dynamic variable names step by step in the Scheme programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Swift programming language