TypeError Traceback (most recent call last) TypeError: only size-1 arrays can be converted to Python scalars The above exception was the direct cause of the following exception: ValueError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_20004\3101636103.py in <module> ----> 1 class ImmuneGA: 2 def __init__(self, pop_size, gene_size): 3 #定义了两个参数,分别为pop_size和gene_size,用于指定遗传算法中种群的大小和个体基因的长度。 4 self.pop_size = pop_size 5 self.gene_size = gene_size ~\AppData\Local\Temp\ipykernel_20004\3101636103.py in ImmuneGA() 32 # 计算适应度值 33 for i in range(population_size): ---> 34 fitness_values[i] = fitness_function(population[i]) 35 36 # 更新最佳个体 ValueError: setting an array element with a sequence.
时间: 2024-02-14 10:26:49 浏览: 145
Unit-18-Practical-task
这个错误是由于尝试将一个序列赋值给数组元素而引起的。在您的代码中,fitness_values是一个数组,而fitness_function(population[i])返回的是一个序列,因此出现了这个错误。您需要确保fitness_function返回的是单个值而不是序列。可以检查一下您的fitness_function的实现,确保它返回一个标量值。
阅读全文