基于javascript对astar算法的实践(目前未完成,持续更新)

基于javascript对geojson数据格式的地图进行astar算法的实践

对于geojson数据格式的处理

pp=JSON.parse(pp) //pp为地图数据
console.log(pp);
var lineObclo=[];var linecooclo=[]
for(i=0;i<pp.features.length;i++)
  {
      if(pp.features[i].geometry.type=="LineString")
      {lineObclo.push(pp.features[i]);linecooclo.push(pp.features[i].geometry.coordinates)}  
  }//提取线特征
var linecooclo2=[]
for(i=0;i<linecooclo.length;i++)
{
var linenode1=[(linecooclo[i][0][0]-121.29)*150000,(linecooclo[i][0][1]-31.187)*150000];
var linenode2=[(linecooclo[i][1][0]-121.29)*150000,(linecooclo[i][1][1]-31.187)*150000]   //这里是针对地图中心经纬度进行的放缩
linecooclo2.push([linenode1,linenode2])
}//特征做变化
linecooclo=linecooclo2
var pointclo=[];//var p=[];
console.log("linecooclo",linecooclo,linecooclo.length)

var list=linecooclo;

点处理

function Node(x,y,parent) {
    this.node={
        x:x,
        y:y,
        parent:parent,//之前走的路点
        g:0,//当前点到起点的值
        h:0,//当前点到终点的距离
        f:0//f=g+h
    };
}

Node.prototype.getX=function() {
    return this.node.x;
}
Node.prototype.setX=function(x) {
    this.node.x=x;
}
Node.prototype.getY=function() {
    return this.node.y;
}
Node.prototype.setY=function(y) {
    this.node.y=y;
}
Node.prototype.getParent=function() {
    return this.node.parent;
}
Node.prototype.setParent=function(parent) {
    this.node.parent=parent;
}
Node.prototype.getG=function() {
    return this.node.g;
}
Node.prototype.setG=function(g) {
    this.node.g=g;
}
Node.prototype.getH=function() {
    return this.node.h;
}
Node.prototype.setH=function(h) {
    this.node.h=h;
}
Node.prototype.getF=function() {
    return this.node.f;
}
Node.prototype.setF=function(f) {
    this.node.f=f;
}
function count(a,b,c,d){
    {
        var aa=Math.abs(a-c);var bb=Math.abs(b-d)
        var cc,dd;
        if(aa>bb){cc=aa-bb;dd=bb;}else{cc=bb-aa;dd=aa;}
        return cc+1.4*bb
    }
}


Node.prototype.getpath=function(){
    var path=[];path.push([this.node.x,this.node.y]);var nodee=this.node.parent;
    while(nodee.getParent()!=null)
    {path.push([nodee.getX(),nodee.getY()]);nodee=nodee.getParent();}
    return path
}

Astar算法主体

function input(a,b,c,d)
{//g精确,h估计
    
    var snode=new Node(a,b,null)
    var enode=new Node(c,d,null)
    snode.setG(0);snode.setH(count(a,b,c,d));snode.setF(count(a,b,c,d));
    var open=[];
    var t=0;
    open.push(snode)
    var close=[];
    var openaclose=[];
    openaclose.push(snode)
    var node=snode;
    var result=[]
    var isFind=false;

    while(open.length>0)
    {   node=open[0];

        
        if(node.getX()==enode.getX()&&node.getY()==enode.getY())
         {
            return node;             
            }
           
            var children=[];
           
            for(i=0;i<map.length;i++)
            {  
                if((((node.getX()-map[i][0])>-3)&&((node.getX()-map[i][0])<3)&&((node.getY()-map[i][1])>-3)
                &&((node.getY()-map[i][1])<3)) &&((node.getX()!=map[i][0])||(node.getY()!=map[i][1])))
                {children.push(map[i]);}
            }
           
            for(i=0;i<children.length;i++)
            {  var k=true;
                for(j=0;j<openaclose.length;j++)
                {if((openaclose[j].getX()==children[i][0])&&(openaclose[j].getY()==children[i][1])){k=false;break;}}
              if(k==true){var nodee=new Node(children[i][0],children[i][1],node)
                nodee.setG(node.getG()+count(nodee.getX(),nodee.getY(),node.getX(),node.getY()));
                nodee.setH(count(nodee.getX(),nodee.getY(),c,d));nodee.setF(nodee.getH()+nodee.getG());//未完成
                openaclose.push(nodee);open.push(nodee)/*;console.log("已添加子节点",nodee,open)*/;}                    
            }
            var instant=node.getG()
            
        for(i=0;i<children.length;i++)
            {
                for(j=0;j<openaclose.length;j++)
                    {
                       if((openaclose[j].getX()==children[i][0])&&(openaclose[j].getY()==children[i][0]))
                      {
                          if((openaclose[j].getG()+count(openaclose[j].getX(),close[j].getY(),node.getX(),node.getY()))<instant)
                          {node.setParent(open[j]);
                            node.setG(openaclose[j].getG()+count(openaclose[j].getX(),openaclose[j].getY(),node.getX(),node.getY()));
                            node.setF(count(node.getH()+node.getG()));instant=node.getG()/*console.log("已更改父节点")*/}
                      }
                    }
            }             
        //更改父节点,同时更改fgh值
        //添加子节点,透视添加fgh值
 
        for(i=open.length-1;i>0;i--)
            {for(j=0;j<i;j++)
                {
                    if(open[j].getF()>open[j+1].getF())
                    {
                       var temp=open[j];open[j]=open[j+1];open[j+1]=temp;//console.log("已排序")
                    }   
                }        
            }           //排列大小,按照f
         close.push(open[0])
         open.splice(0,1);
        if(open.length==0){alert("无路经");return false;}
        t++
    }
}

处理效果分析

目前还无法做到十分完整的效果:
1.大地图数据处理效率过低
2.地图处理会出现断点,地图无法保证Astar算法完全有效
3.对于相同的起始点,二者更换位置寻路结果可能不同。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是基于栅格地图的A*算法的Python实现和测试用例: ```python import heapq class AStar: def __init__(self, grid): self.grid = grid self.width = len(grid[0]) self.height = len(grid) def heuristic(self, a, b): return abs(a[0] - b[0]) + abs(a[1] - b[1]) def get_neighbors(self, node): neighbors = [] for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]: x = node[0] + dx y = node[1] + dy if 0 <= x < self.width and 0 <= y < self.height and self.grid[y][x] == 0: neighbors.append((x, y)) return neighbors def astar(self, start, end): frontier = [] heapq.heappush(frontier, (0, start)) came_from = {} cost_so_far = {} came_from[start] = None cost_so_far[start] = 0 while frontier: current = heapq.heappop(frontier)[1] if current == end: break for neighbor in self.get_neighbors(current): new_cost = cost_so_far[current] + 1 if neighbor not in cost_so_far or new_cost < cost_so_far[neighbor]: cost_so_far[neighbor] = new_cost priority = new_cost + self.heuristic(end, neighbor) heapq.heappush(frontier, (priority, neighbor)) came_from[neighbor] = current path = [] current = end while current != start: path.append(current) current = came_from[current] path.append(start) path.reverse() return path # 测试用例 grid = [[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 1, 0], [0, 0, 0, 0, 0]] astar = AStar(grid) path = astar.astar((0, 0), (4, 4)) print(path) ``` 输出结果为: ``` [(0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (4, 3), (4, 4)] ``` 这是从起点(0, 0)到终点(4, 4)的最短路径。 请问还有什么问题需要我回答吗?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值