python a_python 实现A*算法

import math

def shortest_path(M,start,goal):

sx=M.intersections[start][0]

sy=M.intersections[start][1]

gx=M.intersections[goal][0]

gy=M.intersections[goal][1]

h=math.sqrt((sx-gx)*(sx-gx)+(sy-gy)*(sy-gy))

closedSet=set()

openSet=set()

openSet.add(start)

gScore={}

gScore[start]=0fScore={}

fScore[start]=h

cameFrom={}

sumg=0NEW=0BOOL=Falsewhile len(openSet)!=0:

MAX=1000

for new inopenSet:

print("new",new)if fScore[new]

MAX=fScore[new]

#print("MAX=",MAX)

NEW=newcurrent=NEW

print("current=",current)if current==goal:returnreconstruct_path(cameFrom,current)

openSet.remove(current)

closedSet.add(current)

#dafult=M.roads(current)for neighbor inM.roads[current]:

BOOL=False

print("key=",neighbor)

a={neighbor}if len(a&closedSet)>0:continueprint("key is not in closeSet")if len(a&openSet)==0:

openSet.add(neighbor)else:

BOOL=True

x= M.intersections[current][0]

y= M.intersections[current][1]

x1=M.intersections[neighbor][0]

y1=M.intersections[neighbor][1]

g=math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1))

h=math.sqrt((x1-gx)*(x1-gx)+(y1-gy)*(y1-gy))

new_gScore=gScore[current]+gif BOOL==True:if new_gScore>=gScore[neighbor]:continueprint("new_gScore",new_gScore)

cameFrom[neighbor]=current

gScore[neighbor]=new_gScore

fScore[neighbor]= new_gScore+h

print("fScore",neighbor,"is",new_gScore+h)

print("fScore=",new_gScore+h)

print("__________++--------------++_________")

def reconstruct_path(cameFrom,current):

print("已到达lllll")

total_path=[]

total_path.append(current)for key,value incameFrom.items():

print("key",key,":","value",value)while current incameFrom.keys():

current=cameFrom[current]

total_path.append(current)

total_path=list(reversed(total_path))return total_path

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值