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())); } } }
/// <summary> /// Performs the path discovery on the specified path. The path costs of this path /// are determined and it is checked whether the target router can be reached with /// less path costs using this path. If the target router can be reached with less /// path costs via this path, the distance (priority) of the router is adjusted and /// the previous router on this path stored in the predecessorRouter data structure. /// Finally, the path is set to discovered. /// </summary> /// <param name="path">The path for which the path discovery is performed.</param> public void PerformPathDiscovery(PathScript path) { RouterScript from = path.from.GetComponent <RouterScript>(); RouterScript to = path.to.GetComponent <RouterScript>(); RouterScript previousRouter = null; RouterScript discoveredRouter = null; // Check which router will be discovered. if (from == routerScriptCurrentPlayerPosition) { // Player discovers 'to' router. discoveredRouter = to; previousRouter = routerScriptCurrentPlayerPosition; } else if (bidirectional && to == routerScriptCurrentPlayerPosition) { // Player discovers 'from' router. discoveredRouter = from; previousRouter = routerScriptCurrentPlayerPosition; } // Update the current distance if we have found a shorter path to the discovered router. // This only needs to be done when the discovered router has not already been handled by the dijkstra algorithm. if (priorityQueue.IsContained(discoveredRouter)) { // Path costs to the discovered router are the path cost to the previous router + the path costs of this path. int pathCost = previousRouter.GetPriority() + graphRepresentation2[previousRouter.GetRouterIndex(), discoveredRouter.GetRouterIndex()].GetPathCosts(); // Are the new path costs lower than the currently stored lowest path costs. if (pathCost < discoveredRouter.GetPriority()) { // Update the path costs of the router. priorityQueue.DecreasePriority(discoveredRouter, pathCost); // Set the new predecessor for this router. predecessorRouter[discoveredRouter] = previousRouter; } } // Set path to discovered. graphRepresentation2[previousRouter.GetRouterIndex(), discoveredRouter.GetRouterIndex()].SetDiscovered(true); //graphRepresentation2[previousRouter.GetRouterIndex(), discoveredRouter.GetRouterIndex()].DisplayPathCosts(); // If there is a path back with the same costs, discover this path as well. PathScript backPath = graphRepresentation2[discoveredRouter.GetRouterIndex(), previousRouter.GetRouterIndex()]; if (backPath != null && backPath.GetPathCosts() == path.GetPathCosts()) { backPath.SetDiscovered(true); //backPath.DisplayPathCosts(); } // Test: Output the current priority queue. // Debug.Log(priorityQueue.ToString()); }