public static int[] MakePermutations(int[] order, int position, Point[] checkpoints, ref double shortestDistance, ref int[] bestOrder) { var currentOrder = new int[position]; Array.Copy(order, currentOrder, position); var pathLength = PointExtensions.GetPathLength(checkpoints, currentOrder); if (pathLength < shortestDistance) { if (position == order.Length) { shortestDistance = pathLength; bestOrder = (int[])order.Clone(); return(order); } for (int i = 1; i < order.Length; i++) { var index = Array.IndexOf(order, i, 0, position); if (index != -1) { continue; } order[position] = i; MakePermutations(order, position + 1, checkpoints, ref shortestDistance, ref bestOrder); } } return(order); }
public static int[] FindBestCheckpointsOrder(Point[] checkpoints) { if (tempListBestOrder.Count == checkpoints.Length) { MakeTrivialAction(checkpoints); CloseMethod(); return(outListBestOrder.ToArray());//проблема здесь } if (PointExtensions.GetPathLength(checkpoints, tempListBestOrder.ToArray()) <= minPathLength) { for (int i = 1; i < checkpoints.Length; i++) { var index = Array.IndexOf(tempListBestOrder.ToArray(), i, 0, position); if (index == -1) { tempListBestOrder.Add(i); position += 1; FindBestCheckpointsOrder(checkpoints); } } } if (position == 1) { minPathLength = double.MaxValue; return(outListBestOrder.ToArray()); } CloseMethod(); return(new int[0]); }
private static void MakePermutations(Point[] checpoints, int[] orders, int[] bestOrder, int position, ref double minCost) { var currCost = PointExtensions.GetPathLength(checpoints, orders); if (position == orders.Length) { if (minCost == 0 || minCost > currCost) { minCost = currCost; orders.CopyTo(bestOrder, 0); } return; } for (int i = 1; i < orders.Length; i++) { var index = Array.IndexOf(orders, i, 1, position); if (index != -1) { continue; } orders[position] = i; var currOrders = new int[position + 1]; Array.Copy(orders, currOrders, position + 1); currCost = PointExtensions.GetPathLength(checpoints, currOrders); if (currCost > minCost && minCost != 0) { continue; } MakePermutations(checpoints, orders, bestOrder, position + 1, ref minCost); } }
static void MakePermutations(int[] permutation, int position, List <int[]> result, Point[] checkpoints, double[] minValue) { if (position == permutation.Length) { minValue[0] = PointExtensions.GetPathLength(checkpoints, permutation); result.Add(permutation.ToArray()); } else { for (int i = 1; i < permutation.Length; i++) { var index = Array.IndexOf(permutation, i, 0, position); if (index != -1) { continue; } permutation[position] = i; if (PointExtensions.GetPathLength(checkpoints, permutation.Take(position + 1).ToArray()) >= minValue[0]) { continue; } MakePermutations(permutation, position + 1, result, checkpoints, minValue); } } }
public static bool IsThisWayBetter(Point[] checkpoints, List <int> bestOrder, List <int> permutation) { if (permutation.Count < 2) { return(true); } var len1 = PointExtensions.GetPathLength(checkpoints, permutation.ToArray()); var len2 = PointExtensions.GetPathLength(checkpoints, bestOrder.ToArray()); return(len1 < len2); }
public static void MakeTrivialAction(Point[] checkpoints) { if (PointExtensions.GetPathLength(checkpoints, tempListBestOrder.ToArray()) < minPathLength) { outListBestOrder.Clear(); foreach (var e in tempListBestOrder) { outListBestOrder.Add(e); } minPathLength = PointExtensions.GetPathLength(checkpoints, tempListBestOrder.ToArray()); } }
public static int[] FindBestCheckpointsOrder(Point[] checkpoints) { minPathLength = double.MaxValue; var currentPath = new int[checkpoints.Length]; currentPath[0] = 0; var bestOrder = new List <int[]>(); MakePermutations(checkpoints, currentPath, 1, 0, bestOrder); for (int i = 0; i < bestOrder.Count; i++) { if (PointExtensions.GetPathLength(checkpoints, bestOrder[i]) == minPathLength) { currentPath = bestOrder[i]; } } return(currentPath); }
public static int[] MakePermutations(int[] order, int position, Point[] checkpoints, ref double shortestDistance, ref int[] bestOrder) { var currentOrder = new int[position]; Array.Copy(order, currentOrder, position); var pathLength = PointExtensions.GetPathLength(checkpoints, currentOrder); // J: PointExtension - класс с расширениями. GetPathLength можно вызвать у checkpoints // Подробнее: //https://docs.microsoft.com/ru-ru/dotnet/csharp/programming-guide/classes-and-structs/extension-methods if (pathLength < shortestDistance) { if (position == order.Length) { shortestDistance = pathLength; bestOrder = (int[])order.Clone(); return(order); // Метод возвращает значение, которое не используется. // Метод безболезненно может стать void. } for (int i = 1; i < order.Length; i++) { var index = Array.IndexOf(order, i, 0, position); if (index != -1) { continue; } order[position] = i; MakePermutations(order, position + 1, checkpoints, ref shortestDistance, ref bestOrder); } } return(order); // }