python 2d数组条件

2024-04-19 12:23:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python3.4编写8queen(遗传算法)程序 我用矩阵来保持皇后的位置。但是我在sort()函数中有一个错误,我不知道这个错误。 请帮帮我。。。 我的代码:

from random import randrange

__author__ = 'Moein'


class NQueen:
    NUM_COLS = 8
    POPULATIONS = 100

    current = [[]]

    def __init__(self):
        self.current = [[0 for col in range(self.NUM_COLS + 1)] for row in range(self.POPULATIONS)]

        # generate first Generation
        for i in range(0, self.POPULATIONS):
            for j in range(0, self.NUM_COLS):
                self.current[i][j] = randrange(self.NUM_COLS)

        count = 0
        condition = True

        while condition:
            self.crossover()
            self.mutation()
            self.fitness()
            self.sort()
            count += 1
            print(self.current)
            # print(self.current[0])
            if self.current[0][self.NUM_COLS] == 0:
                condition = False

        print(self.current[0])
        pass

    def fitness(self):
        count = 0

        for i in range(0, self.POPULATIONS):
            for j in range(0, self.NUM_COLS):
                for x in range(j + 1, self.NUM_COLS):
                    if self.current[i][j] == self.current[i][x]:
                        count += 1
                    if abs(j - x) == abs(self.current[i][j] - self.current[i][x]):
                        count += 1
            self.current[i][self.NUM_COLS] = count
            count = 0
        pass

    def sort(self):
        for i in range(0, self.POPULATIONS - 1):
            for j in range(i + 1, self.POPULATIONS):
                if self.current[i][self.NUM_COLS] > self.current[j][self.NUM_COLS]:
                    for x in range(0, self.NUM_COLS + 1):
                        temp = self.current[i][x]
                        self.current[i][x] = self.current
                        self.current[j][x] = temp
        pass

    def crossover(self):
        _new = [[0 for x in range(self.NUM_COLS + 1)] for x in range(self.POPULATIONS)]

        for i in range(0, int(self.POPULATIONS / 2)):
            for j in range(0, int(self.NUM_COLS / 2)):
                _new[i + 49][j] = self.current[i][j]
                _new[i + 49 + 1][j] = self.current[i + 1][j]

            for j in range(int(self.NUM_COLS / 2), self.NUM_COLS):
                _new[i + 49][j] = self.current[i][j]
                _new[i + 49 + 1][j] = self.current[i + 1][j]
        self.current = _new
        pass

    def mutation(self):
        for i in range(0, self.POPULATIONS):
            self.current[i][randrange(self.NUM_COLS)] = randrange(self.NUM_COLS)
        pass


nQueen = NQueen()
print(nQueen.current[0])

我的错误是:

Traceback (most recent call last):
  File "C:/Users/Moein/PycharmProjects/NQueen/project.py", line 81, in <module>
    nQueen = NQueen()
  File "C:/Users/Moein/PycharmProjects/NQueen/project.py", line 27, in __init__
    self.sort()
  File "C:/Users/Moein/PycharmProjects/NQueen/project.py", line 54, in sort
    if self.current[i][self.NUM_COLS] > self.current[j][self.NUM_COLS]:
TypeError: unorderable types: list() > int()

Tags: inselfnewforifdefcountrange
1条回答
网友
1楼 · 发布于 2024-04-19 12:23:46
self.current[i][x] = self.current

我想是这条线引起了问题,因为

self.current

是一个列表,所以您正在设置

self.current[i][x]

是一个列表而不是一个int。所以在这一点上:

if self.current[i][self.NUM_COLS] > self.current[j][self.NUM_COLS]:

当你试图比较这些值时,你可能会比较 一个带有列表的int,它会导致错误。你知道吗

TypeError: unorderable types: list() > int()

干杯

编辑:

我刚试过。 更换

self.current

使用int例如2防止异常发生。你知道吗

相关问题 更多 >