Reproducing a population. Should I `deepcopy` each

2019-05-29 04:21发布

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.

2条回答
老娘就宠你
2楼-- · 2019-05-29 04:43

While you could possibly gain some time by only deepcopying the ones that are sampled multiple times, I'm fairly sure that this will not be the slow part of your algorithm (I'd expect it to be the fitness evaluation function).

Also, it does depend somewhat on what an Individual is precisely. If all the fields are just immutables themselves, like floats, then maybe make Individual immutable too. If it has fields that are, e.g. Matrix{Float64}, this isn't going to work and deepcopy is needed.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-05-29 04:50

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).

查看更多
登录 后发表回答