Package Bio :: Package GA :: Package Mutation :: Module General
[hide private]
[frames] | no frames]

Source Code for Module Bio.GA.Mutation.General

 1  """General functionality for mutations. 
 2  """ 
 3  # standard library 
 4  import random 
 5   
 6  # local stuff 
 7  from Bio.GA.Organism import Organism 
 8   
 9   
10 -class SafeFitnessMutation(object):
11 """Perform mutations, but do not allow decreases in organism fitness. 12 13 This doesn't actually do any mutation work, but just checks that 14 newly create organisms do not have lower fitnesses. 15 """
16 - def __init__(self, actual_mutation, accept_less = 0.0):
17 """Initialize to do safe mutations 18 19 Arguments: 20 21 o actual_mutation - A Mutation class which actually implements 22 mutation. functionality. 23 24 o accept_less - A probability to accept mutations which 25 generate lower fitness. This allows you to accept some 26 crossovers which reduce fitness, but not all of them. 27 """ 28 self._mutation = actual_mutation 29 self._accept_less_percent = accept_less 30 self._accept_less_rand = random.Random()
31
32 - def mutate(self, org):
33 """Perform safe mutation of the specified organism. 34 """ 35 new_org = self._mutation.mutate(org) 36 new_org.recalculate_fitness() 37 38 if org.fitness > new_org.fitness: 39 accept_less_chance = self._accept_less_rand.random() 40 if accept_less_chance <= self._accept_less_percent: 41 return new_org 42 else: 43 return org 44 else: 45 return new_org
46