• Matlab遗传算法的部分映射交叉算子(pmx)源码


    01.

    02.% PMX means Goldberg's Partially Mapped CroSsover)

    03.% Procedure :PMX

    04.% Step1. Select two positions along the string uniformly at random.

    05.%        The substrings defined by the two positions are called the mapping sections.

    06.%        Note:(You can write to select a start point and a length) 

    07.% Step2. Exchange two substrings between parents to produce proto-children.

    08.% Step3. Determine the mapping relationship between two mapping section.

    09.% Step4. Legalize offspring with the mapping relationship.

    10.function [newVa,newVb]=PMX1(Va,Vb)

    11.fprintf('original Va and Vb are:\n')

    12.%Va= [ 1 6 10 3  9 4 5 2 7 8 ];

    13.%Vb= [ 2 9  1 4 10 5 6 8 3 7 ];

    14.%Va=1:9

    15.%Vb=[5 4 6 9 2 1 7 8 3]

    16.%--------------------------------------------------------------------------------

    17.%Step1. Select two positions along the string uniformly at random.

    18.startXorPoint=mod(ceil(rand(1)*10),length(Va) );

    19.if startXorPoint==0

    20.   startXorPoint=startXorPoint+1;

    21.end   

    22.xorLength=mod(floor(rand(1)*10),length(Va));

    23.endXorPoint=startXorPoint+xorLength;

    24.while(endXorPoint>length(Vb) )

    25.   xorLength=mod(floor(rand(1)*10),length(Va));

    26.   endXorPoint=startXorPoint+xorLength;

    27.end   

    28.fprintf('\n The (startXorPoint,endXorPoint)=(%d,%d)\n',startXorPoint,endXorPoint)

    29.%startXorPoint=3

    30.%endXorPoint=6

    31.%--------------------------------------------------------------------------------

    32.% Step2. Exchange two substrings between parents to produce proto-children.

    33.temp1=Va(startXorPoint:endXorPoint);

    34.temp2=Vb(startXorPoint:endXorPoint);

    35.Va(startXorPoint:endXorPoint)=temp2;

    36.Vb(startXorPoint:endXorPoint)=temp1;

    37.clear temp1;

    38.clear temp2;

    39.fprintf('The exchanged Va and Vb are:\n')

    40.Va

    41.Vb

    42.%--------------------------------------------------------------------------------

    43.% Step3. Determine the mapping relationship between two mapping section.

    44.temp1=Va(startXorPoint:endXorPoint);

    45.temp2=Vb(startXorPoint:endXorPoint);

    46.for ix=1:length(temp1)

    47.    rawMapRelation(ix,1:2)=[Va(startXorPoint+ix-1),Vb(startXorPoint+ix-1)];

    48.end  

    49.rawMapRelation

    50.%rawMapRelation=[6 3;9 4;2 5;1 6;3 7]

    51.rowIndex=1;

    52.colIndex=1;

    53.while( rowIndex<=size(rawMapRelation,1) )

    54.   while( colIndex<=size(rawMapRelation,2) )

    55.      rawMapRelation(rowIndex,colIndex ) 

    56.      [i,j]=find(rawMapRelation==rawMapRelation(rowIndex,colIndex ) )  ; 

    57.          if(length(i)>1)

    58.              if( j(1)

    59.                 tempResult=[rawMapRelation(i(2),:), rawMapRelation(i(1),:)];

    60.                   k=1

    61.                    while k

    62.                          if tempResult(1,k)==tempResult(1,k+1)

    63.                          tempResult(k:k+1)=[];

    64.                          end   

    65.                    k=k+1;

    66.                    end

    67.                   tempResult       

    68.                  rawMapRelation(i,:)=[];

    69.                  rawMapRelation(size(rawMapRelation,1)+1,1:2)=tempResult;                  

    70.                  

    71.              else 

    72.                  tempResult=[rawMapRelation(i(1),:), rawMapRelation(i(2),:)];

    73.                   k=1

    74.                    while k

    75.                          if tempResult(1,k)==tempResult(1,k+1)

    76.                          tempResult(k:k+1)=[];

    77.                          end   

    78.                    k=k+1;

    79.                    end

    80.                   tempResult         

    81.                  rawMapRelation(i,:)=[];

    82.                  rawMapRelation(size(rawMapRelation,1)+1,1:2)=tempResult;  

    83.       

    84.              end

    85.           end   

    86.          if(length(i)==1 & length(j)==1) 

    87.             colIndex=colIndex+1;

    88.          else

    89.             rowIndex=1

    90.             colIndex=1;

    91.          end   

    92.   end 

    93.      rowIndex=rowIndex+1;

    94.   end   

    95.      colIndex=1;%Reset

    96.  

    97.rawMapRelation

    98.tMap=[rawMapRelation;fliplr(rawMapRelation)]

    99.Map=tMap'

    100.fprintf('\n The (startXorPoint,endXorPoint)=(%d,%d)\n',startXorPoint,endXorPoint)

    101.Va

    102.Vb

    103.%--------------------------------------------------------------------------------

    104.% Step4. Legalize offspring with the mapping relationship.

    105.if startXorPoint~=1

    106.   for i=1:startXorPoint-1      

    107.      [r,c]=find(Map(1,:)==Va(1,i)) ;  

    108.      if ~isempty(r) & ~isempty(c)

    109.         Va(1,i)=Map(r+1,c);

    110.      end   

    111.      [r1,c1]=find(Map(1,:)==Vb(1,i));   

    112.      if ~isempty(r1) & ~isempty(c1)

    113.         Vb(1,i)=Map(r1+1,c1);

    114.      end   

    115.   end

    116.end

    117.if endXorPoint~=length(Va)

    118.   for i=endXorPoint+1:length(Va)

    119.      [r,c]=find(Map(1,:)==Va(1,i));   

    120.      if ~isempty(r) & ~isempty(c)

    121.         Va(1,i)=Map(r+1,c);

    122.      end   

    123.      [r1,c1]=find(Map(1,:)==Vb(1,i)) ;  

    124.      if ~isempty(r1) & ~isempty(c1)

    125.         Vb(1,i)=Map(r1+1,c1);

    126.      end   

    127.   end   

    128.end   

    129.fprintf('The final Va and Vb are:\n')

    130.newVa=Va

    131.newVb=Vb

    132.
     

  • 相关阅读:
    网络安全(黑客)自学
    华为ac+fit漫游配置案例
    Spark内部原理之运行原理一
    在华为和比亚迪干了5年测试,月薪25K,熬夜总结出来的划水经验.....
    大学生静态HTML鲜花网页设计作品 DIV布局网上鲜花介绍网页模板代码 DW花店网站制作成品 web网页制作与实现
    中秋时节赏明月,五子棋戏月饼趣 — Flutter中秋限定版五子棋
    设信号x(t)=cos(2π×50t)+2×cos(2π×400t),试将它的两个频率分量分离,并绘制它们的时域波形及频谱图
    SpringBoot自动配置原理及启动流程
    想拿高薪?先避开这几个坑!
    【软考】-- 操作系统(上)
  • 原文地址:https://blog.csdn.net/zzjlhlcd/article/details/127644896