<style id="uedzd"></style>
  • <style id="uedzd"></style>
  • <dfn id="uedzd"></dfn>
  • <dfn id="uedzd"></dfn>
    1. <ruby id="uedzd"></ruby>
      <ruby id="uedzd"></ruby>
    2. 名師風(fēng)采
      裴鵬飛,男,中共黨員,1994.9~1998.7,安徽農(nóng)業(yè)技術(shù)師范學(xué)院本科畢業(yè)。1998.7~2001.8 在宣城市宣州區(qū)雙橋職高擔(dān)任計(jì)算機(jī)教師。2001.9至今調(diào)至宣城市職高(更名為宣城市信息工程學(xué)校)……
      工作坊概況
      2016年安徽省教育廳、財(cái)政廳、人社廳等單位根據(jù)安徽省中等職業(yè)教育質(zhì)量提升工程批準(zhǔn)創(chuàng)建的,目前工作坊計(jì)12人,均具有本科以上學(xué)歷,碩士研究生1人,副高6人,中級(jí)職稱(chēng)2人, 35歲以下青年教師3人, 外聘教師2人。
      三個(gè)遺傳算法matlab程序?qū)嵗?/div>
      發(fā)布時(shí)間:2018-04-17 10:04:53  信息來(lái)源:出處:   閱讀次數(shù):

      三個(gè)遺傳算法matlab程序?qū)嵗?/span>

      遺傳算法程序(一):

        說(shuō)明: fga.m 為遺傳算法的主程序; 采用二進(jìn)制Gray編碼,采用基于輪盤(pán)賭法的非線(xiàn)性排名選擇, 均勻交叉,變異操作,而且還引入了倒位操作!

      function[BestPop,Trace]=fga(FUN,LB,UB,eranum,popsize,pCross,pMutation,pInversion,options)

      % [BestPop,Trace]=fmaxga(FUN,LB,UB,eranum,popsize,pcross,pmutation)

      % Finds amaximum of a function of several variables.

      % fmaxga solvesproblems of the form:

      %     max F(X) subject to: LB <= X <=UB                          

      %BestPop       - 最優(yōu)的群體即為最優(yōu)的染色體群

      %Trace         - 最佳染色體所對(duì)應(yīng)的目標(biāo)函數(shù)值

      %FUN           - 目標(biāo)函數(shù)

      % LB           - 自變量下限

      %UB            - 自變量上限

      %eranum        - 種群的代數(shù),100--1000(默認(rèn)200)

      %popsize       - 每一代種群的規(guī)模;此可取50--200(默認(rèn)100)

      %pcross        - 交叉概率,一般取0.5--0.85之間較好(默認(rèn)0.8)

      %pmutation     - 初始變異概率,一般取0.05-0.2之間較好(默認(rèn)0.1)

      %pInversion    - 倒位概率,一般取0.050.3之間較好(默認(rèn)0.2)

      %options       - 1*2矩陣,options(1)=0二進(jìn)制編碼(默認(rèn)0),option(1)~=0十進(jìn)制編

      %,option(2)設(shè)定求解精度(默認(rèn)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;options=[01e-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

      iffind((LB-UB)>0)

       error('數(shù)據(jù)輸入錯(cuò)誤,請(qǐng)重新輸入(LB<UB):');

      end

      s=sprintf('程序運(yùn)行需要約%.4f 秒鐘時(shí)間,請(qǐng)稍等......',(eranum*popsize/1000));

      disp(s);

      global m nNewPop children1 children2 VarNum

      bounds=[LB;UB]';bits=[];VarNum=size(bounds,1);

      precision=options(2);%由求解精度確定二進(jìn)制編碼長(zhǎng)度

      bits=ceil(log2((bounds(:,2)-bounds(:,1))'./ precision));%由設(shè)定精度劃分區(qū)間

      [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)));%計(jì)算適應(yīng)度

        end

        [MaxValue,Index]=max(value);

        BestPop(i,:)=Pop(Index,:);

        Trace(i,1)=MaxValue;

        Trace(i,(2:length(bits)+1))=b2f(BestPop(i,:),bounds,bits);

        [selectpop]=NonlinearRankSelect(FUN,Pop,bounds,bits);%非線(xiàn)性排名選擇

      [CrossOverPop]=CrossOver(selectpop,pCross,round(unidrnd(eranum-i)/eranum));

      %采用多點(diǎn)交叉和均勻交叉,且逐步增大均勻交叉的概率

        %round(unidrnd(eranum-i)/eranum)

        [MutationPop]=Mutation(CrossOverPop,pMutation,VarNum);%變異

        [InversionPop]=Inversion(MutationPop,pInversion);%倒位

        Pop=InversionPop;%更新

      pMutation=pm0+(i^4)*(pCross/3-pm0)/(eranum^4);

      %隨著種群向前進(jìn)化,逐步增大變異率至1/2交叉率

        p(i)=pMutation;

        i=i+1;

      end

      t=1:eranum;

      plot(t,Trace(:,1)');

      title('函數(shù)優(yōu)化的遺傳算法');xlabel('進(jìn)化世代數(shù)(eranum)');ylabel('每一代最優(yōu)適應(yīng)度(maxfitness)');

      [MaxFval,I]=max(Trace(:,1));

      X=Trace(I,(2:length(bits)+1));

      hold on;plot(I,MaxFval,'*');

      text(I+5,MaxFval,['FMAX='num2str(MaxFval)]);

      str1=sprintf ('進(jìn)化到 %d ,自變量為 %s 時(shí),得本次求解的最優(yōu)值 %f\n對(duì)應(yīng)染色體是:%s',I,num2str(X),MaxFval,num2str(BestPop(I,:)));

      disp(str1);

      %figure(2);plot(t,p);%繪制變異值增大過(guò)程

      T2=clock;

      elapsed_time=T2-T1;

      ifelapsed_time(6)<0

        elapsed_time(6)=elapsed_time(6)+60; elapsed_time(5)=elapsed_time(5)-1;

      end

      ifelapsed_time(5)<0

        elapsed_time(5)=elapsed_time(5)+60;elapsed_time(4)=elapsed_time(4)-1;

      end %像這種程序當(dāng)然不考慮運(yùn)行上小時(shí)啦

      str2=sprintf('程序運(yùn)行耗時(shí) %d 小時(shí) %d 分鐘 %.4f ',elapsed_time(4),elapsed_time(5),elapsed_time(6));

      disp(str2);

      %初始化種群

      %采用二進(jìn)制Gray編碼,其目的是為了克服二進(jìn)制編碼的Hamming懸崖缺點(diǎn)

      function[initpop]=InitPopGray(popsize,bits)

      len=sum(bits);

      initpop=zeros(popsize,len);%Thewhole zero encoding individual

      fori=2:popsize-1

        pop=round(rand(1,len));

        pop=mod(([0 pop]+[pop 0]),2);

        %i=1時(shí),b(1)=a(1);i>1時(shí),b(i)=mod(a(i-1)+a(i),2)

        %其中原二進(jìn)制串:a(1)a(2)...a(n),Gray:b(1)b(2)...b(n)

        initpop(i,:)=pop(1:end-1);

      end

      initpop(popsize,:)=ones(1,len);%Thewhole one encoding individual

      %解碼

      function [fval]= b2f(bval,bounds,bits)

      %fval   - 表征各變量的十進(jìn)制數(shù)

      %bval   - 表征各變量的二進(jìn)制編碼串

      % bounds - 各變量的取值范圍

      %bits   - 各變量的二進(jìn)制編碼長(zhǎng)度

      scale=(bounds(:,2)-bounds(:,1))'./(2.^bits-1);%The range of the variables

      numV=size(bounds,1);

      cs=[0cumsum(bits)];

      for i=1:numV

      a=bval((cs(i)+1):cs(i+1));

      fval(i)=sum(2.^(size(a,2)-1:-1:0).*a)*scale(i)+bounds(i,1);

      end

      %選擇操作

      %采用基于輪盤(pán)賭法的非線(xiàn)性排名選擇

      %各個(gè)體成員按適應(yīng)值從大到小分配選擇概率:

      %P(i)=(q/1-(1-q)^n)*(1-q)^i,其中 P(0)>P(1)>...>P(n), sum(P(i))=1

      function[selectpop]=NonlinearRankSelect(FUN,pop,bounds,bits)

      global m n

      selectpop=zeros(m,n);

      fit=zeros(m,1);

      for i=1:m

        fit(i)=feval(FUN(1,:),(b2f(pop(i,:),bounds,bits)));%以函數(shù)值為適應(yīng)值做排名依據(jù)

      end

      selectprob=fit/sum(fit);%計(jì)算各個(gè)體相對(duì)適應(yīng)度(0,1)

      q=max(selectprob);%選擇最優(yōu)的概率

      x=zeros(m,2);

      x(:,1)=[m:-1:1]';

      [yx(:,2)]=sort(selectprob);

      r=q/(1-(1-q)^m);%標(biāo)準(zhǔn)分布基值

      newfit(x(:,2))=r*(1-q).^(x(:,1)-1);%生成選擇概率

      newfit=cumsum(newfit);%計(jì)算各選擇概率之和

      rNums=sort(rand(m,1));

      fitIn=1;newIn=1;

      whilenewIn<=m

        if rNums(newIn)<newfit(fitIn)

            selectpop(newIn,:)=pop(fitIn,:);

            newIn=newIn+1;

        else

            fitIn=fitIn+1;

        end

      end

      %交叉操作

      function[NewPop]=CrossOver(OldPop,pCross,opts)

      %OldPop為父代種群,pcross為交叉概率

      global m nNewPop

      r=rand(1,m);

      y1=find(r<pCross);

      y2=find(r>=pCross);

      len=length(y1);

      iflen>2&mod(len,2)==1%如果用來(lái)進(jìn)行交叉的染色體的條數(shù)為奇數(shù),將其調(diào)整為偶數(shù)

        y2(length(y2)+1)=y1(len);

        y1(len)=[];

      end

      iflength(y1)>=2

        fori=0:2:length(y1)-2

           if opts==0

               [NewPop(y1(i+1),:),NewPop(y1(i+2),:)]=EqualCrossOver(OldPop(y1(i+1),:),OldPop(y1(i+2),:));

           else

               [NewPop(y1(i+1),:),NewPop(y1(i+2),:)]=MultiPointCross(OldPop(y1(i+1),:),OldPop(y1(i+2),:));

           end

       end    

      end

      NewPop(y2,:)=OldPop(y2,:);

      %采用均勻交叉

      function[children1,children2]=EqualCrossOver(parent1,parent2)

      global nchildren1 children2

      hidecode=round(rand(1,n));%隨機(jī)生成掩碼

      crossposition=find(hidecode==1);

      holdposition=find(hidecode==0);

      children1(crossposition)=parent1(crossposition);%掩碼為1,父1為子1提供基因

      children1(holdposition)=parent2(holdposition);%掩碼為0,父2為子1提供基因

      children2(crossposition)=parent2(crossposition);%掩碼為1,父2為子2提供基因

      children2(holdposition)=parent1(holdposition);%掩碼為0,父1為子2提供基因

      %采用多點(diǎn)交叉,交叉點(diǎn)數(shù)由變量數(shù)決定

      function[Children1,Children2]=MultiPointCross(Parent1,Parent2)

      global nChildren1 Children2 VarNum

      Children1=Parent1;

      Children2=Parent2;

      Points=sort(unidrnd(n,1,2*VarNum));

      for i=1:VarNum

        Children1(Points(2*i-1):Points(2*i))=Parent2(Points(2*i-1):Points(2*i));

        Children2(Points(2*i-1):Points(2*i))=Parent1(Points(2*i-1):Points(2*i));

      end

      %變異操作

      function[NewPop]=Mutation(OldPop,pMutation,VarNum)

      global m nNewPop

      r=rand(1,m);

      position=find(r<=pMutation);

      len=length(position);

      if len>=1

        fori=1:len

           k=unidrnd(n,1,VarNum); %設(shè)置變異點(diǎn)數(shù),一般設(shè)置1點(diǎn)

           for j=1:length(k)

               if OldPop(position(i),k(j))==1

                  OldPop(position(i),k(j))=0;

               else

                  OldPop(position(i),k(j))=1;

               end

           end

        end

      end

      NewPop=OldPop;

      %倒位操作

      function[NewPop]=Inversion(OldPop,pInversion)

      global m nNewPop

      NewPop=OldPop;

      r=rand(1,m);

      PopIn=find(r<=pInversion);

      len=length(PopIn);

      if len>=1

        for i=1:len

            d=sort(unidrnd(n,1,2));

            if d(1)~=1&d(2)~=n

               NewPop(PopIn(i),1:d(1)-1)=OldPop(PopIn(i),1:d(1)-1);

               NewPop(PopIn(i),d(1):d(2))=OldPop(PopIn(i),d(2):-1:d(1));

               NewPop(PopIn(i),d(2)+1:n)=OldPop(PopIn(i),d(2)+1:n);

           end

        end

      end

      遺傳算法程序(二):

      functionyouhuafun

      D=code;

      N=50;        % Tunable

      maxgen=50;    % Tunable

      crossrate=0.5;%Tunable

      muterate=0.08;%Tunable

      generation=1;  

      num = length(D);

      fatherrand=randint(num,N,3);

      score =zeros(maxgen,N);

      whilegeneration<=maxgen

       ind=randperm(N-2)+2; % 隨機(jī)配對(duì)交叉

       A=fatherrand(:,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)交叉

      %    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;

       

        %評(píng)價(jià)、選擇

       scoreN=scorefun(fatherrand,D);% 求得N個(gè)個(gè)體的評(píng)價(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)=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('各代的目標(biāo)函數(shù)值');

      F4=D(:,4);

      FF4=F4-fatherrand(:,1);

      FF4=max(FF4,1);

      D(:,5)=FF4;

      save DData D

      function D=code

      load youhua.mat

      % properties F2and 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 groupproperty 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];

      functionScoreN=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;

      % 這里有待優(yōu)化

      for k=1:N

       FF4k=FF4rnd(:,k);

        forite=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;

      遺傳算法程序(三):

      %IAGA

      function best=ga

      clear

      MAX_gen=200;           %最大迭代步數(shù)

      best.max_f=0;          %當(dāng)前最大的適應(yīng)度

      STOP_f=14.5;           %停止循環(huán)的適應(yīng)度

      RANGE=[0255];          %初始取值范圍[0 255]

      SPEEDUP_INTER=5;      %進(jìn)入加速迭代的間隔

      advance_k=0;           %優(yōu)化的次數(shù)

      popus=init;            %初始化

      forgen=1:MAX_gen

        fitness=fit(popus,RANGE);       %求適應(yīng)度

        f=fitness.f;

        picked=choose(popus,fitness);   %選擇

        popus=intercross(popus,picked); %雜交

        popus=aberrance(popus,picked); %變異

        if max(f)>best.max_f

            advance_k=advance_k+1;

            x_better(advance_k)=fitness.x;

            best.max_f=max(f);

            best.popus=popus;

            best.x=fitness.x;

        end

        if mod(advance_k,SPEEDUP_INTER)==0

            RANGE=minmax(x_better);

           

            RANGE

           

            advance=0;

        end

      end

      return;

      functionpopus=init%初始化

      M=50;%種群個(gè)體數(shù)目

      N=30;%編碼長(zhǎng)度

      popus=round(rand(M,N));

      return;

      functionfitness=fit(popus,RANGE)%求適應(yīng)度

      [M,N]=size(popus);

      fitness=zeros(M,1);%適應(yīng)度

      f=zeros(M,1);%函數(shù)值

      A=RANGE(1);B=RANGE(2);%初始取值范圍[0 255]

      for m=1:M

        x=0;

        for n=1:N

            x=x+popus(m,n)*(2^(n-1));

        end

        x=x*((B-A)/(2^N))+A;

        for k=1:5

            f(m,1)=f(m,1)-(k*sin((k+1)*x+k));

        end

      end

      f_std=(f-min(f))./(max(f)-min(f));%函數(shù)值標(biāo)準(zhǔn)化

      fitness.f=f;fitness.f_std=f_std;fitness.x=x;

      return;

      functionpicked=choose(popus,fitness)%選擇

      f=fitness.f;f_std=fitness.f_std;

      [M,N]=size(popus);

      choose_N=3;                %選擇choose_N對(duì)雙親

      picked=zeros(choose_N,2);  %記錄選擇好的雙親

      p=zeros(M,1);              %選擇概率

      d_order=zeros(M,1);

      %把父代個(gè)體按適應(yīng)度從大到小排序

      f_t=sort(f,'descend');%將適應(yīng)度按降序排列

      for k=1:M

        x=find(f==f_t(k));%降序排列的個(gè)體序號(hào)

        d_order(k)=x(1);

      end

      for m=1:M

        popus_t(m,:)=popus(d_order(m),:);

      end

      popus=popus_t;

      f=f_t;

      p=f_std./sum(f_std);                   %選擇概率

      c_p=cumsum(p)';                         %累積概率

      forcn=1:choose_N

        picked(cn,1)=roulette(c_p); %輪盤(pán)賭

        picked(cn,2)=roulette(c_p); %輪盤(pán)賭

        popus=intercross(popus,picked(cn,:));%雜交

      end

      popus=aberrance(popus,picked);%變異

      return;

      functionpopus=intercross(popus,picked) %雜交

      [M_p,N_p]=size(picked);

      [M,N]=size(popus);

      for cn=1:M_p

        p(1)=ceil(rand*N);%生成雜交位置

        p(2)=ceil(rand*N);

        p=sort(p);

        t=popus(picked(cn,1),p(1):p(2));

        popus(picked(cn,1),p(1):p(2))=popus(picked(cn,2),p(1):p(2));

        popus(picked(cn,2),p(1):p(2))=t;

      end

      return;

      functionpopus=aberrance(popus,picked) %變異

      P_a=0.05;%變異概率

      [M,N]=size(popus);

      [M_p,N_p]=size(picked);

      U=rand(1,2);

      for kp=1:M_p

        if U(2)>=P_a        %如果大于變異概率,就不變異

            continue;

        end

        if U(1)>=0.5

            a=picked(kp,1);

        else

            a=picked(kp,2);

        end

        p(1)=ceil(rand*N);%生成變異位置

        p(2)=ceil(rand*N);

        if popus(a,p(1))==1%0 1變換

            popus(a,p(1))=0;

        else

            popus(a,p(1))=1;

        end

        if popus(a,p(2))==1

            popus(a,p(2))=0;

        else

            popus(a,p(2))=1;

        end

      end

      return;

      functionpicked=roulette(c_p) %輪盤(pán)賭

      [M,N]=size(c_p);

      M=max([M N]);

      U=rand;

      if U<c_p(1)

        picked=1;

        return;

      end

      for m=1:(M-1)

        if U>c_p(m) & U<c_p(m+1)

            picked=m+1;

            break;

        end

      end

      全方位的兩點(diǎn)雜交、兩點(diǎn)變異的改進(jìn)的加速遺傳算法(IAGA


      CopyRight ? 2016 宣城市信息工程學(xué)校 地址:宣城市梅溪路902號(hào) 后臺(tái)登錄

      郵編:242000 電話(huà)(傳真):0563-2629508 如有問(wèn)題,歡迎惠賜 Email:xclmq@163.com

      日韩无码第一页,一区_97资源人妻碰碰碰_有码无码人妻Av_午夜亚洲国产理论片2020网站 亚洲无码片AV一级 岛国av无码专区免费看
      <style id="uedzd"></style>
    3. <style id="uedzd"></style>
    4. <dfn id="uedzd"></dfn>
      • <dfn id="uedzd"></dfn>
        1. <ruby id="uedzd"></ruby>
          <ruby id="uedzd"></ruby>