Exemplo n.º 1
0
        /// <summary>
        /// This method is to calculate minimum Turns to deliver the Order assuming the Drone is already in Warehouse. 
        /// </summary>
        /// <param name="o"></param>
        private static void CalculatateMinimunmTurnsPerOrder(Order o)
        {
            var bestWarehouseForeachProduct = (from w in db.Warehouses
                        join wprod in db.WarehouseProducts on w.WarehouseID equals wprod.WarehouseID
                        join oprod in o.Products on wprod.ProductID equals oprod.ProductID
                        join prd in db.Products on wprod.ProductID equals prd.ProductID
                        join dr in db.Drones on 1 equals 1
                        where wprod.Quantity > 0
                       select new { w.WarehouseID,
                                    prd.ProductID,
                                    Distance = GetDistance(w.Warehouselocation, o.DeliveryLocation) + GetDistance(w.Warehouselocation, dr.CurrentLocation),
                                    prd.Weight
                       }).OrderBy(r=>r.Distance).ThenBy(r=>r.Weight)
                       ;

            int turns = 0;
            int weightInDrone = 0;
            int wareHouseId = -1;
            foreach(var p in o.Products)
            {
                var w = (from wa in bestWarehouseForeachProduct
                         where wa.ProductID == p.ProductID
                         select (new { wa.Weight, wa.Distance, wa.WarehouseID })
                         ).OrderBy(r => r.Distance)
                         .ThenBy(r=>r.WarehouseID)
                         .ThenBy(r => r.Weight)
                         .First();

                p.NearestWareHouseId = w.WarehouseID;
                p.DistanceToNearestWareHouseId = w.Distance;

                if (wareHouseId != w.WarehouseID)
                {
                    turns += w.Distance;
                    weightInDrone = 0;
                    wareHouseId = w.WarehouseID;
                }
                if( (weightInDrone + w.Weight) > db.MaxPayLoad )
                {
                    turns += w.Distance;
                    weightInDrone = 0;
                }

                turns++;  //This is for loading product into Drone
                weightInDrone += w.Weight;

            }
            o.MinimumTurns = turns;
        }
Exemplo n.º 2
0
        static StoreDB ReadInputFile(string filePath)
        {
            using (StreamReader sr = new StreamReader(filePath))
            {
                var values = GetIntegerValueFromALine(sr);
                db.GridSizeX = values[0];
                db.GridSizeY = values[1];
                int NoOfDrones = values[2];
                db.MaxTurns = values[3];
                db.MaxPayLoad = values[4];

                values = GetIntegerValueFromALine(sr);
                int NoOfProductTypes = values[0];

                values = GetIntegerValueFromALine(sr);
                for (int i = 0; i < values.Count(); i++)
                {
                    db.Products.Add(new Product() { ProductID = i, Weight = values[i] });
                }

                values = GetIntegerValueFromALine(sr);
                int NoOfWareHouses = values[0];

                for (int warehouseId = 0; warehouseId < NoOfWareHouses; warehouseId++)
                {
                    values = GetIntegerValueFromALine(sr);
                    var warehouse = new Warehouse();
                    warehouse.WarehouseID = warehouseId;
                    warehouse.Warehouselocation = new Location() { X = values[0], Y = values[1] };

                    values = GetIntegerValueFromALine(sr);
                    for (int productId = 0; productId < values.Count(); productId++)
                    {
                        var warehouseProduct = new WarehouseProduct();
                        warehouseProduct.WarehouseID = warehouse.WarehouseID;
                        warehouseProduct.ProductID = productId;
                        warehouseProduct.Quantity = values[productId];
                        warehouseProduct.Warehouselocation = warehouse.Warehouselocation;
                        warehouseProduct.Weight = db.GetProductWeight(productId);

                        warehouse.Products.Add(warehouseProduct);

                        //Adding the same warehouseProduct in db lever collection to make it easy in Linq
                        db.WarehouseProducts.Add(warehouseProduct);
                    }
                    db.Warehouses.Add(warehouse);
                }

                values = GetIntegerValueFromALine(sr);
                int NoOfOrders = values[0];

                for (int orderid = 0; orderid < NoOfOrders; orderid++)
                {
                    var order = new Order();
                    order.OrderID = orderid;

                    values = GetIntegerValueFromALine(sr);
                    order.DeliveryLocation = new Location() { X = values[0], Y = values[1] };

                    values = GetIntegerValueFromALine(sr);
                    int NoOfProducts = values[0];

                    order.PendingItems = NoOfProducts;
                    values = GetIntegerValueFromALine(sr);
                    for (int pid = 0; pid < values.Count(); pid++)
                    {
                        var orderProduct = new OrderProduct();
                        orderProduct.OrderID = orderid;
                        orderProduct.ProductID = values[pid];
                        orderProduct.Weight = db.GetProductWeight(values[pid]);
                        orderProduct.DeliveryLocation = order.DeliveryLocation;
                        order.Products.Add(orderProduct);

                        //Adding the same orderProduct in db lever collection to make it easy in Linq
                        db.OrderProducts.Add(orderProduct);
                    }

                    db.Orders.Add(order);
                }

                db.LoadDrones(NoOfDrones);
            }

             return db;
        }