Ejemplo n.º 1
0
    public AStarUtils(int numCols, int numRows, bool isFourWay = false)
    {
        Debug.Log("AStarUtils: " + numCols + ", " + numRows);
        this.numCols         = numCols;
        this.numRows         = numRows;
        this.isFourWay       = isFourWay;
        this.iAStarHeuristic = new AStarManhattanHeuristic();
        //this.iAStarHeuristic = new AStarDiagonalHeuristic ();

        AStarNode node = null;

        this.nodes = new Dictionary <string, AStarNode>();
        for (int i = 0; i < this.numCols; i++)
        {
            for (int j = 0; j < this.numRows; j++)
            {
                node = new AStarNode(i, j);
                node.AddHeuristic(this.RefreshLinksOfAdjacentNodes, node);
                this.nodes.Add(this.GetNodeKey(i, j), node);
            }
        }
        this.RefreshLinksOfAllNodes();
        //numCols * numRows / 2 只需要判断节点,不需要叶节点
        this.binaryHeapUtils = new BinaryHeapUtils(numCols * numRows / 2);
    }
Ejemplo n.º 2
0
    public AStarUtils(byte[] bytes, bool isFourWay = false)
    {
        ByteArray.StartRead(bytes);
        this.numCols = ByteArray.ReadInt();
        this.numRows = ByteArray.ReadInt();

        this.isFourWay       = isFourWay;
        this.iAStarHeuristic = new AStarManhattanHeuristic();

        AStarNode node = null;

        this.nodes = new Dictionary <int, AStarNode> ();
        int i, j;

        for (i = 0; i < this.numCols; i++)
        {
            for (j = 0; j < this.numRows; j++)
            {
                node          = new AStarNode(i, j);
                node.walkable = ByteArray.ReadByte() == 0 ? true : false;
                node.AddHeuristic(this.RefreshLinksOfAdjacentNodes, node);
                this.nodes.Add(this.GetNodeKey(i, j), node);
            }
        }
        this.RefreshLinksOfAllNodes();
        this.binaryHeapUtils = new BinaryHeapUtils(numCols * numRows / 2);
    }
Ejemplo n.º 3
0
    public AStarUtils(int numCols, int numRows, bool isFourWay = false)
    {
        this.numCols         = numCols;
        this.numRows         = numRows;
        this.isFourWay       = isFourWay;
        this.iAStarHeuristic = new AStarManhattanHeuristic();

        AStarNode node = null;

        this.nodes = new Dictionary <int, AStarNode> ();
        int i, j;

        for (i = 0; i < this.numCols; i++)
        {
            for (j = 0; j < this.numRows; j++)
            {
                node = new AStarNode(i, j);
                node.AddHeuristic(this.RefreshLinksOfAdjacentNodes, node);
                this.nodes.Add(this.GetNodeKey(i, j), node);
            }
        }
        long startTime = GlobalTime.currentTimeMillis;

        this.RefreshLinksOfAllNodes();
        this.binaryHeapUtils = new BinaryHeapUtils(numCols * numRows / 2);
        Debug.Log(GlobalTime.currentTimeMillis - startTime);
    }
Ejemplo n.º 4
0
 public AStarSearch(IWeightedGraph <L, C> graph, IAStarHeuristic <L, C> heuristic)
 {
     defaultGraph     = graph;
     defaultHeuristic = heuristic;
     frontier         = new PriorityQueue <L, C>();
     cameFrom         = new Dictionary <L, L>();
     costSoFar        = new Dictionary <L, C>();
 }
Ejemplo n.º 5
0
        public AStarSearch(TNode start, TNode goal, IAStarHeuristic <TNode> heuristicImpl)
        {
            this.start         = start;
            this.goal          = goal;
            this.heuristicImpl = heuristicImpl;

            InitializeSets();
        }
Ejemplo n.º 6
0
    public L[] Search(L start, L goal, IWeightedGraph <L, C> graph, IAStarHeuristic <L, C> heuristic)
    {
        List <L> result;

        frontier.Clear();
        cameFrom.Clear();
        costSoFar.Clear();

        frontier.Enqueue(start, default(C));
        cameFrom.Add(start, start);
        costSoFar.Add(start, default(C));

        while (frontier.Count() > 0)
        {
            var current = frontier.Dequeue();

            if (current.Equals(goal))
            {
                break;
            }

            var enumerator = graph.Neighbors(current).GetEnumerator();
            L   next;

            while (enumerator.MoveNext())
            {
                next = enumerator.Current;

                C newCost = heuristic.Sum(costSoFar[current], graph.Cost(current, next));

                if (!costSoFar.ContainsKey(next) || newCost.CompareTo(costSoFar[next]) < 0)
                {
                    costSoFar[next] = newCost;
                    C priority = heuristic.Sum(newCost, heuristic.Evaluate(next, goal));
                    frontier.Enqueue(next, priority);
                    cameFrom[next] = current;
                }
            }
        }

        result = new List <L>();
        L    currentLocation = goal;
        bool pathNotFound    = false;

        while (!pathNotFound && !currentLocation.Equals(start))
        {
            result.Insert(0, currentLocation);

            if (!cameFrom.TryGetValue(currentLocation, out currentLocation))
            {
                pathNotFound = true;
                result.Clear();
            }
        }

        return(result.ToArray());
    }
