【问题标题】:MultiPoint crossover using Numpy使用 Numpy 的多点交叉
【发布时间】:2019-04-23 04:39:06
【问题描述】:

我正在尝试使用 numpy 对遗传算法群体进行交叉。 我已经使用父 1 和父 2 对人口进行了切片。

population = np.random.randint(2, size=(4,8))
p1 = population[::2]
p2 = population[1::2]

但我无法找出任何 lambda 或 numpy 命令来对父母进行多点交叉。 这个概念是用ith row of p1随机交换一些位与ith row of p2

【问题讨论】:

    标签: numpy random genetic-algorithm numpy-ndarray crossover


    【解决方案1】:

    我认为您想从 p1 和 p2 中随机选择一个单元格。

    为了更容易理解,我将 p1 更改为 10 到 15,将 p2 更改为 20 到 25。p1 和 p2 是在这些范围内随机生成的。

    p1
    Out[66]: 
    array([[15, 15, 13, 14, 12, 13, 12, 12],
           [14, 11, 11, 10, 12, 12, 10, 12],
           [12, 11, 14, 15, 14, 10, 13, 10],
           [11, 12, 10, 13, 14, 13, 12, 13]])
    
    In [67]: p2
    Out[67]: 
    array([[23, 25, 24, 21, 24, 20, 24, 25],
           [21, 21, 20, 20, 25, 22, 24, 22],
           [24, 22, 25, 20, 21, 22, 21, 22],
           [22, 20, 21, 22, 25, 23, 22, 21]])
    
    In [68]: sieve=np.random.randint(2, size=(4,8))
    In [69]: sieve
    Out[69]: 
    array([[0, 1, 0, 1, 1, 0, 1, 0],
           [1, 1, 1, 0, 0, 1, 1, 1],
           [0, 1, 1, 0, 0, 1, 1, 0],
           [0, 0, 0, 1, 1, 1, 1, 1]])
    In [70]: not_sieve=sieve^1  # Complement of sieve
    
    In [71]: pn = p1*sieve + p2*not_sieve
    
    In [72]: pn
    Out[72]:
    array([[23, 15, 24, 14, 12, 20, 12, 25],
           [14, 11, 11, 20, 25, 12, 10, 12],
           [24, 11, 14, 20, 21, 10, 13, 22],
           [22, 20, 21, 13, 14, 13, 12, 13]])
    

    当 sieve 为 1 时,teens 中的数字来自 p1 20 的数字来自 sieve 为 0 时的 p2

    这可能会变得更有效率,但这是您期望的输出吗?

    【讨论】:

      猜你喜欢
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2016-03-20
      • 2020-06-23
      • 2011-09-15
      • 1970-01-01
      • 2018-03-22
      相关资源
      最近更新 更多