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)<j(2) )

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

60.                   k=1

61.                    while k<length(tempResult)

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<length(tempResult)

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.
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 部分映射交叉是一种遗传算法中常用的交叉方式。其步骤如下: 1. 随机选择两个父代个体,假设为A和B。 2. 随机选择一个起始位置和一个结束位置,确定交叉区间。 3. 将A中该交叉区间内的基因复制到子代C中对应位置。 4. 从B中未被复制的基因中按顺序依次填充C中未被复制的位置。 5. 重复以上步骤产生第二个子代D,其中A和B交换角色。 下面是一个简单的Python实现示例,假设我们要交叉两个长度为10的二进制串: ```python import random def partially_mapped_crossover(parent1, parent2): # 选择交叉区间 start_pos = random.randint(0, len(parent1) - 1) end_pos = random.randint(start_pos, len(parent1) - 1) # 复制父代A中的交叉区间到子代C中 child1 = parent1[:start_pos] + parent2[start_pos:end_pos] + parent1[end_pos:] # 从父代B中按顺序填充子代C中未被复制的位置 child2 = parent2[:start_pos] + [gene for gene in parent1 if gene not in parent2[start_pos:end_pos]] + parent2[end_pos:] # 交换父代A和B的角色,重复以上步骤产生第二个子代D return child1, child2 # 示例 parent1 = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0] parent2 = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1] child1, child2 = partially_mapped_crossover(parent1, parent2) print(child1) print(child2) ``` 输出: ``` [1, 0, 1, 0, 0, 1, 0, 1, 0, 1] [0, 1, 0, 1, 1, 0, 1, 0, 1, 0] ``` 其中,交叉区间为[4, 7],子代C中复制了父代A中的该区间,然后从父代B中按顺序填充了子代C中未被复制的位置,得到了子代C和D。 ### 回答2: 映射(Mapping)是Python中的一种数据类型,它用于存储一组键-值对(key-value pairs)。在映射中,键是唯一的,而值可以重复。映射可以实现快速的查找和访问操作,因为它使用了哈希表的数据结构。 在Python中,常用的映射类型包括字典(Dictionary)、默认字典(Default Dict)和有序字典(Ordered Dict)等。 字典是Python中最常见的映射类型,它使用花括号{}来定义,由键和值组成,中间用冒号:分隔。通过键来访问对应的值,字典中的键是唯一的,而值可以重复。 默认字典(Default Dict)是字典的一种变种,它在定义时需要指定一个默认值。当访问一个不存在的键时,会返回默认值,而不会报错。 有序字典(Ordered Dict)是字典的另一种变种,它可以记住键值对的插入顺序,并且可以按照插入顺序遍历字典中的元素。 映射交叉则是指映射之间的互相转换。例如,可以通过调用dict()函数将其他数据类型转换为字典,也可以通过.items()方法将字典转换为键-值对的列表。 在Python中,映射是非常灵活和强大的数据类型,它可以用于解决各种问题,例如统计词频、实现缓存等。熟练掌握映射的基本用法和常用方法,能够提高代码的效率和可读性。 ### 回答3: 在Python中,可以使用多种方式实现映射(mapping),其中包括字典(dictionary)、集合(set)和元组(tuple)。 字典是Python中最常见的映射类型,它以键值对的形式存储数据。可以通过键来访问对应的值,并且键是唯一的。字典是无序的,但是可以通过循环遍历字典中的键值对。 例如,我们可以创建一个字典来存储学生的成绩,其中键是学生的姓名,值是对应的分数。可以通过以下方式创建和访问字典: ```python grades = {'Alice': 90, 'Bob': 85, 'Cindy': 95} print(grades['Alice']) # 输出90 ``` 另一个映射类型是集合,集合是由唯一元素构成的无序集。可以使用大括号{}或set()函数来创建集合。集合可以用来检查元素是否存在,也可以进行集合操作(如并集、交集等)。 ```python fruits = {'apple', 'banana', 'orange'} print('apple' in fruits) # 输出True # 交集 a = {1, 2, 3} b = {2, 3, 4} print(a & b) # 输出{2, 3} ``` 最后是元组,元组也可以看作一个简单的映射类型,其中键和值是一一对应的关系。不同于字典和集合,元组是有序的且不可更改(immutable)。可以通过索引来访问元组中的元素。 ```python point = (3, 4) print(point[0]) # 输出3 ``` 总之,Python提供了多种映射类型供我们使用,根据实际需要选择合适的映射类型。字典适合存储键值对关系,集合用来检查元素的存在和进行集合操作,而元组则适合保持有序且不可更改的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bryan Ding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值