/// <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()); }
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> /// Equivalent to neighborPaths in the GreedyManager, returns all children/neighbors of this router. /// </summary> /// <returns>The neighbors/children of the current working router</returns> /// <param name="current">Current Working router</param> public List <RouterScript> ExpandNode(RouterScript current) { List <RouterScript> neighbors = new List <RouterScript>(); List <PathScript> neighborPaths = new List <PathScript>(); int currentRouterIndex = current.GetRouterIndex(); //Extract paths from graph representation. for (int i = 0; i < graphRepresentation2.GetLength(1); i++) { if (graphRepresentation2[currentRouterIndex, i] != null) { neighborPaths.Add(graphRepresentation2[currentRouterIndex, i]); } } foreach (PathScript p in neighborPaths) { RouterScript from = p.from.GetComponent <RouterScript>(); RouterScript to = p.to.GetComponent <RouterScript>(); if (current == from) { neighbors.Add(to); } } return(neighbors); }
/// <summary> /// Gets the path between the provided nodes. /// </summary> /// <returns>The path between the nodes.</returns> /// <param name="from">From.</param> /// <param name="to">To.</param> public PathScript GetPathBetweenNodes(RouterScript from, RouterScript to) { PathScript path = graphRepresentation2[ from.GetRouterIndex(), to.GetRouterIndex() ]; if (path == null) { path = graphRepresentation2[ to.GetRouterIndex(), from.GetRouterIndex() ]; } return(path); }
/// <summary> /// Checks whether the current working router is completely handled, i.e. all paths from /// or to this router have been discovered by the player. /// </summary> /// <returns>Returns true, if all paths from or to the current working router are discovered. Returns false, if there /// are still undiscovered paths.</returns> public bool IsCurrentWorkingRouterHandledCompletely() { bool handledCompletely = true; for (int i = 0; i < graphRepresentation2.GetLength(1); i++) { PathScript path = graphRepresentation2[routerScriptCurrentPlayerPosition.GetRouterIndex(), i]; // extract a path from or to the current working router. if (path != null) { if (!path.IsDiscovered()) { handledCompletely = false; // There is still an undiscovered path, so the working router isn't handled completely so far. } } } return(handledCompletely); }
// Use this for initialization. void Start() { if (isLogEnabled) { Debug.Log("Start initializing graph datastructure."); } // Fill graph representation array with information taken from the nodes and edges for (int i = 0; i < listOfPathScripts.Length; i++) { RouterScript from = listOfPathScripts[i].from.GetComponent <RouterScript>(); RouterScript to = listOfPathScripts[i].to.GetComponent <RouterScript>(); graphRepresentation2[from.GetRouterIndex(), to.GetRouterIndex()] = listOfPathScripts[i]; } if (isLogEnabled) { Debug.Log("Finished initializing graph datastructure."); } }
/// <summary> /// Recreates the graph representation. Takes blocked routers into account. /// </summary> public void recreateGraphRepresentation() { graphRepresentation2 = new PathScript[listOfNodes.Length, listOfNodes.Length]; // Nodex x Nodes // Fill graph representation array with information taken from the nodes and edges. // Take blocked routers into the account. for (int i = 0; i < listOfPathScripts.Length; i++) { RouterScript from = listOfPathScripts[i].from.GetComponent <RouterScript>(); RouterScript to = listOfPathScripts[i].to.GetComponent <RouterScript>(); if (!from.Disabled && !to.Disabled) { // Include only active paths. graphRepresentation2[from.GetRouterIndex(), to.GetRouterIndex()] = listOfPathScripts[i]; } else { listOfPathScripts[i].Disabled = true; } } }
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()); } }
protected void Initialize() { if (isLogEnabled) { Debug.Log("Awake the GameManager."); } // Extract scripts from nodes and edges listOfNodes = GameObject.FindGameObjectsWithTag("Node"); listOfEdges = GameObject.FindGameObjectsWithTag("Path"); // Initialize graph datastructure. //graphRepresentation = new int[listOfNodes.Length,listOfNodes.Length]; // Nodes x Nodes graphRepresentation2 = new PathScript[listOfNodes.Length, listOfNodes.Length]; // Nodex x Nodes // Keep track of the predecessor router for each router on the shortest path. //predecessorRouter = new Dictionary<RouterScript, RouterScript> (); if (isLogEnabled) { Debug.Log("Start extracting scripts in GameManager."); } // listOfRouterScripts = new RouterScript[listOfNodes.Length]; // listOfPathScripts = new PathScript[listOfEdges.Length]; // Extract scripts from nodes and edges listOfNodes = GameObject.FindGameObjectsWithTag("Node"); listOfEdges = GameObject.FindGameObjectsWithTag("Path"); // Initialize graph datastructure. //graphRepresentation = new int[listOfNodes.Length,listOfNodes.Length]; // Nodes x Nodes graphRepresentation2 = new PathScript[listOfNodes.Length, listOfNodes.Length]; // Nodex x Nodes listOfRouterScripts = new RouterScript[listOfNodes.Length]; listOfPathScripts = new PathScript[listOfEdges.Length]; for (int i = 0; i < listOfNodes.Length; i++) { listOfRouterScripts[i] = listOfNodes[i].GetComponent <RouterScript>(); } for (int i = 0; i < listOfEdges.Length; i++) { listOfPathScripts[i] = listOfEdges[i].GetComponent <PathScript>(); } // Check router names. checkIntegrityOfRouterNames(); if (isLogEnabled) { Debug.Log("Finished extracting scripts in GameManager."); Debug.Log("Finished Awake method of the GameManager."); } if (isLogEnabled) { Debug.Log("Start initializing graph datastructure."); } // Fill graph representation array with information taken from the nodes and edges for (int i = 0; i < listOfPathScripts.Length; i++) { RouterScript from = listOfPathScripts[i].from.GetComponent <RouterScript>(); RouterScript to = listOfPathScripts[i].to.GetComponent <RouterScript>(); graphRepresentation2[from.GetRouterIndex(), to.GetRouterIndex()] = listOfPathScripts[i]; } if (isLogEnabled) { Debug.Log("Finished initializing graph datastructure."); } }