コード例 #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);
            }
        }