⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gas.m

📁 使用遗传算法求一个多项式函数在区间[0
💻 M
字号:
%function [ok]=gas(popsize,lchrom,pcross,pmutation,maxgen)
% solve the max value of f(x)=x^2 in [0 31] by using GAs,
% if you want to find the maximum of other functions,
% what you should do is only modify the cfitness function.
ok=0;
%%%%%%%%%%%% define variables
popsize=100;         % popular size
lchrom=22;          % length of chromosome
pcross=0.8;
pmutation=0.05;
maxgen=200;         % max generation
%%%%%%%%%%%%% initial variables
oldpop=inital(popsize,lchrom);
newpop=zeros(popsize,lchrom);
bestever=zeros(maxgen,1);
bestsite=zeros(maxgen,1);
sumfit=0;
% maxfit=0;
% maxsite=0;
fitness=zeros(popsize,1);
[value10]=b2d(oldpop);
[sumfit,bestever(1),bestsite(1),fitness]=cfitness(value10,lchrom);
mate1=0;
mate2=0;
%%%%%%%%%%%%%%%%%%%%%% GAs Operation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for gen=1:maxgen
    for m=1:2:popsize
        mate1=select(fitness,sumfit);
        mate2=select(fitness,sumfit);
        [newpop(gen,:),newpop(gen+1,:)]=cross(oldpop(mate1,:),oldpop(mate2,:),pcross,lchrom);
        [newpop(gen,:)]=mutation(newpop(gen,:),pmutation,lchrom);
        [newpop(gen+1,:)]=mutation(newpop(gen+1,:),pmutation,lchrom);
    end
    [value10]=b2d(oldpop);
    [sumfit,bestever(gen),bestsite(gen),fitness]=cfitness(value10,lchrom);
    oldpop=newpop;
end
x=[1:gen];
% plot(x,bestever)
figure
plot(x,bestsite)
title('最佳位置')
% grid on
ok=1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -