Project 1:Search in Pacman(吃豆人搜索实验)(一)
题目链接http://ai.berkeley.edu/search.html
- Q1: Depth First Search
- Q2: Breadth First Search
- Q3: Uniform Cost Search
- Q4: A* Search
- 测试结果
由于能力有限,并没有做到满分。最终成绩为23/25
先更前四个题目,后四个题目陆续更新。
Q1: Depth First Search
第一个问题是需要实现深度优先搜索算法,选择利用util.py文件中已经定义好的Stack结构实现。Stack结构实现了一个后进先出的栈结构,则通过对该栈结构的使用,构造深度优先搜索树。
# 修改search.py 中的depthFirstSearch函数
def depthFirstSearch(problem):"""Search the deepest nodes in the search tree first.Your search algorithm needs to return a list of actions that reaches thegoal. Make sure to implement a graph search algorithm.To get started, you might want to try some of these simple commands tounderstand the search problem that is being passed in:print "Start:", problem.getStartState()print "Is the start a goal?", problem.isGoalState(problem.getStartState())print "Start's successors:", problem.getSuccessors(problem.getStartState())""""*** YOUR CODE HERE ***"#获得当前pacman的初始状态startNode = problem.getStartState()#检测初始状态是否正好为目标状态if problem.isGoalState(startNode):return []#构造搜索树myStack = util.Stack()visitedNode = []#搜索树的节点结构为(当前状态,[actions]从初始状态到达当前状态经过的动作集合)myStack.push((startNode, []))#对搜索树进行遍历,如果遍历结束仍未找到解即返回无解while not myStack.isEmpty():currentNode, action = myStack.pop()if not (currentNode in visitedNode):visitedNode.append(currentNode)if problem.isGoalState(currentNode):return action#扩展搜索树 for nextNode, nextAction, cost in problem.getSuccessors(currentNode):newAction = action + [nextAction]myStack.push((nextNode, newAction))util.raiseNotDefined()
Q2: Breadth First Search
第二个问题是需要实现宽度有限搜索,其与Q1的深度有限搜索唯一的区别是,使用util.py中的Queue结构构造搜索树。(由于与之前的宽度有限搜索的区别较小,则不再详细说明)
#修改search.py中的breadthFirstSearch函数def breadthFirstSearch(problem):"""Search the shallowest nodes in the search tree first.""""*** YOUR CODE HERE ***"startNode = problem.getStartState()if problem.isGoalState(startNode):return []myStack = util.Queue()visitedNode = []myStack.push((startNode, []))while not myStack.isEmpty():currentNode, action = myStack.pop()if not (currentNode in visitedNode):visitedNode.append(currentNode)if problem.isGoalState(currentNode):return actionfor nextNode, nextAction, cost in problem.getSuccessors(currentNode):newAction = action + [nextAction]myStack.push((nextNode, newAction))util.raiseNotDefined()
Q3: Uniform Cost Search
代价一致搜索算法即首先搜索动作代价最小的节点,则使用util.py中的PriorityQueue结构构造搜索树,为搜索树的每个节点增加一个priority参数记录其代价。
def uniformCostSearch(problem):"""Search the node of least total cost first.""""*** YOUR CODE HERE ***"startNode = problem.getStartState()if problem.isGoalState(startNode):return []myCostQueue = util.PriorityQueue()visitedNode = []myCostQueue.push((startNode, []), 0)while not myCostQueue.isEmpty():currentNode, action = myCostQueue.pop()if not (currentNode in visitedNode):visitedNode.append(currentNode)if problem.isGoalState(currentNode):return actionfor nextNode, nextAction, cost in problem.getSuccessors(currentNode):newAction = action + [nextAction]#计算由初始状态到达该节点所需的代价newCost = problem.getCostOfActions(newAction)myCostQueue.push((nextNode, newAction), newCost) util.raiseNotDefined()
Q4: A* Search
A*搜索与代价一致搜索的区别就是,其搜索树节点中的priority参数不仅记录到达此状态已经花费的代价,还需要加上使用启发式函数计算得到的从该状态到目标节点可能花费的代价。
def aStarSearch(problem, heuristic=nullHeuristic):"""Search the node that has the lowest combined cost and heuristic first.""""*** YOUR CODE HERE ***"startNode = problem.getStartState()startPriority = heuristic(startNode, problem) + 0if problem.isGoalState(startNode):return []myQueue = util.PriorityQueue()visitedNode = []myQueue.push((startNode, [], 0), startPriority)while not myQueue.isEmpty():currentNode, action, preCost = myQueue.pop()if not (currentNode in visitedNode):visitedNode.append(currentNode)if problem.isGoalState(currentNode):return actionfor nextNode, nextAction, nextCost in problem.getSuccessors(currentNode):newAction = action + [nextAction]newCost = problem.getCostOfActions(newAction)#其中的newCost即为到达此节点已经花费的代价#heuristic即是用来计算该节点到目标节点可能花费的代价的启发式函数newPriority = newCost + heuristic(nextNode, problem)myQueue.push((nextNode, newAction, newCost), newPriority) util.raiseNotDefined()
测试结果
Question q1
===========*** PASS: test_cases\q1\graph_backtrack.test
*** solution: ['1:A->C', '0:C->G']
*** expanded_states: ['A', 'D', 'C']
*** PASS: test_cases\q1\graph_bfs_vs_dfs.test
*** solution: ['2:A->D', '0:D->G']
*** expanded_states: ['A', 'D']
*** PASS: test_cases\q1\graph_infinite.test
*** solution: ['0:A->B', '1:B->C', '1:C->G']
*** expanded_states: ['A', 'B', 'C']
*** PASS: test_cases\q1\graph_manypaths.test
*** solution: ['2:A->B2', '0:B2->C', '0:C->D', '2:D->E2', '0:E2->F', '0:F->G']
*** expanded_states: ['A', 'B2', 'C', 'D', 'E2', 'F']
*** PASS: test_cases\q1\pacman_1.test
*** pacman layout: mediumMaze
*** solution length: 130
*** nodes expanded: 146### Question q1: 3/3 ###Question q2
===========*** PASS: test_cases\q2\graph_backtrack.test
*** solution: ['1:A->C', '0:C->G']
*** expanded_states: ['A', 'B', 'C', 'D']
*** PASS: test_cases\q2\graph_bfs_vs_dfs.test
*** solution: ['1:A->G']
*** expanded_states: ['A', 'B']
*** PASS: test_cases\q2\graph_infinite.test
*** solution: ['0:A->B', '1:B->C', '1:C->G']
*** expanded_states: ['A', 'B', 'C']
*** PASS: test_cases\q2\graph_manypaths.test
*** solution: ['1:A->C', '0:C->D', '1:D->F', '0:F->G']
*** expanded_states: ['A', 'B1', 'C', 'B2', 'D', 'E1', 'F', 'E2']
*** PASS: test_cases\q2\pacman_1.test
*** pacman layout: mediumMaze
*** solution length: 68
*** nodes expanded: 269### Question q2: 3/3 ###Question q3
===========*** PASS: test_cases\q3\graph_backtrack.test
*** solution: ['1:A->C', '0:C->G']
*** expanded_states: ['A', 'B', 'C', 'D']
*** PASS: test_cases\q3\graph_bfs_vs_dfs.test
*** solution: ['1:A->G']
*** expanded_states: ['A', 'B']
*** PASS: test_cases\q3\graph_infinite.test
*** solution: ['0:A->B', '1:B->C', '1:C->G']
*** expanded_states: ['A', 'B', 'C']
*** PASS: test_cases\q3\graph_manypaths.test
*** solution: ['1:A->C', '0:C->D', '1:D->F', '0:F->G']
*** expanded_states: ['A', 'B1', 'C', 'B2', 'D', 'E1', 'F', 'E2']
*** PASS: test_cases\q3\ucs_0_graph.test
*** solution: ['Right', 'Down', 'Down']
*** expanded_states: ['A', 'B', 'D', 'C', 'G']
*** PASS: test_cases\q3\ucs_1_problemC.test
*** pacman layout: mediumMaze
*** solution length: 68
*** nodes expanded: 269
*** PASS: test_cases\q3\ucs_2_problemE.test
*** pacman layout: mediumMaze
*** solution length: 74
*** nodes expanded: 260
*** PASS: test_cases\q3\ucs_3_problemW.test
*** pacman layout: mediumMaze
*** solution length: 152
*** nodes expanded: 173
*** PASS: test_cases\q3\ucs_4_testSearch.test
*** pacman layout: testSearch
*** solution length: 7
*** nodes expanded: 14
*** PASS: test_cases\q3\ucs_5_goalAtDequeue.test
*** solution: ['1:A->B', '0:B->C', '0:C->G']
*** expanded_states: ['A', 'B', 'C']### Question q3: 3/3 ###Question q4
===========*** PASS: test_cases\q4\astar_0.test
*** solution: ['Right', 'Down', 'Down']
*** expanded_states: ['A', 'B', 'D', 'C', 'G']
*** PASS: test_cases\q4\astar_1_graph_heuristic.test
*** solution: ['0', '0', '2']
*** expanded_states: ['S', 'A', 'D', 'C']
*** PASS: test_cases\q4\astar_2_manhattan.test
*** pacman layout: mediumMaze
*** solution length: 68
*** nodes expanded: 221
*** PASS: test_cases\q4\astar_3_goalAtDequeue.test
*** solution: ['1:A->B', '0:B->C', '0:C->G']
*** expanded_states: ['A', 'B', 'C']
*** PASS: test_cases\q4\graph_backtrack.test
*** solution: ['1:A->C', '0:C->G']
*** expanded_states: ['A', 'B', 'C', 'D']
*** PASS: test_cases\q4\graph_manypaths.test
*** solution: ['1:A->C', '0:C->D', '1:D->F', '0:F->G']
*** expanded_states: ['A', 'B1', 'C', 'B2', 'D', 'E1', 'F', 'E2']### Question q4: 3/3 ###
Project 1:Search in Pacman(吃豆人搜索实验)(一)相关推荐
- pacman吃豆人_通过Tensorflow和强化学习实现自动化吃豆人PacMan
介绍 在涉及GradientCrescent强化学习基础的文章中,我们研究了基于模型和基于样本的强化学习方法. 简而言之,前一类的特征是需要了解所有可能状态转换的完整概率分布,并且可以通过马尔可夫决策 ...
- pacman吃豆人_“植物河豚”狗爪豆,你吃过吗?
植物河豚:大红辣椒清炒狗皮豆 "狗爪豆,性温, 味甘微苦,有小毒 --有"植物河豚"之美誉" --<中草药大辞典> 河豚 鱼不少人都吃过,但植物河豚 ...
- 服饰美妆新品 | 阿迪达斯可循环跑鞋第三代LOOP系列发布;赫丽尔斯X吃豆人跨界限定系列推出...
FILA斐乐.阿迪达斯.Kipling.HELIUS赫丽尔斯发布新品. 服饰 FILA霓虹雪山综训系列 FILA霓虹雪山综训系列主打逐光霓虹的理念,为户外运动爱好者提供极具保暖功能的服饰,保持优雅时尚 ...
- python 吃豆人_pacman 人工智能编程 吃豆人小游戏 可实现智能寻路 吃豆 通关 - 下载 - 搜珍网...
Berkeley人工智能吃豆人作业-Python/ Berkeley人工智能吃豆人作业-Python/再附带一份完整工程源码吧/ Berkeley人工智能吃豆人作业-Python/再附带一份完整工程源 ...
- 【历史上的今天】5 月 22 日:Windows 3.0 发布;虚幻引擎诞生;《吃豆人》问世
整理 | 王启隆 透过「历史上的今天」,从过去看未来,从现在亦可以改变未来. 今天是 2022 年 5 月 22 日,在 1994 年的今天,知名中文论坛曙光 BBS 站开通.1994 年 4 月 2 ...
- 【人工智能导论】吃豆人游戏(上):对抗搜索与Minimax算法
吃豆人实验(The Pac-Man Project)简介 The Pac-Man projects were developed for UC Berkeley's introductory arti ...
- Python游戏开发pygame模块,Python实现吃豆人,儿时的回忆
老规矩,先上效果图 这是一个吃豆人的小游戏.我们8090后这一代人肯定会碰到过.黄点是我们自己,红点就是怪物们.这是最原始版的电子游戏. 然后我们可以在随便一个地方新建一个游戏代码,利用这个包的代码, ...
- 观看5万个游戏视频后,英伟达AI学会了自己开发「吃豆人」
晓查 发自 凹非寺 量子位 报道 | 公众号 QbitAI AI学会玩游戏已经不是什么新鲜事了,无论是星际争霸还是王者荣耀,AI的水平都已经超过了顶级选手. 现在,AI不仅能玩游戏,还学会了造游戏. ...
- 玩了5万局游戏,英伟达让GameGAN自己生成「吃豆人」,世界首创无需游戏引擎...
本文转载自新智元(AI_era). 新智元报道 编辑:元子 [新智元导读]近几年来,英伟达在GAN这个领域越走越远.英伟达推出"GameGAN",不需要任何底层游戏引擎,用 ...
最新文章
- 基于LVS-DR集群的持久链接实验
- tomcat 增加运行内存
- java中两种添加监听器的策略
- PHP $_SERVER['HTTP_REFERER'] 获取前一页面的 URL 地址
- [面试] 算法(五)—— 替换字符串中的空格
- eclipse 远程调试mapreduce
- jQuery画廊插件-GalleryView
- 离散状态方程和连续状态方程 matlab,信号与系统实验(MATLAB 西电版)实验19 离散系统状态方程的求解.ppt...
- 学习笔记(26):玩转Python-Python3基础入门-案例-快递价格计算器(1)
- 汉诺塔问题(看完就记住)
- 您觉得目前网页最小字体应该多大呢?
- 【视觉运控一体机小课堂】三分钟实现图像颜色通道切换和RGB图转灰度图的功能
- 流浪四十年,中国科幻撒点野
- python输出去空格_python不空格
- 分享自学编程收藏的网站学习资源
- git小游戏通关攻略(基础、高级、远程)
- OBS显示器捕获黑屏问题解决
- Luma推出分布式路由器,哪里有WIFI死角放哪里
- matlab求解f非线性微分方程数值解,非线性﹑微分方程数值求解.PPT
- 手把手教会你如何玩转SpringMVC