Ejemplo n.º 7
0
 public AStarPathfinder(ITileMap map, int maxSearchDistance, bool allowDiagMovement, IAStarHeuristic heuristic)
 {
     m_Closed            = new List <AStarNode>();
     m_Open              = new OrderedList <AStarNode>();
     m_Heuristic         = heuristic;
     m_Map               = map;
     m_MaxSearchDistance = maxSearchDistance;
     m_AllowDiagMovement = allowDiagMovement;
     m_Nodes             = new AStarMatrix(map.Width);
 }
Ejemplo n.º 8
0
 public AStarPathfinder(ITileMap map, int maxSearchDistance, bool allowDiagMovement, IAStarHeuristic heuristic)
 {
     m_Closed = new List<AStarNode>();
       m_Open = new OrderedList<AStarNode>();
       m_Heuristic = heuristic;
       m_Map = map;
       m_MaxSearchDistance = maxSearchDistance;
       m_AllowDiagMovement = allowDiagMovement;
       m_Nodes = new AStarMatrix(map.Width);
 }
Ejemplo n.º 9
0
 public void CompleteInsert()
 {
     if (iAStarHeuristic == null)
     {
         iAStarHeuristic = new AStarManhattanHeuristic();
     }
     this.RefreshLinksOfAllNodes();
     if (binaryHeapUtils == null)
     {
         binaryHeapUtils = new BinaryHeapUtils(nodes.Count / 2);
     }
     else
     {
         binaryHeapUtils.Reset();
     }
 }
Ejemplo n.º 10
0
    public AStarAlgorithm(INavigationGraphProvider2D graphProvider, IAStarHeuristic heuristic)
    {
        if (graphProvider == null)
        {
            Debug.LogError("A new instance of an AStarAlgorithm needs to initialized with a graph-provider!");
        }
        this.graphProvider = graphProvider;

        if (heuristic == null)
        {
            this.heuristic = new EuclidianHeuristic();
        }
        else
        {
            this.heuristic = heuristic;
        }
    }
        /// <summary>
        /// Create a path finder
        /// </summary>
        /// <param name="map">The map to be searched</param>
        /// <param name="maxSearchDistance">The maximum depth we'll search before giving up</param>
        /// <param name="allowDiagMovement">True if the search should try diaganol movement</param>
        /// <param name="heuristic">The heuristic used to determine the search order of the map</param>
        public AStarPathFinder(ITileBasedMap map, int maxSearchDistance, bool allowDiagMovement, IAStarHeuristic heuristic)
        {
            Unit = GameMap.Soldier;

            _map = map;
            _maxSearchDistance = maxSearchDistance;
            _allowDiagMovement = allowDiagMovement;
            _heuristic         = heuristic;

            _nodes = new Node[_map.GetWidthInTiles(), _map.GetHeightInTiles()];

            for (var i = 0; i < _map.GetWidthInTiles(); i++)
            {
                for (var j = 0; j < _map.GetHeightInTiles(); j++)
                {
                    _nodes[i, j] = new Node(i, j);
                }
            }
        }
Ejemplo n.º 12
0
    public AStarUtils(int numCols, int numRows, bool isFourWay = false)
    {
        _numCols         = numCols;
        _numRows         = numRows;
        _isFourWay       = isFourWay;
        _iAStarHeuristic = new AStarManhattanHeuristic();
        //_iAStarHeuristic = new AStarDiagonalHeuristic ();

        _nodes = new Dictionary <string, AStarNode>();
        for (var i = 0; i < _numCols; i++)
        {
            for (var j = 0; j < _numRows; j++)
            {
                var node = new AStarNode(i, j);
                node.AddHeuristic(RefreshLinksOfAdjacentNodes, node);
                _nodes.Add(GetNodeKey(i, j), node);
            }
        }
        RefreshLinksOfAllNodes();
        _binaryHeapUtils = new BinaryHeapUtils(numCols * numRows / 2);
    }
