Earlier wrote an article on a A * algorithm: "algorithm: Astar pathfinding algorithm Improvement"
Recently wrote a JS UI framework, the way to implement a JS version of A * algorithm, unlike before, the * algorithm is a two-way *.
What are the benefits of a two-way *?
We know that the time complexity of a * is positively correlated with the number of nodes and the difficulty of the starting point as a power function.
This http://qiao.github.io/PathFinding.js/visual/is a good demonstration of the effect of a two-way *, let's take a look.
Green is the starting point, red means the end point, and Gray is the wall. The two slightly lighter greens represent the open node and the close node, respectively:
When the path through the narrow channel, if the starting point is closer to the channel, it is easy to find the end point, the start and end of the exchange,
It can be noted that the node being looked up is more than one fold.
We can think that the end point in the narrow zone is the worst result, in the open area is the best result. Take a look at the addressing of the bidirectional A * algorithm:
As you can see, the optimal results are very close to one-way.
Observing the light green pathfinding node We can notice that when the open node first touches it, it aborts the lookup.
According to this idea, the implementation algorithm is as follows:
while(true) {Minfnode=Anra. Astarutil.findminnode (openlist); Openlist.removeobject (Minfnode); if(!closedlist.contains (Minfnode)) Closedlist.push (Minfnode); if(Closedlist.length > 500) { return; } if(Minfnode = =NULL||minfnode.equals (endnode)) Break; Anra. Astarutil.search ( This, Minfnode, Openlist, Closedlist, Endnode); Biminfnode=Anra. Astarutil.findminnode (biopenlist); Biopenlist.removeobject (Biminfnode); if(!biclosedlist.contains (Biminfnode)) Biclosedlist.push (Biminfnode); if(Biclosedlist.length > 500) { return; } if(Biminfnode = =NULL||biminfnode.equals (startnode)) Break; Anra. Astarutil.search ( This, Biminfnode, Biopenlist, Biclosedlist, startnode); for(vari = 0; i < openlist.length; i++) { for(varj = 0; J < Biopenlist.length; J + +) { if(Biopenlist[j].equals (Openlist[i])) {Biminfnode=Biopenlist[j]; Minfnode=Openlist[i]; Middlenode=Minfnode; Break; } } if(Middlenode) Break; } if(Middlenode) Break; }
Findminnode:function(openlist) {if(Openlist.length = = 0) return NULL; Else if(Openlist.length = = 1) returnOpenlist[0]; Openlist.sort (function(A, b) {returnA.F ()-B.f (); }); returnOpenlist[0]; }, /*Search*/Search:function(router, node, openlist, Closedlist, Endnode) {varnodes = This. Findaroundnode (router, node); if(Nodes = =NULL) return; for(vari = 0; I < 8; i++) { if(Nodes[i] = =NULL|| Nodes[i].level = =NULL)Continue; NODES[I].G= (i > 3? nodes[i].level[0]: nodes[i].level[1]) +node.g; Nodes[i].h= This. Caculateh (Nodes[i], endnode); if(Closedlist.contains (Nodes[i])) {Continue; } if(!openlist.contains (Nodes[i])) {Openlist.push (nodes[i]); Nodes[i].parent=node; } Else { varIDX =Openlist.indexof (Nodes[i]); varn =Openlist[idx]; if(NODES[I].G <n.g) {openlist.remove (IDX); Closedlist.push (n); Nodes[i].parent=n.parent; Openlist.splice (IDX,0, Nodes[i]); } } } }, /*find available nodes around a specified node*/Findaroundnode:function(router, node) {if(node = =NULL)return NULL; varnodes = []; nodes[0] = anodefactory.create (Router, node.i, NODE.J + 1); nodes[1] = Anodefactory.create (Router, node.i, node.j-1); nodes[2] = anodefactory.create (Router, node.i + 1, NODE.J); nodes[3] = Anodefactory.create (Router, node.i-1, NODE.J); nodes[4] = Anodefactory.create (Router, node.i-1, NODE.J + 1); nodes[5] = anodefactory.create (Router, node.i + 1, node.j-1); nodes[6] = Anodefactory.create (Router, node.i + 1, NODE.J + 1); nodes[7] = anodefactory.create (Router, node.i-1, node.j-1); returnnodes; }, Caculateh:function(P, endnode) {return(Math.Abs (ENDNODE.I-P.I) +Math.Abs (ENDNODE.J-p.j))* P.level[0]; },
Algorithm: Improved Astar pathfinding algorithm, bidirectional * pathfinding algorithm