How to resolve the algorithm Evolutionary algorithm step by step in the Raku programming language
How to resolve the algorithm Evolutionary algorithm step by step in the Raku 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 Raku programming language
Source code in the raku programming language
constant target = "METHINKS IT IS LIKE A WEASEL";
constant @alphabet = flat 'A'..'Z',' ';
constant C = 10;
sub mutate(Str $string, Real $mutate-chance where 0 ≤ * < 1) {
$string.subst: /{ rand < $mutate-chance }> . /, @alphabet.pick, :global
}
sub fitness(Str $string) { [+] $string.comb Zeq target.comb }
printf "\r%6d: '%s'", $++, $_ for
@alphabet.roll(target.chars).join,
{ max :by(&fitness), mutate($_, .001) xx C } ... target;
print "\n";
You may also check:How to resolve the algorithm Ackermann function step by step in the WDTE programming language
You may also check:How to resolve the algorithm Find largest left truncatable prime in a given base step by step in the Maple programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Qi programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the R programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the PL/M programming language