Beispiel #1
0
    void GameManagerInterface.Start(GameTuple startAndEndPoint)
    {
        recreateGraphRepresentation();

        base.InitializeRun(startAndEndPoint);

        RouterScript currentRouter = activeRouter.GetComponent <RouterScript>();

        currentRouter.SetPriority(0);

        neighboursOfActiveRouter = ExpandNode(currentRouter);
        prioQueue = new PriorityQueue <RouterScript>();

        for (int i = 0; i < neighboursOfActiveRouter.Count; i++)
        {
            PathScript pathToNeighbor = graphRepresentation2[currentRouter.GetRouterIndex(), neighboursOfActiveRouter[i].GetRouterIndex()];
            neighboursOfActiveRouter[i].SetPriority(pathToNeighbor.GetPathCosts());

            prioQueue.Enqueue(neighboursOfActiveRouter[i]);

            if (isLogEnabled)
            {
                Debug.Log(string.Format("Added router {0} to prio queue with path costs {1}.",
                                        neighboursOfActiveRouter[i],
                                        pathToNeighbor.GetPathCosts()));
            }
        }
    }
Beispiel #2
0
    /// <summary>
    /// Starts the Dijkstra algorithm. Performs the required initialization and fills the priority queue.
    /// Sets the player's position to the starting router.
    /// </summary>
    /// <param name="startingRouter">The starting router of the algorithm.</param>
    public void StartDijkstraAlgorithm(RouterScript startingRouter)
    {
        if (isLogEnabled)
        {
            Debug.Log("StartDijkstraAlgorithm() called.");
        }

        // Create the priority queue.
        priorityQueue = new PriorityQueue <RouterScript>();

        // Initialize Dijkstra algorithm by storing nodes in priority queue.
        for (int i = 0; i < listOfRouterScripts.Length; i++)
        {
            RouterScript router = listOfRouterScripts[i];
            if (router == startingRouter)
            {
                if (isLogEnabled)
                {
                    Debug.Log("router instance id: " + router.GetInstanceID() + " and starting router instance id " + startingRouter.GetInstanceID());
                }

                // Set the current shortest path of the starting router to 0.
                router.SetPriority(0);
                predecessorRouter[router] = router; // The predecessor of the starting router is the router itself.

                // Set this router as the current player position and as the current working router.
                routerScriptCurrentPlayerPosition = router;
                currentWorkingRouter = router;
            }
            else
            {
                // For all other routers, set the current distance to the max int value.
                Debug.Log("Router=" + router + " i=" + i + " list of routers size=" + listOfRouterScripts.Length);
                router.SetPriority(int.MaxValue);

                // Add the router to the priority queue.
                priorityQueue.Enqueue(router);
            }
        }

        if (isLogEnabled)
        {
            Debug.Log("Finished StartDijkstraAlgorithm().");
        }
    }
Beispiel #3
0
    void GameManagerInterface.PerformHop(PathScript path)
    {
        GameObject hopTarget = null;

        //to=active --> goto from
        if (path.to.gameObject == activeRouter.gameObject)
        {
            hopTarget = path.from.gameObject;
        }
        else           //from=active --> goto to
        if (path.from.gameObject == activeRouter.gameObject)
        {
            hopTarget = path.to.gameObject;
        }

        // Update the active router and the neighbours of the active router.
        activeRouter = hopTarget;
        currentPath.Add(hopTarget.GetComponent <RouterScript>());
        neighboursOfActiveRouter = ExpandNode(activeRouter.GetComponent <RouterScript>());


        // Remove the hop target from the priortiy queue.
        RemoveRouterFromPrioQueue(hopTarget.GetComponent <RouterScript>());

        for (int i = 0; i < neighboursOfActiveRouter.Count; i++)
        {
            RouterScript neighborRouter = neighboursOfActiveRouter[i].GetComponent <RouterScript>();

            // Path costs to the target router are the path cost to the currently active router + the path costs of this path.
            int pathCost = activeRouter.GetComponent <RouterScript>().GetPriority() +
                           graphRepresentation2[
                activeRouter.GetComponent <RouterScript>().GetRouterIndex(),
                neighborRouter.GetRouterIndex()
                           ].GetPathCosts();

            if (currentPath.Contains(neighborRouter))
            {
                continue;
            }

            if (prioQueue.IsContained(neighborRouter))
            {
                if (pathCost < neighborRouter.GetPriority())
                {
                    // Update the path costs of the router.
                    prioQueue.DecreasePriority(neighborRouter, pathCost);

                    if (isLogEnabled)
                    {
                        Debug.Log(string.Format("Updated path costs of router {0}, new path costs are {1}.",
                                                neighborRouter.GetRouterName(), neighborRouter.GetPriority()));
                    }
                }
            }
            else
            {
                neighborRouter.SetPriority(pathCost);

                if (isLogEnabled)
                {
                    Debug.Log(string.Format("Inserted neighbor into prio queue. Neighbor is {0}, path costs are {1}.",
                                            neighborRouter.GetRouterName(), pathCost));
                }

                // Insert neighbor into priority queue.
                prioQueue.Enqueue(neighborRouter);
            }
        }

        if (isLogEnabled)
        {
            Debug.Log("Prio Queue after hop: " + prioQueue.ToString());
        }
    }
