0

I'm trying to make an evolution simulator and I'm having some trouble. I'm trying to add a "random mutation" to the numbers in a list but they wont actually add together.

def Evolve(evolution, creatures):
    print(creatures)
    creatureNumber = 0
    if evolution == 0:
        print("First Generation:")
        for i in creatures:
            creatureIndex = creatureNumber + 1
            creatureNumber += 1
            print("                    Creature", creatureIndex, ":", i)
            randomMutation = random.randint(-1, 2)
            i += randomMutation
    else:
        print("Evolution", evolution, ":")
        for i in creatures:
            creatureIndex = creatureNumber + 1
            creatureNumber += 1
            print("                    Creature", creatureIndex, ":", i)
            randomMutation = random.randint(-1, 2)
            i += randomMutation
    print("")
    print("Leading Creature:", creatures.index(max(creatures))+ 1,":", max(creatures))
    EvolveQuestion(evolution, creatures)

The list "creatures" is defined in a separate function. "i += randomMutation" is where it seems to just simply not add the numbers together.

Ryan Sciarabba
  • 1
  • 1
  • 1
  • 4

2 Answers2

1

I have already pointed out that you have forgot the equal sign in creatureNumber + 1, so you have updated your question with the corrected code, but are still not getting the expected result. OK. Here the next main issue your code has:

If you have a list like creatures = [1,1,1,1] and you want change it to [1,2,3,4] you have to do it as follows:

for i, _ in enumerate(creatures):

     creatures[i] = i+1

Now le's go back to coding.

It is very hard to guess what you want to achieve from the code you have provided, but I will try it just by chance anyway:

import random
creatures = [1,1,1,1]

def Evolve(evolution, creatures):
    print(creatures)
    creatureNumber = 0
    if evolution == 0:
        print("First Generation:")
        for i, item in enumerate(creatures):
            creatureIndex = creatureNumber + 1
            creatureNumber += 1
            print("                    Creature", creatureIndex, ":", creatures[i])
            randomMutation = random.randint(-1, 2)
            creatures[i] += randomMutation
    else:
        print("Evolution", evolution, ":")
        for i, item in enumerate(creatures):
            creatureIndex = creatureNumber + 1
            creatureNumber += 1
            print("                    Creature", creatureIndex, ":", creatures[i])
            randomMutation = random.randint(-1, 2)
            creatures[i] += randomMutation
    print("")
    print(creatures)    
    print("Leading Creature:", creatures.index(max(creatures))+ 1,":", max(creatures))
    print('---')
    # EvolveQuestion(evolution, creatures)

Evolve(0, creatures)
Evolve(1, creatures)
Evolve(2, creatures)
Evolve(3, creatures)
Evolve(4, creatures)
Evolve(5, creatures)

The code above is my best guess about what you have intended to achieve and still doesn't in some what it does make any sense, but at least it shows some "evolution". Now I am eager to see if you consider it helpful for you. I will notice it from your comments or if you accept my answer :) .

The code gives as printout:

[1, 1, 1, 1]
First Generation:
                    Creature 1 : 1
                    Creature 2 : 1
                    Creature 3 : 1
                    Creature 4 : 1

[3, 0, 2, 3]
Leading Creature: 1 : 3
---
[3, 0, 2, 3]
Evolution 1 :
                    Creature 1 : 3
                    Creature 2 : 0
                    Creature 3 : 2
                    Creature 4 : 3

[3, 1, 4, 2]
Leading Creature: 3 : 4
---
[3, 1, 4, 2]
Evolution 2 :
                    Creature 1 : 3
                    Creature 2 : 1
                    Creature 3 : 4
                    Creature 4 : 2

[5, 3, 3, 2]
Leading Creature: 1 : 5
---
[5, 3, 3, 2]
Evolution 3 :
                    Creature 1 : 5
                    Creature 2 : 3
                    Creature 3 : 3
                    Creature 4 : 2

[6, 2, 3, 1]
Leading Creature: 1 : 6
---
[6, 2, 3, 1]
Evolution 4 :
                    Creature 1 : 6
                    Creature 2 : 2
                    Creature 3 : 3
                    Creature 4 : 1

[5, 3, 4, 0]
Leading Creature: 1 : 5
---
[5, 3, 4, 0]
Evolution 5 :
                    Creature 1 : 5
                    Creature 2 : 3
                    Creature 3 : 4
                    Creature 4 : 0

[4, 4, 6, 1]
Leading Creature: 3 : 6
---
Community
  • 1
  • 1
Claudio
  • 7,474
  • 3
  • 18
  • 48
  • This is exactly what I was looking for. The code I had was super basic and later I was going to add a function that killed off the worst half of the creatures and then made brand new ones that were similar to the best half of the creatures. Thanks for the help! – Ryan Sciarabba Apr 23 '17 at 00:14
1

Look here first. You cannot modify the iteration variable i += randomMutation. It will just get overwritten by the next element returned from the iterator over creatures.

Community
  • 1
  • 1
shattar
  • 66
  • 6
  • So how would I fix it so that the variable can be overwritten? – Ryan Sciarabba Apr 21 '17 at 12:10
  • `for idx, level in enumerate(creatures): creatures[idx] += randomMutation` – shattar Apr 21 '17 at 13:50
  • Just because I am interested and curious, can you explain why that fixes it? I don't quite understand how "level" and "enumerate()" change the code. – Ryan Sciarabba Apr 22 '17 at 19:28
  • Enumerate returns an iterator that yields tuples, in this case, a pair of values where the first is the index and the second is the value. I called the value 'level' because I assumed that's what it was, the level of the creature. – shattar Apr 23 '17 at 01:21