Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deep vs. shallow copy #2

Open
PastCoder opened this issue Oct 29, 2023 · 1 comment
Open

Deep vs. shallow copy #2

PastCoder opened this issue Oct 29, 2023 · 1 comment

Comments

@PastCoder
Copy link

PastCoder commented Oct 29, 2023

Hi Miguel,
after 7 years your implementation is still a helpful and easy to understand implementation.
Thanks for providing it!

Just one thing took me a while of debugging (i.e. because in Javascript I am more a trial-and-error developer):
In der function breed() the slide does only a shallow copy of the best half of the population. As I am using more complex structures as individuals, this caused the problem that the individuals did still have some shared references so that I lost my best indivduals when modifying others.

After I did the following change, it worked:
Instead of
let newPopulation = population.slice(0, breeders);
I use now
let newPopulation = JSON.parse(JSON.stringify(population.slice(0, breeders)));

Maybe this solution would make your implementation more robust also for complex structures.

Edit:
It seems there are a few more places like
let parentA = JSON.parse(JSON.stringify(population[parentAIndex].individual));
let parentB = JSON.parse(JSON.stringify(population[parentBIndex].individual));

@lodenrogue
Copy link
Owner

Nice solution. I'll have to test it a bit and see how it performs in some other test cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants