TypeError: '<' is not supported between Python instances

I am solving a genetic algorithm problem in python 3. I have not finished the complete code yet. I check part of the code when I complete it.

I am currently stuck in an error:

TypeError: '<' is not supported between instances of "part" and "part"

Interestingly, this error is not always displayed. Sometimes the code runs smoothly and shows the desired result, but sometimes it shows this error.

What is the reason for this? Please help me. I am attaching a code and an error message. I am using 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) 

error message: enter image description here

Output (which is randomly distributed each time): enter image description here

+5
source share
2 answers

Given that pre_sorted is a list of lists with [fitness, part] elements, this yells whenever two sublists are compared with the same fitness .

Python lists are sorted lexicographically and compared differently from left to right until a mismatch element is detected. In your case, the second part is only available if the suitability of the two parts is the same.

  • [0, part0] < [1, part1] => does not compare part0 and part1 , since the fitness is already different.
  • [0, part0] < [0, part1] => makes a comparison of part0 and part1 , since the fitness is the same.

Suggestion 1:

Sort by fitness only: sorted(pre_sorted, key=operator.itemgetter(0))

Proposition 2: read the documentation of functools.total_ordering to give part general order:

 @total_ordering class part(): [...] def __lt__(self, other): return self.number < other.number. 

And yes, sorting lists of lists seems wrong. Internal elements may be better than tuples, so you cannot accidentally change the contents.

+7
source

So pre_sorted is a list with [int, part] elements. When you sort this list and have two elements with the same integer value, it then compares part values ​​to try to determine what comes first. However, some of them do not have a function to determine whether a part is smaller than any part, throws this error.

Try adding the __le__(self, other) function to order parts.

More about operators here

+2
source

Source: https://habr.com/ru/post/1266873/


All Articles