AttributeError: 'int' object has no attribute 'map' when trying to write a recursive algorithm for a depth limited search
AttributeError: 'int' object has no attribute 'map' when trying to write a recursive algorithm for a depth limited search
正在尝试为 6 拼图游戏编写深度受限搜索的递归算法,但由于某种原因,我不断收到上述错误并且不明白为什么。
这是我的递归深度限制搜索代码:
def rec_dls(node, limit):
global cutoff_occurred
cutoff = 0
failure = -1
if goal_state == node.state:
return node
elif limit == 0:
return cutoff # cutoff
else:
cutoff_occurred = False
actions = moves(node.state) # all possibles children of this state
for action in actions:
child = State(action, node, node.depth+1, node.cost+1)
result = rec_dls(child, limit - 1)
if result == cutoff:
cutoff_occurred = True
elif result != failure:
return result
if cutoff_occurred:
return cutoff
else:
return failure
def dls(limit):
node = State(initial_state, None, 0, 0)
return rec_dls(node, limit)
还有州 class:
class State:
def __init__(self, state, parent, depth, cost):
self.state = state
self.parent = parent
self.depth = depth
self.cost = cost
if self.state:
self.map = ''.join(str(e) for e in self.state)
def __eq__(self, other):
return self.map == other.map
def __lt__(self, other):
return self.map < other.map
这是我遇到的更详细的错误:
作为参考,我的工作逻辑基于此(来自“人工智能,一种现代方法”):
问题不在于 rec_dls
returns 和 int
。它是 returns 你的 State
对象之一。
考虑以下代码行:
result = rec_dls(child, limit - 1)
if result == cutoff:
# ...
假设 rec_dls
这里 returns 您的 State
对象之一。然后,您将 State
对象与 cutoff
进行比较,后者包含 int
值 0
,并且因为您在 State
class,此比较导致调用 State.__eq__
并将 other
设置为 0
。
作为 int
,0
没有 map
属性,因此您的错误。
也许您想在 __eq__
方法中包含检查 other
是否是另一个 State
对象:
def __eq__(self, other):
return isinstance(other, State) and self.map == other.map
正在尝试为 6 拼图游戏编写深度受限搜索的递归算法,但由于某种原因,我不断收到上述错误并且不明白为什么。 这是我的递归深度限制搜索代码:
def rec_dls(node, limit):
global cutoff_occurred
cutoff = 0
failure = -1
if goal_state == node.state:
return node
elif limit == 0:
return cutoff # cutoff
else:
cutoff_occurred = False
actions = moves(node.state) # all possibles children of this state
for action in actions:
child = State(action, node, node.depth+1, node.cost+1)
result = rec_dls(child, limit - 1)
if result == cutoff:
cutoff_occurred = True
elif result != failure:
return result
if cutoff_occurred:
return cutoff
else:
return failure
def dls(limit):
node = State(initial_state, None, 0, 0)
return rec_dls(node, limit)
还有州 class:
class State:
def __init__(self, state, parent, depth, cost):
self.state = state
self.parent = parent
self.depth = depth
self.cost = cost
if self.state:
self.map = ''.join(str(e) for e in self.state)
def __eq__(self, other):
return self.map == other.map
def __lt__(self, other):
return self.map < other.map
这是我遇到的更详细的错误:
作为参考,我的工作逻辑基于此(来自“人工智能,一种现代方法”):
问题不在于 rec_dls
returns 和 int
。它是 returns 你的 State
对象之一。
考虑以下代码行:
result = rec_dls(child, limit - 1)
if result == cutoff:
# ...
假设 rec_dls
这里 returns 您的 State
对象之一。然后,您将 State
对象与 cutoff
进行比较,后者包含 int
值 0
,并且因为您在 State
class,此比较导致调用 State.__eq__
并将 other
设置为 0
。
作为 int
,0
没有 map
属性,因此您的错误。
也许您想在 __eq__
方法中包含检查 other
是否是另一个 State
对象:
def __eq__(self, other):
return isinstance(other, State) and self.map == other.map