Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using MathMod;
  2. using System.Globalization;
  3. internal class Program
  4. {
  5. public static List<LinearProblem> ConverToLinearProblemList(List<string> lines)
  6. {
  7. int step = 1;//1-L(x), 2-type, 3-restrictions, comparison, free
  8. List<LinearProblem> linear_problems = new List<LinearProblem>();
  9. List<List<double>> a = new List<List<double>> { };
  10. List<double> b = new List<double> { };
  11. List<string> comparison = new List<string> { };
  12. List<double> c = new List<double> { };
  13. LinearProblem.problemType type = LinearProblem.problemType.max;
  14. for (int i = 0; i < lines.Count; ++i)
  15. {
  16. if (lines[i].Trim() == string.Empty)
  17. {
  18. linear_problems.Add(new LinearProblem(a.Select(x => x.ToList()).ToList(), comparison.Select(x => x).ToList(), b.Select(x => x).ToList(), c.Select(x => x).ToList(), type));
  19. step = 1;
  20. }
  21. else
  22. {
  23. if (step == 1)
  24. {
  25. c.Clear();
  26. c = new List<double> { };
  27. c = lines[i].Split().Select(x => double.Parse(x, NumberStyles.AllowLeadingSign)).ToList();
  28. //c = lines[i].Split().Select(double.Parse).ToList();
  29. step = 2;
  30. }
  31. else if (step == 2)
  32. {
  33. if (lines[i].Trim().ToLower() == "max") type = LinearProblem.problemType.max;
  34. else if (lines[i].Trim().ToLower() == "min") type = LinearProblem.problemType.min;
  35. step = 3;
  36. a.Clear(); b.Clear(); comparison.Clear();
  37. a = new List<List<double>> { };
  38. b = new List<double> { };
  39. comparison = new List<string> { };
  40. }
  41. else if (step == 3)
  42. {
  43. a.Add(new List<double>());
  44. List<string> temp_a_comparsion_b = lines[i].Split().ToList();
  45. int line_count = temp_a_comparsion_b.Count;
  46. for (int j = 0; j < line_count - 2; ++j)
  47. a[a.Count - 1].Add(Convert.ToDouble(temp_a_comparsion_b[j]));
  48. comparison.Add(temp_a_comparsion_b[line_count - 2]);
  49. b.Add(Convert.ToDouble(temp_a_comparsion_b[line_count - 1]));
  50. }
  51. }
  52. }
  53. return linear_problems;
  54. }
  55. public static void PrintMatrix<T>(List<List<T>> matrix)
  56. {
  57. for (int i = 0; i < matrix.Count; ++i)
  58. {
  59. for (int j = 0; j < matrix[0].Count; ++j)
  60. Console.Write(matrix[i][j] + " ");
  61. Console.WriteLine();
  62. }
  63. }
  64. public static List<T> Init1DList<T>(int size, T fill)
  65. {
  66. List<T> list = new List<T>();
  67. for (int i = 0; i < size; ++i)
  68. list.Add(fill);
  69. return list;
  70. }
  71. public static List<T> Init1DList<T>(List<T> orig)
  72. {
  73. List<T> copy = new List<T>();
  74. for (int i = 0; i < orig.Count; ++i)
  75. copy.Add(orig[i]);
  76. return copy;
  77. }
  78. public static List<(T, D)> Init1DList<T, D>(List<(T, D)> orig)
  79. {
  80. List<(T, D)> copy = new List<(T, D)>();
  81. for (int i = 0; i < orig.Count; ++i)
  82. copy.Add((orig[i].Item1, orig[i].Item2));
  83. return copy;
  84. }
  85. public static List<List<T>> Init2DList<T>(int size0, int size1, T fill)
  86. {
  87. List<List<T>> list = new List<List<T>>();
  88. for (int i = 0; i < size0; ++i)
  89. list.Add(Init1DList(size1, fill));
  90. return list;
  91. }
  92. public static List<List<T>> Init2DList<T>(List<List<T>> orig)
  93. {
  94. List<List<T>> copy = new List<List<T>>();
  95. for (int i = 0; i < orig.Count; ++i)
  96. copy.Add(Init1DList(orig[i]));
  97. return copy;
  98. }
  99. public const double inf = double.PositiveInfinity;
  100. private static void Main(string[] args)
  101. {
  102. //ПОТЕНЦИАЛЫ
  103. //Console.WriteLine("-----Задача№1-----------------------------------------------------------------");
  104. //List<int> a1 = new List<int> { 90, 400, 110 };
  105. //List<int> b1 = new List<int> { 140, 300, 160 };
  106. //List<List<int>> c1 = new List<List<int>> { };
  107. //c1.Add(new List<int> { 2, 5, 2 });
  108. //c1.Add(new List<int> { 4, 1, 5 });
  109. //c1.Add(new List<int> { 3, 6, 8 });
  110. //TransportProblem transport_problem1 = new TransportProblem(a1, b1, c1);
  111. //transport_problem1.MethodOfMinElement();
  112. //transport_problem1.PrintReferencePlan();
  113. //bool is_optimal = transport_problem1.AssessOptimality();
  114. //if (is_optimal)
  115. //{
  116. // Console.WriteLine("L(x) = " + transport_problem1.GetObjectiveFunction());
  117. // Console.WriteLine("Матрица оптимального решения:");
  118. // transport_problem1.PrintReferencePlan();
  119. //}
  120. //Console.WriteLine("\n-----Задача№2. Вырожденный опорный план----------------------------------------");
  121. //List<int> a2 = new List<int> { 6000, 3000, 4000 };
  122. //List<int> b2 = new List<int> { 4000, 5000, 1000, 3000 };
  123. //List<List<int>> c2 = new List<List<int>> { };
  124. //c2.Add(new List<int> { 6, 4, 9, 8 });
  125. //c2.Add(new List<int> { 5, 3, 2, 8 });
  126. //c2.Add(new List<int> { 2, 3, 6, 8 });
  127. //TransportProblem transport_problem2 = new TransportProblem(a2, b2, c2);
  128. //transport_problem2.MethodOfMinElement();
  129. //transport_problem2.PrintReferencePlan();
  130. //is_optimal = transport_problem2.AssessOptimality();
  131. //if (is_optimal)
  132. //{
  133. // Console.WriteLine("L(x) = " + transport_problem2.GetObjectiveFunction());
  134. // Console.WriteLine("Матрица оптимального решения:");
  135. // transport_problem2.PrintReferencePlan();
  136. //}
  137. //Console.WriteLine("\n-----Задача№3. Открытая задача------------------------------------------------");
  138. //List<int> a3 = new List<int> { 40, 40, 60 };
  139. //List<int> b3 = new List<int> { 27, 25, 30, 35 };
  140. //List<List<int>> c3 = new List<List<int>> { };
  141. //c3.Add(new List<int> { 70, 85, 55, 120 });
  142. //c3.Add(new List<int> { 110, 90, 75, 110 });
  143. //c3.Add(new List<int> { 115, 115, 70, 90, 120 });
  144. //TransportProblem transport_problem3 = new TransportProblem(a3, b3, c3);
  145. //transport_problem3.MethodOfVogelApproximation();
  146. //transport_problem3.PrintReferencePlan();
  147. //is_optimal = transport_problem3.AssessOptimality();
  148. //if (is_optimal)
  149. //{
  150. // Console.WriteLine("L(x) = " + transport_problem3.GetObjectiveFunction());
  151. // Console.WriteLine("Матрица оптимального решения:");
  152. // transport_problem3.PrintReferencePlan();
  153. //}
  154. //СИМПЛЕКС
  155. //const string input = "symplex_data.txt";
  156. //List<string> lines = File.ReadAllLines(input).ToList();
  157. //List<LinearProblem> linear_problems = new List<LinearProblem>();
  158. //linear_problems = ConverToLinearProblemList(lines);
  159. //for (int i = 0; i < linear_problems.Count; ++i)
  160. //{
  161. // linear_problems[i].SimplexMethod();
  162. // Console.WriteLine($"{i}:\n{linear_problems[i].GetObjectiveFun()}");
  163. //}
  164. //АЛГОРИТ ЛИТТЛА
  165. //List<List<double>> m0 = new List<List<double>> {
  166. //new List<double>{inf, 20, 18, 12, 8},
  167. //new List<double>{5, inf, 14, 7, 11},
  168. //new List<double>{12, 18, inf, 6, 11},
  169. //new List<double>{11, 17, 11, inf, 12},
  170. //new List<double>{5, 5, 5, 5, inf}
  171. //};
  172. //TravelingSalesmanProblem p0 = new TravelingSalesmanProblem(m0);
  173. //p0.Solve();
  174. //List<(int, int)> edges0 = p0.GetPath();
  175. //double res0 = p0.GetResult();
  176. //Console.WriteLine($"Ответ: {res0}");
  177. //foreach ((int a, int b) in edges0)
  178. // Console.Write($"({a}, {b}) \n");
  179. //List<List<double>> m1 = new List<List<double>> {
  180. //new List<double>{inf, 4,5,7,5},
  181. //new List<double>{8, inf, 5,6,6},
  182. //new List<double>{3,5, inf, 9,6},
  183. //new List<double>{3,5,6, inf, 2},
  184. //new List<double>{6,2,3,8, inf}
  185. //};
  186. //TravelingSalesmanProblem p1 = new TravelingSalesmanProblem(m1);
  187. //p1.Solve();
  188. //List<(int, int)> edges1 = p1.GetPath();
  189. //double res1 = p1.GetResult();
  190. //Console.WriteLine($"Ответ: {res1}");
  191. //foreach ((int a, int b) in edges1)
  192. // Console.Write($"({a}, {b})\n");
  193. //АЛГОРИТМ ДЕЙКСТРЫ
  194. List<List<double>> m2 = new List<List<double>> {
  195. new List<double>{inf,7,9,inf,inf,14},
  196. new List<double>{7,inf,10,15,inf,inf},
  197. new List<double>{9,10,inf,11,inf,2},
  198. new List<double>{inf,15,11,inf,6,inf},
  199. new List<double>{inf,inf,inf,6,inf,9},
  200. new List<double>{14,inf,2,inf,9,inf}
  201. };
  202. int from = 0;
  203. DijkstrasAlgorithm d0 = new DijkstrasAlgorithm(m2);
  204. (Dictionary<int, double> res, Dictionary<int, List<int>> paths) = d0.Solve(from);
  205. foreach ((int vertex, double value) in res)
  206. {
  207. Console.Write($"{from + 1}->{vertex + 1}: {value}.\t");
  208. foreach (int passesVertex in paths[vertex])
  209. {
  210. Console.Write($"{passesVertex + 1} ");
  211. }
  212. Console.WriteLine();
  213. }
  214. Console.WriteLine();
  215. List<List<double>> m3 = new List<List<double>>
  216. {
  217. new List<double>{inf,20,25,30,inf,inf,inf,inf,inf,inf},//a
  218. new List<double>{20,inf,inf,5,inf,10,inf,inf,inf,inf},
  219. new List<double>{25,inf,inf,inf,inf,10,15,inf,inf,inf},
  220. new List<double>{30,5,inf,inf, 25,inf,inf,inf,inf,inf},//г
  221. new List<double>{inf,inf,inf,25, inf, 15,inf,5,10,20},
  222. new List<double>{inf,10,10,inf, 15, inf,10,inf,inf,inf },
  223. new List<double>{inf,inf,15,inf, inf, 10,inf,15,inf,inf },//ж
  224. new List<double>{inf,inf,inf,inf, 5, inf,15,inf,inf,10 },
  225. new List<double>{inf,inf,inf,inf, 10, inf,inf,inf,inf,10 },
  226. new List<double>{inf,inf,inf,inf, 20, inf,inf,10,10,inf }//к
  227. };
  228. from = 0;
  229. DijkstrasAlgorithm d1 = new DijkstrasAlgorithm(m3);
  230. (res,paths) = d1.Solve(from);
  231. foreach ((int vertex, double value) in res)
  232. {
  233. Console.Write($"{Convert.ToChar('А'+from)}->{Convert.ToChar('А' + vertex)}: {value}.\t");
  234. foreach (int passesVertex in paths[vertex])
  235. {
  236. Console.Write($"{Convert.ToChar('А' + passesVertex)} ");
  237. }
  238. Console.WriteLine();
  239. }
  240. }
  241. }