public override bool Find(DirectionGraph graph, Node start, Node end)
    {
        priorityQueue = new FastPriorityQueue <Node>(graph.VerticeCount());
        searchSteps   = new Queue <Node>();

        System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
        watch.Start();

        costForStartToTarget        = new Dictionary <Node, int>();
        costForStartToTarget[start] = 0;

        priorityQueue.Enqueue(start, 0);
        while (priorityQueue.Count > 0)
        {
            Node min = priorityQueue.Dequeue();
            if (min == end)
            {
                watch.Stop();
                executionTimes = watch.Elapsed.TotalMilliseconds;
                CalculateCostAndGeneratePath(start, end, graph);
                return(true);
            }
            List <DirectionEdge> minNeighbors = graph.GetNeighborEdge(min);
            int costForStartToMin             = costForStartToTarget[min];
            for (int i = 0, count = minNeighbors.Count; i < count; i++)
            {
                Node neighbor = minNeighbors[i].to;
                int  costForStartToNeighbor = int.MaxValue;
                if (costForStartToTarget.ContainsKey(neighbor))
                {
                    costForStartToNeighbor = costForStartToTarget[neighbor];
                }
                int newCostSoFar = costForStartToMin + minNeighbors[i].weight;
                if (newCostSoFar < costForStartToNeighbor)
                {
                    costForStartToTarget[neighbor] = newCostSoFar;
                    nodesComeFrom[neighbor]        = min;
                    if (priorityQueue.Contains(neighbor))
                    {
                        priorityQueue.UpdatePriority(neighbor, newCostSoFar);
                    }
                    else
                    {
                        priorityQueue.Enqueue(neighbor, newCostSoFar);
                    }
                }
            }
            if (min != start)
            {
                searchSteps.Enqueue(min);
            }
        }
        watch.Stop();
        executionTimes = watch.Elapsed.TotalMilliseconds;
        searchSteps.Clear();
        return(false);
    }
Example #2
0
 void Start()
 {
     graph = new DirectionGraph(mapView.tiles, mapView.nativeNodes);
     mainUI.onSelectedTile = OnSelectedTile;
     path = new Queue <Node>();
     mapView.onUpdateStartEndPos = delegate(Point start, Point end){
         mainUI.UpdateCoordLable(mapView.startPos, mapView.endPos);
     };
     mainUI.UpdateCoordLable(mapView.startPos, mapView.endPos);
 }
    protected void CalculateCostAndGeneratePath(Node start, Node end, DirectionGraph graph)
    {
        Stack <Node> stack = new Stack <Node>();
        Node         to    = end;

        while (to != start)
        {
            stack.Push(to);
            Node from = nodesComeFrom[to];
            totalCost += graph.GetEdge(from, to).weight;
            to         = from;
        }
        stack.Push(start);
        while (stack.Count > 0)
        {
            searchedPath.Enqueue(stack.Pop());
        }
    }
Example #4
0
    public override bool Find(DirectionGraph graph, Node start, Node end)
    {
        priorityQueue = new FastPriorityQueue <Node>(graph.VerticeCount());
        searchSteps   = new Queue <Node>();

        System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
        watch.Start();

        priorityQueue.Enqueue(start, 0);
        while (priorityQueue.Count > 0)
        {
            Node min = priorityQueue.Dequeue();
            if (min == end)
            {
                watch.Stop();
                executionTimes = watch.Elapsed.TotalMilliseconds;
                CalculateCostAndGeneratePath(start, end, graph);
                return(true);
            }
            List <DirectionEdge> minNeighbors = graph.GetNeighborEdge(min);
            for (int i = 0, count = minNeighbors.Count; i < count; i++)
            {
                Node neighbor = minNeighbors[i].to;
                if (neighbor.visited)
                {
                    continue;
                }
                neighbor.visited = true;
                int newCostSoFar = Heuristic(neighbor, end);//Mathf.Abs(neighbor.coord_x - end.coord_x) + Mathf.Abs(neighbor.coord_y - end.coord_y);// costForStartToMin + minNeighbors[i].weight;
                nodesComeFrom[neighbor] = min;
                priorityQueue.Enqueue(neighbor, newCostSoFar);
            }
            if (min != start)
            {
                searchSteps.Enqueue(min);
            }
        }
        watch.Stop();
        executionTimes = watch.Elapsed.TotalMilliseconds;
        searchSteps.Clear();
        return(false);
    }
 public override bool Find(DirectionGraph graph, Node start, Node end)
 {
     stack = new Stack <Node>(graph.VerticeCount());
     System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
     watch.Start();
     start.visited = true;
     stack.Push(start);
     while (stack.Count > 0)
     {
         Node min = stack.Pop();
         if (min == end)
         {
             watch.Stop();
             executionTimes = watch.Elapsed.TotalMilliseconds;
             CalculateCostAndGeneratePath(start, end, graph);
             return(true);
         }
         List <DirectionEdge> minNeighbors = graph.GetNeighborEdge(min);
         for (int i = 0, count = minNeighbors.Count; i < count; i++)
         {
             Node neighbor = minNeighbors[i].to;
             if (neighbor.visited)
             {
                 continue;
             }
             stack.Push(neighbor);
             neighbor.visited        = true;
             nodesComeFrom[neighbor] = min;
         }
         if (min != start)
         {
             searchSteps.Enqueue(min);
         }
     }
     watch.Stop();
     executionTimes = watch.Elapsed.TotalMilliseconds;
     searchSteps.Clear();
     return(false);
 }
 public virtual bool Find(DirectionGraph graph, Node start, Node end)
 {
     return(false);
 }