mardi 18 avril 2017

TypeError: '<' not supported between instances Python

I am solving a problem with genetic algorithm in python 3. I have not completed the full code yet. I test a part of the code whenever i complete it.

At present I am stuck in a error saying:

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

The interesting thing this error does not show always. Sometimes the code run smoothly and show the desired output but sometimes it shows this error.

What is the reason of this? Please help me. I am attaching the code and the 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):

def create_initial_population():
    part_family = []

    for i in range(8):

    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 = ' ')

    return sort

def evolve(population):
    population = sort_population_by_fitness(population)
    return population

population = create_initial_population()
population = evolve(population)

the error message: enter image description here

The Output is(which is randomized every time): enter image description here


0 commentaires:

Enregistrer un commentaire