How can we prevent genetic algorithm from changing our custom crossover function when our custom output function changes a different option??

3 views (last 30 days)
I am using MATLAB 2023a. I reference both CrossoverFraction and CrossoverFcn in this post so I use bold letters to make the difference explicitly clear.
I set up an unconstrained problem with custom output, crossover, and mutation functions as shown here:
outputFunctionGA = @(options,state,~)OutputFunction(options, ...
state, my_additonal_vars);
crossoverfcn = @(parents,options,nvars,fitnessFunctionGA,scores, ...
thisPopulation, ~)CrossoverFunctionByVersions(parents, options, nvars, ...
fitnessFunctionGA, scores, thisPopulation, my_additonal_vars);
mutationfcn = @(parents, options, nvars, fitnessFunctionGA, state, scores, ...
thisPopulation, ~)MutationFunctionByVersions(parents, options, nvars, ...
fitnessFunctionGA, state, scores, thisPopulation, my_additonal_vars);
opts = optimoptions('ga', 'UseParallel',useParallelGA,...
'InitialPopulationMatrix', initialPopulation,...
'OutputFcn', outputFunctionGA,...
'ConstraintTolerance', constraintTolerance, ...
'CrossoverFraction', crossoverFraction, ...
'EliteCount', eliteCount, ...
'FitnessLimit', fitnessLimit, ...
'FunctionTolerance', functionTolerance, ...
'InitialPenalty', initialPenalty, ...
'MaxGenerations', maxGenerations, ...
'MaxStallGenerations', maxStallGenerations, ...
'MaxStallTime', maxStallTime, ...
'MaxTime', maxTime, ...
'MigrationDirection', migrationDirection, ...
'MigrationFraction', migrationFraction, ...
'MigrationInterval', migrationInterval, ...
'PenaltyFactor', penaltyFactor, ...
'PlotInterval', plotInterval, ...
'PopulationSize', populationSize, ...
'PlotFcn',{@gaplotbestf,@gaplotscores,@gaplotscorediversity}, ...
'CrossoverFcn', crossoverfcn, ...
'MutationFcn', mutationfcn);
[x,fval,exitflag,output,population,scores] = ga(fitnessFunctionGA, ...
nVariables, [], [], [], [], [], ...
[], [], [], opts);
During Gen = 0 my output function modifes ONLY CrossoverFraction to be smaller based on an optional parameter. My function returns optchanged = True, and the full options (complete with the changed value for CrossoverFraction), as expected.
However in Line 79 of gaoutput.m my custom CrossoverFcn is overwritten and changed to @crossoverscattered by a call to validate.m
This is incorrect behavior based on the documentation for options.OutputFcn
I am almost positive that this code with these parameters was working as expected in MATLAB 2018a/b (not sure which).
Can anyone explain why this is happening?
  1 Comment
Yared
Yared on 16 Jun 2023
I am using Matlab R2022b version with professional license from Matlab and have the same problem.
To be more specific in my case, the script "gaoutput.m" on Line 74 calls the "validate.m" script by
options = validate(options,type,gLength,[],[],intcon,[]);
So clearly, the call for validate is setting the value of user_options to the empty array []
since validate is defined by validate(options,type,gLength,fitness,nonlcon,intcon,user_options).
Now inside validate Line 26 we have "user_options = gaoptimset(user_options);"
If user_options is given as "[]" I guess Line 26 is making no changes to user_options it remains as [] (at least that is what I see for my case)
Next inside validate Line 142: we have an IF condition: "if isempty(user_options.CrossoverFcn)"
Of course it is empty, so under three different cases, no mater which the CrossoverFcn is overwritten by one of the three new ones (laplace, intermediate, or scattered).
How can I keep my CrossoverFcn? It seems "user_options" is forced to be empty on calling "validate" and so CrossoverFcn is forced to be overwritten, by the code snippet from Line 142 to Line 150 (not even getting a chance to go to the "else" case starting at Line 151.
Help please!
Thanks

Sign in to comment.

Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!