0

i'm working on 8 queen(Genetic Algorithm) program with python 3.4 i use a matrix for keep queens position. but i have an error in sort() function,i dont underestand this error. please help me ... my code:

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

and my error:

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

1 Answer 1

3
self.current[i][x] = self.current

I guess that its this line causing the problem, since

self.current

is a list, so you are setting

self.current[i][x]

to be a list instead of an int. So at this point:

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

when you try to compare those values it might happen, that you compare an int with a list, which causes the error.

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

Cheers

EDIT:

I just tried it out. Replacing

self.current

with an int for example 2 prevents the Exception from occurring.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.