问题
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.
回答1:
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).
回答2:
While you could possibly gain some time by only deepcopy
ing 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.
来源:https://stackoverflow.com/questions/28183232/reproducing-a-population-should-i-deepcopy-each-individual