DawkinsWeasel

NAME

Algorithm::DawkinsWeasel - An Illustration of Cumulative Selection

SYNOPSIS

use Algorithm::DawkinsWeasel;
my $weasel = Algorithm::DawkinsWeasel.new(
    target-phrase      => 'METHINKS IT IS LIKE A WEASEL',
    mutation-threshold => 0.05,
    copies             => 100,
  );
for $weasel.evolution {
    say .count.fmt('%04d '), .current-phrase, ' [', .hi-score, ']';
  }

DESCRIPTION

Algorithm::DawkinsWeasel is a simple model illustrating the idea of cumulative selection in evolution.

The original form of it looked like this:

1. Start with a random string of 28 characters.
  2. Make 100 copies of this string, with a 5% chance per character of that
     character being replaced with a random character.
  3. Compare each new string with "METHINKS IT IS LIKE A WEASEL", and give
     each a score (the number of letters in the string that are correct and
     in the correct position).
  4. If any of the new strings has a perfect score (== 28), halt.
  5. Otherwise, take the highest scoring string, and go to step 2

This module parametrizes the target string, mutation threshold, and number of copies per round.

METHODS

new(target-phrase => Str, mutation-threshold => Rat, copies => Int)

Creates a new Algorithm::DawkinsWeasel object.
  • target-phrase

A string of characters in the set A-Z plus spaces.  This defaults to
  "METHINKS IT IS LIKE A WEASEL"
  • mutation-threshold

The percentage chance per round that a character in the phrase will "mutate",
  i.e will change to another random character, expressed as a rational number
  between 0 and 1.  This defaults to 0.05 (5%).
  • copies

The amount of copies of the phrase which will be made in each round.  This
  defaults to 100.

Seq evolution()

This is the main method in this class.  Each iteration of the returned
  sequence represents one round of the algorithm until the target phrase is
  reached.

Int copies()

Returns the number of copies of the phrase made in each round as set in the
  constructor.

Int count()

Returns the number of rounds of the algorithm which have taken place.

Str current-phrase()

Returns the current state of the phrase including any mutations that have
  taken place.

Int hi-score()

During each round of the algorithm, each copy of the current phrase will be
  given a score of +1 for every letter which is the same as the similarly
  placed letter in the target phrase.  This method will return the value of
  the highest score.  When the high score equals the length of the target
  phrase, you will know the algorithm has ended successfully.

Rat mutation-threshold()

Returns the percentage chance of a letter mutating per round as set in the
  constructor.

Str target-phrase()

Returns the phrase the algorithm is trying to evolve towards as set in the
  constructor.

SEE ALSO

Weasel Program at Wikipedia

AUTHOR

Jaldhar H. Vyas <[email protected]>

COPYRIGHT AND LICENSE

Copyright (C) 2017, Consolidated Braincells Inc. All rights reserved.

This distribution is free software; you can redistribute it and/or modify it under the terms of either:

a) the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version, or

b) the Artistic License version 2.0.

The full text of the license can be found in the LICENSE file included with this distribution.

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.