public double Run() { Point toPoint, fromPoint; SetStartingPoint(); for (int i = 0; i < Graph.Dimensions; i++) { fromPoint = GetCurrentNode(); if (UnvisitedNodes.Count > 0) // if we have nodes to visit; { toPoint = ChooseNextPoint(fromPoint); VisitedNodes.Add(toPoint); UnvisitedNodes.Remove(toPoint); } else { toPoint = VisitedNodes[0]; // if visited every node, just go back to start } Edge edge = Graph.GetEdge(fromPoint.Id, toPoint.Id); Path.Add(edge); TourDistance += edge.Length; } return(Math.Round(TourDistance)); }
private void SetStartingPoint() { Point startPoint = Graph.Points.First(); // We can set any point as starting point. I choosed first one. VisitedNodes.Add(startPoint); UnvisitedNodes.Remove(startPoint); }
public Edge Move() { Point endPoint; var startPoint = CurrentNode(); if (UnvisitedNodes.Count == 0) { endPoint = VisitedNodes[0]; // if ant visited every node, just go back to start } else { endPoint = ChooseNextPoint(); VisitedNodes.Add(endPoint); UnvisitedNodes.RemoveAt(UnvisitedNodes.FindIndex(x => x.Id == endPoint.Id)); } var edge = Graph.GetEdge(startPoint.Id, endPoint.Id); Path.Add(edge); Distance += edge.Length; return(edge); }
private void UpdateNeighboorAStar(Node Current, Node Neighboor) { //Check if neighboor has been visited or is an obstacle if (Neighboor.IsVisited || Neighboor.IsObstacle) { return; } Label label; Point P; bool BetterScore; double DistanceToStart, DistnaceToEnd, GScore; //Calculate new score BetterScore = false; DistanceToStart = Current.DistanceToStart + Current.GetDistanceBetween(Neighboor); DistnaceToEnd = (Neighboor.GetDistanceBetween(EndNode)) * (1 - Math.Pow(10, -12)); GScore = DistanceToStart + DistnaceToEnd; //May update old score if (algorithm == Algorithm.AStart && Neighboor.Score > GScore) { BetterScore = true; Neighboor.DistanceToStart = DistanceToStart; Neighboor.EstimatedDistanceToEnd = DistnaceToEnd; Neighboor.Score = GScore; Neighboor.PriorNode = Current; if (ShowAlgorithmScore) { P = new Point(Neighboor.X, Neighboor.Y); PlotToCanvas(ref P); label = Labels[(int)Neighboor.X, (int)Neighboor.Y]; //CanvasPath.Children.Remove(label); label.Content = Math.Round(GScore * 100) / 100; if (IsDelay) { Wait(label, 0); } Canvas.SetLeft(label, P.X + 0.5 * (dx - label.ActualWidth)); Canvas.SetTop(label, P.Y - dy + 0.5 * (dy - label.ActualHeight)); //CanvasPath.Children.Add(label); } } else if (algorithm == Algorithm.Dijkstra && Neighboor.DistanceToStart > DistanceToStart) { BetterScore = true; Neighboor.DistanceToStart = DistanceToStart; Neighboor.PriorNode = Current; if (ShowAlgorithmScore) { P = new Point(Neighboor.X, Neighboor.Y); PlotToCanvas(ref P); label = Labels[(int)Neighboor.X, (int)Neighboor.Y]; label.Content = Math.Round(DistanceToStart * 100) / 100; if (IsDelay) { Wait(label, 0); } Canvas.SetLeft(label, P.X + 0.5 * (dx - label.ActualWidth)); Canvas.SetTop(label, P.Y - dy + 0.5 * (dy - label.ActualHeight)); } } //Check if neighboor has been found yet if (!Neighboor.IsFound) { Neighboor.IsFound = true; if (algorithm == Algorithm.AStart) { UnvisitedNodes.Enqueue(Neighboor, (float)Neighboor.Score); } else if (algorithm == Algorithm.Dijkstra) { UnvisitedNodes.Enqueue(Neighboor, (float)Neighboor.DistanceToStart); } //Mark node in canvas MarkNodeInCanvas(Neighboor); } //Update priority if new score is lower else if (Neighboor.IsFound && BetterScore) { if (algorithm == Algorithm.AStart) { UnvisitedNodes.UpdatePriority(Neighboor, (float)Neighboor.Score); } else if (algorithm == Algorithm.Dijkstra) { UnvisitedNodes.UpdatePriority(Neighboor, (float)Neighboor.DistanceToStart); } } }