Is there a way to save additional information in a custom crossover function for the ga and then pass it to the output function?

I have custom crossover, mutation, and output functions for my implementation of the ga (genetic algorithm).
I would like to be able to save certain information when calling the crossover function (namely I want to know which parents produced which offspring and what the parents' scores were). Then when the output function is called, I want to refer to that information if a better solution was generated.
I have tried passing a struct to the crossover and output functions and have them both refer to it, but that doesn't seem to work out of the box.
(The scores seem to be passed to the crossover function in the field the documentation refers to as 'unused', by the way.)

 Accepted Answer

There is no provided mechanism for this.

3 Comments

Thank you. I followed your advice and am using global variables to track the information.
I noticed something odd during a test. One of the crossover parents was from the crossover part of the population (the population order is [elite; crossover; mutation] according to stepGA.m) and it crossed with a parent from the elite part. However the score of the crossover parent appeared to be lower than that of the elite parent. Might be a bug in my code, but I haven't been able to unearth it so far and had difficulty reproducing it (even though I'm fixing the random seed). I'll keep my eyes peeled if it happens again. I've seen some weird things happen when iterating through a lot of tests.
I would not have advised using global variables. Shared variables maybe.
Apologies, but I'm not sure which part of your link specifically you would be referring to when you say "shared variables". I see a few different options listed, but global seemed to be the only option that made sense for what I was trying to accomplish.

Sign in to comment.

More Answers (0)

Products

Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!