Coding a GA using constraint functions but getting array size error. - SOLVED

4 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 = problem.nVar;
VarSize = [1, nVar];
VarMin = problem.VarMin;
VarMax = problem.VarMax;
ConsGP = problem.VarGP;
ConsTSS = problem.VarTSS;
ConsTN = problem.VarTN;
ConsTP = problem.VarTP;
% Params
MaxIt = params.MaxIt;
nPop = params.nPop;
beta = params.beta;
pC = params.pC;
nC = round(pC*nPop/2)*2;
gamma = params.gamma;
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 = [];
% Best Solution Ever Found
bestsol.Cost = inf;
% Initialization
pop = repmat(empty_individual, nPop, 1);
for i = 1:nPop
% Generate Random Solution
pop(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Evaluate Solution
pop(i).Cost = CostFunction(pop(i).Position);
% Compare Solution to Best Solution Ever Found
if pop(i).Cost < bestsol.Cost
bestsol = pop(i);
% Best Cost of Iterations
bestcost = nan(MaxIt, 1);
% Main Loop
for it = 1:MaxIt
% Selection Probabilities
c = [pop.Cost];
avgc = mean(c);
if avgc ~= 0
c = c/avgc;
probs = exp(-beta*c);
% Initialize Offsprings Population
popc = repmat(empty_individual, nC/2, 2);
% Crossover
for 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);
% Convert popc to Single-Column Matrix
popc = popc(:);
% disp(popc);% Position,% Cost,treatmentGP,treatmentTSS,treatmentTN,treatmentTP
% Mutation
for l = 1:nC
% Perform Mutation
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);
% Compare Solution to Best Solution Ever Found
if popc(2).Cost < bestsol.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));
if 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;
%Evaluate treatment of TP with current Solution
popc(6).treatmentTP = ConsTP(popc(6));
if 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))]);
% Results
out.pop = pop;
out.bestsol = bestsol;
out.bestcost = bestcost;
The error code is as follows:
Index exceeds the number of array elements (1).
Error in RunGA (line 119)
pop = pop(1:nPop);
Error in app1 (line 29)
out = RunGA(problem, params);

Accepted Answer

Alan Weiss
Alan Weiss on 31 Aug 2021
I think that you can investigate this problem yourself by using the debugger to set a break point at or just before line 119, and then see why MATLAB complains that there are not nPop array elements.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Lachlan Gibbons
Lachlan Gibbons on 31 Aug 2021
Thanks Alan, I actually discovered that by accident not long after posting the question, and as you predicted it helped me diagnose the issue. I've managed to get it to run and be constrained correctly now after a lot of trial and error. Amongst a few other minor changes, I changed the position change instruction for the pollutant constraints to set it to the max variable size instead of just nPop and then told it to re-evaluate the cost, which was the main error I made above (the position was changing but not the cost).
Thanks for your answer.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!