本文要实现 Uniform Cost Search( 统一代价搜索算法) ,首先搜索总成本最小的节点,  统一代价搜索算法搜索到达目标。

PriorityQueue实现一个优先级队列的数据结构,每个插入元素具有与之关联的优先级,client快速检索队列中优先级最低的元素,以 O(1) 时间复杂度访问最低优先级的元素。

class PriorityQueue:"""Implements a priority queue data structure. Each inserted itemhas a priority associated with it and the client is usually interestedin quick retrieval of the lowest-priority item in the queue. Thisdata structure allows O(1) access to the lowest-priority item."""def  __init__(self):self.heap = []self.count = 0def push(self, item, priority):entry = (priority, self.count, item)heapq.heappush(self.heap, entry)self.count += 1def pop(self):(_, _, item) = heapq.heappop(self.heap)return itemdef isEmpty(self):return len(self.heap) == 0def update(self, item, priority):# If item already in priority queue with higher priority, update its priority and rebuild the heap.# If item already in priority queue with equal or lower priority, do nothing.# If item not in priority queue, do the same thing as self.push.for index, (p, c, i) in enumerate(self.heap):if i == item:if p <= priority:breakdel self.heap[index]self.heap.append((priority, c, item))heapq.heapify(self.heap)breakelse:self.push(item, priority)

Heap queue(heapq 堆队列)中堆是数组,对于所有k,a[k]<=a[2*k+1]和a[k]<=a[2*k+2],从0开始计算元素。为了比较,不存在的元素被认为是无限的,堆a[0]始终是其最小元素。

Heap queue 堆队列的用法:

heap = []            # 新建一个空堆
heappush(heap, item) # 将一个新的元素压入堆
item = heappop(heap) # 从堆中取出最小的元素
item = heap[0]       #最小是元素是heap【0】,直接获取
heapify(x)           #将列表按线性时间转换为堆
item = heapreplace(heap, item) # 弹出并返回最小的元素,并添加新的元素;堆大小不变

统一代价搜索算法代码:

# search.py
# ---------
# Licensing Information:  You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel (pabbeel@cs.berkeley.edu).
def uniformCostSearch(problem):"""Search the node of least total cost first.""""*** YOUR CODE HERE ***"#util.raiseNotDefined()path =Path([problem.getStartState()],[],0)if problem.isGoalState(problem.getStartState()):return path.directions#创建优先队列queue = util.PriorityQueue()# push的第一个参数path是状态项Path,属性(位置、方向、成本)#push的第二个参数0是优先级#在heapq.heappush中将(优先级priority, 计数索引self.count, 状态项item)三元组#根据优先级priority, 计数索引self.count的组合优先级#即(优先级priority如一样,按计数索引判断优先级),将状态项item压入队列。queue.push(path,0) visited =[problem.getStartState()]#和广度优先搜索的区别,仅在于queue.push的不同。while not queue.isEmpty():#如队列不为空,取最先进入队列的元素(List的最后一个元素),获取当前路径currentPath = queue.pop()currentLocation = currentPath.locations[-1]#如果当前位置已经是终点的位置,则返回当前路径的方向列表,用于移动pac man。if problem.isGoalState(currentLocation):return currentPath.directionselse:#在搜索问题中取得当前位置后继的下一个状态.getSuccessors中for循环遍历北、南、东、西四个方向,#directionToVector取得方向到坐标偏移向量的转换值,在当前坐标上加上位移的坐标偏移量值,#如果下一步坐标移动的点不是围墙,则在后续状态列表中加入三元组( nextState, action, cost)nextSteps = problem.getSuccessors(currentLocation)for nextStep in nextSteps:#遍历下一步的状态,依次获得位置、方向、成本信息nextLocation =nextStep[0]nextDirection = nextStep[1]nextCost = nextStep[2]# 不在当前路径里面而且下一个位置还没被访问(多条路径交叉点)if (nextLocation not in currentPath.locations) and (nextLocation not in visited):if not problem.isGoalState(nextLocation):visited.append(nextLocation)print("访问的位置:", visited)#获取当前路径列表集nextLocations =currentPath.locations[:]#将新的位置加入到当前路径的列表里面nextLocations.append(nextLocation)print("当前位置:",currentLocation)print("当前位置下一步可能的移动位置:",nextLocation)print("加到当前位置列表集:",nextLocations)#print()#print()#print(currentLocation,nextLocation,nextLocations)#获取当前的方向集nextDirections = currentPath.directions[:]#将新的方向加入到当前方向集的列表里面nextDirections.append(nextDirection)nextCosts = currentPath.cost +nextCostnextPath =Path(nextLocations,nextDirections,nextCosts)#下一步的状态,入优先级别的队列# push的第一个参数nextPath是状态项Path,属性(位置、方向、成本)#push的第二个参数nextCosts是优先级#在heapq.heappush中将(优先级priority, 计数索引self.count, 状态项item)三元组#根据优先级priority, 计数索引self.count的组合优先级#即(优先级priority如一样,按计数索引判断优先级),将状态项item压入队列。print("优先级:",nextCosts)print()print()queue.push(nextPath, nextCosts)#队列为空,仍未到达终点,返回空集return []

