Psychic bug

Jennifer Hallinan j.s.hallinan at ncl.ac.uk
Wed Feb 22 14:04:48 EST 2006


Hi,

I have a mutate function for a genetic algorithm which is giving me
odd results. I suspect I'm missing somthing really simple, so I'd be
grateful for any suggestions. Basically, when I comment out the line
which is commented out below, it works fine (although of course it
doesn't change the genome). When I uncomment it gen[letter]
automagically gets the value base in the print statements, before the
assignment has been made. And it still doesn't update the genome.
Genome is a string of integers in the range 0- 3, hence the
conversion.

    def Mutate(self, mrate):
        g = Random(rseed)
        # If no mutation rate specified, use 1 per genome
        if mrate == -1:
            mrate = 1.0/len(self.genome)
        print "mrate is ", mrate

        print "original genome: "
        print self.genome
        # convert genome to a list
        gen = [ord(letter)-48 for letter in self.genome]

        for letter in range(len(gen)):
            rnum = g.random()
            if rnum < mrate:
                base = g.randint(0,3)
                print "base is ", base
                print "pos ", letter, gen[letter], " to ", base
#                gen[letter] = base

        # convert list back to a string
        self.genome = ''.join([str(x) for x in gen])
        print "final genome"
        print self.genome


Output with line commented out:
base is  1
pos  40 0  to  1
base is  0
pos  185 2  to  0
base is  3
pos  223 0  to  3

...etc

Output with line included:
base is  1
pos  40 1  to  1
base is  0
pos  185 0  to  0
base is  3
pos  223 3  to  3

Thanks,

Jen




More information about the Python-list mailing list