TypeError:“<”实例之间不支持Python [英] TypeError: '<' not supported between instances Python

查看:92
本文介绍了TypeError:“<”实例之间不支持Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解决python 3中的遗传算法问题。我尚未完成完整的代码。每当我完成代码时,我都会测试部分代码。

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 with an error saying:


TypeError:在'part'和'part'的实例之间不支持'<'

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

有趣的是,此错误并不总是显示。有时代码运行平稳并显示所需的输出,但有时却显示此错误。

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

这是什么原因?

我正在附加代码和错误消息。

我正在使用PyCharm。

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):
        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)

错误消息:

the error message:

输出为(随机每次):

The Output is (which is randomized every time):

推荐答案

鉴于 pre_sorted 是包含项目 [fitness,part] ,每当比较两个具有相同 fitness 的子列表时,就会发疯。

Given that pre_sorted is a list of lists with items [fitness, part], this croaks whenever comparing two sublists with the same fitness.

Python列表按字典顺序排序,并且从左到右逐个元素地比较,直到找到不匹配的元素。在您的情况下,仅当两个部分的适合度相同时才访问第二个元素( part )。

Python lists sort lexicographically and are compared element-wise left to right until a mismatching element is found. In your case, the second element (part) is only accessed if the fitness of two parts is the same.


  • [0,part0]< [1,part1] =>不比较 part0 part1 ,因为适应度为

  • [0,part0]< [0,part1] => 确实比较 part0 part1 因为适应度相同。

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

排序仅根据适合度: sorted(pre_sorted,key = operator.itemgetter(0))

阅读 functools.total_ordering 给出部分的总订单:

@total_ordering
class part():
    [...]

    def __lt__(self, other):
        return self.number < other.number

是的,对列表进行排序似乎是错误的。内部元素最好是元组,因此您不能意外地修改内容。

And yeah, sorting lists of lists seems wrong. The inner elements might better be tuples, so you cannot accidentally modify the contents.

这篇关于TypeError:“&lt;”实例之间不支持Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