虽然BFS会找到一条通向目标的最少行动路径,但我们可能希望找到其他意义上“最好”的路径。考虑一下mediumDottedMaze 迷宫和mediumScaryMaze迷宫。通过改变成本函数,我们可以鼓励Pacman找到不同的路径。例如,我们可以对在幽灵出入地区的危险步骤收取更高的费用,或者对食物丰富地区的步骤收取更少的费用,而一个理性的Pacman代理应该调整它的行为来做出反应。在search.py中的uniformCostSearch函数中实现了统一成本图搜索算法。可查看util.py,了解一些在实现中可能有用的数据结构。

观察以下三种布局中的成功行为,其中下面的代理使用不同的成本函数(代理和成本函数已编写),StayEastSearchAgent的成本函数是costFn = lambda pos: .5 ** pos[0];StayWestSearchAgent的成本函数是costFn = lambda pos: 2 ** pos[0],StayEastSearchAgent and StayWestSearchAgen的路径成本应该分别非常低和非常高。

python pacman.py -l mediumMaze -p SearchAgent -a fn=ucs
python pacman.py -l mediumDottedMaze -p StayEastSearchAgent
python pacman.py -l mediumScaryMaze -p StayWestSearchAgent

运行结果分别如下:

E:\2019reforce_learning\cs188\proj1-search-python3>python pacman.py -l mediumMaze -p SearchAgent -a fn=ucs
[SearchAgent] using function ucs
[SearchAgent] using problem type PositionSearchProblem
Path found with total cost of 68 in 0.3 seconds
Search nodes expanded: 269
Pacman emerges victorious! Score: 442
Average Score: 442.0
Scores:        442.0
Win Rate:      1/1 (1.00)
Record:        WinE:\2019reforce_learning\cs188\proj1-search-python3>python pacman.py -l mediumDottedMaze -p StayEastSearchAgent
Path found with total cost of 1 in 0.2 seconds
Search nodes expanded: 186
Pacman emerges victorious! Score: 646
Average Score: 646.0
Scores:        646.0
Win Rate:      1/1 (1.00)
Record:        WinE:\2019reforce_learning\cs188\proj1-search-python3>python pacman.py -l mediumScaryMaze -p StayWestSearchAgent
Path found with total cost of 68719479864 in 0.2 seconds
Search nodes expanded: 108
Pacman emerges victorious! Score: 418
Average Score: 418.0
Scores:        418.0
Win Rate:      1/1 (1.00)
Record:        Win

如使用mediumMaze 布局:

E:\2019reforce_learning\cs188\proj1-search-python3>python pacman.py -l mediumMaze -p SearchAgent -a fn=ucs
[SearchAgent] using function ucs
[SearchAgent] using problem type PositionSearchProblem
Path found with total cost of 68 in 0.3 seconds
Search nodes expanded: 269
Pacman emerges victorious! Score: 442
Average Score: 442.0
Scores:        442.0
Win Rate:      1/1 (1.00)
Record:        WinE:\2019reforce_learning\cs188\proj1-search-python3>python pacman.py -l mediumMaze -p StayEastSearchAgent
Path found with total cost of 1 in 0.2 seconds
Search nodes expanded: 260
Pacman emerges victorious! Score: 436
Average Score: 436.0
Scores:        436.0
Win Rate:      1/1 (1.00)
Record:        WinE:\2019reforce_learning\cs188\proj1-search-python3>python pacman.py -l mediumMaze -p StayWestSearchAgent
Path found with total cost of 17183280440 in 0.2 seconds
Search nodes expanded: 173
Pacman emerges victorious! Score: 358
Average Score: 358.0
Scores:        358.0
Win Rate:      1/1 (1.00)
Record:        Win

欢迎关注微信公众号:“从零起步学习人工智能”。

