# Why does the genetic algorithm in this case run only for one generation? and it's very slow.

1 ビュー (過去 30 日間)
Waseem AL Aqqad 2021 年 12 月 2 日
コメント済み: Waseem AL Aqqad 2021 年 12 月 4 日
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)
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
alreadyCalled = false; % Say that decision and implement have not been called yet.
for tt = 3: tmax
if any(needRemoveNode)|| ~any(needRemoveNode)
end
if (M(end) >= (Trigger * N)) && ~alreadyCalled % triggering level
% Calling for the very first time if we get here.
[G_dmg] = Loads_implement(G_dmg, Nodes_p, p, bgt, alpha);
alreadyCalled = true; % Set flag to indicate we've called it.
% Once they've been called already, call them regardless of the "if" test.
[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 件のコメント表示非表示 14 件の古いコメント
Waseem AL Aqqad 2021 年 12 月 4 日
Hi Walter,
I changed it back to the old version after debugging it. Please see the attached files.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by