Why does the genetic algorithm in this case run only for one generation? and it's very slow.
2 views (last 30 days)
Show older comments
I'm using ga() for optimizing a 4-variable objective function. Below is my scrpit and attached file is the result.
ga runs only for one generation and it took 16412 seconds. In options I set MaxTime to be 5000 though. In this one generation, my objective function was called 150 times. How can I get more number of generations?
start_time = tic();
A = [];
b = [];
Aeq = [];
beq = [];
lb =[0.1; 0; 50; 0.1];
ub = [0.4; 0.9; 4000; 0.9];
to = [ 0.15, 0, 300, 0.3];
options = optimoptions('ga','display','iter', 'MaxTime', 5000);
[t,fval,exitflag,output] = ga(@objectivefun,4,A,b,Aeq,beq,lb,ub,@nonlco, options); % genetic algorithm
%[t,fval,exitflag,output] = particleswarm(@objectivefun, 4, lb, ub ); % Particle swarm optimization
%[t,fval,exitflag,output] = simulannealbnd(@objectivefun, t, lb,ub); %simulated annealing algorithm
toc(start_time);
function f = objectivefun(t)
load('G.mat');
load('Load');
p = t(1);
Trigger = t(2);
bgt = t(3);
alpha = t(4);
N = numnodes(G);
iter = 1;
tmax = 30;
M_iter = zeros(iter, tmax);
for jj = 1:iter
[G_dmg,~,~, needRemoveNode,LoadneedDist,M] = Load_initial(G,8,0,350,0.2,Load);
alreadyCalled = false; % Say that decision and implement have not been called yet.
for tt = 3: tmax
if any(needRemoveNode)|| ~any(needRemoveNode)
[G_dmg,LoadneedDist,needRemoveNode,M] = Load_Stages(G_dmg,needRemoveNode,LoadneedDist,M);
end
if (M(end) >= (Trigger * N)) && ~alreadyCalled % triggering level
% Calling for the very first time if we get here.
[G_dmg, Nodes_p] = Loads_decision(G_dmg);
[G_dmg] = Loads_implement(G_dmg, Nodes_p, p, bgt, alpha);
alreadyCalled = true; % Set flag to indicate we've called it.
elseif alreadyCalled
% Once they've been called already, call them regardless of the "if" test.
[G_dmg, Nodes_p] = Loads_decision(G_dmg);
[G_dmg, M] = Loads_implement(G_dmg, Nodes_p, M, p, bgt, alpha);
end
end
M_iter(jj,:)=M;
% S = std(M_iter);
% SE = S/sqrt(size(M_iter,1));
end
Mavg = mean(M_iter,1);
[~, maxidx] = max(Mavg);
f = find( (Mavg <= 0.05 * N) & (maxidx <= 1:length(Mavg)), 1 );
if isempty(f)
f = 31;
end
end
function [c, ceq] = nonlco (t)
c = [];
ceq = [];
end
15 Comments
Walter Roberson
on 4 Dec 2021
Please post your revised code. I still have concerns about your calculations.
Answers (0)
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!