CS 188 (4) Uniform Cost Search( 统一代价搜索算法)相关推荐

  1. 【人工智能】一致代价搜索(Uniform Cost Search, UCS) Python实现

    带环检测的一致代价搜索(Uniform Cost Search, UCS) Python实现 罗马尼亚旅行问题 求从城市Arad到城市Bucharest的最短路径 States:表示当前所处的城市: ...

  2. Uniform Cost Search (UCS)

    BFS用于找到每个边的权值是一样的图的最短路径,如果图中每个边的权值不一样了,就用到了UCS. 参考了https://blog.csdn.net/jdh99/article/details/80872 ...

  3. NO.66——人工智能学习:python实现一致代价搜索算法

    目的: 在广度优先算法上进行进化.一致代价搜索算法每次扩展的是当前路径消耗g(n)最小的节点n. 源码: 数据结构: frontier : 边缘,存储未扩展的节点.通过维护一个优先级队列,按路径损耗来 ...

  4. CS 188 Project4(RL) Introduction:Ghostbusters

    Pacman一生都在逃避幽灵,但事情并非总是如此.传说很多年前,Pacman的曾祖父爷爷就学会了捕猎幽灵.然而,他被自己的能力蒙蔽了双眼,只能通过幽灵的砰砰声和叮当声来追踪他们.在这个项目中,您将设计 ...

  5. CS 188 Project3(RL) Q10:Approximate Q-Learning

    实现一个近似的Q-learning学习代理,它学习状态特征的权重,其中许多状态可能共享相同的特征.在qlearningAgents.py中的ApproximateQAgent类中编写实现,它是Pacm ...

  6. Jump Search-跳跃搜索算法

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...

  7. 人工智能导论学习笔记(考前复习)

    文章目录 前言 正文 搜索问题的形式化 无信息搜索 什么是树搜索和图搜索 Uniform Cost Search 一致代价搜索 深度受限搜索 迭代加深的深度优先搜索 双向搜索 无信息搜索的评价 有信息 ...

  8. AI(人工智能:一种现代的方法)学习之:无信息搜索(uninformed search)算法——广度优先搜索、深度优先搜索、Uniform-cost search

    文章目录 参考 搜索算法 深度优先搜索 depth-first search 性能分析 完整性 complete 最优性 optimal 时间复杂度 空间复杂度 广度优先搜索 breadth-firs ...

  9. Project 1:Search in Pacman(吃豆人搜索实验)(一)

    题目链接http://ai.berkeley.edu/search.html Q1: Depth First Search Q2: Breadth First Search Q3: Uniform C ...

最新文章

  1. ATLAS入门篇之HoverMenuExtender控件编程(2)
  2. tflearn alexnet iter 10
  3. 一些非常实用的JSON 教程
  4. 【温故知新】CSS学习笔记(行高简介)
  5. SRM 698 div1 RepeatString
  6. oracle中having作用,oracle中having与where的区别
  7. Python高级编程(三)
  8. python fortran混合编程_python调用fortran模块
  9. 计算机高效课堂建设,基于信息技术的小学音乐高效课堂的构建
  10. c 语言鼠标钩子,鼠标钩子程序示例
  11. 怎样用关系代数表达式表示查询要求?求过程
  12. Linux虚拟机设置全屏
  13. 记录oracle表-字段小写转大写-自用
  14. RuntimeWarning: divide by zero encountered in log错误解决
  15. linux云自动化系统运维19(磁盘阵列raid,lvm管理)
  16. Node.js、npm环境配置与Vue项目创建
  17. SOLIDWORKS怎样做填充阵列
  18. 企业技术中台架构全景图(多图)
  19. thinksns java_ThinkSNS+ 更新播报
  20. 云计算大好前途下 头部厂商的激烈博弈

热门文章

  1. 解决Ubuntu更新后无线网卡不能使用的问题
  2. linux shell响铃程序,响铃提示命令
  3. 江苏省徐州市科目三考场分析
  4. 天地图服务http转https报错
  5. Gopher China 2019 讲师专访-Grab/地图团队资深架构师胡泊
  6. LWIP应用开发|心跳机制
  7. 二叉树探究之非叶子结点和叶子结点对半分且最多差一个
  8. FFmpeg:截取视频片段转成GIF动画
  9. 区块链与安全多方计算结合
  10. 计算机是中北大学双一流建设学科不,中北大学创建“双一流”大学迎来新消息,网友:山西大学仍需努力...