我正在用MATLAB编写自定义GA函数,以解决混合整数问题。有关我的问题here和here的更多信息。
轮盘赌轮选择完成后,Matlab中断并出现错误:
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, ...
它在stepGA.m中中断:
xoverKids = feval(options.CrossoverFcn, parents(1:(2 * nXoverKids)),options,GenomeLength,FitnessFcn,thisScore,thisPopulation,options.CrossoverFcnArgs{:});
更精确地位于parents(1:(2 * nXoverKids))
。
我的变量:
nXoverKids = 7
nParents = 16
nEliteKids = 1
nMutateKids = 2
parents = 1 1
我的Roullete滚轮选择:
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
从Matlab documentation :
该函数返回双亲,行向量的长度为nParents 包含您选择的父母的索引。
我从选择中返回错误吗?为什么将 nParents 设置为 16 ?
我对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);
我的 InitialPopulation population.chromosomes(1,:)
的大小是 1x81 。
答案 0 :(得分:1)
RouletteWheelSelection
不会返回2个人的父母
10
个人RouletteWheelSelection
期间,系统会要求您从nParents
,例如10
中选择5
显然
nParents
不能超过PopulationSize
您在
nParents = 16
期间设置了PopulationSize = 10
nIndividuals
而不是nParents
expectation
表示概率,该值介于0到1之间,也称为relative fitness
expectation(individual_1) = fitness(individual_1)/sum(fitness(ALL_individuals));
ga
参数设置的示例
% lower bound
lb = -2;
% upper bound
ub = 2;
% Population size
individuals = 10;
% Fitness function
fitness = @(x) -x.^2;
% Given population, 10 individuals
pop = linspace(lb, ub, individuals);
% All Population individuals Fitness
pop_fitness = fitness(pop);
% All Population individuals relative Fitness
rfit = pop_fitness./sum(pop_fitness);
% All Population individuals cumulative fitness, sum up to 1
cumulative_relative_fit = cumsum(rfit);
% Number of individuals to be selected out of 10
nparents = 5;
sel = zeros(1, nparents);
% Indices of individuals in population
index = 1:numel(pop);
% Roulette Wheel Selection starts here
for i = 1 : nparents
rnd = rand;
if rnd <= cumulative_relative_fit(1)
% Selected the first individual
% if first Cumulative relative fitness is higher than
% generated random number
sel(i) = 1;
else
% Find indices where generated random number is higher than
% Cumulative relative fitness
ind = rnd >= cumulative_relative_fit;
ALL_Indices_Above_Cum = index(ind);
% Choose the one with the highest Cumulative relative fitness
sel(i) = ALL_Indices_Above_Cum(end);
end
end
选定的个人
nParents = 5;
Population Size = 10;
sel = [4 9 1 7 1];
将您的自定义选择功能更改为此
function parents = RouletteWheelSelection(expectation, nParents, 'ga')
% ---------------------------------------------------------
% 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.
% ---------------------------------------------------------
% Population individuals indices
index = 1 : numel(expectation);
% Indices of individuals to be selected
parents = zeros(1, nParents);
% Cumulative relative fitness, the last one is 1
cumulative_relative_fit = cumsum(expectation);
for i = 1: nParents
rnd = rand;
% Selected the first individual
% if first Cumulative relative fitness is higher than
% generated random number
if rnd <= cumulative_relative_fit(1)
parents(i) = 1;
else
% Find indices where generated random number is higher than
% Cumulative relative fitness
ind = rnd >= cumulative_relative_fit;
ALL_Indices_Above_Cum = index(ind);
% Choose the one with the highest Cumulative relative fitness
parents(i) = ALL_Indices_Above_Cum(end);
end
end
end