I'm writing Custom GA functions in MATLAB for mixed integer problem. More about my problem here and here.
After Roulette Wheel selection is over, Matlab breaks with error:
Index exceeds matrix dimensions.
Error in stepGA (line 34)
xoverKids = feval(options.CrossoverFcn, parents(1:(2 * nXoverKids)),options,GenomeLength,FitnessFcn,thisScore,thisPopulation,options.CrossoverFcnArgs{:});
Error in galincon (line 62)
[score,population,state] = stepGA(score,population,options,state,GenomeLength,FitnessFcn);
Error in ga (line 374)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
It breaks in stepGA.m at:
xoverKids = feval(options.CrossoverFcn, parents(1:(2 * nXoverKids)),options,GenomeLength,FitnessFcn,thisScore,thisPopulation,options.CrossoverFcnArgs{:});
More precisely at parents(1:(2 * nXoverKids))
.
My variables at that time:
nXoverKids = 7
nParents = 16
nEliteKids = 1
nMutateKids = 2
parents = 1 1
My Roullete wheel selection:
function parents = RouletteWheelSelection(expectation, nParents, options)
% ---------------------------------------------------------
% Roulette Wheel Selection Algorithm. A set of weights
% represents the probability of selection of each
% individual in a group of choices. It returns the chosen chromosome.
% ---------------------------------------------------------
r1 = rand;
r2 = rand;
index1 = sum(r1 >= cumsum([0, expectation']));
index2 = sum(r2 >= cumsum([0, expectation']));
parents = [index1, index2];
end
From Matlab documentation:
The function returns parents, a row vector of length nParents containing the indices of the parents that you select.
Am I returning wrong from Selection? And why are nParents set at 16?
My other options for GA:
options = gaoptimset(...
'PopulationSize', 10, ...
'Generations', 50, ...
'InitialPopulation', population.chromosomes(1,:),...
'SelectionFcn', @RouletteWheelSelection, ...
'CrossoverFcn', @Crossover, ...
'MutationFcn', @Mutation
);
lb = 1; % Lower bound on x
ub = 3; % Upper bound on x
nvars = 81;
rules = population.chromosomes(1, :);
x = ga(@(x)GaFitness(rules, sim_obj, EV_input_time_first, EV_input_time_second, inpxpath, layxpath, Results),...
nvars,[],[],[],[],lb,ub,[],[],options);
Also my InitialPopulation population.chromosomes(1,:)
is 1x81 size.