1

I'm attempting a Python version of Richard Dawkin's Weasel Program that demonstrates the difference between random selection versus cumulative selection and am having some problems with my mutation algorithm. I'm thinking perhaps I don't understand how to use the Python random library. Here's the offending bit of code:

#Cumulative selection guesser
while counter <  2:
    for i in range(gen):
        for j in range(len(child)):
            mutation = random.randint(0,99)
            if mutation in range(5):
                 child[j] = random.choice(chars)
        offspring.append(child)
    counter += 1

"Counter" is used for testing and will be changed to a test for equality with the target phrase once the code is complete, "child" is a list of 28 random letters, "gen" is the number of generations and is just a constant number like 100 or 50, "chars" is a string of the uppercase alphabet and the space character, and "offspring" is an empty list I want to put all my generated "children" in. If this doesn't make sense I can include the rest of the code, but the main problem I have is that every child in each generation is exactly the same.

What should be happening is the code goes through each element in "child" rolls the dice for a mutation, if the mutation happens change that specific element in child to a new random character. Then the "child" gets added to the list of offspring. So the offspring should be something like:

DDSMMHYODHFZTZRKWQUQYGMLUDB

DDSMMHYODHFZTZRKWMUQYGMLUDB

DDSMIHYODHFZTZRKWQUQYGMLUDB

DPSMMHYODHFZTZRKWQUQYGMLQDB

(variations bolded)

times how many are specified by the variable "gen". Instead I get each child exactly the same with no variations, like this:

DDSMMHYODHFZTZRKWQUQYGMLUDB

DDSMMHYODHFZTZRKWQUQYGMLUDB

DDSMMHYODHFZTZRKWQUQYGMLUDB

DDSMMHYODHFZTZRKWQUQYGMLUDB

etc. I've spent a long time trying to figure out what the problem is and have tried testing each part of the code separately and it appears to function as I would expect. The only thing I can think is that the value for "mutation" is not changing because when "random.randint()" is called it only generates a random integer once. Is this how the random function works? I would expect that a new random integer would be generated for every iteration of the for loop.

If anyone has any thoughts or suggestions I would appreciate it. I'm banging my head against the wall here. Also, I can post the entire code if it helps. Thanks!

3
  • Python 2 or 3? You cannot change a string, btw.
    – Martijn Pieters
    Jan 1, 2013 at 9:58
  • What does print(mutation) show?
    – mmmmmm
    Jan 1, 2013 at 9:58
  • Apart from the fact that strings are immutable, your code should run fine. Check your assumptions about gen and counter with pdb or print statements / function calls.
    – Martijn Pieters
    Jan 1, 2013 at 10:02

1 Answer 1

1

Make a copy of child before you append it to offspring:

offspring.append(child[:])

child[:] returns a new list with the same contents as child. Since it is a new list, future mutations of child will not affect child[:].

If you don't do this, offspring will contain multiple references to the exact same list. So every mutation of child affects every item in offspring. Every item in offspring ends up equal to the last value of child.

1
  • This solved the problem, thanks so much! I guess I should go read up more on Python's list type as I obviously didn't understand it well enough. I appreciate the help, though, because I was at a point where I didn't know what I was not comprehending. Thanks again. Jan 1, 2013 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.