如标题所示,我的迭代深度受限搜索返回次优路径。
这不是为了分配;只是对 IDLS 与 BFS 相比如何工作感兴趣
显然,我忽略了一些东西,但我的代码没有返回最佳路径,并且当以设定的限制(无迭代)运行时,或者刚好高于最佳路径时,它不会返回任何路径。
节点包含:状态、路径成本和解决方案(路径)
以下是代码:
def depthLimitedSearch(problem, limit = 999999):
"""
Search the deepest nodes in the search tree first, up to limit depth.
"""
node = Node(problem.getStartState())
if problem.isGoalState(node.state):
return node.solution
frontier = util.Stack()
frontier.push(node);
explored = set([node.state]);
while not frontier.isEmpty():
node = frontier.pop()
explored.add(node.state)
for child in node.createChildren(problem):
if child.pathCost <= limit:
if (child.state not in explored):
if (child not in frontier.list):
if problem.isGoalState(child.state):
return child.solution
frontier.push(child)
return []
def iterateDLS(problem, limit = 999999, inc = 1):
''' iterate DLS starting from limit and rising in inc increments'''
result = []
while result == []:
result = depthLimitedSearch(problem, limit)
limit += inc
return result
''' testing for fixed limit'''
## limit = 31
## result = depthLimitedSearch(problem, limit)