Program.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Diagnostics;
  2. using AntColony.Algorithm;
  3. namespace AntColony;
  4. class Program
  5. {
  6. /// <summary></summary>
  7. /// <param name="file">Путь до файла с ребрами</param>
  8. /// <param name="hasHeader">Файл содержит заголовок</param>
  9. /// <param name="iterations">Количество итераций</param>
  10. /// <param name="ants">Количество муравьев</param>
  11. /// <param name="alpha">Значение α (определяет вес феромонов)</param>
  12. /// <param name="beta">Значение β (определяет вес расстояния)</param>
  13. /// <param name="evaporationRate">Скорость испарения феромонов</param>
  14. /// <param name="q">Значение Q</param>
  15. public static void Main(FileInfo file, bool hasHeader = false, int iterations = 100, int ants = 20, double alpha = 1, double beta = 2, double evaporationRate = 0.1, double q = 100)
  16. {
  17. Console.Write("Чтение файла...");
  18. var sw = new Stopwatch();
  19. sw.Start();
  20. double[][] distances = ParseEdgeFile(file.FullName, hasHeader);
  21. Console.WriteLine($"\rСчитано {distances.Length} вершин за {sw.Elapsed} ({sw.ElapsedMilliseconds} мс).\n");
  22. var aco = new AntColonyOptimizer(
  23. distances: distances,
  24. numberOfAnts: ants,
  25. alpha: alpha,
  26. beta: beta,
  27. evaporationRate: evaporationRate,
  28. Q: q
  29. );
  30. sw.Restart();
  31. var (bestTour, bestDistance) = aco.Solve(iterations);
  32. Console.WriteLine($"Задача решена за {sw.Elapsed} ({sw.ElapsedMilliseconds} мс).");
  33. if (bestTour.Count == 0)
  34. {
  35. Console.WriteLine("Гамильтонов цикл не найден.");
  36. return;
  37. }
  38. Console.WriteLine($"Лучший путь: {string.Join(" -> ", bestTour)}.");
  39. Console.WriteLine($"Итоговое расстояние: {bestDistance}.");
  40. }
  41. private static double[][] ParseEdgeFile(string filePath, bool skipHeader)
  42. {
  43. List<string> lines = [.. File.ReadAllLines(filePath)];
  44. if (skipHeader) lines.RemoveAt(0);
  45. HashSet<int> vertices = [];
  46. int maxVertex = 0;
  47. foreach (string line in lines)
  48. {
  49. string[] parts = line.Split();
  50. if (parts.Length != 3)
  51. continue;
  52. if (int.TryParse(parts[0], out int v1))
  53. vertices.Add(v1);
  54. if (int.TryParse(parts[1], out int v2))
  55. vertices.Add(v2);
  56. maxVertex = int.Max(maxVertex, v1);
  57. maxVertex = int.Max(maxVertex, v2);
  58. }
  59. double[][] adjacencyMatrix = new double[maxVertex + 1][];
  60. for (int i = 0; i < maxVertex + 1; i++)
  61. {
  62. adjacencyMatrix[i] = new double[maxVertex + 1];
  63. for (int j = 0; j < maxVertex + 1; j++)
  64. {
  65. adjacencyMatrix[i][j] = 0.0;
  66. }
  67. }
  68. foreach (string line in lines)
  69. {
  70. string[] parts = line.Split();
  71. if (parts.Length != 3)
  72. continue;
  73. if (int.TryParse(parts[0], out int v1) &&
  74. int.TryParse(parts[1], out int v2) &&
  75. double.TryParse(parts[2], out double distance))
  76. {
  77. adjacencyMatrix[v1][v2] = distance;
  78. adjacencyMatrix[v2][v1] = distance;
  79. }
  80. }
  81. return adjacencyMatrix;
  82. }
  83. }