예제 #1
0
 public Delta(DataMatrix data, SolutionMatrix solution)
 {
     ActualBestSolution = solution;
     Data        = data;
     SwapCounter = 0;
     ActualBestSolution.Score = Convert.ToInt32(RateSolution(solution.Solution.ToArray(), data));
     CalculateDeltaTable();
 }
        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));
            }
        }
예제 #3
0
        private void Finalise()
        {
            AssignmentMatrix = new Matrix(_m);

            if (_isTransposed)
            {
                AssignmentMatrix = AssignmentMatrix.Transpose();
            }

            SolutionMatrix = AssignmentMatrix.MultiplyElements(CostMatrix);
            TotalCost      = SolutionMatrix.Sum();

            if (IsShowDiagnostics)
            {
                ShowSolutionMatrix();
                ShowTotalCost();
            }
        }
예제 #4
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());
            }
        }
예제 #5
0
 public ActionResult SteepestSolutionFile(SolutionMatrix solution)
 {
     return(View(solution));
 }
예제 #6
0
 public ActionResult GreedySolutionFile(SolutionMatrix solution)
 {
     return(View(solution));
 }
 public ActionResult SteepestSolutionGenerated(SolutionMatrix solution)
 {
     return(View(solution));
 }
 public ActionResult GreedySolutionGenerated(SolutionMatrix solution)
 {
     return(View(solution));
 }
예제 #9
0
        public override SolutionMatrix GetSolution()
        {
            var builder = new ConfigurationBuilder();

            builder.SetBasePath(Directory.GetCurrentDirectory());
            builder.AddJsonFile("appsettings.json");
            var    config           = builder.Build();
            string connectionString = config.GetConnectionString("DefaultConnection");

            var optionsBuilder = new DbContextOptionsBuilder <AnaliticsContext>();
            var options        = optionsBuilder
                                 .UseSqlServer(connectionString)
                                 .Options;

            AnaliticsContext db = new AnaliticsContext(options);

            Stopwatch      stopwatch = new Stopwatch();
            SolutionMatrix solution  = new SolutionMatrix
            {
                Dimension = Data.Distances.Length,
                Solution  = this.GetList(this.GetRandomInitSolution())
            };

            stopwatch.Start();

            FirstSolution = solution.Solution.ToArray();
            Delta betterSolution = new Delta(Data, solution);

            CheckedElems = 0;
            bool isLocalMinimum = false;

            while (!isLocalMinimum)
            {
                if (!CheckBestNeighbor(betterSolution))
                {
                    isLocalMinimum = true;
                }
            }
            CheckedElems = Steps * Data.Dimension * (Data.Dimension - 1);
            int score = 0;

            for (int i = 1; i < solution.Solution.ToArray().Length; i++)
            {
                score += Data.Distances[solution.Solution.ToArray()[i - 1]][solution.Solution.ToArray()[i]] * Data.Flows[i - 1][i];
            }

            stopwatch.Stop();

            var analitics = new Analitics();

            analitics.Dimenssion = Data.Dimension;
            long     ticks    = stopwatch.ElapsedTicks;
            TimeSpan interval = TimeSpan.FromTicks(ticks);

            analitics.Method   = "Метод вектора спаду";
            analitics.WorkTime = interval.ToString();
            analitics.Score    = score;
            db.AnaliticsTable.Add(analitics);
            db.SaveChangesAsync();

            solution.Score         = score;
            solution.SolutionArray = betterSolution.ActualBestSolution.Solution.ToArray();
            return(solution);
        }
예제 #10
0
        public override SolutionMatrix GetSolution()
        {
            var builder = new ConfigurationBuilder();

            builder.SetBasePath(Directory.GetCurrentDirectory());
            builder.AddJsonFile("appsettings.json");
            var    config           = builder.Build();
            string connectionString = config.GetConnectionString("DefaultConnection");

            var optionsBuilder = new DbContextOptionsBuilder <AnaliticsContext>();
            var options        = optionsBuilder
                                 .UseSqlServer(connectionString)
                                 .Options;

            AnaliticsContext db = new AnaliticsContext(options);

            Stopwatch stopwatch           = new Stopwatch();
            var       solutionMatrix      = new SolutionMatrix();
            int       dimension_          = this.Data.Dimension;
            var       distancesPotential_ = new List <int>();
            var       flowPotential_      = new List <int>();

            stopwatch.Start();

            for (int i = 0; i < dimension_; ++i)
            {
                int potential = 0;
                for (int j = 0; j < dimension_; ++j)
                {
                    if (i != j)
                    {
                        potential += this.Data.Distances[i][j];
                    }
                }
                distancesPotential_.Add(potential);
            }

            for (int i = 0; i < dimension_; ++i)
            {
                int potential = 0;
                for (int j = 0; j < dimension_; ++j)
                {
                    if (i != j)
                    {
                        potential += this.Data.Flows[i][j];
                    }
                }
                flowPotential_.Add(potential);
            }

            int[] solution_ = Enumerable.Repeat(-1, Data.Dimension).ToArray();
            for (int j = 0; j < dimension_; ++j)
            {
                int maxPos = 0;
                int minPos = 0;
                for (int i = 0; i < dimension_; ++i)
                {
                    if (distancesPotential_[i] < distancesPotential_[maxPos])
                    {
                        maxPos = i;
                    }
                    if (flowPotential_[i] > flowPotential_[minPos])
                    {
                        minPos = i;
                    }
                }
                solution_[maxPos]           = minPos;
                distancesPotential_[maxPos] = -1;
                flowPotential_[minPos]      = Int32.MaxValue;
            }
            solutionMatrix.SolutionArray = solution_;
            int score = 0;

            for (int i = 1; i < solution_.Length; i++)
            {
                score += Data.Distances[solution_[i - 1]][solution_[i]] * Data.Flows[i - 1][i];
            }
            solutionMatrix.Score = score;
            stopwatch.Stop();
            var analitics = new Analitics();

            analitics.Dimenssion = Data.Dimension;
            long     ticks    = stopwatch.ElapsedTicks;
            TimeSpan interval = TimeSpan.FromTicks(ticks);

            analitics.Method   = "Жадібний алгоритм";
            analitics.WorkTime = interval.ToString();
            analitics.Score    = score;
            db.AnaliticsTable.Add(analitics);
            db.SaveChangesAsync();
            return(solutionMatrix);
        }