diff options
| author | Skip Montanaro <[email protected]> | 2021-02-16 20:14:16 -0600 |
|---|---|---|
| committer | Skip Montanaro <[email protected]> | 2021-02-16 20:14:16 -0600 |
| commit | c2587c76f1b416cdbecb979e54941933246bf856 (patch) | |
| tree | bb61ee9128075ce22af4eafa232f13c2e5a07896 /lib/selection.py | |
| parent | d90761a005b24018ae237bf551515772a1de656f (diff) | |
| download | python-0.9.1-patched-QoL-c2587c76f1b416cdbecb979e54941933246bf856.tar.xz python-0.9.1-patched-QoL-c2587c76f1b416cdbecb979e54941933246bf856.zip | |
starting over
Diffstat (limited to 'lib/selection.py')
| -rw-r--r-- | lib/selection.py | 78 |
1 files changed, 39 insertions, 39 deletions
diff --git a/lib/selection.py b/lib/selection.py index 22a0ea9..48947cc 100644 --- a/lib/selection.py +++ b/lib/selection.py @@ -1,6 +1,6 @@ # DAWKINS' BLIND WATCHMAKER -- "METHINKS IT IS LIKE A WEASEL" EXAMPLE # -# Start with a random phrase. Successively: +# Start with a random phrase. Successively: # Breed a next generation by copying with small mutations. # Choose the member of the next generation that most closely resembles # the target phrase to start breeding the next generation. @@ -16,57 +16,57 @@ Alphabet = ' abcdefghijklmnopqrstuvwxyz' GenerationSize = 100 MutationChance = 1.0/28.0 -IndexSet = range(len(Target)) # Used to speed up loops +IndexSet = range(len(Target)) # Used to speed up loops def choice(sequence): - n = len(sequence) - i = int(random() * float(n)) % n - return sequence[i] + n = len(sequence) + i = int(random() * float(n)) % n + return sequence[i] def random_phrase(): - phrase = '' - for i in IndexSet: - phrase = phrase + choice(Alphabet) - return phrase + phrase = '' + for i in IndexSet: + phrase = phrase + choice(Alphabet) + return phrase def mutate_phrase(phrase): - mutant = phrase - for i in range(int(0.5 + MutationChance*float(len(phrase)))): - i = choice(IndexSet) - c = choice(Alphabet) - mutant = mutant[:i] + c + mutant[i+1:] - #print `mutant` - return mutant + mutant = phrase + for i in range(int(0.5 + MutationChance*float(len(phrase)))): + i = choice(IndexSet) + c = choice(Alphabet) + mutant = mutant[:i] + c + mutant[i+1:] + #print `mutant` + return mutant def matching_factor(phrase): - factor = 0 - for i in IndexSet: - if phrase[i] = Target[i]: factor = factor + 1 - return factor + factor = 0 + for i in IndexSet: + if phrase[i] = Target[i]: factor = factor + 1 + return factor def breed_generation(phrase): - generation = [phrase] - while len(generation) < GenerationSize: - generation.append(mutate_phrase(phrase)) - return generation + generation = [phrase] + while len(generation) < GenerationSize: + generation.append(mutate_phrase(phrase)) + return generation def select_best(generation): - factor, selected = -1, '' - for phrase in generation: - f = matching_factor(phrase) - if f > factor: - factor, selected = f, phrase - return selected + factor, selected = -1, '' + for phrase in generation: + f = matching_factor(phrase) + if f > factor: + factor, selected = f, phrase + return selected def main(): - gen = 0 - phrase = random_phrase() - print gen, `phrase` - while phrase <> Target: - next_generation = breed_generation(phrase) - #print next_generation - phrase = select_best(next_generation) - gen = gen+1 - print gen, `phrase` + gen = 0 + phrase = random_phrase() + print gen, `phrase` + while phrase <> Target: + next_generation = breed_generation(phrase) + #print next_generation + phrase = select_best(next_generation) + gen = gen+1 + print gen, `phrase` main() |
