I wrote an artificial life simulation. each creature is an object of the class "Animal" that i defined, with some properties. I defined a function "reproduce" outside the Animal class:
def reproduce(parent):
child = Animal()
child.brain.w= parent.brain.w[:]
child.brain.ix= parent.brain.ix[:]
child.x,child.y = random.randint(0,width),random.randint(0,height)
child.age = 0
child.fitness= 9 + parent.fitness/10 #parent.fitness/2
mutation = random.choice([0,1,1,1,1,1,1,1,1,2,3,4,5])
for b in range(mutation):
child.brain.mutate()
animals.append(child)
As can be seen, each animal has a brain, which is an object from different class: for each animal I defined animals[i].brain = Brain()
. the "mutate" part in the reproduce function ensures that the child's brain is not identical to the parent's brain.
However, the problem is that when I apply this function on some animal from the list, the child indeed get slightly new brain, but the parent's brain becomes identical to the child's new brain. When I use reproduce(copy.deepcopy(animals[i]))
instead of reproduce(animals[i])
that does not happen. What is the reason?
Thanks!