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

Source Code for Module Bio.GA.Mutation.General

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