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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Evolutionary algorithm step by step in the Seed7 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 Seed7 programming language

Source code in the seed7 programming language

$ include "seed7_05.s7i";

const string: table is "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";

const func integer: unfitness (in string: a, in string: b) is func
  result
    var integer: sum is 0;
  local
    var integer: index is 0;
  begin
    for index range 1 to length(a) do
      sum +:= ord(a[index] <> b[index]);
    end for;
  end func;

const proc: mutate (in string: a, inout string: b) is func
  local
    var integer: index is 0;
  begin
    b := a;
    for index range 1 to length(a) do
      if rand(1, 15) = 1 then
        b @:= [index] table[rand(1, 27)];
      end if;
    end for;
  end func;

const proc: main is func
  local
     const string: target is "METHINKS IT IS LIKE A WEASEL";
     const integer: OFFSPRING is 30;
     var integer: index is 0;
     var integer: unfit is 0;
     var integer: best is 0;
     var integer: bestIndex is 0;
     var integer: generation is 1;
     var string: parent is " " mult length(target);
     var array string: children is OFFSPRING times " " mult length(target);
  begin
    for index range 1 to length(target) do
      parent @:= [index] table[rand(1, 27)];
    end for;
    repeat
      for index range 1 to OFFSPRING do
        mutate(parent, children[index]);
      end for;
      best := succ(length(parent));
      bestIndex := 0;
      for index range 1 to OFFSPRING do
        unfit := unfitness(target, children[index]);
        if unfit < best then
          best := unfit;
          bestIndex := index;
        end if;
      end for; 
      if bestIndex <> 0 then
        parent := children[bestIndex];
      end if;
      writeln("generation " <& generation <& ": score " <& best <& ": " <& parent);
      incr(generation);
    until best = 0;
  end func;

  

You may also check:How to resolve the algorithm Loops/Infinite step by step in the HicEst programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Call a function step by step in the Factor programming language
You may also check:How to resolve the algorithm Isqrt (integer square root) of X step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Greyscale bars/Display step by step in the Ring programming language