2014-10-29 50 views
0

我正在使用python 3.4的8皇后(遗传算法)程序我使用矩阵保留皇后位置。但我有sort()函数中的错误,我没有underestand这个错误。 请帮我... 我的代码:python 2d数组条件

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

回答

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

干杯

编辑

我只是尝试了一下。 具有例如一个int更换

self.current 

防止异常的发生。