示例#1
0
        public void ResearchForSize(StreamWriter fileWriter, int size, bool flag)
        {
            Solver    solve;
            Stopwatch clock = new Stopwatch();

            bool file = false;

            if (fileWriter != null)
            {
                file = true;
            }

            ResearchGraph researchGraph = new ResearchGraph();

            var startX  = researchGraph.GetStableX;
            var startYD = 0;
            var startYF = 0;
            var counter = 0;

            for (int i = 0; i < size; i++)
            {
                _console.RandomGenerate(bound);
                solve = new Solver(_console.From - 1, _console.To - 1, _console.N, _console.CMatrix);

                Console.WriteLine($"№{i + 1}");
                _console.WriteFlowMatrixToConsole(_console.CMatrix, _console.GetNullMatrix());
                Console.WriteLine("\n\nFord-Fulkerson solution:");
                clock.Start();
                solve.FordFulkerson();
                clock.Stop();
                FordFulkersonCount.Add(clock.Elapsed.TotalMilliseconds);

                if (flag)
                {
                    researchGraph.DrawGraphLine(true, startX, startYF, size, clock.Elapsed.TotalMilliseconds);
                    startYF = Convert.ToInt32(clock.Elapsed.TotalMilliseconds * 100);
                }


                Console.WriteLine($"\nResult: Max F = {solve.F}; Time spent for solving: {clock.Elapsed}");
                _console.WriteFlowMatrixToConsole(_console.CMatrix, solve.FlowMatrix.FlowToIntMatrix());
                Console.WriteLine();

                if (file)
                {
                    fileWriter.WriteLine($"№{i + 1}");
                    _console.WriteFlowMatrixToFile(_console.CMatrix, _console.GetNullMatrix(), fileWriter);
                    fileWriter.WriteLine("\n\nFord-Fulkerson solution:");
                    fileWriter.WriteLine($"\nResult: Max F = {solve.F}; Time spent for solving: {clock.Elapsed}");
                    _console.WriteFlowMatrixToFile(_console.CMatrix, solve.FlowMatrix.FlowToIntMatrix(), fileWriter);
                    fileWriter.WriteLine();
                }
                clock.Reset();

                Console.WriteLine("Dinics solution:");

                var dinics = new Dinics(_console.From - 1, _console.To - 1, _console.N, _console.CMatrix);
                clock.Start();
                dinics.Run();
                clock.Stop();
                DinicsCount.Add(clock.Elapsed.TotalMilliseconds);

                if (flag)
                {
                    researchGraph.DrawGraphLine(false, startX, startYD, size, clock.Elapsed.TotalMilliseconds);
                    startYD = Convert.ToInt32(clock.Elapsed.TotalMilliseconds * 100);
                }

                Console.WriteLine($"\nResult: Max F = {dinics.F.ToString()} ; Time spent for solving: { clock.Elapsed}");
                _console.WriteFlowMatrixToConsole(_console.CMatrix, dinics.FlowMatrix.FlowToIntMatrix());
                Console.WriteLine();
                if (file)
                {
                    fileWriter.WriteLine("Dinics solution:");
                    fileWriter.WriteLine($"\nResult: Max F = {dinics.F.ToString()} ; Time spent for solving: { clock.Elapsed}");
                    _console.WriteFlowMatrixToFile(_console.CMatrix, dinics.FlowMatrix.FlowToIntMatrix(), fileWriter);
                    fileWriter.WriteLine();
                }
                clock.Reset();



                var greedy = new Greedy(_console.CMatrix, _console.N, _console.From - 1, _console.To - 1);
                clock.Start();
                (int costGreedy, List <int> pathGreedy) = greedy.GreedyAlgorithm();
                clock.Stop();
                GreedyCount.Add(clock.Elapsed.TotalMilliseconds);

                Console.WriteLine("\nGreedy solution:");
                _console.WriteListToConsole(pathGreedy, costGreedy);
                Console.WriteLine($"Time spent for solving: { clock.Elapsed}");
                if (file)
                {
                    fileWriter.WriteLine("\nGreedy solution:");
                    _console.WriteListToFile(pathGreedy, costGreedy, fileWriter);
                    fileWriter.WriteLine($"Time spent for solving: { clock.Elapsed}");
                    fileWriter.WriteLine();
                }
                clock.Reset();

                var dfs = new DFS(_console.CMatrix, _console.N, _console.From - 1, _console.To - 1);
                clock.Start();
                (int costDFS, List <int> pathDFS) = dfs.Run();
                clock.Stop();
                DFSCount.Add(clock.Elapsed.TotalMilliseconds);

                Console.WriteLine("\n\nDFS solution:");
                _console.WriteListToConsole(pathDFS, costDFS);
                Console.WriteLine($"Time spent for solving: { clock.Elapsed}");
                if (file)
                {
                    fileWriter.WriteLine("\nDFS solution:");
                    _console.WriteListToFile(pathDFS, costDFS, fileWriter);
                    fileWriter.WriteLine($"Time spent for solving: { clock.Elapsed}");
                    fileWriter.WriteLine("\n\n");
                }
                clock.Reset();

                Console.WriteLine("\n\n");

                if (flag)
                {
                    var partsize = size / 5;

                    if (i == 0) // numbers on X
                    {
                        researchGraph.DrawXNumbers(i, startX);
                        counter++;
                    }
                    else if (i == counter * partsize || i == size - 1 && counter < 6)
                    {
                        researchGraph.DrawXNumbers(i, startX + ((researchGraph.GetX - 420) / size));
                        counter++;
                    }

                    startX += ((researchGraph.GetX - 420) / size);
                }
            }

            if (flag)
            {
                researchGraph.DrawYNumbers(FordFulkersonCount, DinicsCount);
                researchGraph.DrawAxes("(task number)", "(msec)");
                researchGraph.DrawLegend(size, _console);

                researchGraph.SaveJPG(1);
            }
        }
