/// <summary> /// Create entries for the routers that have been handeld already by the dijkstra algorithm /// in the current run. The entries are concatenated to a string. /// </summary> /// <param name="routers">The list of all routers that are involved in the current dijkstra run.</param> /// <returns>A string containing the routing table entries of the already handled routers.</returns> private string createHandledRouterRoutingTableEntries(RouterScript[] routers) { System.Text.StringBuilder s = new System.Text.StringBuilder(); for (int i = 0; i < routers.Length; i++) { // Check whether the router is already handled. if (dijkstraManager.IsRouterAlreadyHandled(routers[i])) { string currentRouter = string.Empty; // Request predecessor. RouterScript predecessorRouter = dijkstraManager.GetPredecessorRouterOnShortestPath(routers[i]); if (predecessorRouter != null) { // Add entry to the routing table. currentRouter = string.Format("Tisch {0}: ({1},{2})" + System.Environment.NewLine, routers[i].GetRouterName(), routers[i].GetPriority(), predecessorRouter.GetRouterName()); } // Is the router the current working router? if (routers[i] == dijkstraManager.GetCurrentWorkingRouter().GetComponent <RouterScript>()) { currentRouter = colorizeString(currentRouter, currentWorkingRouterColor); } s.Append(currentRouter); } } string output = s.ToString(); output = colorizeString(output, handledRouterColor); if (isDebugging) { Debug.Log(" In handled routers: " + output); } return(output); }
/// <summary> /// Create entries for the routers that have not been handeld by the dijkstra algorithm so far /// in the current run. The entries are concatenated to a string. /// </summary> /// <param name="routers">The list of all routers that are involved in the current dijkstra run.</param> /// <returns>A string containing the routing table entries of the not yet handled routers.</returns> private string createUnhandledRouterRoutingTableEntries(RouterScript[] routers) { System.Text.StringBuilder s = new System.Text.StringBuilder(); for (int i = 0; i < routers.Length; i++) { // Check whether the router is already handled. if (!dijkstraManager.IsRouterAlreadyHandled(routers[i])) { // Request predecessor. RouterScript predecessorRouter = dijkstraManager.GetPredecessorRouterOnShortestPath(routers[i]); if (predecessorRouter != null) { // Add entry to the routing table. s.Append(string.Format("Tisch {0}: ({1},{2})" + System.Environment.NewLine, routers[i].GetRouterName(), routers[i].GetPriority(), predecessorRouter.GetRouterName())); } else { // Add entry to the routing table without predecessor. s.Append(string.Format("Tisch {0}: (inf,-)" + System.Environment.NewLine, routers[i].GetRouterName())); } } } string output = s.ToString(); output = colorizeString(output, unhandledRouterColor); if (isDebugging) { Debug.Log(" In unhandled routers: " + output.Substring(1, output.Length - 1)); } return(output); }
/// <summary> /// Searches all routers in the priority queue which currently have the shortest distance and /// are a suitable candidate for the next working router in the Dijkstra algorithm. /// </summary> /// <returns>A string which contains the names of the candidate routers.</returns> public string GetNextRouterSuggestionAsString() { if (priorityQueue.Count() == 0) { return(string.Empty); } System.Text.StringBuilder sb = new System.Text.StringBuilder(); RouterScript headOfQueue = priorityQueue.PullHighest(); sb.Append(headOfQueue.GetRouterName() + ", "); // Check if the next element in the priority queue also has the same distance. if (headOfQueue.GetCurrentDistance() == priorityQueue.Peek().GetCurrentDistance()) { // Retrieve all routers that have the same current distance. List <RouterScript> routerCache = new List <RouterScript>(); while (headOfQueue.GetCurrentDistance() == priorityQueue.Peek().GetCurrentDistance()) { sb.Append(priorityQueue.Peek().GetRouterName() + ", "); routerCache.Add(priorityQueue.PullHighest()); } // Put the routers back in. foreach (RouterScript router in routerCache) { priorityQueue.Enqueue(router); } } // Remove the last comma. sb.Remove(sb.Length - 2, 2); // Put head of priority queue back. priorityQueue.Enqueue(headOfQueue); return(sb.ToString()); }
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()); } }