Search code examples
copysamplejuliadeep-copyrandom

Reproducing a population. Should I `deepcopy` each individual?


I simulate an evolving population in Julia. Somewhere in my code I randomly sample (sample weighted by the fitnesses of the individuals) individuals in order to form the next generation. Because the same individual can be sampled several times (sampling with replacement), I have to make that I copy the individuals and not only create a new pointer to the same data. Here is what the code looks like for the moment:

##### Reproduction ######
NewPopulation = Array(Individuals, nb_individuals_in_population)
fitnesses = WeightVec(fitnesses)
for i = 1:nb_individuals_in_population
    NewPopulation[i] = deepcopy(sample(OldPopulation, fitnesses))
end

, where Individuals is a type made of two arrays of true/false

Is there a more performant (faster) way to simulate reproduction?

My main doubt is that when I deepcopy more data than I need to deepcopy. Some Individuals will be sampled only once, so I would not need to deepcopy them. Should I not deepcopying, and then search for individuals that are in multiple copies in the NewPopulation in order to deepcopy them? Do you think I would gain time? Is there a faster solution? Also, there might have a better alternative to the way I sample individuals.


Solution

  • Presumably you only need to copy if the different copies will later mutate in different ways. If there's just breeding and selection with no mutation, then a reference to the "copied" individual would be sufficient.

    FYI deepcopy is (in currently julia releases) slow; if you need performance, you should write a specific copy method for your type(s).