/// <summary> /// Displays the minimum tour information. /// </summary> /// <param name="points">The given points.</param> /// <param name="tour">The indices of a minimum-length tour, in order, in the linklist.</param> /// <param name="tourLen">The length of a shortest tour.</param> private void DisplayResults(Point[] points, LinkedListCell <int> cell, double tourLen) { uxTourPoints.Items.Add(points[cell.Data]); LinkedListCell <int> temp = cell; while (temp.Next != null) { uxPanel.DrawLine(points[temp.Data], points[temp.Next.Data]); uxTourPoints.Items.Add(points[temp.Next.Data]); temp = temp.Next; } uxPanel.DrawLine(points[temp.Data], points[cell.Data]); uxTourPoints.Items.Add(points[cell.Data]); MessageBox.Show("Tour length: " + tourLen); }
/// <summary> /// Finds the minimum-length tour. /// </summary> /// <param name="curPoint">What point the function is currently at</param> /// <param name="pathLentoCur">Current path length for the current path</param> /// <param name="visited">Array to tell if the point has been visited before</param> /// <param name="distances">2d Array that shows the distance between all points</param> /// <param name="pointsInCurPath">Amount of points in the current path</param> /// <param name="minTour">The minimum tour length as found by the algorithim</param> /// <param name="bestTourFinish">Linkedlist cell linking to the optimal path</param> /// <returns></returns> private double ComputeMinumumLengthTour(int curPoint, double pathLentoCur, bool[] visited, double[,] distances, int pointsInCurPath, double minTour, out LinkedListCell <int> bestTourFinish) { bestTourFinish = null; LinkedListCell <int> currentTour = new LinkedListCell <int>(); if (pointsInCurPath == visited.Length) { pathLentoCur += distances[0, curPoint]; LinkedListCell <int> tempcell = new LinkedListCell <int>(); tempcell.Data = curPoint; bestTourFinish = tempcell; return(pathLentoCur); } if (minTour < pathLentoCur) { bestTourFinish = null; return(Double.PositiveInfinity); } else { for (int i = 0; i < visited.Length; i++) { if (visited[i] == false) { visited[i] = true; LinkedListCell <int> outLinked; double temp = ComputeMinumumLengthTour(i, pathLentoCur + distances[curPoint, i], visited, distances, pointsInCurPath + 1, minTour, out outLinked); visited[i] = false; if (temp < minTour) { minTour = temp; currentTour = outLinked; } } } } LinkedListCell <int> tempCell = new LinkedListCell <int>(); tempCell.Data = curPoint; tempCell.Next = currentTour; bestTourFinish = tempCell; return(minTour); }
/// <summary> /// Displays the minimum tour information. /// </summary> /// <param name="points">Array of points entered by the user</param> /// <param name="tour">Linked list of the optimal tour for the points</param> /// <param name="tourLen">How long the optimal tour is</param> private void DisplayResults(Point[] points, LinkedListCell <int> tour, double tourLen) { int first = tour.Data; uxTourPoints.Items.Add(points[first]); while (tour.Next != null) { uxPanel.DrawLine(points[tour.Data], points[tour.Next.Data]); if (tour.Data != 0) { uxTourPoints.Items.Add(points[tour.Data]); } tour = tour.Next; } uxPanel.DrawLine(points[tour.Data], points[first]); uxTourPoints.Items.Add(points[tour.Data]); uxTourPoints.Items.Add(points[first]); MessageBox.Show("Tour length: " + tourLen); }
/// <summary> /// Handles a Find Tour event. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxFindTour_Click(object sender, EventArgs e) { Point[] points = uxPanel.Points; uxTourPoints.Items.Clear(); uxPanel.ClearLines(); if (points.Length < 2) { MessageBox.Show("You must plot at least 2 points."); return; } double[,] distances = GetDistances(points); int[] tour = new int[points.Length]; int n = points.Length; bool[] visited = new bool[n]; visited[0] = true; LinkedListCell <int> cell = new LinkedListCell <int>(); double minTour = OptimalLength(0, 0, visited, 1, distances, Double.PositiveInfinity, out cell); DisplayResults(points, cell, minTour); }
/// <summary> /// Finds the minimum-length tour. /// </summary> /// <param name="curPoint">index of current point in current tour</param> /// <param name="pathLenToCur">distance traveled from start to current point along the current path</param> /// <param name="visited">marks which points have been visited in the current path visited</param> /// <param name="pointsInCurPath">distances between all pairs of points</param> /// <param name="distances">how many points are already in the current path</param> /// <param name="minTour">shortest tour distance found so far</param> /// <param name="bestTourFinish">stores the best tour in the LinkList</param> /// <returns></returns> private double OptimalLength(int curPoint, double pathLenToCur, bool[] visited, int pointsInCurPath, double[,] distances, double minTour, out LinkedListCell <int> bestTourFinish) { if (minTour <= pathLenToCur) { bestTourFinish = null; return(pathLenToCur); } if (pointsInCurPath == visited.Length) { bestTourFinish = new LinkedListCell <int>(); bestTourFinish.Data = curPoint; return(pathLenToCur + distances[curPoint, 0]); } LinkedListCell <int> temp = new LinkedListCell <int>(); for (int i = 0; i < visited.Length; i++) { if (visited[i] == false) { visited[i] = true; double curr = OptimalLength(i, (pathLenToCur + distances[i, curPoint]), visited, pointsInCurPath + 1, distances, minTour, out bestTourFinish); if (curr < minTour) { minTour = curr; temp = bestTourFinish; } visited[i] = false; } } LinkedListCell <int> cell = new LinkedListCell <int>(); cell.Data = curPoint; cell.Next = temp; bestTourFinish = cell; return(minTour); }