コード例 #1
0
        public void ResearchByTimePerSize(StreamWriter fileWriter)
        {
            List <Average> time = new List <Average>();

            ResearchGraph researchGraph = new ResearchGraph();
            var           startX        = researchGraph.GetStableX;
            var           startYD       = 0;
            var           startYF       = 0;
            var           counter       = 0;

            string result;

            bool file = false;

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

            Console.WriteLine("Enter start size:");
            var startSize = Convert.ToInt32(Console.ReadLine());

            if (startSize <= 0)
            {
                throw new Exception("Not positive value");
            }
            Console.WriteLine("Enter finish size:");
            var finishSize = Convert.ToInt32(Console.ReadLine());

            if (startSize <= 0 || startSize > finishSize)
            {
                throw new Exception("Invalid value");
            }
            Console.WriteLine("Enter step:");
            var step = Convert.ToInt32(Console.ReadLine());

            if (step <= 0)
            {
                throw new Exception("Invalid value");
            }
            Console.WriteLine("A max value of flow: ");
            bound = Convert.ToInt32(Console.ReadLine());
            if (bound <= 0)
            {
                throw new Exception("Not positive value");
            }
            Console.WriteLine("Enter research size:");
            var researchSize = Convert.ToInt32(Console.ReadLine());

            if (researchSize <= 0)
            {
                throw new Exception("Not positive value");
            }


            for (int i = startSize; i <= finishSize; i += step)
            {
                _console.ResearchInput(i, 1, i);
                CounterReset();
                Console.WriteLine($"\t\t___MATRIX SIZE: {i}___\n");
                if (fileWriter != null)
                {
                    fileWriter.WriteLine($"\t\t___MATRIX SIZE: {i}___\n");
                }
                ResearchForSize(fileWriter, researchSize, false);
                time.Add(new Average(i, Compare(null)));

                researchGraph.DrawGraphLine(true, startX, startYF, ((finishSize - startSize) / step) + 1, time[(i - startSize) / step].Time[0] / 10);
                startYF = Convert.ToInt32(time[(i - startSize) / step].Time[0] * 10);

                researchGraph.DrawGraphLine(false, startX, startYD, ((finishSize - startSize) / step) + 1, time[(i - startSize) / step].Time[1] / 10);
                startYD = Convert.ToInt32(time[(i - startSize) / step].Time[1] * 10);

                var divider  = ((finishSize - startSize) / step) >= 5 ? 5 : ((finishSize - startSize) / step);
                var partsize = ((finishSize - startSize) / step) / divider;
                if (i == (startSize + (counter * partsize * step)) || i == finishSize && counter < 6)
                {
                    researchGraph.DrawXNumbers(i, startX + ((researchGraph.GetX - 420) / (((finishSize - startSize) / step) + 1)));
                    counter++;
                }

                startX += ((researchGraph.GetX - 420) / (((finishSize - startSize) / step) + 1));
            }
            researchGraph.DrawYNumbers(time);
            researchGraph.DrawAxes("size", "msec");
            researchGraph.DrawLegend(startSize, finishSize, step, researchSize);

            researchGraph.SaveJPG(2);

            result = "RESULTS FOR TIME PER SIZE:\n";
            for (int i = 0; i < time.Count; i++)
            {
                result += $"Size: {time[i].Size}\n";
                result += $"    Ford-Falkerson time: \t{time[i].Time[0]}\n";
                result += $"    Dinics time: \t\t{time[i].Time[1]}\n";
                result += $"    Greedy time: \t\t{time[i].Time[2]}\n";
                result += $"    DFS time: \t\t\t{time[i].Time[3]}\n";
            }
            Console.WriteLine(result);
            if (file)
            {
                fileWriter.WriteLine(result);
                _console.WriteResearch(time);
            }
        }
コード例 #2
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);
            }
        }