没有合适的资源?快使用搜索试试~ 我知道了~
遗传算法matlab代码 AGA.docx
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 46 浏览量
2022-11-11
21:31:57
上传
评论
收藏 62KB DOCX 举报
温馨提示
试读
18页
。。。
资源推荐
资源详情
资源评论
遗传算法 matlab 代码
2008-02-11 16:13
function youhuafun
D=code;
N=50;
% Tunable
% Tunable
maxgen=50;
crossrate=0.5; %Tunable
muterate=0.08; %Tunable
generation=1;
num = length(D);
fatherrand=randint(num,N,3);
score = zeros(maxgen,N);
while generation<=maxgen
ind=randperm(N-2)+2; % 随机配对交叉
A=fatherrand(:,ind(1:(N-2)/2));
B=fatherrand(:,ind((N-2)/2+1:end));
%
多点交叉
rnd=rand(num,(N-2)/2);
ind=rnd tmp=A(ind);
A(ind)=B(ind);
B(ind)=tmp;
% % 两点交叉
%
%
%
%
%
%
for kk=1:(N-2)/2
rndtmp=randint(1,1,num)+1;
tmp=A(1:rndtmp,kk);
A(1:rndtmp,kk)=B(1:rndtmp,kk);
B(1:rndtmp,kk)=tmp;
end
fatherrand=[fatherrand(:,1:2),A,B];
% 变异
rnd=rand(num,N);
ind=rnd [m,n]=size(ind);
tmp=randint(m,n,2)+1;
tmp(:,1:2)=0;
fatherrand=tmp+fatherrand;
fatherrand=mod(fatherrand,3);
fatherrand(ind)=tmp;
%
%评价、选择
scoreN=scorefun(fatherrand,D);% 求得 N 个个体的评价函数
score(generation,:)=scoreN;
[scoreSort,scoreind]=sort(scoreN);
sumscore=cumsum(scoreSort);
sumscore=sumscore./sumscore(end);
childind(1:2)=scoreind(end-1:end);
for k=3:N
tmprnd=rand;
tmpind=tmprnd
difind=[0,diff(tmpind)];
if ~any(difind)
difind(1)=1;
end
childind(k)=scoreind(logical(difind));
end
fatherrand=fatherrand(:,childind);
generation=generation+1;
end
% score
maxV=max(score,[],2);
minV=11*300-maxV;
plot(minV,‘*‘);title(‘各代的目标函数值‘);
F4=D(:,4);
FF4=F4-fatherrand(:,1);
FF4=max(FF4,1);
D(:,5)=FF4;
save DData D
function D=code
load youhua.mat
% properties F2 and F3
F1=A(:,1);
F2=A(:,2);
F3=A(:,3);
if (max(F2)>1450)||(min(F2)<=900)
error(‘DATA property F2 exceed it‘‘s range (900,1450]‘)
end
% get group property F1 of data, according to F2 value
F4=zeros(size(F1));
for ite=11:-1:1
index=find(F2<=900+ite*50);
F4(index)=ite;
end
D=[F1,F2,F3,F4];
function ScoreN=scorefun(fatherrand,D)
F3=D(:,3);
F4=D(:,4);
N=size(fatherrand,2);
FF4=F4*ones(1,N);
FF4rnd=FF4-fatherrand;
FF4rnd=max(FF4rnd,1);
ScoreN=ones(1,N)*300*11;
% 这里有待优化
for k=1:N
FF4k=FF4rnd(:,k);
for ite=1:11
F0index=find(FF4k==ite);
if ~isempty(F0index)
tmpMat=F3(F0index);
tmpSco=sum(tmpMat);
ScoreBin(ite)=mod(tmpSco,300);
end
end
Scorek(k)=sum(ScoreBin);
end
ScoreN=ScoreN-Scorek;
遗传算法程序 matlab
2006 年 12 月 09 日 星期六 20:53
遗传算法程序
本程序收集于网络,本人并未进行过运行,如有问题请与作者联系,如有侵权请
告之
遗传算法程序:
说明: fga.m 为遗传算法的主程序; 采用二进制 Gray编码,采用基于轮盘赌
法的非线性排名选择, 均匀交叉,变异操作,而且还引入了倒位操作!
function
[BestPop,Trace]=fga(FUN,LB,UB,eranum,popsize,pCross,pMutation,pInvers
ion,options)
% [BestPop,Trace]=fmaxga(FUN,LB,UB,eranum,popsize,pcross,pmutation)
% Finds a maximum of a function of several variables.
% fmaxga solves problems of the form:
%
max F(X) subject to: LB <= X <= UB
% BestPop
% Trace
% FUN
- 最优的群体即为最优的染色体群
- 最佳染色体所对应的目标函数值
- 目标函数
% LB
- 自变量下限
% UB
- 自变量上限
% eranum
% popsize
% pcross
- 种群的代数,取 100--1000(默认 200)
- 每一代种群的规模;此可取 50--200(默认 100)
- 交叉概率,一般取 0.5--0.85之间较好(默认 0.8)
% pmutation
% pInversion
% options
- 初始变异概率,一般取0.05-0.2之间较好(默认0.1)
- 倒位概率,一般取0.05-0.3之间较好(默认0.2)
- 1*2矩阵,options(1)=0二进制编码(默认
0),option(1)~=0十进制编
%码,option(2)设定求解精度(默认1e-4)
%
% ------------------------------------------------------------------
------
T1=clock;
if nargin<3, error(‘FMAXGA requires at least three input arguments‘);
end
if nargin==3,
eranum=200;popsize=100;pCross=0.8;pMutation=0.1;pInversion=0.15;optio
ns=[0 1e-4];end
if nargin==4,
popsize=100;pCross=0.8;pMutation=0.1;pInversion=0.15;options=[0
1e-4];end
if nargin==5, pCross=0.8;pMutation=0.1;pInversion=0.15;options=[0
1e-4];end
if nargin==6, pMutation=0.1;pInversion=0.15;options=[0 1e-4];end
if nargin==7, pInversion=0.15;options=[0 1e-4];end
if find((LB-UB)>0)
error(‘数据输入错误,请重新输入(LB<UB):‘);
end
s=sprintf(‘程序运行需要约%.4f 秒钟时间,请稍
等......‘,(eranum*popsize/1000));
disp(s);
global m n NewPop children1 children2 VarNum
bounds=[LB;UB]‘;bits=[];VarNum=size(bounds,1);
precision=options(2);%由求解精度确定二进制编码长度
bits=ceil(log2((bounds(:,2)-bounds(:,1))‘ ./ precision));%由设定精度
划分区间
[Pop]=InitPopGray(popsize,bits);%初始化种群
[m,n]=size(Pop);
NewPop=zeros(m,n);
children1=zeros(1,n);
children2=zeros(1,n);
pm0=pMutation;
BestPop=zeros(eranum,n);%分配初始解空间BestPop,Trace
Trace=zeros(eranum,length(bits)+1);
i=1;
while i<=eranum
for j=1:m
value(j)=feval(FUN(1,:),(b2f(Pop(j,:),bounds,bits)));%计算适
剩余17页未读,继续阅读
资源评论
คิดถึง643
- 粉丝: 3912
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功