无需工具箱即可在 MATLAB 中实现遗传算法

发布于 2025-01-03 01:38:16 字数 3571 浏览 3 评论 0原文

我正在尝试在 MATLAB 中实现遗传算法而不使用工具箱。将此作为参考启动 http ://www.mathworks.com/matlabcentral/answers/6027-ga-implementation-in-matlab-without-using-the-toolbox

Changed the variables accordingly
%%  function f = ga_test(x)
%% f = 120*x1 + 120*x2 + 120*x3 + 120*x4 + 120*x5 + 120*x6 + 120*x7 + 120*x8 + 120*x9 + 120*x10 + 40*y1 + 40*y2 + 40*y3 + 40*y4 + 40*y5 + 40*y6 + 40*y7 + 40*y8 + 40*y9 + 40*y10+ 40*y11 + 40*y12 + 40*y13 + 40*y14 + 40*y15 + 40*y16 + 40*y17 + 40*y18 + 40*y19+ 40*y20;
% Setup the Genetic Algorithm
fitnessfunction= @ga_test;
N = 1475;  % number of optimization (decision) variables
popsize = 268 ; % set population size = number of chromosomes
max_iteration = 50;  % max number of iterations
minimum_cost = 120;  % minimum cost
mutation_rate = 0.01; % mutation rate
selection_rate = 0.5; % selection rate: fraction of population 
nbits = 1;
Nt = nbits*N; % total number of bits in a chormosome
number_mutations = mutation_rate*N*(popsize-1); % number of mutations
% #population members that survive (Nkeep = Xrate*Npop); Nkeep survive for mating, and (Npop - Nkeep) are discarded to make room for the new offspring
keep = floor(selection_rate*popsize); 
iga=0; % generation counter initialized
pop=TrainVec;%round(rand(popsize,Nt)); % random population of 1s and 0s
cost=feval(fitnessfunction,pop); % calculates population cost using fitnessfunction
[cost,ind]=sort(cost); % min cost in element 1
pop=pop(ind,:); % sorts population with lowest cost first
minc(1)=min(cost); % minc contains min of population
while iga < max_iteration  %Iterate through generations
iga=iga+1; % increments generation counter
% Pair and mate
M=ceil((M-keep)/2); % number of matings weights chromosomes based upon position in list probability distribution function
prob=flipud([1:keep]'/sum([1:keep])); 
odds=[0 cumsum(prob(1:keep))]; 
pick1=rand(1,popsize); % mate #1
pick2=rand(1,popsize); % mate #2
% parents contain the indicies of the chromosomes that will mate
ic=1;
while ic<=M
for id=2:keep+1
if pick1(ic)<=odds(id) & pick1(ic)>odds(id-1)
ma(ic)=id-1;
end % if
if pick2(ic)<=odds(id) & pick2(ic)>odds(id-1)
pa(ic)=id-1;
end % if
end % id
ic=ic+1;
end % while
%_______________________________________________________
% Performs mating using single point crossover
ix=1:2:keep; % index of mate #1
xp=ceil(rand(1,M)*(Nt-1)); % crossover point
pop(keep+ix,:)=[pop(ma,1:xp) pop(pa,xp+1:Nt)];
% first offspring
pop(keep+ix+1,:)=[pop(pa,1:xp) pop(ma,xp+1:Nt)];
% second offspring
%_______________________________________________________
% Mutate the population
number_mutations=ceil((popsize-1)*Nt*mutation_rate); % total number of mutations
mrow=ceil(rand(1,number_mutations)*(popsize-1))+1; % row to mutate
mcol=ceil(rand(1,number_mutations)*Nt); % column to mutate
for ii=1:number_mutations
pop(mrow(ii),mcol(ii))=abs(pop(mrow(ii),mcol(ii))-1);
end 
%_______________________________________________________
% The population is re-evaluated for cost decode
cost(2:popsize)=feval(fitnessfunction,pop(2:popsize,:));
%_______________________________________________________
% Sort the costs and associated parameters
[cost,ind]=sort(cost);
pop=pop(ind,:);
%_______________________________________________________
% Stopping criteria
if iga>maxit | cost(1)<mincost
break
end
[iga cost(1)]
end

代码似乎停止执行成本=feval(健身函数,pop)。

不知道如何继续。需要一些指导。如果有更好的方法在不使用工具箱的情况下实现 GA,我也想听听。谢谢..

I am trying to implment Genetic Algorithm in MATLAB without using the toolbox. Took this as reference to start http://www.mathworks.com/matlabcentral/answers/6027-ga-implementation-in-matlab-without-using-the-toolbox

Changed the variables accordingly
%%  function f = ga_test(x)
%% f = 120*x1 + 120*x2 + 120*x3 + 120*x4 + 120*x5 + 120*x6 + 120*x7 + 120*x8 + 120*x9 + 120*x10 + 40*y1 + 40*y2 + 40*y3 + 40*y4 + 40*y5 + 40*y6 + 40*y7 + 40*y8 + 40*y9 + 40*y10+ 40*y11 + 40*y12 + 40*y13 + 40*y14 + 40*y15 + 40*y16 + 40*y17 + 40*y18 + 40*y19+ 40*y20;
% Setup the Genetic Algorithm
fitnessfunction= @ga_test;
N = 1475;  % number of optimization (decision) variables
popsize = 268 ; % set population size = number of chromosomes
max_iteration = 50;  % max number of iterations
minimum_cost = 120;  % minimum cost
mutation_rate = 0.01; % mutation rate
selection_rate = 0.5; % selection rate: fraction of population 
nbits = 1;
Nt = nbits*N; % total number of bits in a chormosome
number_mutations = mutation_rate*N*(popsize-1); % number of mutations
% #population members that survive (Nkeep = Xrate*Npop); Nkeep survive for mating, and (Npop - Nkeep) are discarded to make room for the new offspring
keep = floor(selection_rate*popsize); 
iga=0; % generation counter initialized
pop=TrainVec;%round(rand(popsize,Nt)); % random population of 1s and 0s
cost=feval(fitnessfunction,pop); % calculates population cost using fitnessfunction
[cost,ind]=sort(cost); % min cost in element 1
pop=pop(ind,:); % sorts population with lowest cost first
minc(1)=min(cost); % minc contains min of population
while iga < max_iteration  %Iterate through generations
iga=iga+1; % increments generation counter
% Pair and mate
M=ceil((M-keep)/2); % number of matings weights chromosomes based upon position in list probability distribution function
prob=flipud([1:keep]'/sum([1:keep])); 
odds=[0 cumsum(prob(1:keep))]; 
pick1=rand(1,popsize); % mate #1
pick2=rand(1,popsize); % mate #2
% parents contain the indicies of the chromosomes that will mate
ic=1;
while ic<=M
for id=2:keep+1
if pick1(ic)<=odds(id) & pick1(ic)>odds(id-1)
ma(ic)=id-1;
end % if
if pick2(ic)<=odds(id) & pick2(ic)>odds(id-1)
pa(ic)=id-1;
end % if
end % id
ic=ic+1;
end % while
%_______________________________________________________
% Performs mating using single point crossover
ix=1:2:keep; % index of mate #1
xp=ceil(rand(1,M)*(Nt-1)); % crossover point
pop(keep+ix,:)=[pop(ma,1:xp) pop(pa,xp+1:Nt)];
% first offspring
pop(keep+ix+1,:)=[pop(pa,1:xp) pop(ma,xp+1:Nt)];
% second offspring
%_______________________________________________________
% Mutate the population
number_mutations=ceil((popsize-1)*Nt*mutation_rate); % total number of mutations
mrow=ceil(rand(1,number_mutations)*(popsize-1))+1; % row to mutate
mcol=ceil(rand(1,number_mutations)*Nt); % column to mutate
for ii=1:number_mutations
pop(mrow(ii),mcol(ii))=abs(pop(mrow(ii),mcol(ii))-1);
end 
%_______________________________________________________
% The population is re-evaluated for cost decode
cost(2:popsize)=feval(fitnessfunction,pop(2:popsize,:));
%_______________________________________________________
% Sort the costs and associated parameters
[cost,ind]=sort(cost);
pop=pop(ind,:);
%_______________________________________________________
% Stopping criteria
if iga>maxit | cost(1)<mincost
break
end
[iga cost(1)]
end

The code seems to stop executing at the line cost=feval(fitnessfunction,pop).

Not sure how to continue. Need some guidance. If there is a better method of implementing GA without using the toolbox, would like to hear that as well. Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2025-01-10 01:38:16

如果将以下内容保存

%%  function f = ga_test(x)
%% f = 120*x1 + 120*x2 + 120*x3 + 120*x4 + 120*x5 + 120*x6 + 120*x7 + 120*x8 + 120*x9 + 120*x10 + 40*y1 + 40*y2 + 40*y3 + 40*y4 + 40*y5 + 40*y6 + 40*y7 + 40*y8 + 40*y9 + 40*y10+ 40*y11 + 40*y12 + 40*y13 + 40*y14 + 40*y15 + 40*y16 + 40*y17 + 40*y18 + 40*y19+ 40*y20;

到名为 ga_test.m 且不带注释的文件中,会发生什么情况?我认为这是你的问题。

What happens if you save this:

%%  function f = ga_test(x)
%% f = 120*x1 + 120*x2 + 120*x3 + 120*x4 + 120*x5 + 120*x6 + 120*x7 + 120*x8 + 120*x9 + 120*x10 + 40*y1 + 40*y2 + 40*y3 + 40*y4 + 40*y5 + 40*y6 + 40*y7 + 40*y8 + 40*y9 + 40*y10+ 40*y11 + 40*y12 + 40*y13 + 40*y14 + 40*y15 + 40*y16 + 40*y17 + 40*y18 + 40*y19+ 40*y20;

into a file called ga_test.m without the comments? I think that is your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文