[Tutor] sorry, another update bug

Kent Johnson kent37 at tds.net
Thu Jan 13 12:07:54 CET 2005


The problem is your mutate_genome() function which mutates the genome in place. So all the 
living_species are using the same genome (they all reference the same list). The solution is to have 
mutate_genome() copy the genome. You can do this by adding the line
     a_genome = a_genome[:]
to the start of the function.

You could also rewrite mutate_genome() to use a list comprehension (which creates a new list). This 
might clean up the function a bit also:

def mutate_gene(gene, rate):
     if random.random() <= rate:
         return gene + 1
     else:
         return gene

def mutate_genome(a_genome, rate):
     "For each base in a genome there is a rate chance it is set to 1"
     new_genome = [ mutate_gene(gene, rate) for gene in a_genome ]
     return new_genome

Kent

Vincent Wan wrote:
> sorry to inflict another dumb bug on you all so soon...
> 
> I have written a program to replicated simulate the evolution of  
> species and their genomes based on David Raup and Stephen Jay Gould's  
> 1974 paper in systematic zoology 23: 305-322. The program correctly  
> evolves species and keeps track of relationships. The genome of the  
> species is mutated correctly (shown by the debug write lines) but all  
> species in the genomes in the living_species are updated to the last  
> mutated genome.
> 
> sample output:
> 
> parent species genome:            [0, 0,  0, 0, 0, 0, 0, 0,  0, 0]
> species  1 genome after mutation: [1, 1,  1, 1, 0, 1, 1, 0,  1, 1]
> parent species genome:            [1, 1,  1, 1, 0, 1, 1, 0,  1, 1]
> species  2 genome after mutation: [2, 1,  2, 1, 1, 1, 1, 0,  2, 2]
> parent species genome:            [2, 1,  2, 1, 1, 1, 1, 0,  2, 2]
> species  3 genome after mutation: [2, 2,  3, 1, 2, 1, 2, 0,  3, 2]
> parent species genome:            [2, 2,  3, 1, 2, 1, 2, 0,  3, 2]
> species  4 genome after mutation: [2, 2,  3, 2, 3, 1, 3, 0,  4, 2]
> parent species genome:            [2, 2,  3, 2, 3, 1, 3, 0,  4, 2]
> species  2 genome after mutation: [2, 2,  3, 2, 3, 2, 3, 1,  5, 3]
> parent species genome:            [2, 2,  3, 2, 3, 2, 3, 1,  5, 3]
> species  2 genome after mutation: [2, 3,  3, 2, 3, 3, 3, 2,  6, 4]
> parent species genome:            [2, 3,  3, 2, 3, 3, 3, 2,  6, 4]
> species  2 genome after mutation: [2, 3,  3, 2, 4, 4, 3, 3,  6, 5]
> parent species genome:            [2, 3,  3, 2, 4, 4, 3, 3,  6, 5]
> species  3 genome after mutation: [2, 4,  4, 2, 5, 4, 3, 4,  6, 5]
> parent species genome:            [2, 4,  4, 2, 5, 4, 3, 4,  6, 5]
> species  4 genome after mutation: [3, 4,  4, 2, 5, 4, 4, 4,  7, 5]
> parent species genome:            [3, 4,  4, 2, 5, 4, 4, 4,  7, 5]
> species  4 genome after mutation: [3, 4,  5, 2, 5, 5, 4, 4,  8, 6]
> parent species genome:            [3, 4,  5, 2, 5, 5, 4, 4,  8, 6]
> species  4 genome after mutation: [3, 5,  6, 2, 5, 6, 5, 4,  9, 7]
> parent species genome:            [3, 5,  6, 2, 5, 6, 5, 4,  9, 7]
> species  5 genome after mutation: [3, 6,  6, 2, 5, 7, 6, 5, 10, 7]
> parent species genome:            [3, 6,  6, 2, 5, 7, 6, 5, 10, 7]
> species  5 genome after mutation: [4, 7,  7, 3, 6, 7, 7, 6, 11, 7]
> parent species genome:            [4, 7,  7, 3, 6, 7, 7, 6, 11, 7]
> species  6 genome after mutation: [4, 8,  8, 4, 6, 8, 8, 6, 11, 8]
> parent species genome:            [4, 8,  8, 4, 6, 8, 8, 6, 11, 8]
> species  7 genome after mutation: [4, 9,  8, 5, 7, 8, 8, 6, 12, 8]
> parent species genome:            [4, 9,  8, 5, 7, 8, 8, 6, 12, 8]
> species  6 genome after mutation: [5, 10, 8, 6, 8, 9, 9, 7, 13, 9]
> 
> 15  species evolved
> living_species:
> species:  6 born time:  18 died time:  0 genome: [5, 10, 8, 6, 8, 9, 9,  
> 7, 13, 9]
> species:  8 born time:  29 died time:  0 genome: [5, 10, 8, 6, 8, 9, 9,  
> 7, 13, 9]
> species: 10 born time:  31 died time:  0 genome: [5, 10, 8, 6, 8, 9, 9,  
> 7, 13, 9]
> species: 14 born time:  39 died time:  0 genome: [5, 10, 8, 6, 8, 9, 9,  
> 7, 13, 9]
> species: 15 born time:  41 died time:  0 genome: [5, 10, 8, 6, 8, 9, 9,  
> 7, 13, 9]
> species: 16 born time:  43 died time:  0 genome: [5, 10, 8, 6, 8, 9, 9,  
> 7, 13, 9]
> 
> dead_species:
> species:  4 born time:   3 died time:   5 genome: [5, 10, 8, 6, 8, 9,  
> 9, 7, 13, 9]
> species:  3 born time:   1 died time:   8 genome: [5, 10, 8, 6, 8, 9,  
> 9, 7, 13, 9]
> species:  2 born time:   1 died time:   9 genome: [5, 10, 8, 6, 8, 9,  
> 9, 7, 13, 9]
> species:  1 born time:   1 died time:  17 genome: [5, 10, 8, 6, 8, 9,  
> 9, 7, 13, 9]
> species:  5 born time:  13 died time:  20 genome: [5, 10, 8, 6, 8, 9,  
> 9, 7, 13, 9]
> species:  0 born time:   0 died time:  30 genome: [5, 10, 8, 6, 8, 9,  
> 9, 7, 13, 9]
> species:  7 born time:  27 died time:  32 genome: [5, 10, 8, 6, 8, 9,  
> 9, 7, 13, 9]
> species:  9 born time:  30 died time:  36 genome: [5, 10, 8, 6, 8, 9,  
> 9, 7, 13, 9]
> species: 12 born time:  36 died time:  41 genome: [5, 10, 8, 6, 8, 9,  
> 9, 7, 13, 9]
> species: 11 born time:  33 died time:  42 genome: [5, 10, 8, 6, 8, 9,  
> 9, 7, 13, 9]
> species: 13 born time:  37 died time:  43 genome: [5, 10, 8, 6, 8, 9,  
> 9, 7, 13, 9]
> 
> tree:   
> (((0,((6,9),((7,((10,((13,16),14)),(11,(12,15)))),8))),4),((1,5),(2,3)))
> 
> code:
> 
> import random
> 
> debug = 1
> 
> living_species = [[0, 0, 0, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]]
> dead_species = []
> tree = "*0*"
> 
> current_time = 0
> current_species_counter = 0
> max_species = 16    # the number of species to be evolved + 1
> branching_probablity = .1
> extinction_probablity = .1
> mutation_rate = .5
> 
> def mutate_genome(a_genome, rate):
>     "For each base in a genome there is a rate chance it is set to 1"
>     current_base = 0
>     while current_base < len(a_genome):
>         if random.random() <= rate:
>             a_genome[current_base] = a_genome[current_base]+ 1
>         current_base += 1
>     return a_genome
> 
> while current_species_counter < max_species:
>     counter = 0
>     while counter < len(living_species):
>         if random.random() < branching_probablity:
>             current_species_counter += 1
>             current_species = [current_species_counter, current_time,  
> 0, living_species[counter][3]]
>             if debug: print 'parent species genome:',  
> living_species[counter][3]
>             living_species.append(current_species)
>             living_species[len(living_species)-1][3] =  
> mutate_genome(current_species[3], mutation_rate)
>             if debug: print 'species ', len(living_species)-1, 'genome  
> after mutation:', living_species[len(living_species)-1][3]
> 
>             # updates species tree
>             target = '*' + str(living_species[counter][0]) + '*'
>             replacement = '(' + target + ',*' + str(current_species[0])  
> + '*)'
>             tree = tree.replace(target, replacement)
>         counter += 1
> 
> #       if debug: print 'at time ', current_time, ' living_species: ',  
> [row[0] for row in living_species]
>         print 'at time ', current_time, ' living_species: ',  
> living_species
> 
>     counter = 0
>     while counter < len(living_species):
>         if random.random() < extinction_probablity:
>             newly_dead = living_species[counter]
>             newly_dead[2] = current_time
>             dead_species.append(newly_dead)
>             living_species.remove(living_species[counter])
>             if len(living_species) == 0:    # when the last  
> living_species goes extinct exit
>                 break
>         counter += 1
> 
> #       if debug: print 'at time ', current_time, ' dead_species : ',  
> [row[0] for row in dead_species]
>     if len(living_species) == 0:    # when the last living_species goes  
> extinct exit
>         break
>     current_time += 1
> 
> if current_species_counter < max_species:
>     print '\nall extinct with only ', current_species_counter + 1, '  
> species of ', max_species
> else:
>     print max_species - 1, ' species evolved'
> print 'living_species:'
> for each in living_species:
>     print 'species:', each[0], 'born time: ', each[1], 'died time: ',  
> each[2]
>     print 'genome:', each[3]
> print '\ndead_species: '
> for each in dead_species:
>     print 'species:', each[0], 'born time: ', each[1], 'died time: ',  
> each[2]
>     print 'genome:', each[3]
> tree = tree.replace('*','')
> print 'tree: ', tree
> 
> 
> Vincent
> 
> ------------------------------------------------------------------------ 
> --------------
> PhD Candidate
> Committee on the Conceptual and Historical Studies of Science
> University of Chicago
> 
> PO Box 73727
> Fairbanks, AK 99707
> 
> wan AT walrus DOT us (change CAPS to @ and . )
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list