我正在为TJ Wriggler问题编写一个AI解决方案,我遇到的问题导致我的程序挂起。我不确定它是什么,因为我应该有足够的内存来运行代码。相反,我认为我正在改变一个我不应该做的参考,但我似乎无法找出确切的位置。
我将执行直到程序挂起的程序:
首先,搜索功能:
def BFTS(the_frontier):
index = 0
frontier = [the_frontier]
while(True):
if (not frontier):
return None
leaf = frontier.pop(0)
if (leaf.GoalTest()):
return leaf
index = index + 1
moves_list = leaf.FindMoves(index)
while(len(moves_list) > 0):
frontier.append(moves_list.pop(0))
这会调用FindMoves()函数:
def FindMoves(self, current_index):
moves_list = []
for wriggler in self.State.Wrigglers:
for seg in [wriggler.Head(), wriggler.Tail()]:
is_head = {
wriggler.Head() : True,
wriggler.Tail() : False}[seg]
for move in self.State.FindEmptySpaces(seg):
if (move is not None):
moves_list.append(self.Move(wriggler.Index,
is_head, move['x'], move['y'], current_index))
return moves_list
FindEmptySpaces()查找可以移入的所有空格并返回表示笛卡尔坐标的字典列表。 FindMoves()函数然后调用Move()函数:
def Move(self, wriggler_id, is_head, new_x, new_y, current_index):
head_or_tail = {True : 0, False : 1}[is_head]
# Find action
new_action = ("%s %s %s %s" % (wriggler_id, head_or_tail, new_x, new_y))
new_node = self.CopyNode()
new_index = current_index
new_node.State.MoveWriggler(wriggler_id, new_y, new_x, is_head)
return Node(new_node.State, new_action, new_index, self.Cost + Node.Step_Cost)
此函数调用CopyNode()函数:
def CopyNode(self):
# Create new Spaces List-of-List
new_spaces = DataStructures.CopyListOfLists(self.State.Spaces)
new_state = Board(new_spaces)
# Create new List of Actions
new_action = []
for action in self.Action:
new_action.append(action)
# Create new Node
return Node(new_state, new_action, self.Parent, self.Cost)
CopyNode()函数调用CopyListOfLists()函数:
def CopyListOfLists(the_list_of_list):
new_list = []
for lists in the_list_of_list:
temp_list = []
for items in lists:
temp_list.append(items)
new_list.append(temp_list)
return new_list
据我所知,该程序挂起函数CopyNode(),但奇怪的是它只在第二次通过Search_BFTS()时执行此操作。正如我所说,我可能在某个地方更改了引用,但我在Python中没有足够的经验来确定这是否是实际问题。
任何可以提供的帮助都会很棒。
谢谢,