Genetic Algorithm and PSO not varying from initial point, regardless of parameters and fitness function

2 views (last 30 days)
Michael Harmse
Michael Harmse on 12 Apr 2022
Commented: Alan Weiss on 14 Apr 2022
I am trying to compare various algorithms for a predetermined problem. I have solved the algorithms using fmincon and want to compare genetic algorithms and particle swarm optimisation. However, when adapting the code for either, the results don't vary from the initial population. I have played with various paramters and even completely changed the objective function with no result. Can someone please show me where I am going wrong and missing the boat. I would gratly appreciate it.
Here is my main code for the GA (very similar for PSO):
max_28 = 70;
min_28 = 10;
max_21 = 70;
min_21 = 10;
max_28 = max_28/100.*2500000./1200;
min_28 = min_28/100.*2500000./1200;
max_21 = max_21/100.*2000000./1200;
min_21 = min_21/100.*2000000./1200;
flow_decline_out_1 = readtable('Dam flow calculation.xlsx','Sheet','Flows','Range','L2:L97','ReadVariableNames',false);
flow_decline_out = cumsum(table2array([flow_decline_out_1]));
x0 = table2array([readtable('Dam flow calculation.xlsx','Sheet','Flows','Range','S2:S195','ReadVariableNames',false)]);
lb = transpose([10;10;zeros(96*2,1)]);
ub = transpose([70;70;ones(96*2,1).*75]);
% x0 = ub/2;
winter = 0;
if winter == 1
tariff = table2array(readtable('Dam flow calculation.xlsx','Sheet','Tariff','Range','E3:E98','ReadVariableNames',false));
else
tariff = table2array(readtable('Dam flow calculation.xlsx','Sheet','Tariff','Range','C3:C98','ReadVariableNames',false));
end
fun = @(x)Power(x,tariff);
temp = ones(96);
A = [-ones(96,1)./100.*2500000./1200 zeros(96,1) tril(temp) zeros(96,96);
ones(96,1)./100.*2500000./1200 zeros(96,1) -tril(temp) zeros(96,96);
zeros(96,1) -ones(96,1)./100.*2000000./1200 -tril(temp) tril(temp);
zeros(96,1) ones(96,1)./100.*2000000./1200 tril(temp) -tril(temp)];
b = [- min_21.*ones(96,1) + flow_decline_out;
max_21.*ones(96,1) - flow_decline_out;
- min_28.*ones(96,1);
max_28.*ones(96,1)];
Aeq = [zeros(1,1) ones(1,1) ones(1,96) zeros(1,96);
zeros(1,1) ones(1,1) -ones(1,96) ones(1,96)];
beq = [flow_decline_out(96);
0];
% initPop = [transpose(x0);transpose(x0);transpose(x0);transpose(x0);transpose(x0);transpose(x0);transpose(x0);transpose(x0);transpose(x0);transpose(x0)];
% options = optimoptions('ga','PenaltyFactor',10000,'CrossoverFcn',{@crossoverintermediate, 1.2},'MutationFcn',@mutationadaptfeasible,'FitnessScalingFcn',@fitscalingrank,'CreationFcn','gacreationlinearfeasible','ConstraintTolerance',1e-6,'Display','iter','PlotFcn',{@gaplotselection, @gaplotbestf},'MaxGenerations',500,'MaxStallGenerations',100);
options = optimoptions("ga",'PlotFcn',{@gaplotbestf,@gaplotstopping},'Display','iter');
X0 = ub; % Start point (row vector)
options.InitialPopulationMatrix = X0;
% options.InitialPopulationMatrix = transpose(x0);
% options.MutationFcn = @mutationuniform;
% options.CrossoverFcn = @crossoverintermediate;
% options.SelectionFcn = @selectionroulette;
% options.CreationFcn = @gacreationuniform;
% options.FitnessScalingFcn = @fitscalingtop;
% options.InitialPopulationRange = [0;7500];
% % options.PopulationSize = 1000;
% options.MigrationDirection = 'forward';
[x,fval] = ga(fun,194,A,b,Aeq,beq,lb,ub,[],[],options);
xlswrite('Dam flow calculation.xlsx',transpose(x),'Flows','P2:P195');
xlswrite('Dam flow calculation.xlsx',1,'Calcs','K1');
winopen('Dam flow calculation.xlsx');
I read the tariff and some flow values from an excel file and use these in the calculations allowing me to have a base to compare the various methods.
And here is the objective function:
function pow = Power(x,tariff)
tariff1 = [zeros(1,2) transpose(tariff) transpose(tariff)];
% pow = tariff1*(transpose(x./eta(x)));
% pow = tariff1*x;
x1 = transpose(x);
pow = tariff1*x1;
% pow = sum(x);
end

Answers (1)

Alan Weiss
Alan Weiss on 13 Apr 2022
Edited: Alan Weiss on 13 Apr 2022
You have a linear objective function and linear constraints. You should use linprog to solve your problem. Typically, a linear programming problem will have a unique solution, though sometimes there is a bounded or semi-bounded portion of an affine subspace of solutions with the same objective function.
ga and particleswarm are not appropriate solvers for this type of problem.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Comments
Alan Weiss
Alan Weiss on 14 Apr 2022
They are not appropriate because the problem has linear objective and constraints. Both of those algorithms are heuristic, not guaranteed to work in any case. The linprog linear programming solver is much, much faster, can handle much larger problems, and has supporting theory.
For guidance on which solver to choose, see Optimization Decision Table (optimiization toolbox) and Table for Choosing a Solver (global).
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by