在函数中发现问题HerusticSort
,我会检查一下。
我使用嵌套列表编写了一段python代码,例如:
opentab = [[start], [0], [None]]
node = opentab[0].pop(0)
但是执行代码的时候提示
HerusticSearchA 中的文件“\Herustic Search.py”,第 97 行
节点 = opentab[0].pop(0)
AttributeError: 'tuple' 对象没有属性 'pop'
这很奇怪,因为我没有更改它的类,并且我已经将它定义为嵌套列表。
出现问题后,我设置了一个断言以确保它是正确的。
assert isinstance(opentab[0], list)
当然现在我得到了一个AssertionError
我也在IDLE中一一试了,没问题。
我只是把完整的代码片段放在这里,我不知道为什么会这样......
def HerusticSearchA(start, end):
'''
'''
def H_Function(state, depth):
'''
state is a 3 x 3 list, stands for the puzzle state.
returns the herustic assessment value
g for the depth of the node, h for the incorrect tile number
'''
crit = copy.deepcopy(end)
count = 0
for i in range(len(state)):
for j in range(len(state[0])):
if state[i][j] - crit[i][j] != 0:
count += 1
return depth + count
#1. Variables
#[[state], [value], [father]]
opentab = [[start], [0], [None]]
close = [[], [], []]
depth = 0
done = False
print(start, end)
while len(opentab[0]) > 0:
#2. Nothing to continue
if opentab == [[], [], []]:
return None
#3. remove n from OPEN
assert isinstance(opentab[0], list)
node = opentab[0].pop(0)
nodevalue = opentab[1].pop(0)
father = opentab[2].pop(0)
close[0].append(node)
close[1].append(nodevalue)
close[2].append(father)
#4. Got result
if node == target:
close[0].append(node)
close[1].append(nodevalue)
close[2].append(father)
done = True
break
#5. Extract followers
else:
nexts = NextStep(node)
for subnode in nexts:
newvalue = H_Function(subnode, depth)
#conditions:
#6.
if subnode not in opentab[0] and subnode not in close[0]:
opentab[0].append(subnode)
opentab[1].append(newvalue)
opentab[2].append(node)
#7.
if subnode in opentab[0]:
idx = opentab[0].index(subnode)
if newvalue < opentab[1][idx]:
opentab[1][idx] = newvalue
opentab[2][idx] = node
#8.
if subnode in close[0]:
idx = close[0].index(subnode)
if newvalue < close[1][idx]:
close[1][idx] = newvalue
close[2][idx] = node
#9. sort opentab
HerusticSort(opentab, 1)
depth += 1
return close