簡體   English   中英

交叉失敗的二進制編碼字符串

[英]Cross-over is failing binary encoded string

我正在實施 G.netic 算法 (GA)。 43個號碼[救護車位置]可供選擇(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39) , 我選擇了3地方,因為我有3輛救護車.

我只能將我的救護車停在1-39 locations個地點中的3地點(限制)

染色體樣本: [000010000000000000100000000000100000000] 這代表我要把我的Ambulance放在5th, 19th, and 31 positions. 5th位、第19th位和第31位的位是1 ,而 rest 位是0 換句話說,我正在打開5-bit, 19-bit, and 31-bit

比方說

Parent1 (111000000000000000000000000000000000000) 

Parent2 (000000000000000000000000000000000000111)

cross-over ,我得到這個:

('111000000000000000000000000000000000111', '000000000000000000000000000000000000000')

在第一個后代中,我有six 1's ,在第二個后代中,我Zero 1's 這些generated off-springs對我來說是illegal的,因為我只需要three 1's后代字符串。

我正在使用單點交叉。 這是我的代碼:

from typing import Union
import random

Parent 1 ="111000000000000000000000000000000000000"
Parent 2 ="000000000000000000000000000000000000111"


def crossover(cs1: str, cs2: str) -> Union[str, str]:
    index: int = random.randint(0, len(cs1))
    return cs1[:index] + cs2[index:], cs2[:index] + cs1[index:]

crossover(Cs1,Cs2)

在保持1-39 bits中的3位的同時執行交叉的好方法是什么?

IIUC,你想隨機混合兩個父母,正好保持 3'1's?

您可以隨機獲取每個父項中的 1 和 select 的索引:

import random

Parent1 ="111000000000000000000000000000000000000"
Parent2 ="000000000000000000000000000000000000111"

def indices(s):
    return {i for i,c in enumerate(s) if c=='1'}

def crossover(p1, p2, k=3):

    idx = set(random.sample(list(indices(p1) | indices(p2)), k=k))

    return ''.join('1' if i in idx else '0' for i in range(len(p1)))

out = crossover(Parent1, Parent2, k=Parent1.count('1'))
# '110000000000000000000000000000000000100'

如果您想為兩個字符串中均為 1 的 position 賦予更多權重,您可以修改上面的內容以使用Counter代替集合:

import random
from collections import Counter

Parent1 ="111000000000000000000000000000000000000"
Parent2 ="000000000000000000000000000000000000111"

def indices(s):
    return Counter(i for i,c in enumerate(s) if c=='1')

def crossover(p1, p2, k=3):

    # count the number of 1 per position
    pool = indices(p1) | indices(p2)
    
    # randomly select indices
    # using the counts as weights
    idx = set(random.sample(list(pool),
                            counts=pool.values(),
                            k=k))

    return ''.join('1' if i in idx else '0' for i in range(len(p1)))

out = crossover(Parent1, Parent2, k=Parent1.count('1'))
# '010000000000000000000000000000000000101'

在兩個后代之間洗牌:

使用集合操作

import random

def indices(s):
    return {i for i,c in enumerate(s) if c=='1'}

def crossover(p1, p2):
    # get positions of 1s for each string
    idx1 = indices(p1)
    idx2 = indices(p2)
    
    # positions that are different in both strings
    differ = idx1.symmetric_difference(idx2)
    # identical positions
    common = idx1&idx2
    
    # pick half of the different positions randomly
    select = set(random.sample(list(differ), k=len(differ)//2))
    
    # offspring 1 get those positions + the common ones
    select1 = select | common
    # offstring 2 gets the other positions + the common ones
    select2 = (differ-select) | common
    
    # make strings from the selected positions for each offspring
    out1 = ''.join('1' if i in select1 else '0' for i in range(len(p1)))
    out2 = ''.join('1' if i in select2 else '0' for i in range(len(p1)))
    
    return out1, out2
    
crossover(Parent1, Parent2)

例如 output:

('101000000000000000000000000000000000001',
 '010000000000000000000000000000000000110')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM