using MathMod; using System.Globalization; internal class Program { public static List ConverToLinearProblemList(List lines) { int step = 1;//1-L(x), 2-type, 3-restrictions, comparison, free List linear_problems = new List(); List> a = new List> { }; List b = new List { }; List comparison = new List { }; List c = new List { }; LinearProblem.problemType type = LinearProblem.problemType.max; for (int i = 0; i < lines.Count; ++i) { if (lines[i].Trim() == string.Empty) { 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)); step = 1; } else { if (step == 1) { c.Clear(); c = new List { }; c = lines[i].Split().Select(x => double.Parse(x, NumberStyles.AllowLeadingSign)).ToList(); //c = lines[i].Split().Select(double.Parse).ToList(); step = 2; } else if (step == 2) { if (lines[i].Trim().ToLower() == "max") type = LinearProblem.problemType.max; else if (lines[i].Trim().ToLower() == "min") type = LinearProblem.problemType.min; step = 3; a.Clear(); b.Clear(); comparison.Clear(); a = new List> { }; b = new List { }; comparison = new List { }; } else if (step == 3) { a.Add(new List()); List temp_a_comparsion_b = lines[i].Split().ToList(); int line_count = temp_a_comparsion_b.Count; for (int j = 0; j < line_count - 2; ++j) a[a.Count - 1].Add(Convert.ToDouble(temp_a_comparsion_b[j])); comparison.Add(temp_a_comparsion_b[line_count - 2]); b.Add(Convert.ToDouble(temp_a_comparsion_b[line_count - 1])); } } } return linear_problems; } public static void PrintMatrix(List> matrix) { for (int i = 0; i < matrix.Count; ++i) { for (int j = 0; j < matrix[0].Count; ++j) Console.Write(matrix[i][j] + " "); Console.WriteLine(); } } public static List Init1DList(int size, T fill) { List list = new List(); for (int i = 0; i < size; ++i) list.Add(fill); return list; } public static List Init1DList(List orig) { List copy = new List(); for (int i = 0; i < orig.Count; ++i) copy.Add(orig[i]); return copy; } public static List<(T, D)> Init1DList(List<(T, D)> orig) { List<(T, D)> copy = new List<(T, D)>(); for (int i = 0; i < orig.Count; ++i) copy.Add((orig[i].Item1, orig[i].Item2)); return copy; } public static List> Init2DList(int size0, int size1, T fill) { List> list = new List>(); for (int i = 0; i < size0; ++i) list.Add(Init1DList(size1, fill)); return list; } public static List> Init2DList(List> orig) { List> copy = new List>(); for (int i = 0; i < orig.Count; ++i) copy.Add(Init1DList(orig[i])); return copy; } public const double inf = double.PositiveInfinity; private static void Main(string[] args) { //ПОТЕНЦИАЛЫ //Console.WriteLine("-----Задача№1-----------------------------------------------------------------"); //List a1 = new List { 90, 400, 110 }; //List b1 = new List { 140, 300, 160 }; //List> c1 = new List> { }; //c1.Add(new List { 2, 5, 2 }); //c1.Add(new List { 4, 1, 5 }); //c1.Add(new List { 3, 6, 8 }); //TransportProblem transport_problem1 = new TransportProblem(a1, b1, c1); //transport_problem1.MethodOfMinElement(); //transport_problem1.PrintReferencePlan(); //bool is_optimal = transport_problem1.AssessOptimality(); //if (is_optimal) //{ // Console.WriteLine("L(x) = " + transport_problem1.GetObjectiveFunction()); // Console.WriteLine("Матрица оптимального решения:"); // transport_problem1.PrintReferencePlan(); //} //Console.WriteLine("\n-----Задача№2. Вырожденный опорный план----------------------------------------"); //List a2 = new List { 6000, 3000, 4000 }; //List b2 = new List { 4000, 5000, 1000, 3000 }; //List> c2 = new List> { }; //c2.Add(new List { 6, 4, 9, 8 }); //c2.Add(new List { 5, 3, 2, 8 }); //c2.Add(new List { 2, 3, 6, 8 }); //TransportProblem transport_problem2 = new TransportProblem(a2, b2, c2); //transport_problem2.MethodOfMinElement(); //transport_problem2.PrintReferencePlan(); //is_optimal = transport_problem2.AssessOptimality(); //if (is_optimal) //{ // Console.WriteLine("L(x) = " + transport_problem2.GetObjectiveFunction()); // Console.WriteLine("Матрица оптимального решения:"); // transport_problem2.PrintReferencePlan(); //} //Console.WriteLine("\n-----Задача№3. Открытая задача------------------------------------------------"); //List a3 = new List { 40, 40, 60 }; //List b3 = new List { 27, 25, 30, 35 }; //List> c3 = new List> { }; //c3.Add(new List { 70, 85, 55, 120 }); //c3.Add(new List { 110, 90, 75, 110 }); //c3.Add(new List { 115, 115, 70, 90, 120 }); //TransportProblem transport_problem3 = new TransportProblem(a3, b3, c3); //transport_problem3.MethodOfVogelApproximation(); //transport_problem3.PrintReferencePlan(); //is_optimal = transport_problem3.AssessOptimality(); //if (is_optimal) //{ // Console.WriteLine("L(x) = " + transport_problem3.GetObjectiveFunction()); // Console.WriteLine("Матрица оптимального решения:"); // transport_problem3.PrintReferencePlan(); //} //СИМПЛЕКС //const string input = "symplex_data.txt"; //List lines = File.ReadAllLines(input).ToList(); //List linear_problems = new List(); //linear_problems = ConverToLinearProblemList(lines); //for (int i = 0; i < linear_problems.Count; ++i) //{ // linear_problems[i].SimplexMethod(); // Console.WriteLine($"{i}:\n{linear_problems[i].GetObjectiveFun()}"); //} //АЛГОРИТ ЛИТТЛА //List> m0 = new List> { //new List{inf, 20, 18, 12, 8}, //new List{5, inf, 14, 7, 11}, //new List{12, 18, inf, 6, 11}, //new List{11, 17, 11, inf, 12}, //new List{5, 5, 5, 5, inf} //}; //TravelingSalesmanProblem p0 = new TravelingSalesmanProblem(m0); //p0.Solve(); //List<(int, int)> edges0 = p0.GetPath(); //double res0 = p0.GetResult(); //Console.WriteLine($"Ответ: {res0}"); //foreach ((int a, int b) in edges0) // Console.Write($"({a}, {b}) \n"); //List> m1 = new List> { //new List{inf, 4,5,7,5}, //new List{8, inf, 5,6,6}, //new List{3,5, inf, 9,6}, //new List{3,5,6, inf, 2}, //new List{6,2,3,8, inf} //}; //TravelingSalesmanProblem p1 = new TravelingSalesmanProblem(m1); //p1.Solve(); //List<(int, int)> edges1 = p1.GetPath(); //double res1 = p1.GetResult(); //Console.WriteLine($"Ответ: {res1}"); //foreach ((int a, int b) in edges1) // Console.Write($"({a}, {b})\n"); //АЛГОРИТМ ДЕЙКСТРЫ List> m2 = new List> { new List{inf,7,9,inf,inf,14}, new List{7,inf,10,15,inf,inf}, new List{9,10,inf,11,inf,2}, new List{inf,15,11,inf,6,inf}, new List{inf,inf,inf,6,inf,9}, new List{14,inf,2,inf,9,inf} }; int from = 0; DijkstrasAlgorithm d0 = new DijkstrasAlgorithm(m2); (Dictionary res, Dictionary> paths) = d0.Solve(from); foreach ((int vertex, double value) in res) { Console.Write($"{from + 1}->{vertex + 1}: {value}.\t"); foreach (int passesVertex in paths[vertex]) { Console.Write($"{passesVertex + 1} "); } Console.WriteLine(); } Console.WriteLine(); List> m3 = new List> { new List{inf,20,25,30,inf,inf,inf,inf,inf,inf},//a new List{20,inf,inf,5,inf,10,inf,inf,inf,inf}, new List{25,inf,inf,inf,inf,10,15,inf,inf,inf}, new List{30,5,inf,inf, 25,inf,inf,inf,inf,inf},//г new List{inf,inf,inf,25, inf, 15,inf,5,10,20}, new List{inf,10,10,inf, 15, inf,10,inf,inf,inf }, new List{inf,inf,15,inf, inf, 10,inf,15,inf,inf },//ж new List{inf,inf,inf,inf, 5, inf,15,inf,inf,10 }, new List{inf,inf,inf,inf, 10, inf,inf,inf,inf,10 }, new List{inf,inf,inf,inf, 20, inf,inf,10,10,inf }//к }; from = 0; DijkstrasAlgorithm d1 = new DijkstrasAlgorithm(m3); (res,paths) = d1.Solve(from); foreach ((int vertex, double value) in res) { Console.Write($"{Convert.ToChar('А'+from)}->{Convert.ToChar('А' + vertex)}: {value}.\t"); foreach (int passesVertex in paths[vertex]) { Console.Write($"{Convert.ToChar('А' + passesVertex)} "); } Console.WriteLine(); } } }