目的:

在广度优先算法上进行进化。一致代价搜索算法每次扩展的是当前路径消耗g(n)最小的节点n。

源码:

数据结构:

  • frontier : 边缘,存储未扩展的节点。通过维护一个优先级队列,按路径损耗来排列。
  • explored :探索集,保存已访问的节点。

算法流程:

  • 如果边缘为空,则返回失败。操作:EMPTY?(frontier)
  • 否则从边缘中选择一个叶子节点。操作:POP(frontier)
  • 目标测试:通过返回,否则将叶子节点的状态放在探索集
  • 遍历叶子节点的所有动作

每个动作产生子节点

如果子节点的状态不在探索集或者边缘,则插入到边缘集合。操作:INSERT(child, frontier)

否则如果边缘集合中如果存在此状态且有更高的路径消耗,则用子节点替代边缘集合中的状态

算法性能分析:

当所有的单步消耗都相等时,一致代价搜索与广度优先搜索类似。在终止条件上,广度优先搜索在找到解时终止,而一致代价搜索会检查目标深度的所有节点,看谁的代价最小。在这种情况下,一致代价搜索在深度d无意义的做了更多工作。

示例代码:(参考http://blog.csdn.net/jdh99)

import pandas as pd
from pandas import Series, DataFrame# 城市信息:city1 city2 path_cost
_city_info = None# 按照路径消耗进行排序的FIFO,低路径消耗在前面
_frontier_priority = []# 节点数据结构
class Node:def __init__(self, state, parent, action, path_cost):self.state = stateself.parent = parentself.action = actionself.path_cost = path_costdef main():global _city_infoimport_city_info()while True:src_city = input('input src city\n')dst_city = input('input dst city\n')# result = breadth_first_search(src_city, dst_city)result = uniform_cost_search(src_city, dst_city)if not result:print('from city: %s to city %s search failure' % (src_city, dst_city))else:print('from city: %s to city %s search success' % (src_city, dst_city))path = []while True:path.append(result.state)if result.parent is None:breakresult = result.parentsize = len(path)for i in range(size):if i < size - 1:print('%s->' % path.pop(), end='')else:print(path.pop())def import_city_info():global _city_infodata = [{'city1': 'Oradea', 'city2': 'Zerind', 'path_cost': 71},{'city1': 'Oradea', 'city2': 'Sibiu', 'path_cost': 151},{'city1': 'Zerind', 'city2': 'Arad', 'path_cost': 75},{'city1': 'Arad', 'city2': 'Sibiu', 'path_cost': 140},{'city1': 'Arad', 'city2': 'Timisoara', 'path_cost': 118},{'city1': 'Timisoara', 'city2': 'Lugoj', 'path_cost': 111},{'city1': 'Lugoj', 'city2': 'Mehadia', 'path_cost': 70},{'city1': 'Mehadia', 'city2': 'Drobeta', 'path_cost': 75},{'city1': 'Drobeta', 'city2': 'Craiova', 'path_cost': 120},{'city1': 'Sibiu', 'city2': 'Fagaras', 'path_cost': 99},{'city1': 'Sibiu', 'city2': 'Rimnicu Vilcea', 'path_cost': 80},{'city1': 'Rimnicu Vilcea', 'city2': 'Craiova', 'path_cost': 146},{'city1': 'Rimnicu Vilcea', 'city2': 'Pitesti', 'path_cost': 97},{'city1': 'Craiova', 'city2': 'Pitesti', 'path_cost': 138},{'city1': 'Fagaras', 'city2': 'Bucharest', 'path_cost': 211},{'city1': 'Pitesti', 'city2': 'Bucharest', 'path_cost': 101},{'city1': 'Bucharest', 'city2': 'Giurgiu', 'path_cost': 90},{'city1': 'Bucharest', 'city2': 'Urziceni', 'path_cost': 85},{'city1': 'Urziceni', 'city2': 'Vaslui', 'path_cost': 142},{'city1': 'Urziceni', 'city2': 'Hirsova', 'path_cost': 98},{'city1': 'Neamt', 'city2': 'Iasi', 'path_cost': 87},{'city1': 'Iasi', 'city2': 'Vaslui', 'path_cost': 92},{'city1': 'Hirsova', 'city2': 'Eforie', 'path_cost': 86}]_city_info = DataFrame(data, columns=['city1', 'city2', 'path_cost'])
# print(_city_info)'''
def breadth_first_search(src_state, dst_state):global _city_infonode = Node(src_state, None, None, 0)frontier = [node]explored = []while True:if len(frontier) == 0:return Falsenode = frontier.pop(0)explored.append(node.state)# 目标测试if node.state == dst_state:return nodeif node.parent is not None:print('deal node:state:%s\tparent state:%s\tpath cost:%d' % (node.state, node.parent.state, node.path_cost))else:print('deal node:state:%s\tparent state:%s\tpath cost:%d' % (node.state, None, node.path_cost))# 遍历子节点for i in range(len(_city_info)):dst_city = ''if _city_info['city1'][i] == node.state:dst_city = _city_info['city2'][i]elif _city_info['city2'][i] == node.state:dst_city = _city_info['city1'][i]if dst_city == '':continuechild = Node(dst_city, node, 'go', node.path_cost + _city_info['path_cost'][i])print('\tchild node:state:%s path cost:%d' % (child.state, child.path_cost))if child.state not in explored and not is_node_in_frontier(frontier, child):frontier.append(child)print('\t\t add child to child')
'''def is_node_in_frontier(frontier, node):for x in frontier:if node.state == x.state:return Truereturn Falsedef uniform_cost_search(src_state, dst_state):global _city_info, _frontier_prioritynode = Node(src_state, None, None, 0)frontier_priority_add(node)explored = []while True:if len(_frontier_priority) == 0:return Falsenode = _frontier_priority.pop(0)explored.append(node.state)# 目标测试if node.state == dst_state:print('\t this node is goal!')return nodeif node.parent is not None:print('deal node:state:%s\tparent state:%s\tpath cost:%d' % (node.state, node.parent.state, node.path_cost))else:print('deal node:state:%s\tparent state:%s\tpath cost:%d' % (node.state, None, node.path_cost))# 遍历子节点for i in range(len(_city_info)):dst_city = ''if _city_info['city1'][i] == node.state:dst_city = _city_info['city2'][i]elif _city_info['city2'][i] == node.state:dst_city = _city_info['city1'][i]if dst_city == '':continuechild = Node(dst_city, node, 'go', node.path_cost + _city_info['path_cost'][i])print('\tchild node:state:%s path cost:%d' % (child.state, child.path_cost))if child.state not in explored and not is_node_in_frontier(_frontier_priority, child):frontier_priority_add(child)print('\t\t add child to frontier')elif is_node_in_frontier(_frontier_priority, child):# 替代为路径消耗少的节点frontier_priority_replace_by_priority(child)def frontier_priority_add(node):""":param Node node::return:"""global _frontier_prioritysize = len(_frontier_priority)for i in range(size):#如果新加入的节点存在阈值较小的情况,插入队列if node.path_cost < _frontier_priority[i].path_cost:_frontier_priority.insert(i, node)return#否则,新添加的节点比优先级队列中现有的节点阈值都大,直接添加到队列末尾_frontier_priority.append(node)def frontier_priority_replace_by_priority(node):""":param Node node::return:"""global _frontier_prioritysize = len(_frontier_priority)for i in range(size):if _frontier_priority[i].state == node.state and _frontier_priority[i].path_cost > node.path_cost:print('\t\t replace state: %s old cost:%d new cost:%d' % (node.state,_frontier_priority[i].path_cost,node.path_cost))_frontier_priority[i] = nodereturnif __name__ == '__main__':main()

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

  1. 人工智能学习:python实现迭代加深的深度优先搜索

    人工智能学习:python实现深度优先搜索算法 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN10 python版本:3.5 开发 ...

  2. python与人工智能编程-python学习(一)——python与人工智能

    最近在朋友圈转起了一张图.抱着试一试的心态,我肝了些课程.都是与python相关的. 课程一:你不知道的python 讲师:王玉杰(混沌巡洋舰联合创始人 & web开发工程师 & 数据 ...

  3. python人工智能学多久_Python人工智能学习需要多久?什么学历可以学习?

    当下有很多热门的技术,Python人工智能就是其中之一,在薪资待遇方面AI类岗位.大数据类岗位普遍排名在前.同时人才需求量一直处于上升趋势中,因此越来越多人都想参加Python人工智能培训,那么人工智 ...

  4. 学python对数学要求高吗_人工智能的小男孩 大专学历的人没有数学基础想学习python技术未来能往大数据或人工智能方向进行职业发展吗?...

    内容由传智播客提供,电器吧机器人网提供人工智能的小男孩相关内容,小编烟酉为您整理并发布于人工智能栏目下,原标题:大专学历的人没有数学基础想学习python技术未来能往大数据或人工智能方向进行职业发展吗 ...

  5. python人工智能pdf_800G人工智能学习资料:AI电子书+Python语言入门+教程+机器学习等限时免费领取!...

    人工智能的热潮在今年再创高峰.无论是政策上还是资本市场上,抑或是我们日常生活中,关于人工智能的消息从未间断,逐渐占据我们每个人的视线.可以看出,人工智能得到了由上至下,国内国外的一致支持. 基于这股热 ...

  6. python 人工智能课程对孩子的好处_少儿编程有什么好处?儿童编程课程学习Python的4大原因...

    儿童编程Python是什么课程? Python是强制用一种面向对象的解释型计算机程序设计语言,它是纯粹的自由软件, 源代码和解释器CPython遵循 GPL(GNU General Public Li ...

  7. 学习Python人工智能前景如何

    Python技术一直是和人工智能挂钩比较多的,因为在人工智能领域Python的技术非常重要,那么学习Python人工智能前景如何呢?来看看下面的详细介绍就知道了. 学习Python人工智能前景如何?人 ...

  8. 人工智能学python还是c_考虑到未来人工智能的发展,应该学习C++/C语言还是Python语言?...

    从成本考虑 如果是应用还是用python 比较方便. 因为未来人工智能的一些工作并非完全由计算机专业人做,人工智能是工具,大多数人会使用即可,可能底层算法用c或c++,这好比java,目前看来pyth ...

  9. Python全栈+人工智能学习路线

    2019最新Python全栈+人工智能学习路线升级版 全面涵盖前端.后端.爬虫.数据挖掘.人工智能等课程(课程大纲+视频教程+网盘资源下载)! https://www.cnblogs.com/coff ...

最新文章

  1. 将中文日期转换成自己想要的格式如:2018年09月29日转换成2018-09-29
  2. mybatis 源码分析, 初始化
  3. PEP 20 -- The Zen of Python(Python之禅)
  4. 编译apache过程中出现如下错误及解决办法
  5. 前端解读面向切面编程(AOP)
  6. 密码学专题 鉴别协议|实际应用的混合协议
  7. python网盘系统_python做系统
  8. hpunix查看oracle监听,hp-ux 网络查看
  9. VMWare学习总结(3)——Vmware Workstation 14虚拟机网卡桥接连不上网络解决方法
  10. 修改服务器资产信息,CMDB服务器管理系统【s5day90】:创建资产更新服务器硬盘信息...
  11. websocket传输数据大小限制_websocket 发送字符串数据上限是多少
  12. schedule和scheduleAtFixedRate
  13. echarts设置之stack参数
  14. 模式匹配算法逐步精简
  15. 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)
  16. 如何为长期旅行做准备
  17. 一文搞懂HTTP协议(带图文)
  18. 采集商标网的10个经典方法
  19. 丹佛机场行李处理系统
  20. nmon在linux应用

热门文章

  1. 超级品牌符号怎么设计?大咖有方法
  2. Ruby Ruport实践—中文PDF报表之PRAWN
  3. 电子烟HPHCs测试
  4. bootstrap表格插件php,bootstrap table表格插件使用详解
  5. 移动联通基站接口文档
  6. 支付宝手机网站支付签约强开WAP支付,提示“系统综合评估签约条件不满足”或不满足国家法律法规或支付宝用户服务协议等的解决方案!支付宝H5支付开通方法详解
  7. 火山PC官方python库使用-正确调用
  8. mui使用百度语音合成来制作文字转语音来播放
  9. 华为日本设立研发中心,4大“X实验室”布局5G+物联网时代
  10. 未注册老域名扫描软件-免费未注册老域名挖掘