类型错误:只有整数标量数组可以转换为标量索引 - 同时在交叉函数中合并两个 numpy 数组 我收到的错误/输出

问题描述

我正在尝试在遗传算法的交叉函数中合并两个 numpy 数组。当我使用以下功能时,我正面临这个错误。 交叉函数用于遗传算法的主要驱动函数

def crossover(couple):
    ancestor1 = couple[0]
    ancestor2 = couple[1]

   
    c1,c2 = ancestor1.copy(),ancestor2.copy()
    
    pt = random.randint(1,len(ancestor1)-2)
    # perform crossover
    print(ancestor1[:pt])
    print(ancestor2[pt:])
    
    c1 = np.concatenate(ancestor1[:pt],ancestor2[pt:])
    c2 = np.concatenate(ancestor2[:pt],ancestor1[pt:])

    return [c1,c2]
  

我收到的错误/输出

[[-0.9755204  -1.2085407  -1.56664802  1.45028755  0.27840493]
 [ 0.43001932 -0.45127276 -1.57321509 -1.65487651  0.46746863]
 [ 0.92472237 -0.44634248 -0.34847973  0.08923225  0.5979351 ]
 [-0.87931023 -1.3329496  -1.1219304   0.21738985 -1.10573347]
 [ 0.67267637  0.28836078 -1.77171789 -1.76432283  0.94284975]
 [-0.4975102  -0.91719893  1.8001638  -1.34945301  1.89547442]
 [ 1.82821105  0.68452817  0.9447278  -1.97638976  0.96546824]]
[[ 1.41115649  1.87721758 -0.43987672  0.34670115  0.49645554]
 [ 0.33425397  1.48035246 -0.19784205  0.99582527  0.83447731]
 [-1.51148388 -0.06211828 -0.03352744 -1.37389412 -1.25469939]]

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-36-b3accedf9ca8> in <module>
----> 1 runGA(10,5,list(zip(np.ones(5)*-2,np.ones(5)*2)),fopt13,4,0.4,25)

<ipython-input-12-6619b9c7d476> in runGA(npop,ngenes,limits,fitness,nelitism,mutp,ngenerations)
      8     # TO DO: Complete your GA!
      9         couples = couples_selection(new_pop,nelitism)
---> 10         popp = get_offspring(couples,limits)
     11         eval_pop_result = eval_pop(pop,fitness)
     12 

<ipython-input-35-ba6d73ba2587> in get_offspring(couples,limits)
     35 def get_offspring(couples,limits):
     36 
---> 37     children = [crossover(couple) for couple in couples]
     38     mutation_roulette = [choice([True,False],1,p=[mutp,1-mutp]) for _ in children]
     39     children_roulette = list(zip(children,mutation_roulette))

<ipython-input-35-ba6d73ba2587> in <listcomp>(.0)
     35 def get_offspring(couples,mutation_roulette))

<ipython-input-35-ba6d73ba2587> in crossover(couple)
     26     print(ancestor2[pt:])
     27 
---> 28     c1 = np.concatenate(ancestor1[:pt],ancestor2[pt:])
     29     c2 = np.concatenate(ancestor2[:pt],ancestor1[pt:])
     30 

<__array_function__ internals> in concatenate(*args,**kwargs)

TypeError: only integer scalar arrays can be converted to a scalar index


任何帮助将不胜感激

解决方法

def crossover(couple):
    ancestor1 = couple[0]
    ancestor2 = couple[1]

    # TO DO: Complete the crossover function according to what's described in section 5.5.1 and figure 52 of the subject's materials.
    c1,c2 = ancestor1.copy(),ancestor2.copy()
    
    pt = random.randint(1,len(ancestor1)-2)
    # perform crossover    
    x1 = list(ancestor1[:pt])
    x2 = list(ancestor1[pt:]) 
    x3 = list(ancestor2[:pt]) 
    x4 = list(ancestor2[pt:])
    
    c1 = np.array(x1 + x4)
    c2 = np.array(x3 + x2)
    # END OF TO DO
    return np.array([c1,c2])