N-Queens退火计划不起作用

时间:2017-04-02 06:12:05

标签: python n-queens simulated-annealing

我正在尝试重新创建n-queens问题并使用模拟退火来解决它,尽管当我尝试使用len(board)** 2添加温度时,我的Object类中的board对象会抛出错误。任何帮助将非常感谢!!我已经包含了源代码和输出。谢谢!

ErrorException in efd7d9fe55aa0c18c60d0a857a8a44ed1725a390.php line 213:
Call to undefined method stdClass::checkSubcategory()

1 个答案:

答案 0 :(得分:0)

您需要为Board类定义__len__方法,否则将无法定义对len(board)的调用。 the Python 3 documentation中提到了这个和许多其他特殊的双下划线方法(或者,如果您愿意,可以the Python 2 documentation)。假设皇后的长度是你想要的数量:

class Board(object):
"""An N-queens solution attempt."""

def __init__(self, queens):
    """Instances differ by their queen placements."""
    self.queens = queens.copy()

def __len__(self):
     return len(self.queens)