-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngraph.path.min.js
1 lines (1 loc) · 15.7 KB
/
ngraph.path.min.js
1
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ngraphPath=f()}})(function(){var define,module,exports;return function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r}()({1:[function(require,module,exports){module.exports=NodeHeap;function NodeHeap(data,options){if(!(this instanceof NodeHeap))return new NodeHeap(data,options);if(!Array.isArray(data)){options=data;data=[]}options=options||{};this.data=data||[];this.length=this.data.length;this.compare=options.compare||defaultCompare;this.setNodeId=options.setNodeId||noop;if(this.length>0){for(var i=this.length>>1;i>=0;i--)this._down(i)}if(options.setNodeId){for(var i=0;i<this.length;++i){this.setNodeId(this.data[i],i)}}}function noop(){}function defaultCompare(a,b){return a-b}NodeHeap.prototype={push:function(item){this.data.push(item);this.setNodeId(item,this.length);this.length++;this._up(this.length-1)},pop:function(){if(this.length===0)return undefined;var top=this.data[0];this.length--;if(this.length>0){this.data[0]=this.data[this.length];this.setNodeId(this.data[0],0);this._down(0)}this.data.pop();return top},peek:function(){return this.data[0]},updateItem:function(pos){this._down(pos);this._up(pos)},_up:function(pos){var data=this.data;var compare=this.compare;var setNodeId=this.setNodeId;var item=data[pos];while(pos>0){var parent=pos-1>>1;var current=data[parent];if(compare(item,current)>=0)break;data[pos]=current;setNodeId(current,pos);pos=parent}data[pos]=item;setNodeId(item,pos)},_down:function(pos){var data=this.data;var compare=this.compare;var halfLength=this.length>>1;var item=data[pos];var setNodeId=this.setNodeId;while(pos<halfLength){var left=(pos<<1)+1;var right=left+1;var best=data[left];if(right<this.length&&compare(data[right],best)<0){left=right;best=data[right]}if(compare(best,item)>=0)break;data[pos]=best;setNodeId(best,pos);pos=left}data[pos]=item;setNodeId(item,pos)}}},{}],2:[function(require,module,exports){module.exports=aStarBi;var NodeHeap=require("./NodeHeap");var makeSearchStatePool=require("./makeSearchStatePool");var heuristics=require("./heuristics");var defaultSettings=require("./defaultSettings");var BY_FROM=1;var BY_TO=2;var NO_PATH=defaultSettings.NO_PATH;module.exports.l2=heuristics.l2;module.exports.l1=heuristics.l1;function aStarBi(graph,options){options=options||{};var oriented=options.oriented;var heuristic=options.heuristic;if(!heuristic)heuristic=defaultSettings.heuristic;var distance=options.distance;if(!distance)distance=defaultSettings.distance;var pool=makeSearchStatePool();return{find:find};function find(fromId,toId){var from=graph.getNode(fromId);if(!from)throw new Error("fromId is not defined in this graph: "+fromId);var to=graph.getNode(toId);if(!to)throw new Error("toId is not defined in this graph: "+toId);if(from===to)return[from];pool.reset();var callVisitor=oriented?orientedVisitor:nonOrientedVisitor;var nodeState=new Map;var openSetFrom=new NodeHeap({compare:defaultSettings.compareFScore,setNodeId:defaultSettings.setHeapIndex});var openSetTo=new NodeHeap({compare:defaultSettings.compareFScore,setNodeId:defaultSettings.setHeapIndex});var startNode=pool.createNewState(from);nodeState.set(fromId,startNode);startNode.fScore=heuristic(from,to);startNode.distanceToSource=0;openSetFrom.push(startNode);startNode.open=BY_FROM;var endNode=pool.createNewState(to);endNode.fScore=heuristic(to,from);endNode.distanceToSource=0;openSetTo.push(endNode);endNode.open=BY_TO;var lMin=Number.POSITIVE_INFINITY;var minFrom;var minTo;var currentSet=openSetFrom;var currentOpener=BY_FROM;while(openSetFrom.length>0&&openSetTo.length>0){if(openSetFrom.length<openSetTo.length){currentOpener=BY_FROM;currentSet=openSetFrom}else{currentOpener=BY_TO;currentSet=openSetTo}var current=currentSet.pop();current.closed=true;if(current.distanceToSource>lMin)continue;graph.forEachLinkedNode(current.node.id,callVisitor);if(minFrom&&minTo){return reconstructBiDirectionalPath(minFrom,minTo)}}return NO_PATH;function nonOrientedVisitor(otherNode,link){return visitNode(otherNode,link,current)}function orientedVisitor(otherNode,link){if(currentOpener===BY_FROM){if(link.fromId===current.node.id)return visitNode(otherNode,link,current)}else if(currentOpener===BY_TO){if(link.toId===current.node.id)return visitNode(otherNode,link,current)}}function canExit(currentNode){var opener=currentNode.open;if(opener&&opener!==currentOpener){return true}return false}function reconstructBiDirectionalPath(a,b){var pathOfNodes=[];var aParent=a;while(aParent){pathOfNodes.push(aParent.node);aParent=aParent.parent}var bParent=b;while(bParent){pathOfNodes.unshift(bParent.node);bParent=bParent.parent}return pathOfNodes}function visitNode(otherNode,link,cameFrom){var otherSearchState=nodeState.get(otherNode.id);if(!otherSearchState){otherSearchState=pool.createNewState(otherNode);nodeState.set(otherNode.id,otherSearchState)}if(otherSearchState.closed){return}if(canExit(otherSearchState,cameFrom)){var potentialLMin=otherSearchState.distanceToSource+cameFrom.distanceToSource;if(potentialLMin<lMin){minFrom=otherSearchState;minTo=cameFrom;lMin=potentialLMin}return}var tentativeDistance=cameFrom.distanceToSource+distance(otherSearchState.node,cameFrom.node,link);if(tentativeDistance>=otherSearchState.distanceToSource){return}var target=currentOpener===BY_FROM?to:from;var newFScore=tentativeDistance+heuristic(otherSearchState.node,target);if(newFScore>=lMin){return}otherSearchState.fScore=newFScore;if(otherSearchState.open===0){currentSet.push(otherSearchState);currentSet.updateItem(otherSearchState.heapIndex);otherSearchState.open=currentOpener}otherSearchState.parent=cameFrom;otherSearchState.distanceToSource=tentativeDistance}}}},{"./NodeHeap":1,"./defaultSettings":4,"./heuristics":5,"./makeSearchStatePool":6}],3:[function(require,module,exports){module.exports=aStarPathSearch;var NodeHeap=require("./NodeHeap");var makeSearchStatePool=require("./makeSearchStatePool");var heuristics=require("./heuristics");var defaultSettings=require("./defaultSettings.js");var NO_PATH=defaultSettings.NO_PATH;module.exports.l2=heuristics.l2;module.exports.l1=heuristics.l1;function aStarPathSearch(graph,options){options=options||{};var oriented=options.oriented;var heuristic=options.heuristic;if(!heuristic)heuristic=defaultSettings.heuristic;var distance=options.distance;if(!distance)distance=defaultSettings.distance;var pool=makeSearchStatePool();return{find:find};function find(fromId,toId){var from=graph.getNode(fromId);if(!from)throw new Error("fromId is not defined in this graph: "+fromId);var to=graph.getNode(toId);if(!to)throw new Error("toId is not defined in this graph: "+toId);pool.reset();var nodeState=new Map;var openSet=new NodeHeap({compare:defaultSettings.compareFScore,setNodeId:defaultSettings.setHeapIndex});var startNode=pool.createNewState(from);nodeState.set(fromId,startNode);startNode.fScore=heuristic(from,to);startNode.distanceToSource=0;openSet.push(startNode);startNode.open=1;var cameFrom;while(openSet.length>0){cameFrom=openSet.pop();if(goalReached(cameFrom,to))return reconstructPath(cameFrom);cameFrom.closed=true;graph.forEachLinkedNode(cameFrom.node.id,visitNeighbour,oriented)}return NO_PATH;function visitNeighbour(otherNode,link){var otherSearchState=nodeState.get(otherNode.id);if(!otherSearchState){otherSearchState=pool.createNewState(otherNode);nodeState.set(otherNode.id,otherSearchState)}if(otherSearchState.closed){return}if(otherSearchState.open===0){openSet.push(otherSearchState);otherSearchState.open=1}var tentativeDistance=cameFrom.distanceToSource+distance(otherNode,cameFrom.node,link);if(tentativeDistance>=otherSearchState.distanceToSource){return}otherSearchState.parent=cameFrom;otherSearchState.distanceToSource=tentativeDistance;otherSearchState.fScore=tentativeDistance+heuristic(otherSearchState.node,to);openSet.updateItem(otherSearchState.heapIndex)}}}function goalReached(searchState,targetNode){return searchState.node===targetNode}function reconstructPath(searchState){var path=[searchState.node];var parent=searchState.parent;while(parent){path.push(parent.node);parent=parent.parent}return path}},{"./NodeHeap":1,"./defaultSettings.js":4,"./heuristics":5,"./makeSearchStatePool":6}],4:[function(require,module,exports){var NO_PATH=[];if(typeof Object.freeze==="function")Object.freeze(NO_PATH);module.exports={heuristic:blindHeuristic,distance:constantDistance,compareFScore:compareFScore,NO_PATH:NO_PATH,setHeapIndex:setHeapIndex,setH1:setH1,setH2:setH2,compareF1Score:compareF1Score,compareF2Score:compareF2Score};function blindHeuristic(){return 0}function constantDistance(){return 1}function compareFScore(a,b){var result=a.fScore-b.fScore;return result}function setHeapIndex(nodeSearchState,heapIndex){nodeSearchState.heapIndex=heapIndex}function compareF1Score(a,b){return a.f1-b.f1}function compareF2Score(a,b){return a.f2-b.f2}function setH1(node,heapIndex){node.h1=heapIndex}function setH2(node,heapIndex){node.h2=heapIndex}},{}],5:[function(require,module,exports){module.exports={l2:l2,l1:l1};function l2(a,b){var dx=a.x-b.x;var dy=a.y-b.y;return Math.sqrt(dx*dx+dy*dy)}function l1(a,b){var dx=a.x-b.x;var dy=a.y-b.y;return Math.abs(dx)+Math.abs(dy)}},{}],6:[function(require,module,exports){function NodeSearchState(node){this.node=node;this.parent=null;this.closed=false;this.open=0;this.distanceToSource=Number.POSITIVE_INFINITY;this.fScore=Number.POSITIVE_INFINITY;this.heapIndex=-1}function makeSearchStatePool(){var currentInCache=0;var nodeCache=[];return{createNewState:createNewState,reset:reset};function reset(){currentInCache=0}function createNewState(node){var cached=nodeCache[currentInCache];if(cached){cached.node=node;cached.parent=null;cached.closed=false;cached.open=0;cached.distanceToSource=Number.POSITIVE_INFINITY;cached.fScore=Number.POSITIVE_INFINITY;cached.heapIndex=-1}else{cached=new NodeSearchState(node);nodeCache[currentInCache]=cached}currentInCache++;return cached}}module.exports=makeSearchStatePool},{}],7:[function(require,module,exports){module.exports=nba;var NodeHeap=require("../NodeHeap");var heuristics=require("../heuristics");var defaultSettings=require("../defaultSettings.js");var makeNBASearchStatePool=require("./makeNBASearchStatePool.js");var NO_PATH=defaultSettings.NO_PATH;module.exports.l2=heuristics.l2;module.exports.l1=heuristics.l1;function nba(graph,options){options=options||{};var oriented=options.oriented;var quitFast=options.quitFast;var heuristic=options.heuristic;if(!heuristic)heuristic=defaultSettings.heuristic;var distance=options.distance;if(!distance)distance=defaultSettings.distance;var pool=makeNBASearchStatePool();return{find:find};function find(fromId,toId){var from=graph.getNode(fromId);if(!from)throw new Error("fromId is not defined in this graph: "+fromId);var to=graph.getNode(toId);if(!to)throw new Error("toId is not defined in this graph: "+toId);pool.reset();var forwardVisitor=oriented?visitN1Oriented:visitN1;var reverseVisitor=oriented?visitN2Oriented:visitN2;var nodeState=new Map;var open1Set=new NodeHeap({compare:defaultSettings.compareF1Score,setNodeId:defaultSettings.setH1});var open2Set=new NodeHeap({compare:defaultSettings.compareF2Score,setNodeId:defaultSettings.setH2});var minNode;var lMin=Number.POSITIVE_INFINITY;var startNode=pool.createNewState(from);nodeState.set(fromId,startNode);startNode.g1=0;var f1=heuristic(from,to);startNode.f1=f1;open1Set.push(startNode);var endNode=pool.createNewState(to);nodeState.set(toId,endNode);endNode.g2=0;var f2=f1;endNode.f2=f2;open2Set.push(endNode);var cameFrom;while(open2Set.length&&open1Set.length){if(open1Set.length<open2Set.length){forwardSearch()}else{reverseSearch()}if(quitFast&&minNode)break}var path=reconstructPath(minNode);return path;function forwardSearch(){cameFrom=open1Set.pop();if(cameFrom.closed){return}cameFrom.closed=true;if(cameFrom.f1<lMin&&cameFrom.g1+f2-heuristic(from,cameFrom.node)<lMin){graph.forEachLinkedNode(cameFrom.node.id,forwardVisitor)}if(open1Set.length>0){f1=open1Set.peek().f1}}function reverseSearch(){cameFrom=open2Set.pop();if(cameFrom.closed){return}cameFrom.closed=true;if(cameFrom.f2<lMin&&cameFrom.g2+f1-heuristic(cameFrom.node,to)<lMin){graph.forEachLinkedNode(cameFrom.node.id,reverseVisitor)}if(open2Set.length>0){f2=open2Set.peek().f2}}function visitN1(otherNode,link){var otherSearchState=nodeState.get(otherNode.id);if(!otherSearchState){otherSearchState=pool.createNewState(otherNode);nodeState.set(otherNode.id,otherSearchState)}if(otherSearchState.closed)return;var tentativeDistance=cameFrom.g1+distance(cameFrom.node,otherNode,link);if(tentativeDistance<otherSearchState.g1){otherSearchState.g1=tentativeDistance;otherSearchState.f1=tentativeDistance+heuristic(otherSearchState.node,to);otherSearchState.p1=cameFrom;if(otherSearchState.h1<0){open1Set.push(otherSearchState)}else{open1Set.updateItem(otherSearchState.h1)}}var potentialMin=otherSearchState.g1+otherSearchState.g2;if(potentialMin<lMin){lMin=potentialMin;minNode=otherSearchState}}function visitN2(otherNode,link){var otherSearchState=nodeState.get(otherNode.id);if(!otherSearchState){otherSearchState=pool.createNewState(otherNode);nodeState.set(otherNode.id,otherSearchState)}if(otherSearchState.closed)return;var tentativeDistance=cameFrom.g2+distance(cameFrom.node,otherNode,link);if(tentativeDistance<otherSearchState.g2){otherSearchState.g2=tentativeDistance;otherSearchState.f2=tentativeDistance+heuristic(from,otherSearchState.node);otherSearchState.p2=cameFrom;if(otherSearchState.h2<0){open2Set.push(otherSearchState)}else{open2Set.updateItem(otherSearchState.h2)}}var potentialMin=otherSearchState.g1+otherSearchState.g2;if(potentialMin<lMin){lMin=potentialMin;minNode=otherSearchState}}function visitN2Oriented(otherNode,link){if(link.toId===cameFrom.node.id)return visitN2(otherNode,link)}function visitN1Oriented(otherNode,link){if(link.fromId===cameFrom.node.id)return visitN1(otherNode,link)}}}function reconstructPath(searchState){if(!searchState)return NO_PATH;var path=[searchState.node];var parent=searchState.p1;while(parent){path.push(parent.node);parent=parent.p1}var child=searchState.p2;while(child){path.unshift(child.node);child=child.p2}return path}},{"../NodeHeap":1,"../defaultSettings.js":4,"../heuristics":5,"./makeNBASearchStatePool.js":8}],8:[function(require,module,exports){module.exports=makeNBASearchStatePool;function NBASearchState(node){this.node=node;this.p1=null;this.p2=null;this.closed=false;this.g1=Number.POSITIVE_INFINITY;this.g2=Number.POSITIVE_INFINITY;this.f1=Number.POSITIVE_INFINITY;this.f2=Number.POSITIVE_INFINITY;this.h1=-1;this.h2=-1}function makeNBASearchStatePool(){var currentInCache=0;var nodeCache=[];return{createNewState:createNewState,reset:reset};function reset(){currentInCache=0}function createNewState(node){var cached=nodeCache[currentInCache];if(cached){cached.node=node;cached.p1=null;cached.p2=null;cached.closed=false;cached.g1=Number.POSITIVE_INFINITY;cached.g2=Number.POSITIVE_INFINITY;cached.f1=Number.POSITIVE_INFINITY;cached.f2=Number.POSITIVE_INFINITY;cached.h1=-1;cached.h2=-1}else{cached=new NBASearchState(node);nodeCache[currentInCache]=cached}currentInCache++;return cached}}},{}],9:[function(require,module,exports){module.exports={aStar:require("./a-star/a-star.js"),aGreedy:require("./a-star/a-greedy-star"),nba:require("./a-star/nba/index.js")}},{"./a-star/a-greedy-star":2,"./a-star/a-star.js":3,"./a-star/nba/index.js":7}]},{},[9])(9)});