我正在用python 3解決一個遺傳算法的問題。我還沒有完成完整的代碼。每當我完成它時,我都會測試一部分代碼。TypeError:實例之間不支持「<」Python
目前我被困在一個錯誤說:
TypeError: '<' not supported between instances of 'part' and 'part'
有趣的這個錯誤並不總是顯示。有時代碼運行平穩並顯示所需的輸出,但有時會顯示此錯誤。
這是什麼原因?請幫幫我。我附上代碼和錯誤消息。我正在使用PyCharm。
import random
class part():
def __init__(self, number):
self.number = number
self.machine_sequence = []
def add_volume(self, volume):
self.volume = volume
def add_machine(self, machine_numbers):
self.machine_sequence.append(machine_numbers)
def create_initial_population():
part_family = []
for i in range(8):
part_family.append(part(i))
part_population = []
for i in range(6):
part_population.append(random.sample(part_family, len(part_family)))
for i in part_population:
for j in i:
j.add_volume(random.randrange(100, 200))
return part_population
def fitness(part_family):
sum_of_boundary = []
for i in range(0, 8, 2):
sum_of_boundary.append(sum(j.volume for j in part_family[i:i + 2]))
fitness_value = 0
for i in range(len(sum_of_boundary) - 1):
for j in range(i + 1, len(sum_of_boundary)):
fitness_value = fitness_value + abs(sum_of_boundary[i] - sum_of_boundary[j])
return fitness_value
def sort_population_by_fitness(population):
pre_sorted = [[fitness(x),x] for x in population]
sort = [x[1] for x in sorted(pre_sorted)]
for i in sort:
for j in i:
print(j.volume, end = ' ')
print()
return sort
def evolve(population):
population = sort_population_by_fitness(population)
return population
population = create_initial_population()
population = evolve(population)
錯誤消息:
輸出是(這是隨機每次):
我可以建議你下載並使用[命令提示符,它允許複製(https://conemu.github.io/),所以你不需要不必要的屏幕截圖添加到您的問題。 –