Ejemplo n.º 13
0
	public AStarUtils(byte[] bytes,bool isFourWay = false)
	{
		ByteArray.StartRead(bytes);
		this.numCols = ByteArray.ReadInt ();
		this.numRows = ByteArray.ReadInt ();

		this.isFourWay = isFourWay;
		this.iAStarHeuristic = new AStarManhattanHeuristic ();

		AStarNode node = null;
		this.nodes = new Dictionary<int, AStarNode> ();
		int i, j;
		for(i = 0; i < this.numCols; i++)
		{
			for(j = 0; j < this.numRows; j++)
			{
				node = new AStarNode(i, j);
				node.walkable =  ByteArray.ReadByte() == 0 ? true : false;
				node.AddHeuristic(this.RefreshLinksOfAdjacentNodes, node);
				this.nodes.Add(this.GetNodeKey(i, j), node);
			}
		}
		this.RefreshLinksOfAllNodes();
		this.binaryHeapUtils = new BinaryHeapUtils(numCols * numRows / 2);
	}
Ejemplo n.º 14
0
 public DepthFirst(IAStarHeuristic <TNode> heuristicImpl)
 {
     this.heuristicImpl = heuristicImpl;
     visitedVertexes    = new List <TNode>();
     parentToNode       = new Dictionary <TNode, TNode>();
 }
Ejemplo n.º 15
0
 public DepthFirst(IAStarHeuristic <TNode> heuristicImpl)
 {
     this.heuristicImpl = heuristicImpl;
 }
Ejemplo n.º 16
0
 private void Initialize(GraphNode destination)
 {
     this._openList = new LinkedList<GraphNode>();
     this._closedList = new LinkedList<GraphNode>();
     this._destination = destination;
     this._heuristic = new EuclideanDistanceHeuristic(destination);
 }
Ejemplo n.º 17
0
 public AStarShortestPath(IShortestPathOperators <W> @operator, IAStarHeuristic <T, W> heuristic)
 {
     this.@operator = @operator;
     this.heuristic = heuristic;
 }
Ejemplo n.º 18
0
	public AStarUtils(int numCols, int numRows, bool isFourWay = false)
	{
		this.numCols = numCols;
		this.numRows = numRows;
		this.isFourWay = isFourWay;
		this.iAStarHeuristic = new AStarManhattanHeuristic ();
		//this.iAStarHeuristic = new AStarDiagonalHeuristic ();
		
		AStarNode node = null;
		this.nodes = new Dictionary<string, AStarNode> ();
		for(int i = 0; i < this.numCols; i++)
		{
			for(int j = 0; j < this.numRows; j++)
			{
				node = new AStarNode(i, j);
				node.AddHeuristic(this.RefreshLinksOfAdjacentNodes, node);
				this.nodes.Add(this.GetNodeKey(i, j), node);
			}
		}
		this.RefreshLinksOfAllNodes();
		this.binaryHeapUtils = new BinaryHeapUtils(numCols * numRows / 2);
	}
Ejemplo n.º 19
0
 public AStarShortestPath(IShortestPathOperators <W> operators, IAStarHeuristic <T, W> heuristic)
 {
     this.operators = operators;
     this.heuristic = heuristic;
 }
Ejemplo n.º 20
0
	public AStarUtils(int numCols, int numRows, bool isFourWay = false)
	{
		this.numCols = numCols;
		this.numRows = numRows;
		this.isFourWay = isFourWay;
		this.iAStarHeuristic = new AStarManhattanHeuristic ();

		AStarNode node = null;
		this.nodes = new Dictionary<int, AStarNode> ();
		int i, j;
		for(i = 0; i < this.numCols; i++)
		{
			for(j = 0; j < this.numRows; j++)
			{
				node = new AStarNode(i, j);
				node.AddHeuristic(this.RefreshLinksOfAdjacentNodes, node);
				this.nodes.Add(this.GetNodeKey(i, j), node);
			}
		}
		long startTime = GlobalTime.currentTimeMillis;
		this.RefreshLinksOfAllNodes();
		this.binaryHeapUtils = new BinaryHeapUtils(numCols * numRows / 2);
		Debug.Log (GlobalTime.currentTimeMillis-startTime);
	}
Ejemplo n.º 21
0
 public AStarSearch(IGraph <V, int> graph, IVertex <V> source, IVertex <V> target, IAStarHeuristic <V> heuristic)
     : base(graph, source)
 {
     this.heuristic = heuristic;
     this.target    = target;
 }
Ejemplo n.º 22
0
 internal AStarWrap(IAStarHeuristic <T, W> heuristic, T destinationVertex)
 {
     this.heuristic         = heuristic;
     this.destinationVertex = destinationVertex;
 }
Ejemplo n.º 23
0
 public AStar(IAStarHeuristic <TNode> heuristicImpl)
 {
     this.heuristicImpl = heuristicImpl;
 }