82. Dawkins' weasel

This program is actually quite simple. The following code shows how the example solution initializes itself:

private Random Rand = new Random();
private string Target, Letters;
private int TargetLength;

private void Form1_Load(object sender, EventArgs e)
{
Target = "METHINKS IT IS LIKE A WEASEL";
TargetLength = Target.Length;
Letters = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
}

This code creates a Random object for use when generating strings. It defines variables to hold the target string, a string containing allowed characters (the letters A through Z, plus the space character), and the length of the target string.

The form's Load event handler sets the target string to METHINKS IT IS LIKE A WEASEL, saves the string's length, and initializes the Letters variable.

The following RunWeasel method performs the simulation:

// Perform the weasel transformation.
private void RunWeasel(int generationSize, double mutationProb)
{
// Start with a random string.
string parent = RandomString(Letters, TargetLength);

// Repeat until we find the target.
int error = FindError(parent, Target);
while (error > 0)
{
// Display this parent.
stepsListBox.Items.Add(error + ": " + parent);

// Process a generation.
int bestError = int.MaxValue;
string bestOffspring = "";
for (int i = 0; i < generationSize; i++)
{
// Make a mutated offspring.
string testOffspring = "";
foreach (char ch in parent)
{
if (Rand.NextDouble() <= mutationProb)
testOffspring += RandomLetter(Letters);
else
testOffspring += ch;
}

// See if this is an improvement.
int testError = FindError(testOffspring, Target)

if (testError < bestError)
{
bestError = testError;
bestOffspring = testOffspring;
}
}

// Replace the parent with the best offspring.
parent = bestOffspring;
error = bestError;
}
numStepsLabel.Text = $"# Steps:
{stepsListBox.Items.Count.ToString()}";

stepsListBox.Items.Add(error + ": " + parent);
stepsListBox.SelectedIndex = stepsListBox.Items.Count - 1;
}

This method calls the RandomString method described shortly to generate the initial string. It calls the FindError method, also described shortly, to see by how much that string differs from the target string.

The program then enters a loop that continues as long as the string in the parent variable does not match the target string. Inside the loop, the code adds the current parent string to a list box. It then creates a generation consisting of generationSize new child strings. The program makes each child string by copying the parent's characters, possibly mutating some randomly.

After it has created the new generation, the code saves the child closest to the target string in the parent variable and repeats the process until it finishes.

The following code shows how the program generates random strings and characters:

private string RandomString(string letters, int length)
{
string result = "";
for (int i = 0; i < length; i++)
result += RandomLetter(letters);
return result;
}

private char RandomLetter(string letters)
{
int index = Rand.Next(0, letters.Length);
return letters[index];
}

The RandomString method simply loops through the desired number of letters, calls the RandomLetter method to pick a random letter, and concatenates them.

The RandomLetter method simply picks a random letter from the letters string.

The following code shows the FindError method, which returns an indication of how far a test string is from the target string:

private int FindError(string string1, string string2)
{
int error = 0;
for (int i = 0; i < string1.Length; i++)
error += Math.Abs((int)string1[i] - (int)string2[i]);
return error;
}

This method just loops through the characters in two strings and subtracts the corresponding characters. It returns the sum of the characters' errors.

That's about all there is to this program. Download the DawkinsWeasel example solution to see additional details.

Richard Dawkins originally intended this simulation to show how simple selection could produce evolutionary results relatively quickly. However, the program presupposes a goal, in this case, "Methinks it is like a weasel." Real-world evolution does not have a predetermined goal, so the program fails in that respect. However, it is still a useful example of a simple simulation. It also demonstrates a very useful incremental improvement strategy where a program makes small random changes to a potential solution to try to find a better one.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset