The idea is to start with a goal string
g of upper case
letters and spaces, for example
METHINKS IT IS LIKE A WEASELAn initial string of the same length is chosen randomly as the first parent,
p. Unless you are really lucky this string will not resemble the goal string.
g,...,g[n]are the ascii codes of the characters of
p,...,p[n]are the codes for
pthen the closeness of
gis defined as
closeness(p, g) = abs(p - g) + ... + abs(p[n] - g[n])This has the useful property that
closeness(p,g) = 0if and only if
p = g
c = p with one character replaced by a random letter or space
Choose a goal string g Choose an inital parent p randomly while (closeness(p,g) > 0) Choose new p = mutation of p end whileYou can try this with the applet below by choosing
1as the number of children and pressing the start button. The applet uses
METHINKS IT IS A WEASELas a default goal string. You will quickly discover that there is no convergence to the goal string. This is clear since with only one child per generation we have no natural selection, only random chance. Click the suspend button to stop the applet.
You may think that the goal string is too long so type a smaller one such
HELLO and press the start button. Again the chances of
producing the goal string is pretty slim.
Choose the number of children, n, per generation Choose a goal string g Choose an initial parent p randomly while (closeness(p,g) > 0) Choose n children c, ... c[n-1] as mutations of p Choose new p as the closest child to g end whileNow with a sufficient number of children we obtain rapid evolution to the goal string. For example, try 100 children in the applet and the default goal string. Convergence is obtained in 50 to 60 generations. Experiment with different strings and numbers of children.