遗传算法中的交叉配对 Posted on 2018-09-22 | In 遗传算法 | 选择DNA的部分进行交叉配对 123456789101112131415161718192021222324252627282930313233import numpy as npDNA_SIZE =10POP_SIZE=10pop = np.random.randint(2, size=[POP_SIZE,DNA_SIZE])print(pop)for parent in pop: print('parent:',parent) #依次选择pop中的DNA进行交叉 i_ = np.random.randint(0, POP_SIZE,size=1) # 选取pop中某一个DNA print('选择pop中第:',i_,'个DNA') print('任意生成DNA:',np.random.randint(0,2,size=DNA_SIZE)) # 任意生成DNA # 利用任意生成的DNA产生交叉点,为布尔值组成的列表 cross_points = np.random.randint(0, 2, size=DNA_SIZE).astype(np.bool) print('cosspoint:',cross_points) # 生成交叉点 print('截取第',i_,'个DNA的片段',pop[i_, cross_points]) parent[cross_points] = pop[i_,cross_points] print('cross_parent:',parent) print('#######') 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163输出:[[1 0 1 0 1 0 1 1 0 1][0 1 1 0 0 1 0 1 1 1][1 1 0 1 0 0 0 1 1 0][0 0 1 1 0 1 1 1 0 1][1 0 0 0 1 0 1 1 1 1][1 1 1 0 1 0 1 0 0 0][0 0 1 1 0 1 1 0 0 1][0 0 0 0 0 1 1 1 0 0][1 0 0 1 1 1 1 1 0 0][1 1 1 1 0 0 1 0 0 0]]parent: [1 0 1 0 1 0 1 1 0 1]选择pop中第: [0] 个DNA任意生成DNA: [1 1 1 1 0 0 0 1 0 0]cosspoint: [ True True False True False False True False False True]截取第 [0] 个DNA的片段 [1 0 0 1 1]cross_parent: [1 0 1 0 1 0 1 1 0 1]#######parent: [0 1 1 0 0 1 0 1 1 1]选择pop中第: [4] 个DNA任意生成DNA: [0 1 0 1 0 0 0 0 1 1]cosspoint: [ True False True False False True False False False True]截取第 [4] 个DNA的片段 [1 0 0 1]cross_parent: [1 1 0 0 0 0 0 1 1 1]#######parent: [1 1 0 1 0 0 0 1 1 0]选择pop中第: [2] 个DNA任意生成DNA: [0 1 1 1 1 0 1 1 1 1]cosspoint: [ True False True True False True True True False False]截取第 [2] 个DNA的片段 [1 0 1 0 0 1]cross_parent: [1 1 0 1 0 0 0 1 1 0]#######parent: [0 0 1 1 0 1 1 1 0 1]选择pop中第: [2] 个DNA任意生成DNA: [1 1 1 1 0 0 0 1 1 0]cosspoint: [ True False True True True False False True True False]截取第 [2] 个DNA的片段 [1 0 1 0 1 1]cross_parent: [1 0 0 1 0 1 1 1 1 1]#######parent: [1 0 0 0 1 0 1 1 1 1]选择pop中第: [4] 个DNA任意生成DNA: [1 1 0 0 1 0 0 1 1 0]cosspoint: [False True False False False True False False False False]截取第 [4] 个DNA的片段 [0 0]cross_parent: [1 0 0 0 1 0 1 1 1 1]#######parent: [1 1 1 0 1 0 1 0 0 0]选择pop中第: [5] 个DNA任意生成DNA: [1 1 1 1 0 0 1 0 0 1]cosspoint: [ True False True False False False False True False True]截取第 [5] 个DNA的片段 [1 1 0 0]cross_parent: [1 1 1 0 1 0 1 0 0 0]#######parent: [0 0 1 1 0 1 1 0 0 1]选择pop中第: [9] 个DNA任意生成DNA: [0 1 1 1 0 1 0 0 0 0]cosspoint: [ True False True True True True True True True False]截取第 [9] 个DNA的片段 [1 1 1 0 0 1 0 0]cross_parent: [1 0 1 1 0 0 1 0 0 1]#######parent: [0 0 0 0 0 1 1 1 0 0]选择pop中第: [6] 个DNA任意生成DNA: [1 1 1 1 1 0 1 1 0 1]cosspoint: [ True True False True False True True True False False]截取第 [6] 个DNA的片段 [1 0 1 0 1 0]cross_parent: [1 0 0 1 0 0 1 0 0 0]#######parent: [1 0 0 1 1 1 1 1 0 0]选择pop中第: [3] 个DNA任意生成DNA: [1 1 0 1 0 0 1 1 0 1]cosspoint: [ True False False True False False False True True True]截取第 [3] 个DNA的片段 [1 1 1 1 1]cross_parent: [1 0 0 1 1 1 1 1 1 1]#######parent: [1 1 1 1 0 0 1 0 0 0]选择pop中第: [1] 个DNA任意生成DNA: [0 1 0 1 1 0 1 1 0 1]cosspoint: [ True True True False False False True False False True]截取第 [1] 个DNA的片段 [1 1 0 0 1]cross_parent: [1 1 0 1 0 0 0 0 0 1]#######Process finished with exit code 0 参考自 https://www.jianshu.com/p/adc995053ad8