Python 深度优先搜索算法跳过迷宫中的空间?
在结束哈佛大学人工智能课程关于edX的第一堂课后,我决定实施所教授的概念,首先是深度优先搜索算法 该程序的目标是在文本文件Python 深度优先搜索算法跳过迷宫中的空间?,python,algorithm,search,artificial-intelligence,depth-first-search,Python,Algorithm,Search,Artificial Intelligence,Depth First Search,在结束哈佛大学人工智能课程关于edX的第一堂课后,我决定实施所教授的概念,首先是深度优先搜索算法 该程序的目标是在文本文件mazefile中输入迷宫,并使用深度优先搜索算法找到从S到G的路径 该项目目前由4个文件组成,(1)带有类方法的代码,用于操作或使用(2)包含迷宫的文本文件,另一个包含结果文件(AI已经探索过的地方)的文本文件(3)和主python脚本(4)。它们就在这里,可以随意复制并粘贴到文件夹中,看看它们是如何运行的 processText.py(文件1) TestingTrack.
mazefile
中输入迷宫,并使用深度优先搜索算法找到从S
到G
的路径
该项目目前由4个文件组成,(1)带有类方法的代码,用于操作或使用(2)包含迷宫的文本文件,另一个包含结果文件(AI已经探索过的地方)的文本文件(3)和主python脚本(4)。它们就在这里,可以随意复制并粘贴到文件夹中,看看它们是如何运行的
processText.py(文件1)
TestingTrack.py(主文件)
mazefile(程序将读取此文件以获取迷宫)
还有一个resultfile,但是如果您只创建一个具有该名称(无扩展名)的空文本文件,程序将用结果填充它
问题在于resultfile,如下所示:
DEPTH FIRST SEARCH
F - Free
O - Occupied
S - Starting point
G - Goal
E - Explored/Visited
(Abdulaziz Albastaki 2020)
(top left coordinate - 0,0)
OOOOOOOOOOOOOOOO
OFFFFFFFFFFFFFGO
OFOOOOOOOOOOOOEO
OFOOOOOOOOOOOOEO
OFOOOOOOOOOOOOEO
OEOOOOOOOOOOOOEO
OEFFFEEEEEEEEEEO
OOOOOOOOOOOOOOOO
Original problem
OOOOOOOOOOOOOOOO
OFFFFFFFFFFFFFGO
OFOOOOOOOOOOOOFO
OFOOOOOOOOOOOOFO
OFOOOOOOOOOOOOFO
OFOOOOOOOOOOOOFO
OSFFFFFFFFFFFFFO
OOOOOOOOOOOOOOOO
人工智能跳过了一些空间来达到目标,为什么会这样
请随时向我询问任何澄清。有以下问题:
中的最后一个nearbyfreespace
块使用了错误的索引:if
应该是:if self.whatIs(self.currentPosition[1],self.currentPosition[1]+1) == search: self.freeSpaces.append([self.currentPosition[1],self.currentPosition[1]+1])
if self.whatIs(self.currentPosition[0],self.currentPosition[1]+1) == search: self.freeSpaces.append([self.currentPosition[0],self.currentPosition[1]+1])
explored.append(result)
- 最终位置未正确添加到路径中。此块的最后一行:
……应该是:if len(finger.nearbyFreeSpaces("G")) == 1: #If the goal is bordering this space result = finger.nearbyFreeSpaces("G")[0] explored.append(finger.currentPosition)
if self.whatIs(self.currentPosition[0],self.currentPosition[1]+1) == search: self.freeSpaces.append([self.currentPosition[0],self.currentPosition[1]+1])
explored.append(result)
中的最后一个nearbyfreespace
块使用了错误的索引:if
应该是:if self.whatIs(self.currentPosition[1],self.currentPosition[1]+1) == search: self.freeSpaces.append([self.currentPosition[1],self.currentPosition[1]+1])
if self.whatIs(self.currentPosition[0],self.currentPosition[1]+1) == search: self.freeSpaces.append([self.currentPosition[0],self.currentPosition[1]+1])
explored.append(result)
- 最终位置未正确添加到路径中。此块的最后一行:
……应该是:if len(finger.nearbyFreeSpaces("G")) == 1: #If the goal is bordering this space result = finger.nearbyFreeSpaces("G")[0] explored.append(finger.currentPosition)
if self.whatIs(self.currentPosition[0],self.currentPosition[1]+1) == search: self.freeSpaces.append([self.currentPosition[0],self.currentPosition[1]+1])
explored.append(result)
- 存在以下问题: