/// <summary> /// Check a neighbour of the current node to see if it needs to be added to the tentative list /// or the shortest route to it needs to be updated. /// </summary> /// <param name="neighbour">The neighbour to check.</param> /// <param name="current">The current node.</param> /// <param name="dist">The distance from the root to the current node.</param> /// <param name="link">The link between current and neighbour.</param> private void checkNeighbour(DijkstraNode neighbour, DijkstraNode current, float dist, IMLink link) { float newDistance = dist + link.Weight; try { //If the new route from root to neighbour is shorter than the old route or there was no old route //set the current node as the first step towards the root from neighbour if (_cont && !_tentative.Contains(neighbour)) { _tentative.AddFirst(neighbour); neighbour.SetDistanceFromRoot(ID, newDistance, current, link); } else if (_cont && newDistance <= neighbour.GetDistanceFromRoot(ID)) { neighbour.SetDistanceFromRoot(ID, newDistance, current, link); } } catch (KeyNotFoundException e) { //If the AddLink being checked is no Keyer in the list of links the algorithm needs to stop _cont = false; if (_visualise) { VisualisedNode = null; } Logger.Debug("Trying to work with a neighbour (" + neighbour.Name + ") which is not actually connected to " + current.Name + " (root = " + Name + ")"); } //Incase another thread has modified the distance if (_cont && newDistance < neighbour.GetDistanceFromRoot(ID)) { neighbour.SetDistanceFromRoot(ID, newDistance, neighbour, link); } }
/// <summary> /// Visualises the process of checking a neighbour node and prints out text explaining what is happening. /// </summary> /// <param name="neighbour">The neighbour to check.</param> /// <param name="current">The current node.</param> /// <param name="dist">The distance from the root to the current node.</param> /// <param name="link">The link between current and neighbour.</param> private void VisualiseCheckNeighbourText(DijkstraNode neighbour, DijkstraNode current, float dist, IMLink link) { if (!_visualise && !_text) return; float newDistance = dist + link.Weight; if (_cont) { //if (_cont && _text) // current.Say("Examining neighbour " + neighbour.Name); Pause(4f); link.Colour = Color.Blue; if (_cont && _text) { Pause(); current.Say("5. Test cost to " + neighbour.Name + " = " + dist + " + " + link.Weight + " = " + newDistance); Pause(); } Pause(4f); } try { if (_cont && !_tentative.Contains(neighbour)) { if (_cont && _text) current.Say(neighbour.Name + " is not in tentative list"); _tentative.AddFirst(neighbour); neighbour.SetDistanceFromRoot(ID, newDistance, current, link); if (_cont) { Pause(4f); neighbour.Selected = tentativeGlow; if (_cont && _text) { current.Say("6a. Adding " + neighbour.Name + " to the tentative list"); Pause(); current.Say("6b. " + neighbour.Name + ".cost = test cost (" + newDistance + ")"); } } } //If the new route from root to neighbour is shorter than the old route or there was no old route //set the current node as the first step towards the root from neighbour else if (_cont && newDistance <= neighbour.GetDistanceFromRoot(ID)) { neighbour.SetDistanceFromRoot(ID, newDistance, current, link); if (_cont && _text) { current.Say(neighbour.Name + ".cost (" + neighbour.GetDistanceFromRoot(ID) + ") > test cost (" + newDistance + ")"); Pause(); current.Say("7. " + neighbour.Name + ".cost = test cost (" + newDistance + ")"); Pause(); } } } catch (KeyNotFoundException e) { //If the AddLink being checked is no Keyer in the list of links the algorithm needs to stop _cont = false; if (_visualise) VisualisedNode = null; Logger.Debug("Trying to work with a neighbour (" + neighbour.Name + ") which is not actually connected to " + current.Name + " (root = " + Name + ")"); } if (_cont && newDistance < neighbour.GetDistanceFromRoot(ID)) neighbour.SetDistanceFromRoot(ID, newDistance, neighbour, link); if (_cont) { Pause(4f); link.Colour = Color.White; Pause(6f); } }
/// <summary> /// Check a neighbour of the current node to see if it needs to be added to the tentative list /// or the shortest route to it needs to be updated. /// </summary> /// <param name="neighbour">The neighbour to check.</param> /// <param name="current">The current node.</param> /// <param name="dist">The distance from the root to the current node.</param> /// <param name="link">The link between current and neighbour.</param> private void checkNeighbour(DijkstraNode neighbour, DijkstraNode current, float dist, IMLink link) { float newDistance = dist + link.Weight; try { //If the new route from root to neighbour is shorter than the old route or there was no old route //set the current node as the first step towards the root from neighbour if (_cont && !_tentative.Contains(neighbour)) { _tentative.AddFirst(neighbour); neighbour.SetDistanceFromRoot(ID, newDistance, current, link); } else if (_cont && newDistance <= neighbour.GetDistanceFromRoot(ID)) neighbour.SetDistanceFromRoot(ID, newDistance, current, link); } catch (KeyNotFoundException e) { //If the AddLink being checked is no Keyer in the list of links the algorithm needs to stop _cont = false; if (_visualise) VisualisedNode = null; Logger.Debug("Trying to work with a neighbour (" + neighbour.Name + ") which is not actually connected to " + current.Name + " (root = " + Name + ")"); } //Incase another thread has modified the distance if (_cont && newDistance < neighbour.GetDistanceFromRoot(ID)) neighbour.SetDistanceFromRoot(ID, newDistance, neighbour, link); }
/// <summary> /// Visualises the process of checking a neighbour node and prints out text explaining what is happening. /// </summary> /// <param name="neighbour">The neighbour to check.</param> /// <param name="current">The current node.</param> /// <param name="dist">The distance from the root to the current node.</param> /// <param name="link">The link between current and neighbour.</param> private void VisualiseCheckNeighbourText(DijkstraNode neighbour, DijkstraNode current, float dist, IMLink link) { if (!_visualise && !_text) { return; } float newDistance = dist + link.Weight; if (_cont) { //if (_cont && _text) // current.Say("Examining neighbour " + neighbour.Name); Pause(4f); link.Colour = Color.Blue; if (_cont && _text) { Pause(); current.Say("5. Test cost to " + neighbour.Name + " = " + dist + " + " + link.Weight + " = " + newDistance); Pause(); } Pause(4f); } try { if (_cont && !_tentative.Contains(neighbour)) { if (_cont && _text) { current.Say(neighbour.Name + " is not in tentative list"); } _tentative.AddFirst(neighbour); neighbour.SetDistanceFromRoot(ID, newDistance, current, link); if (_cont) { Pause(4f); neighbour.Selected = tentativeGlow; if (_cont && _text) { current.Say("6a. Adding " + neighbour.Name + " to the tentative list"); Pause(); current.Say("6b. " + neighbour.Name + ".cost = test cost (" + newDistance + ")"); } } } //If the new route from root to neighbour is shorter than the old route or there was no old route //set the current node as the first step towards the root from neighbour else if (_cont && newDistance <= neighbour.GetDistanceFromRoot(ID)) { neighbour.SetDistanceFromRoot(ID, newDistance, current, link); if (_cont && _text) { current.Say(neighbour.Name + ".cost (" + neighbour.GetDistanceFromRoot(ID) + ") > test cost (" + newDistance + ")"); Pause(); current.Say("7. " + neighbour.Name + ".cost = test cost (" + newDistance + ")"); Pause(); } } } catch (KeyNotFoundException e) { //If the AddLink being checked is no Keyer in the list of links the algorithm needs to stop _cont = false; if (_visualise) { VisualisedNode = null; } Logger.Debug("Trying to work with a neighbour (" + neighbour.Name + ") which is not actually connected to " + current.Name + " (root = " + Name + ")"); } if (_cont && newDistance < neighbour.GetDistanceFromRoot(ID)) { neighbour.SetDistanceFromRoot(ID, newDistance, neighbour, link); } if (_cont) { Pause(4f); link.Colour = Color.White; Pause(6f); } }