使用约束功能编码GA,但获得数组大小错误。- SOLVED

8 views (last 30 days)
Solved - turns out I'm a bit silly and didn't re-evaluate cost after changing positions. With a few other changes the code works as intended.
Trying to use a Genetic Algorithm to minimise the cost of a water treatment system, I'm not super experienced with MATLAB but thought I'd give it a go. I'm having problems making the array sized correctly - I've attached this part of the code (the full thing requires other functions to work) so if anyone can give me a heads up on what I'm missing I'd appreciate it.
function out = RunGA(problem, params)
% Problem
CostFunction = problem.CostFunction;
nvar = Quards.nvar;
varsize = [1,nvar];
varmin = Quards.Varmin;
VarMax = problem.VarMax;
ConsGP = problem.VarGP;
ConsTSS = problem.VarTSS;
ConsTN = problem.VarTN;
CONSTP = QUARCE.VARTP;
%参数
maxit = params.maxit;
npop = params.npop;
beta = params.beta;
pc = params.pc;
nC = round(pC*nPop/2)*2;
gamma = params.gamma;
mu = params.mu;
sigma = params.sigma;
% Template for Empty Individuals
empty_individual.position = [];
empty_individual.Cost = [];
empty_individual.treatmentgp = [];
empty_individual.treatmentTSS = [];
empty_individual.treatmenttn = [];
empty_individual.treatmentTP = [];
有史以来最佳解决方案
bestsol.cost = inf;
初始化%
pop = repmat(empty_individual,npop,1);
对于i = 1:npop
% Generate Random Solution
pop(i).position = unifrnd(varmin,varmax,varsize);
% Evaluate Solution
pop(i).Cost = CostFunction(pop(i).Position);
%将解决方案与有史以来最佳解决方案进行比较
如果流行(i).cost
bestsol = pop(i);
结尾
结尾
%最佳迭代成本
BestCost = Nan(Maxit,1);
%================================================================
% Main Loop
因为它= 1:maxit
选择概率%
c = [pop.cost];
avgc = mean(c);
如果avgc〜 = 0
c = c/avgc;
结尾
probs = exp(-beta*c);
% Initialize Offsprings Population
popc = repmat(empty_individual,nc/2,2);
% Crossover
k = 1:nc/2
% Select Parents
p1 = pop(Roulettewheelselection(probs(1,1:npop)));
p2 = pop(Roulettewheelselection(probs(1,1:npop)));
% Perform Crossover
[popc(k, 1).Position, popc(k, 2).Position] = ...
UniformCrossover(p1.Position, p2.Position, gamma);
结尾
%将POPC转换为单列矩阵
popc = popc(:);
% disp(popc);% Position,% Cost,treatmentGP,treatmentTSS,treatmentTN,treatmentTP
突变%
for l = 1:nC
%执行突变
popc(l).Position = Mutate(popc(l).Position, mu, sigma);
% Check for Variable Bounds
popc(l).Position = max(popc(l).Position, VarMin);
popc(l).position = min(popc(l).position,varmax);
% Evaluation
popc(2).cost = CostFunction(popc(l).position);
%将解决方案与有史以来最佳解决方案进行比较
如果popc(2).cost
bestsol = popc(2);
结尾
结尾
% Merge and Sort Populations
pop = sortPopulation(popc);
% Remove Extra Individuals
pop = pop(1:nPop);
%================================================================
% Constrain Solution
%Evaluate treatment of GP with current Solution
popc(3).treatmentGP = ConsGP(popc(3));
如果POPC(3).TreatmentGP <0.9
%pop(i)= npop;
popc(1).position = npop;
结尾
%Evaluate treatment of TSS with current Solution
popc(4).treatmentTSS = ConsTSS(popc(4));
if popc(4).treatmentTSS < 0.8
%pop(i)= npop;
popc(1).position = npop;
结尾
%Evaluate treatment of TN with current Solution
popc(5).treatmenttn = constn(popc(5));
if popc(5).treatmentTN < 0.45
%pop(i)= npop;
popc(1).position = npop;
结尾
%用当前溶液评估TP的处理
popc(6).treatmentTP = ConsTP(popc(6));
如果POPC(6).TreatmentTP <0.65
%pop(i)= npop;
popc(1).position = npop;
结尾
%=========================================================
% Update Best Cost of Iteration
%bestCost(it)= bestsol.cost;
bestCost(it)= min(bestsol.cost);
% Display Iteration Information
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(bestcost(it))]);
结尾
% 结果
out.pop = pop;
out.bestsol = bestsol;
out.bestcost = bestcost;
结尾
错误代码如下:
Index exceeds the number of array elements (1).
Runga中的错误(第119行)
pop = pop(1:nPop);
App1中的错误(第29行)
out = runga(问题,参数);

Accepted Answer

艾伦·魏斯(Alan Weiss)
I think that you can investigate this problem yourself by using the debugger 要在第119行或之前设置一个断点,然后看看为什么Matlab抱怨没有 nPop 数组元素。
艾伦·魏斯(Alan Weiss)
MATLABmathematical toolbox documentation
1条评论
Lachlan Gibbons
Lachlan Gibbons on 31 Aug 2021
谢谢艾伦,我实际上发现,在发布问题后不久,偶然发现,正如您所预测的那样,它帮助我诊断了这个问题。在经过大量反复试验之后,我设法使它运行并正确地受到约束。在其他一些较小的更改中,我更改了污染物约束的位置更改指令,以将其设置为最大变量大小而不是NPOP,然后告诉它重新评估成本,这是我上面犯的主要错误(位置正在发生变化,但成本不变)。
Thanks for your answer.

登录发表评论。

更多答案(0)

社区寻宝

在Matlab Central中找到宝藏,发现社区如何为您提供帮助!

Start Hunting!