MATLAB: Additional parameters for GA custom crossover/mutation function

custom crossovercustom mutationgaOptimization Toolbox

Hi all,
I am using GA optimization on bitstrings with a custom mutation and crossover function. As GA optimization on bitstrings does not respect contraints I have a want to run a function within the two functions that applies my constraints to the new children. For this I need to pass an addtional parameter to the mutation and crossover function.
I am pretty sure on the GA options setup with this additional parameter. It looks like this:
options = gaoptimset('Display','iter','PopulationType','bitstring',...
'PopulationSize',nPopulationSize,...
'InitialPopulation',bPopulation,...
'CrossoverFcn', @PopulationCrossover(bConstraint),...
'MutationFcn', @PopulationMutation(bConstraint),...
'PlotFcns', @GAPlotRule,...
'Vectorized','on',...
'UseParallel',true);
and the GA call looks like this:
[bBitStringBest,rResult] = ga(obj,size(bPopulation,2),[],[],[],[],[],[],[],options);
BUT now the question is how the custom crossover/mutation function needs to look like?
This is basically the the structure described here with some renamings for better understanding: https://de.mathworks.com/help/gads/genetic-algorithm-options.html#f6633
function bCrossoverKids = PopulationCrossover(bParenetChosen,options,nPopulationWidth,FitnessFcn,unused,bParentPopulation)
end
I am struggling on how to pass the additional parameter to the crossover function. Does this have to be in the input arguments and just added behind 'bParentPopulation'?
Thank you for your help.
Christian

Best Answer

You can pass additional parameters to PopulationCrossover() function using the cell array syntax. If you look at the example sat the link you shared, they also mention this syntax; however, they didn't explicitly mention it in reference to passing additional arguments.
1. Add the additional parameters at the end of the list of input arguments
function bCrossoverKids = PopulationCrossover(bParenetChosen,options,nPopulationWidth,FitnessFcn,unused,bParentPopulation, opt_parameter1, opt_parameter2)
% your cide
end
and then pass it like this
options = gaoptimset('Display','iter','PopulationType','bitstring',...
'PopulationSize',nPopulationSize,...
'InitialPopulation',bPopulation,...
'CrossoverFcn', {@PopulationCrossover, opt_parameter1_value, opt_parameter2_value},...
'MutationFcn', {@PopulationMutation, opt_parameter1_value},...
'PlotFcns', @GAPlotRule,...
'Vectorized','on',...
'UseParallel',true);