Beispiel #4
0
    /// <summary>
    /// Finds the optimal between the source and the destination router using the metric hop count.
    /// </summary>
    /// <returns>The optimal path as a list.</returns>
    /// <param name="source">Source.</param>
    /// <param name="destination">Destination.</param>
    protected List <GameObject> findOptimalHopCountPath(GameObject source, GameObject destination)
    {
        if (isLogEnabled)
        {
            Debug.Log("Finding best path!");
        }

        prioQueue = new PriorityQueue <RouterScript>();
        RouterScript current = source.GetComponent <RouterScript>();

        current.SetPriority(0);

        Dictionary <RouterScript, RouterScript> parentRelationship = new Dictionary <RouterScript, RouterScript>();

        List <RouterScript> closed   = new List <RouterScript>();
        List <RouterScript> children = new List <RouterScript>();

        if (isLogEnabled)
        {
            Debug.Log("Starting findOptimalHopCountPath from " + current.name + " to " + destination.name);
        }

        while (current.gameObject != destination)
        {
            if (!closed.Contains(current)) // Don't need to check this node again.
            {
                children = ExpandNode(current);
                foreach (RouterScript tmp in children)
                {
                    // Check whether the router has already been handled. If this is the case,
                    // we don't need to add them to the priority queue again.
                    if (closed.Contains(tmp))
                    {
                        continue;
                    }

                    // Increase the priority. One more hop.
                    tmp.SetPriority(current.GetPriority() + 1);

                    // Store parent (predecessor router of current router) for child.
                    // Only do this if no router has already been defined as the predecessor router.
                    if (parentRelationship.ContainsKey(tmp))
                    {
                        // do nothing.
                        if (isLogEnabled)
                        {
                            Debug.Log("There is already a predecessor for the router " + tmp.name + "defined.");
                        }
                    }
                    else
                    {
                        parentRelationship.Add(tmp, current);

                        if (isLogEnabled)
                        {
                            Debug.Log("Setting predecessor for router: " + tmp.name + ", the predecessor is: " + current.name);
                        }
                    }

                    if (isLogEnabled)
                    {
                        Debug.Log("Adding to prioQueue: router: " + tmp.name + " | with priority: " + tmp.GetPriority());
                    }

                    prioQueue.Enqueue(tmp);
                }

                // Mark router as handled.
                closed.Add(current);
            }

            if (isLogEnabled)
            {
                Debug.Log("Handling of router: " + current.name + " is done. Taking the next one.");
            }

            // Continue with node that has the lowest priority at that time.
            current = prioQueue.PullHighest();
        }


        // Calculate optimal path.
        List <GameObject> optimalPath = new List <GameObject>();

        optimalPath.Insert(0, current.gameObject);

        while (parentRelationship[current].gameObject != source)
        {
            current = parentRelationship[current];
            optimalPath.Insert(0, current.gameObject);
        }
        optimalPath.Insert(0, source);

        if (isLogEnabled)
        {
            string logString = "Optimal Path: ";
            foreach (GameObject go in optimalPath)
            {
                logString += go.GetComponent <RouterScript>().GetRouterName() + " -> ";
            }
            Debug.Log(logString.Substring(0, logString.Length - 3));
        }

        return(optimalPath);
    }