Back to index

python-biopython  1.60
Public Member Functions | Private Attributes
test_GAQueens.QueensMutation Class Reference

List of all members.

Public Member Functions

def __init__
def mutate

Private Attributes

 _mutation_rate

Detailed Description

Mutation operation to help in the N-Queens problem.

This performs mutation, but instead of randomly mutating a single
item to any other, it tries to mutate it to a row that is not already
taken at some other position in the genome. This thus tries to
generate more 'correct' mutations that will help achieve the solution.

Definition at line 337 of file test_GAQueens.py.


Constructor & Destructor Documentation

def test_GAQueens.QueensMutation.__init__ (   self,
  mutation_rate = 0.001 
)
Inititialize a mutator.

Arguments:

o mutation_rate -- The change of a mutation happening at any
position in the genome.

Definition at line 345 of file test_GAQueens.py.

00345 
00346     def __init__(self, mutation_rate = 0.001):
00347         """Inititialize a mutator.
00348 
00349         Arguments:
00350 
00351         o mutation_rate -- The change of a mutation happening at any
00352         position in the genome.
00353         """
00354         self._mutation_rate = mutation_rate


Member Function Documentation

def test_GAQueens.QueensMutation.mutate (   self,
  organism 
)
Mutate the genome trying to put in 'helpful' mutations.

Definition at line 355 of file test_GAQueens.py.

00355 
00356     def mutate(self, organism):
00357         """Mutate the genome trying to put in 'helpful' mutations.
00358         """
00359         new_org = organism.copy()
00360         gene_choices = list(new_org.genome.alphabet.letters)
00361 
00362         # potentially mutate any gene in the genome
00363         for gene_index in range(len(new_org.genome)):
00364             mutation_chance = random.random()
00365             # if we have a mutation
00366             if mutation_chance <= self._mutation_rate:
00367                 # find only choices that are not already taken elsewhere
00368                 # in the genome
00369                 gene_choices = list(new_org.genome.alphabet.letters)
00370 
00371                 for gene in new_org.genome:
00372                     if gene in gene_choices:
00373                         gene_choices.remove(gene)
00374 
00375                 # if there are no choices left, we are stuck going for random
00376                 if len(gene_choices) == 0:
00377                     gene_choices = list(new_org.genome.alphabet.letters)
00378                 
00379                 # get a new letter with the left-over choices
00380                 new_letter = random.choice(gene_choices)
00381                 new_org.genome[gene_index] = new_letter
00382 
        return new_org

Member Data Documentation

Definition at line 353 of file test_GAQueens.py.


The documentation for this class was generated from the following file: