Starting with:

 The target string: "METHINKS IT IS LIKE A WEASEL".
 An array of random characters chosen from the set of upper-case letters together with the space, and of the same length as the target string. (Call it the parent).
 A fitness function that computes the ‘closeness’ of its argument to the target string.
 A mutate function that given a string and a mutation rate returns a copy of the string, with some characters probably mutated.
 While the parent is not yet the target:
 copy the parent C times, each time allowing some random probability that another character might be substituted using mutate.
 Assess the fitness of the parent and all the copies to the target and make the most fit string the new parent, discarding the others.
 repeat until the parent converges, (hopefully), to the target.


See also

   Wikipedia entry:   Weasel algorithm.
   Wikipedia entry:   Evolutionary algorithm.

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,

 While the parent is not yet the target:
 copy the parent C times, each time allowing some random probability that another character might be substituted using mutate.
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"

(:* repeat until the parent converges, (hopefully), to the target.

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.

Create a new string based on the TOS, changing randomly any characters which
don't already match the target:

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.




' version 01-07-2018
' compile with: fbc -s console
 
Randomize Timer
Const As UInteger children = 100
Const As Double mutate_rate = 0.05
 
Function fitness(target As String, tmp As String) As UInteger
 
    Dim As UInteger x, f
 
    For x = 0 To Len(tmp) -1
        If tmp[x] = target[x] Then f += 1
    Next
    Return f
 
End Function
 
Sub mutate(tmp As String, chars As String, mute_rate As Double)
 
    If Rnd <= mute_rate Then
        tmp[Int(Rnd * Len(tmp))] = chars[Int(Rnd * Len(chars))]
    End If
 
End Sub
 
' ------=< MAIN >=------
 
Dim As String target = "METHINKS IT IS LIKE A WEASEL"
Dim As String chars  = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim As String parent, mutation()
Dim As UInteger x, iter, f, fit(), best_fit, parent_fit
 
For x = 1 To Len(target)
    parent += Chr(chars[Int(Rnd * Len(chars))])
Next
 
f = fitness(target, parent)
parent_fit = f
best_fit = f
 
Print "iteration  best fit   Parent"
Print "=========  ========   ============================"
Print Using "     ####      ####   ";iter; best_fit;
Print parent
 
Do
    iter += 1
    ReDim mutation(1 To children),fit(1 To children)
 
    For x = 1 To children
        mutation(x) = parent
        mutate(mutation(x), chars, mutate_rate)
    Next
 
    For x = 1 To children
        If mutation(x) <> parent Then
            f = fitness(target, mutation(x))
            If best_fit < f Then
                best_fit = f
                fit(x) = f
            Else
                fit(x) = parent_fit
            End If
        End If
    Next
 
    If best_fit > parent_fit Then
        For x = 1 To children
            If fit(x) = best_fit Then
                parent = mutation(x)
                Print Using "     ####      ####   ";iter; best_fit;
                Print parent
            End If
        Next
    End If
 
Loop Until parent = target
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End