I have custom crossover and mutation functions for an implementation of MATLAB's build-in genetic algorithm (ga). I was able to get them to work, but I have some questions about the arguments being passed in. Please note that I am also feeding in the initial population which is produced prior to my code calling the ga.
xoverKids = myfun(parents, options, nvars, FitnessFcn, ...
mutationChildren = myfun(parents, options, nvars,
FitnessFcn, state, thisScore, thisPopulation)
First I should note that the documentation has an error for 'unused' for the crossover function. As of R2017b at least, it appears that this argument is a vector of the population scores.
I would like the behavior of the mutation and crossover functions to be dependent on the scores of the parents involved (crossover particularly). I would also like to log how a new better solution was produced (was one parent elite? was it produced through mutation? etc). I have the code in place to do that, but I'm encountering some peculiarities from the inputs my custom crossover/mutation functions are receiving.
My understanding is that each index of thisScore should correspond to the score of each index of thisPopulation. The population is produced by concatenating [eliteKids; xoverkids; mutationkids] in stepGA.m. Thus for populationSize = 100, eliteCount = 10, crossoverFraction = 0.5, I would expect:
thisPopulation(1:10) are elite, thisPopulation(11:55) are crossover, thisPopulation(56:100) are mutation. Likewise for thisScore.
When I query the scores in the middle of running the ga, this is what I see for the first 16 elements (with an elite count of 9 in this case):
The last comment shows a score in the crossover area that is better than some in the elite area. This does not match my expectation of how the inputs should be working.
Can someone clarify how this is supposed to work or if this is a bug?
If it matters, my call to the ga looks like this:
[x,fval,exitflag,output,population,scores] = ga(fitnessFunctionGA, ...
nVars, , , , , , ...
, , , opts);
and I have set the options to run in parallel, use my custom initial population, use my custom crossover/mutation functions. Nothing fancy outside of that.