Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
 public AnaliticsController(AnaliticsContext analiticsContext)
 {
     db = analiticsContext;
 }