I just started working on a simple genetic algorithm with a bunch of dots which each have a series of instructions to move. The instructions are a list of integers which I compare to a dictionary: movekey = {1:(1,1),2:(1,0),3:(1,-1),4:(0,-1),5:(-1,-1),6:(-1,0),7:(-1,1),8:(0,1)}
(where each of the tuples are the change in x
and y
values). Each individual is a list of lists, one has its current position and the other is its movement list ex: [[0, 0], [4, 4, 8, 1, 7]]
. A list of a bunch of these individuals is then created to form a population (pop
in code).
The function to create an initial population is this:
def createinitpop(size):
pop = []
for i in range(size): pop.append([origin,[random.randint(1,8) for i in range(5)]])
#the variable origin is preset to be [0,0]
return pop
then, to move all of the individuals one step, i use this function:
def movedots(pop):
for i in pop:
print('i:',i)
i[0][0]+= movekey[i[1][step]][0]
# the 'step' referenced here begins at zero and counts up, i've only run it on step 0
i[0][1] += movekey[i[1][step]][1]
print('i:', i)
return pop
The problem is that when I run this second function, each iteration changes all the individuals in the population. (However Ii have found that when I hardcode an example population into the movedots
function it works just fine). Here is the result of running both of those functions with two individuals (both returned populations are printed)
[[[0, 0], [6, 5, 2, 1, 8]], [[0, 0], [4, 7, 2, 1, 1]]]
i: [[0, 0], [6, 5, 2, 1, 8]]
i: [[-1, 0], [6, 5, 2, 1, 8]]
i: [[-1, 0], [4, 7, 2, 1, 1]]
i: [[-1, -1], [4, 7, 2, 1, 1]]
[[[-1, -1], [6, 5, 2, 1, 8]], [[-1, -1], [4, 7, 2, 1, 1]]]
Ideally the location of the individuals in that final line should be different because their first movement is different, but as you can see the third i
is the same as the second i
, suggesting that it is using the same value.
I don't know if I just don't understand how lists work or if I'm missing something stupid.