public ActionResult MatrixGenerated(DataMatrix problem2, [FromQuery] string myMethod = null)
        {
            bool sym     = true;
            var  problem = problem2;

            for (int i = 0; i < problem.Dimension; i++)
            {
                for (int j = 0; j < problem.Dimension; j++)
                {
                    if (problem.Distances[i][j] != problem.Distances[j][i])
                    {
                        sym = false;
                    }
                }
            }
            for (int i = 0; i < problem.Dimension; i++)
            {
                for (int j = 0; j < problem.Dimension; j++)
                {
                    if (problem.Flows[i][j] != problem.Flows[j][i])
                    {
                        sym = false;
                    }
                }
            }
            if (sym != true)
            {
                return(RedirectToAction("Report"));
            }

            if (myMethod == "Greedy")
            {
                SolutionMatrix solution = new SolutionMatrix();
                solution.SolutionArray = new int[problem.Flows.Count()];
                var greedySolver = new GreedySolver(problem);
                solution = greedySolver.GetSolution();
                return(RedirectToAction("GreedySolutionGenerated", solution));
            }
            else if (myMethod == "Steepest")
            {
                SolutionMatrix solution = new SolutionMatrix();
                solution.SolutionArray = new int[problem.Flows.Count()];
                var steepestSolver = new SteepestSolver(problem);
                solution.SolutionArray = steepestSolver.GetSolution().SolutionArray;
                solution.Score         = steepestSolver.GetSolution().Score;
                return(RedirectToAction("SteepestSolutionGenerated", solution));
            }
            else
            {
                return(View(problem));
            }
        }
Esempio n. 2
0
        public ActionResult DataFile(IFormFile postedFile, [FromQuery] string myMethod = null)
        {
            _ = this.Environment.WebRootPath;
            _ = this.Environment.ContentRootPath;

            string path = Path.Combine(this.Environment.WebRootPath, "UploadedFiles");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string fileName = Path.GetFileName(postedFile.FileName);


            var problem = new DataMatrix();

            using (var sr = new StreamReader(System.IO.File.OpenRead(Path.Combine(path, fileName))))
            {
                string data     = sr.ReadToEnd();
                var    splitted = data.Split((char[])null, StringSplitOptions.RemoveEmptyEntries)
                                  .ToList()
                                  .Select(x => Convert.ToInt32(x))
                                  .ToList();

                int matrixSize = splitted[0];
                problem.Dimension = matrixSize;
                if (problem.Dimension != 0)
                {
                    problem.Distances = new int[problem.Dimension][];
                    for (int i = 0; i < problem.Dimension; i++)
                    {
                        problem.Distances[i] = new int[problem.Dimension];
                    }
                    problem.Flows = new int[problem.Dimension][];
                    for (int i = 0; i < problem.Dimension; i++)
                    {
                        problem.Flows[i] = new int[problem.Dimension];
                    }
                }
                var qapDataFlow     = new int[matrixSize][];
                var qapDataDistance = new int[matrixSize][];

                var chunked = splitted.Skip(1).Chunk(matrixSize).ToList();

                for (int i = 0; i < matrixSize; ++i)
                {
                    problem.Distances[i] = chunked[i].ToArray();
                }

                for (int i = matrixSize; i < 2 * matrixSize; ++i)
                {
                    problem.Flows[i - matrixSize] = chunked[i].ToArray();
                }
            }
            bool sym = true;

            for (int i = 0; i < problem.Dimension; i++)
            {
                for (int j = 0; j < problem.Dimension; j++)
                {
                    if (problem.Distances[i][j] != problem.Distances[j][i])
                    {
                        sym = false;
                    }
                }
            }
            for (int i = 0; i < problem.Dimension; i++)
            {
                for (int j = 0; j < problem.Dimension; j++)
                {
                    if (problem.Flows[i][j] != problem.Flows[j][i])
                    {
                        sym = false;
                    }
                }
            }
            if (sym != true)
            {
                return(RedirectToAction("Report"));
            }

            var problemResult = new DataMatrix()
            {
                Dimension = problem.Dimension,
                Distances = problem.Distances,
                Flows     = problem.Flows
            };

            if (myMethod == "Greedy")
            {
                SolutionMatrix solution = new SolutionMatrix();
                solution.SolutionArray = new int[problem.Flows.Count()];
                var greedySolver = new GreedySolver(problem);
                solution = greedySolver.GetSolution();
                return(RedirectToAction("GreedySolutionFile", solution));
            }
            else if (myMethod == "Steepest")
            {
                SolutionMatrix solution = new SolutionMatrix();
                solution.SolutionArray = new int[problem.Flows.Count()];
                var steepestSolver = new SteepestSolver(problem);
                solution.SolutionArray = steepestSolver.GetSolution().SolutionArray;
                solution.Score         = steepestSolver.GetSolution().Score;
                return(RedirectToAction("SteepestSolutionFile", solution));
            }
            else
            {
                return(View());
            }
        }