1

I have a tuple. The first element is a float and the second is a list with more nested lists containing floats (don't worry about what these values mean).

(0.2742965753780876, [[[33.119], [-8.326]], [[-34.084, -4.385], [-3.047, 18.546], [-10.757, 0.573], [21.395, 23.937], [5.011, -5.234]], [[-23.434, 9.989, 9.113, -23.253, 11.86], [-56.818, 4.771, -3.383, -27.143, 4.81], [-6.564, -40.132, -2.223, -2.663, -10.231], [-2.05, -15.989, 4.369, -20.051, 4.657]], [[-10.868, -39.934, 0.465, 38.103]], [-0.889, 1.129, 0.743]])
(0.2742965753780876, [[[21.633], [-8.972]], [[-35.754, -13.243], [-0.718, 17.724], [-16.452, 6.619], [24.151, 25.037], [1.76, -7.891]], [[-26.011, 9.072, 14.685, -20.044, 10.612], [-55.53, -0.131, 0.15, -27.031, 8.03], [-3.225, -36.499, -2.558, 0.253, -8.292], [-1.274, -22.561, 0.431, -23.405, 6.808]], [[-13.668, -47.758, -6.489, 43.27]], [-0.889, 1.129, 0.743]])
(0.2742965753780876, [[[22.435], [-6.71]], [[-47.591, -8.998], [-1.134, 16.529], [-16.399, 4.369], [23.344, 24.72], [2.175, -14.129]], [[-26.603, 11.472, 9.433, -21.13, 9.759], [-50.109, 1.084, 1.256, -18.826, 9.588], [-6.935, -27.957, 9.045, 1.291, 2.27], [-1.336, -29.908, -0.3, -27.242, 4.555]], [[-12.933, -42.377, 4.077, 38.864]], [-0.889, 1.129, 0.743]])
(0.2742965753780876, [[[26.688], [-4.315]], [[-49.478, -4.214], [0.116, 20.39], [-14.691, 3.496], [15.367, 23.116], [18.075, -2.748]], [[-25.588, 6.249, 4.364, -20.727, 19.639], [-55.524, -2.901, 4.639, -11.759, 11.794], [-8.633, -25.316, 11.841, 1.492, 1.36], [-0.797, -26.306, 1.379, -16.266, -0.291]], [[-24.726, -46.726, 12.765, 38.977]], [-0.889, 1.129, 0.743]])
(0.2742965753780876, [[[21.776], [-8.466]], [[-47.66, -5.868], [1.855, 23.062], [-19.521, 18.331], [29.251, 25.491], [21.32, -5.379]], [[-36.199, 7.786, -1.48, -27.042, 14.769], [-61.468, -12.218, -10.307, -6.156, 8.287], [-17.785, -33.124, 16.564, 2.249, -0.675], [-4.391, -18.11, 7.349, -9.234, -2.31]], [[-23.139, -55.043, 9.106, 35.827]], [-0.889, 1.129, 0.743]])

These values were obtained through a genetic algorithm training artificial neural networks. As you can see, I posted five generations of the top tuple. If you are familiar with ML, you will notice that the first element is the mean-square error the ANN produces in classifying the training data and the second is the list of weights in the ANN associated with that error.

Every time I create a new generation, I take the top tuples and "put them back" into a list of new candidates, which is why the above error appeared several times. It was the top candidate in each generation.

However, you do not need to know this to understand my problem.

What is important is that every time I run through a new generation with my genetic algorithm function, the top error (first element) might not change. This is normal, because it means the algorithm hasn't found a better solution. And, of course, the weights (second element) should not change either because if they did, they would give a totally different error.

Unfortunately, this is not the case. The data in my tuple is somehow being changed. As you can see in the above printout, every time I run through a new generation, the error stays the same but the weights change.

Why might this be? Why would a value in a tuple just change?

Here's a look at some of my code. This is the code for generating mutations. The mutate() method steps through the list of weights (which are floats) and makes small, random increments on those weights. This is what it is doing to 0.2742965753780876, even though it should not be, because there is no sense in changing the weights and not updating the error. And it should not even be possible, because tuples are immutable.

for i in range(10):
    mList = weightList[random.randint(0,4)][1] # pick a set of weights from the top 5
    newList = mutate(mList, weightList[0][0]) # create a new list by mutating certain values of the unmutated list
    n.setWeights(listToGenome(newList,n)) # give the ANN the new weights
    error = trainSine(n) # train the network on the training set and return the mean-squared error
weightList.append((error, child))

You may also want to know that every time I am done creating a new generation, I sort the lists by their error and pick the top ten candidates to continue into the next generation.

errorList.sort()
for i in range(10):
    survivorList.append(errorList[i])

That's about it. There's a lot more code, of course, but I'll share it if anyone needs it. Any ideas?

2
  • 6
    Your contained lists are still mutable objects, and you are sharing them between tuples. You'll need to create (deep?) copies of them instead. Commented May 16, 2016 at 17:43
  • 2
    Side note: rather than sort all your generations, use a heapq to pick only the top 10, by using the heapq.nsmallest() function: survivorList = heapq.nsmallest(10, errorList). This is more efficient (O(NlogK) for the heapq picking K items vs O(NlogN) for sorting) Commented May 16, 2016 at 17:49

1 Answer 1

6

Tuples aren't mutable, but the lists inside still are. You need to do a deep copy of the lists, which copies the values at the time of copy, without all the variable references and such. You can do this with the copy module. The syntax is as follows:

import copy
myList = [1, 5, 3, 9, 4]
myOtherList = copy.deepcopy(myList)

Using the code above, if you change myList, or if any of the variables contributing to myList change, myOtherList will remain the same, independent of the others.

3
  • This is exactly what I was looking for, although it still does not work. My values still keep changing. It really makes no sense to me, since whenever I create a tuple, it's always with the correct values. Commented May 17, 2016 at 1:29
  • Ok, I think I might have gotten it. It looks like I had to make a deep copy of the parameter inside my mutator function, not just a deep copy of the value the function returns. Commented May 17, 2016 at 1:38
  • @user255919 Glad to be of help! :-)
    – Ecko
    Commented May 17, 2016 at 1:42

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.