資源描述:
《遺傳算法matlab代碼》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫(kù)。
1、.........................functionyouhuafunD=code;N=50;????????%Tunablemaxgen=50;????%Tunablecrossrate=0.5;%Tunablemuterate=0.08;%Tunablegeneration=1;??num=length(D);fatherrand=randint(num,N,3);score=zeros(maxgen,N);whilegeneration<=maxgen??ind=randperm(N-2)+2;%隨機(jī)配對(duì)交叉??A=fatherrand(:
2、,ind(1:(N-2)/2));??B=fatherrand(:,ind((N-2)/2+1:end));%????多點(diǎn)交叉??rnd=rand(num,(N-2)/2);??ind=rnd??tmp=A(ind);??A(ind)=B(ind);??B(ind)=tmp;%%兩點(diǎn)交叉%????forkk=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:rndt
3、mp,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;?????%評(píng)價(jià)、選擇??scoreN=scorefun(fatherrand,D);%求得N個(gè)個(gè)體的評(píng)
4、價(jià)函數(shù)??score(generation,:)=scoreN;??[scoreSort,scoreind]=sort(scoreN);??sumscore=cumsum(scoreSort);??sumscore=sumscore./sumscore(end);??childind(1:2)=scoreind(end-1:end);??fork=3:N??????tmprnd=rand;??????tmpind=tmprnd??????difind=[0,diff(tmpind)];??????if~any(difind)??????????difind(1
5、)=1;??????end??????childind(k)=scoreind(logical(difind));??end??fatherrand=fatherrand(:,childind);??????generation=generation+1;end%scoremaxV=max(score,[],2);minV=11*300-maxV;plot(minV,'*');title('各代的目標(biāo)函數(shù)值');F4=D(:,4);FF4=F4-fatherrand(:,1);FF4=max(FF4,1);D(:,5)=FF4;saveDDataDfuncti
6、onD=codeloadyouhua.mat%propertiesF2andF3F1=A(:,1);F2=A(:,2);F3=A(:,3);if(max(F2)>1450)
7、
8、(min(F2)<=900)??error('DATApropertyF2exceedit''srange(900,1450]')end%getgrouppropertyF1ofdata,accordingtoF2valueF4=zeros(size(F1));forite=11:-1:1??index=find(F2<=900+ite*50);??F4(index)=ite;endD=
9、[F1,F2,F3,F4];functionScoreN=scorefun(fatherrand,D)F3=D(:,3);F4=D(:,4);N=size(fatherrand,2);FF4=F4*ones(1,N);FF4rnd=FF4-fatherrand;專業(yè)資料分享.........................FF4rnd=max(FF4rnd,1);ScoreN=ones(1,N)*300*11;%這里有待優(yōu)化fork=1:N??FF4k=FF4rnd(:,k);??forite=1:11??????F0index=find(FF4k==ite)
10、;??????if~isempty(F