public IActionResult Random(int Amount, int leftSum, int rightSum, int leftAmount, int rightAmount)
        {
            FrankWolf frank  = new FrankWolf();
            Random    random = new Random();

            if (leftSum > rightSum || leftAmount > rightAmount || leftSum < 0 || rightAmount < 0 || rightAmount < 0 || leftAmount < 0)
            {
                return(RedirectToAction(nameof(ChooseAmount)));
            }
            Order order = RandomHandler.GetRandomizedOrder(Amount, leftSum, rightSum, leftAmount, rightAmount);

            ViewBag.Order = order;

            List <OrderProduct> changedFrank     = new List <OrderProduct>();
            List <OrderProduct> changedAnnealing = new List <OrderProduct>();
            List <OrderProduct> changedGenetic   = new List <OrderProduct>();

            if (order.OrdersProducts.Count() != 0)
            {
                var xNew = frank.FrankWolfMethod(order, order.OrdersProducts).ToList();

                changedFrank = BetterPrices(changedFrank, order.OrdersProducts.ToList(), xNew);
                int[] q = new int[Amount];
                int[] s = new int[Amount];
                int   i = 0;
                foreach (var op in order.OrdersProducts)
                {
                    s[i] = op.WriteOffSum;
                    q[i] = op.Amount;
                    i++;
                }
                Methods.Task       task = new Methods.Task(q, s, order.AllWriteOffSum);
                SimulatedAnnealing simulatedAnnealing = new SimulatedAnnealing(task, 200, 100);
                TaskPair           pair = simulatedAnnealing.executeAlgorithm();
                changedAnnealing = BetterPrices(changedAnnealing, order.OrdersProducts.ToList(), pair.X.ToList());

                Genetic  genetic     = new Genetic(task);
                TaskPair pairGenetic = genetic.ExecuteAlgorithm();
                changedGenetic = BetterPrices(changedGenetic, order.OrdersProducts.ToList(), pairGenetic.X.ToList());

                ViewBag.ProductsFrank     = changedFrank;
                ViewBag.ProductsAnnealing = changedAnnealing;
                ViewBag.ProductsGenetic   = changedGenetic;

                var sumFrank = 0; var writeOffFrank = 0;
                foreach (var op in changedFrank)
                {
                    sumFrank      += op.Sum;
                    writeOffFrank += op.WriteOffSum;
                }
                ViewBag.SumFrank        = sumFrank;
                ViewBag.AllSumFrank     = writeOffFrank;
                ViewBag.DisbalanceFrank = Math.Abs(sumFrank - writeOffFrank);
                var sumAnnealing = 0; var writeOffAnnealing = 0;
                foreach (var op in changedAnnealing)
                {
                    sumAnnealing      += op.Sum;
                    writeOffAnnealing += op.WriteOffSum;
                }
                ViewBag.SumAnnealing        = sumAnnealing;
                ViewBag.AllSumAnnealing     = writeOffAnnealing;
                ViewBag.DisbalanceAnnealing = Math.Abs(sumAnnealing - writeOffAnnealing);

                var sumGenetic = 0; var writeOffGenetic = 0;
                foreach (var op in changedGenetic)
                {
                    sumGenetic      += op.Sum;
                    writeOffGenetic += op.WriteOffSum;
                }
                ViewBag.SumGenetic        = sumGenetic;
                ViewBag.AllSumGenetic     = writeOffGenetic;
                ViewBag.DisbalanceGenetic = Math.Abs(sumGenetic - writeOffGenetic);
            }
            _context.SaveChanges();
            return(View(order.OrdersProducts));
        }
        public IActionResult File(string fileName)
        {
            FileReading         fileReading      = new FileReading();
            FrankWolf           frank            = new FrankWolf();
            Product             product          = new Product();
            List <OrderProduct> changedFrank     = new List <OrderProduct>();
            List <OrderProduct> changedAnnealing = new List <OrderProduct>();
            List <OrderProduct> changedGenetic   = new List <OrderProduct>();

            Order order = fileReading.FileRead(fileName);
            int   count = order.OrdersProducts.Count;

            order.AllSum = 0;

            if (order.OrdersProducts.Count() != 0)
            {
                foreach (var orderProduct in order.OrdersProducts)
                {
                    order.AllWriteOffSum = order.AllWriteOffSum + orderProduct.WriteOffSum;
                    orderProduct.Price   = Convert.ToInt32((double)orderProduct.WriteOffSum / orderProduct.Amount);
                    orderProduct.Sum     = Convert.ToInt32((double)orderProduct.Price * orderProduct.Amount);
                    order.AllSum         = order.AllSum + (orderProduct.Amount * orderProduct.Price);
                    order.Disbalance     = Math.Abs(order.AllWriteOffSum - order.AllSum);
                }

                ViewBag.Order = order;
                var xNew = frank.FrankWolfMethod(order, order.OrdersProducts).ToList();

                changedFrank = BetterPrices(changedFrank, order.OrdersProducts.ToList(), xNew);
                int[] q = new int[count];
                int[] s = new int[count];
                int   i = 0;
                foreach (var op in order.OrdersProducts)
                {
                    s[i] = op.WriteOffSum;
                    q[i] = op.Amount;
                    i++;
                }
                Methods.Task       task = new Methods.Task(q, s, order.AllWriteOffSum);
                SimulatedAnnealing simulatedAnnealing = new SimulatedAnnealing(task, 200, 100);
                TaskPair           pair = simulatedAnnealing.executeAlgorithm();
                changedAnnealing = BetterPrices(changedAnnealing, order.OrdersProducts.ToList(), pair.X.ToList());

                Genetic  genetic     = new Genetic(task);
                TaskPair pairGenetic = genetic.ExecuteAlgorithm();
                changedGenetic = BetterPrices(changedGenetic, order.OrdersProducts.ToList(), pairGenetic.X.ToList());

                ViewBag.ProductsFrank     = changedFrank;
                ViewBag.ProductsAnnealing = changedAnnealing;
                ViewBag.ProductsGenetic   = changedGenetic;

                var sumFrank = 0; var writeOffFrank = 0;
                foreach (var op in changedFrank)
                {
                    sumFrank      += op.Sum;
                    writeOffFrank += op.WriteOffSum;
                }
                ViewBag.SumFrank        = sumFrank;
                ViewBag.AllSumFrank     = writeOffFrank;
                ViewBag.DisbalanceFrank = Math.Abs(sumFrank - writeOffFrank);
                var sumAnnealing = 0; var writeOffAnnealing = 0;
                foreach (var op in changedAnnealing)
                {
                    sumAnnealing      += op.Sum;
                    writeOffAnnealing += op.WriteOffSum;
                }
                ViewBag.SumAnnealing        = sumAnnealing;
                ViewBag.AllSumAnnealing     = writeOffAnnealing;
                ViewBag.DisbalanceAnnealing = Math.Abs(sumAnnealing - writeOffAnnealing);

                var sumGenetic = 0; var writeOffGenetic = 0;
                foreach (var op in changedGenetic)
                {
                    sumGenetic      += op.Sum;
                    writeOffGenetic += op.WriteOffSum;
                }
                ViewBag.SumGenetic        = sumGenetic;
                ViewBag.AllSumGenetic     = writeOffGenetic;
                ViewBag.DisbalanceGenetic = Math.Abs(sumGenetic - writeOffGenetic);
            }
            _context.SaveChanges();
            return(View(order.OrdersProducts));
        }
        public IActionResult Details(int orderId)
        {
            FrankWolf frank = new FrankWolf();

            if (orderId == 0)
            {
                return(NotFound());
            }

            var order         = _context.Orders.FirstOrDefault(x => x.OrderId == orderId);
            var orderProducts = _context.OrderProducts.Where(x => x.OrderId == orderId);

            foreach (var p in orderProducts)
            {
                p.Product = _context.Products.FirstOrDefault(x => x.ProductId == p.ProductId);
            }
            ViewBag.Order = order;

            List <OrderProduct> changedFrank     = new List <OrderProduct>();
            List <OrderProduct> changedAnnealing = new List <OrderProduct>();
            List <OrderProduct> changedGenetic   = new List <OrderProduct>();

            if (orderProducts.Count() != 0 || orderProducts.Count() != 1)
            {
                int count = orderProducts.Count();
                var xNew  = frank.FrankWolfMethod(order, orderProducts).ToList();

                changedFrank = BetterPrices(changedFrank, orderProducts.ToList(), xNew);

                int[] q = new int[count];
                int[] s = new int[count];
                int   i = 0;
                foreach (var op in orderProducts)
                {
                    s[i] = op.WriteOffSum;
                    q[i] = op.Amount;
                    i++;
                }
                Methods.Task       task = new Methods.Task(q, s, order.AllWriteOffSum);
                SimulatedAnnealing simulatedAnnealing = new SimulatedAnnealing(task, 200, 100);
                TaskPair           pair = simulatedAnnealing.executeAlgorithm();
                changedAnnealing = BetterPrices(changedAnnealing, orderProducts.ToList(), pair.X.ToList());

                Genetic  genetic     = new Genetic(task);
                TaskPair pairGenetic = genetic.ExecuteAlgorithm();
                changedGenetic = BetterPrices(changedGenetic, orderProducts.ToList(), pairGenetic.X.ToList());

                ViewBag.Products          = changedFrank;
                ViewBag.ProductsAnnealing = changedAnnealing;
                ViewBag.ProductsGenetic   = changedGenetic;
                var sum = 0; var writeOff = 0;
                foreach (var op in changedFrank)
                {
                    sum      += op.Sum;
                    writeOff += op.WriteOffSum;
                }
                ViewBag.Sum        = sum;
                ViewBag.AllSum     = writeOff;
                ViewBag.Disbalance = Math.Abs(sum - writeOff);

                var sumAnnealing = 0; var writeOffAnnealing = 0;
                foreach (var op in changedAnnealing)
                {
                    sumAnnealing      += op.Sum;
                    writeOffAnnealing += op.WriteOffSum;
                }
                ViewBag.SumAnnealing        = sumAnnealing;
                ViewBag.AllSumAnnealing     = writeOffAnnealing;
                ViewBag.DisbalanceAnnealing = Math.Abs(sumAnnealing - writeOffAnnealing);

                var sumGenetic = 0; var writeOffGenetic = 0;
                foreach (var op in changedGenetic)
                {
                    sumGenetic      += op.Sum;
                    writeOffGenetic += op.WriteOffSum;
                }
                ViewBag.SumGenetic        = sumGenetic;
                ViewBag.AllSumGenetic     = writeOffGenetic;
                ViewBag.DisbalanceGenetic = Math.Abs(sumGenetic - writeOffGenetic);
            }

            return(View(orderProducts));
        }