首页 > 编程语言 >python a* 寻址算法 我是按照像素来判断是不是障碍物

python a* 寻址算法 我是按照像素来判断是不是障碍物

时间:2022-08-20 10:44:33浏览次数:64  
标签:map cost python self pos 像素 openlist 寻址 location

from random import randint
from PIL import Image
class SearchEntry():
    def __init__(self, x, y, g_cost, f_cost=0, pre_entry=None):
        self.x = x
        self.y = y
        # cost move form start entry to this entry
        self.g_cost = g_cost
        self.f_cost = f_cost
        self.pre_entry = pre_entry
     
    def getPos(self):
        return (self.x, self.y)
 
class Map():
    # 默认初始
    def __init__(self):
        self.img = Image.open('xiaoditu.png')
        width, height = self.img.size
        self.width = width
        self.height = height
        self.map = [[0 for y in range(self.height)] for x in range(self.width)]
        self.pathlist=[];
     
    # 遍历图片吧黑色像素都设置1  障碍物
    def createBlock(self):
        pixels = self.img.load()
        for y in range(self.height):
          for x in range(self.width):
            r, g, b = pixels[x, y]
            
            # in case your image has an alpha channel
            # r, g, b, a = pixels[x, y]
            if(r>=15):
               self.map[x][y]=1
  
     
#    最后再循环遍历map绘图  0是白色 可通行 1 障碍物 黑色不可通行 2 红色 可通行陆星
    def showMap(self):
        for y in range(self.height):
          for x in range(self.width):
                val = self.map[x][y]
                if(val==0):
                    self.img.putpixel((x,y),(255,255,255,255))
                elif(val==1):
                    self.img.putpixel((x,y),(0,0,0,255))
                else:
                    self.pathlist.append([x,y]);
                    self.img.putpixel((x,y),(255,0,0,255))
        self.img.save('daohang.png') 
        return self.pathlist
 
def AStarSearch(map, source, dest):
    def getNewPosition(map, locatioin, offset):
        x,y = (location.x + offset[0], location.y + offset[1])
        if x < 0 or x >= map.width or y < 0 or y >= map.height or map.map[y][x] == 1:
            return None
        return (x, y)
         
    def getPositions(map, location):
        # use four ways or eight ways to move
        offsets = [(-1,0), (0, -1), (1, 0), (0, 1)]
        #offsets = [(-1,0), (0, -1), (1, 0), (0, 1), (-1,-1), (1, -1), (-1, 1), (1, 1)]
        poslist = []
        for offset in offsets:
            pos = getNewPosition(map, location, offset)
            if pos is not None:         
                poslist.append(pos)
        return poslist
     
    # imporve the heuristic distance more precisely in future
    def calHeuristic(pos, dest):
        return abs(dest.x - pos[0]) + abs(dest.y - pos[1])
         
    def getMoveCost(location, pos):
        if location.x != pos[0] and location.y != pos[1]:
            return 1.4
        else:
            return 1
 
    # check if the position is in list
    def isInList(list, pos):
        if pos in list:
            return list[pos]
        return None
     
    # add available adjacent positions
    def addAdjacentPositions(map, location, dest, openlist, closedlist):
        poslist = getPositions(map, location)
        for pos in poslist:
            # if position is already in closedlist, do nothing
            if isInList(closedlist, pos) is None:
                findEntry = isInList(openlist, pos)
                h_cost = calHeuristic(pos, dest)
                g_cost = location.g_cost + getMoveCost(location, pos)
                if findEntry is None :
                    # if position is not in openlist, add it to openlist
                    openlist[pos] = SearchEntry(pos[0], pos[1], g_cost, g_cost+h_cost, location)
                elif findEntry.g_cost > g_cost:
                    # if position is in openlist and cost is larger than current one,
                    # then update cost and previous position
                    findEntry.g_cost = g_cost
                    findEntry.f_cost = g_cost + h_cost
                    findEntry.pre_entry = location
     
    # find a least cost position in openlist, return None if openlist is empty
    def getFastPosition(openlist):
        fast = None
        for entry in openlist.values():
            if fast is None:
                fast = entry
            elif fast.f_cost > entry.f_cost:
                fast = entry
        return fast
 
    openlist = {}
    closedlist = {}
    location = SearchEntry(source[0], source[1], 0.0)
    dest = SearchEntry(dest[0], dest[1], 0.0)
    openlist[source] = location
    while True:
        location = getFastPosition(openlist)
        if location is None:
            # not found valid path
            print("can't find valid path")
            break;
         
        if location.x == dest.x and location.y == dest.y:
            break
         
        closedlist[location.getPos()] = location
        openlist.pop(location.getPos())
        addAdjacentPositions(map, location, dest, openlist, closedlist)
         
    #mark the found path at the map
    while location is not None:
        map.map[location.y][location.x] = 2
        location = location.pre_entry   
 
     
map = Map()
#  注意Y,X
source = (8,3)
dest =(142,200)
map.createBlock();
AStarSearch(map, source, dest)
pathlist=map.showMap()
# 返回路径数组 x y
print(pathlist);

  

标签:map,cost,python,self,pos,像素,openlist,寻址,location
From: https://www.cnblogs.com/newmiracle/p/16607281.html

相关文章

  • Python快速生成无用大文件(GB)
    importtimedefcreatfilesize(n):local_time=time.strftime("%Y%m%d%H%M%S",time.localtime())file_name=r"D:\data\test\\"+str(local_time)+".txt"......
  • python 对文本进行分词
    #导入正则表达式相关模块importre#定义一个函数,通过该函数查找文本字符串中的每一个单词#然后计算每个单词出现的次数,最后按照出现次数从多到少放到变量中defg......
  • 学习:python 程序打包exe文件
    python程序打包exe.py首先要安裝模块pipinstall pyinstaller按住shift右鍵 打开命令窗口-输入命令intaller-F文件名.py,等待执行完成后,文件夹内会多一个dis......
  • python 中生成列表矩阵
     001、>>>[[0]*5foriinrange(3)]##生成3行5列,元素为0的矩阵[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]] 002、>>>......
  • python 中 判断列表、元组、字符串、字典、集合为空的方法
     001、>>>test1=[]>>>test1[]>>>ifnottest1:##判断列表为空...print("noelement")...noelement 002、>>>test......
  • python 中(序列)内置函数enumerate
     pyhton中内置函数enumerate用于将序列生成二元组。001、>>>str1="hello!"##测试字符串>>>foriinenumerate(str1):##enumerate用于......
  • python中 pysam包FastxFile函数
     001、读取fasta文件root@PC1:/home/test#lsa.fastaroot@PC1:/home/test#cata.fasta##测试数据>Rosalind_1ATCCAGCT>Rosalind_2GG......
  • Python PyInstaller安装和使用教程(详解版)
    在创建了独立应用(自包含该应用的依赖包)之后,还可以使用PyInstaller将 Python 程序生成可直接运行的程序,这个程序就可以被分发到对应的Windows或MacOSX平台上运行......
  • python操作mysql的应用(重复运行注册用户的接口)
    1.comm里放置数据操作代码2.conftest.py里放置删除用户代码3.test_register.py里放置测试注册用户的代码(运行注册接口之前先从数据库删除注册的账号)  ----------......
  • python-docx操作word
    python-docx学习资料比较不错的,随后附上 用于修改表格边框的函数及相关网站OfficeOpenXML(OOXML)-WordProcessing-TableBordersfromdocx.oxmlimportOxmlE......