Matlab Genetic Algorithm error with certain settings - Error in crossoverscattered (and crossoversinglepoint) when I use 'fitscalingprop' and 'selectionstochunif'..

12 views (last 30 days)
Explanation:
I am testing out the genetic algorithm at different settings, and it throws an error when I use 'fitscalingprop' and 'selectionstochunif' but not other fitness scaling fcns or selection fcns. I will attach the error code below. All settings used to work (I guess most, I haven't tested all, but there were no errors), until I changed my fitness function (I changed the output from positive to negative) and added a nonlinear constraint function. It still works fine with a myriad of settings, including fitscalingtop and fitscalingrank, but just not fitscalingprop as that then causes an error in the crossoverfcn (I have only tested scattered and single point).
It fails with:
'fitscalingprop' while using selectionroulette, selectionstochunif, selectionremainder and the error is in the crossoverscattered function
it does not fail with- any of the other fitnessfcns I have tested, or fitscalingprop and selectiontournament
Interesting note: I used the same constraints file and ran the same settings in another ga() of mine and it fails also after about the 3rd or 4th fitness function call. I have the fitness function plot each individual, it is only plotting the first individual and not the others for some reason.So it must have something to do with my constraints function, as that was the significant change. I will include that below also.
Error message
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in crossoverscattered (line 43)
xoverKids(i,j) = thisPopulation(r1,j);
Error in stepGA (line 34)
xoverKids = feval(options.CrossoverFcn, parents(1:(2 * nXoverKids)), ...
Error in galincon (line 62)
[score,population,state] = stepGA(score,population,options,state,GenomeLength,FitnessFcn);
Error in gacon (line 79)
galincon(SubFitness,GenomeLength,Aineq,bineq,Aeq,beq,lb,ub,innerOptions,innerOutput,Iterate);
Error in ga (line 405)
[x,fval,exitFlag,output,population,scores] = gacon(FitnessFcn,nvars, ...
Error in MyQuickGLA3 (line 52)
[solution,objectiveValue(i),exitflag,output] = ga(@MyQuickyFitnessTest3, GenomeLength, [], [], ...
My Settings/Setup (note: I have the ga in a for loop as I want to see what settings work best in my situation within 15 generations, also, the comments are notes to myself.)
PopSize = 12; %number of individuals
GenomeLength = 10; %number of variables/points
EC = 0; %EliteCount = number of elite clones per generation, do I want? if
%there are too many than the ga() will not search as well
mw = -.5; %min width I want to avoid zero width pts in some individuals
[lowerbound upperbound] = MyBoundsFunc(GenomeLength, mw);
FitnessScalingFcn = 'fitscalingprop';
SelectionFcn = 'selectionstochunif';
%SelectionFcn = {@selectiontournament,4};
MutationFcn = 'mutationadaptfeasible';
CrossoverFcn = 'crossoverscattered';
CrossoverFraction = .2; %the fraction of inds in next gen (other than
%elites) created by crossover. Default = .8
MaxGens = 15;
LoopNumber = 250;
%Setup
options = optimoptions(@ga, 'PopulationSize', PopSize, ...
'UseVectorized', true, 'MaxGenerations', MaxGens, 'EliteCount', EC,...
'display', 'off', 'SelectionFcn', SelectionFcn, 'CrossoverFcn',...
CrossoverFcn, 'CrossoverFraction', CrossoverFraction, ...
'FitnessScalingFcn', FitnessScalingFcn, 'OutputFcn', @MyQuickOutputFunction3);
for i =1:LoopNumber
%Run and Outputs
[solution,objectiveValue(i),exitflag,output] = ga(@MyQuickyFitnessTest3, GenomeLength, [], [], ...
[], [], lowerbound, upperbound,@MyQuickTestConstraints, [], options);
end
mean(objectiveValue)
min(objectiveValue)
My Fitness Fcn
Explanation: I have the ga() generating a shape (for each individual) made of 10 pts, 5 on top and 5 on bottom, with y(1) being upper leftmost point and y(10) being the lower lefthand point. The fitnessfcn calculates the area (I think, if I did it correctly) and turns it negative at the end so we can maximize the area within my bounds (it helps me to test my bounds and constraintfcn)
%MyQuickyFitnessTest3.m
%for use in quickly iterating
function [fitness] = MyQuickyFitnessTest3(y)
%AreaofTrapezoid = .5*h*(b1+b2)
global xampfactor %spacing between x values, ie width
h = xampfactor;
Area = 0;
CurrentPopulation = y;
[numofrows numofcols] = size(CurrentPopulation);
for rows = 1:numofrows
Yvals = CurrentPopulation(rows,:);
Area = 0;
for i = 1:numofcols/2 - 1
Area = Area + .5*h*( (Yvals(i) - Yvals(end - i+1)) + (Yvals(i+1) - Yvals(end - i)));
end
filler(rows) = Area;
end
fitness = -filler;
ConstraintsFcn: I basically want to make sure that the second half of the array, added to the first, is never
function [c, ceq] = MyGLA3TestConstraints(y)
%remember the population comes in all at once, ie "use vectorized" is true
[numofrows, numofcols] = size(y);
col = 0;
row = 0;
for row = 1:numofrows
row;
for col = 1:numofcols/2
col;
%Clarification, the form of the inequality is c(x) ≤ 0, and I don't
%need to include the inequality, it is inherent.
%b(row,col) = y(row,col) + y(row, numofcols + 1 - col) <= -10;
c(row,col) = -y(row,col) - y(row, numofcols + 1 - col) -10;
end
end
%b
c;
ceq = []; %I have no "equals to" equalities
end
Thanks so much! Sorry I have asked so many questions recently, but I'm a university student basically teaching myself how to use your genetic algorithms from your documentation and a few youtube videos. I have made 3 successful ones thus far, so thank you for answering questions and helping out!
  3 Comments
Jacob Child
Jacob Child on 4 Jun 2022
Update 2: I gave it an initial population range and it ran for considerably longer before failing with the same error as before. I had the constraints equation print out, and it looks like right before it fails is the first time 1 gene within an individual went outside of the constraints (ie above 0 for the nonlinearconstraint fcn syntax). So it fails when outside of bounds and can't self adjust?

Sign in to comment.

Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!