示例#2
0
        static void Main()
        {
            IOConsole console = new IOConsole();

            while (true)
            {
                try
                {
                    Console.Clear();
                    if (console.CMatrix == null)
                    {
                        Console.WriteLine("*** There is no flow matrix to work with ***\n");
                    }
                    else
                    {
                        Console.WriteLine("*** The matrix is ready ***\n");
                    }
                    Console.WriteLine("1. Read flow matrix from file");
                    Console.WriteLine("2. Console input of matrix");
                    Console.WriteLine("3. Random generating matrix");
                    Console.WriteLine("4. Matrix Output");
                    Console.WriteLine("5. Ford-Fulkerson solution");
                    Console.WriteLine("6. Dinics solution");
                    Console.WriteLine("7. Greedy search of path");
                    Console.WriteLine("8. DFS of path");
                    Console.WriteLine("9. Experimental research");
                    Console.WriteLine("10. Exit");
                    var key = Console.ReadLine();
                    Console.Clear();
                    switch (key)
                    {
                    case "1":
                        if (console.CMatrix != null)
                        {
                            Console.WriteLine("You have already got a matrix. " +
                                              "Do you really want to rewrite it? [y/n]");
                            char k;
                            do
                            {
                                k = Console.ReadKey().KeyChar;
                            } while (k != 'n' && k != 'y');
                            if (k == 'n')
                            {
                                break;
                            }
                        }

                        Console.WriteLine("\nEnter the filename: ");
                        var filename = Console.ReadLine();
                        if (!File.Exists(filename))
                        {
                            Console.WriteLine("The file \"" + filename + "\" does not exist");
                            Console.ReadKey();
                            continue;
                        }
                        console = new IOConsole(filename);
                        console.ReadMatrix();
                        console.WriteMatrix(console.CMatrix);
                        Console.ReadKey();
                        break;

                    case "2":
                        if (console.CMatrix != null)
                        {
                            Console.WriteLine("You have already got a matrix. " +
                                              "Do you really want to rewrite it? [y/n]");
                            char k;
                            do
                            {
                                k = Console.ReadKey().KeyChar;
                            } while (k != 'n' && k != 'y');
                            if (k == 'n')
                            {
                                break;
                            }
                        }

                        console = new IOConsole();
                        console.ConsoleInputMatrix();
                        console.WriteMatrix(console.CMatrix);
                        Console.ReadKey();
                        break;

                    case "3":
                        if (console.CMatrix != null)
                        {
                            Console.WriteLine("You have already got a matrix. " +
                                              "Do you really want to rewrite it? [y/n]");
                            char k;
                            do
                            {
                                k = Console.ReadKey().KeyChar;
                            } while (k != 'n' && k != 'y');
                            if (k == 'n')
                            {
                                break;
                            }
                        }

                        console = new IOConsole();
                        int bound = console.RandomInput();
                        console.RandomGenerate(bound);
                        console.WriteMatrix(console.CMatrix);
                        Console.ReadKey();
                        break;

                    case "4":
                        if (console.CMatrix == null)
                        {
                            Console.WriteLine("You haven`t got any matrix to use");
                        }
                        else
                        {
                            console.WriteMatrix(console.CMatrix);
                        }
                        Console.ReadKey();
                        break;

                    case "5":
                        if (console.CMatrix == null)
                        {
                            Console.WriteLine("You haven`t got any matrix to use");
                            Console.ReadKey();
                            continue;
                        }

                        Console.WriteLine("Ford-Fulkerson solution:");
                        Solver solve = new Solver(console.From - 1, console.To - 1, console.N, console.CMatrix);
                        console.WriteFlowMatrixToConsole(console.CMatrix, console.GetNullMatrix());
                        solve.FordFulkerson();

                        Console.WriteLine($"Result: Max F = {solve.F}");
                        console.WriteFlowMatrixToConsole(console.CMatrix, solve.FlowMatrix.FlowToIntMatrix());
                        Console.ReadKey();
                        break;

                    case "6":
                        if (console.CMatrix == null)
                        {
                            Console.WriteLine("You haven`t got any matrix to use");
                            Console.ReadKey();
                            continue;
                        }

                        Console.WriteLine("\nDinics solution:");
                        Dinics dinics = new Dinics(console.From - 1, console.To - 1, console.N, console.CMatrix);
                        console.WriteFlowMatrixToConsole(console.CMatrix, console.GetNullMatrix());
                        dinics.Run();

                        Console.WriteLine("\nResult: Max F = " + dinics.F.ToString());
                        console.WriteFlowMatrixToConsole(console.CMatrix, dinics.FlowMatrix.FlowToIntMatrix());
                        Console.ReadKey();
                        break;

                    case "7":
                        if (console.CMatrix == null)
                        {
                            Console.WriteLine("You haven`t got any matrix to use");
                            Console.ReadKey();
                            continue;
                        }

                        Console.WriteLine("\nGreedy solution:");
                        var greedy = new Greedy(console.CMatrix, console.N, console.From - 1, console.To - 1);
                        (int costGreedy, List <int> pathGreedy) = greedy.GreedyAlgorithm();
                        console.WriteListToConsole(pathGreedy, costGreedy);
                        Console.ReadKey();
                        break;

                    case "8":
                        if (console.CMatrix == null)
                        {
                            Console.WriteLine("You haven`t got any matrix to use");
                            Console.ReadKey();
                            continue;
                        }

                        Console.WriteLine("\nDFS solution:");
                        var dfs = new DFS(console.CMatrix, console.N, console.From - 1, console.To - 1);
                        (int costDFS, List <int> pathDFS) = dfs.Run();
                        console.WriteListToConsole(pathDFS, costDFS);
                        Console.ReadKey();
                        break;

                    case "9":
                        console = new IOConsole();
                        Research experimentor = new Research();
                        experimentor.Menu();
                        break;

                    case "10":
                        Environment.Exit(0);
                        break;

                    default:
                        Console.WriteLine("Invalid input");
                        Console.ReadKey();
                        break;
                    }
                }catch (StackOverflowException e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadKey();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadKey();
                }
            }
        }