//public void slottingA(int[,] slotslist, List<ItemsSlotter> itemsA) //{ // // partitioningItems(); // // List<Shelf> shelflist = getShelfs(); // // int[,] slotslist = getEmptySlots(shelflist); // int slotspace; // int totalweight = 0; // int start; // int end = -1; // foreach (ItemsSlotter item in itemsA) // { // totalweight = totalweight + item.weight; // } // for (int i = 0; i < slotslist.Length / 4; i++) // { // slotspace = slotslist[i, 3] - slotslist[i, 2] + 1; // if (totalweight <= slotspace && slotslist[i, 1] == 65) // { // start = slotslist[i, 2]; // foreach (ItemsSlotter item in itemsA) // { // Console.Write("First"); // end = start + item.weight - 1; // instructions.Add(new Instructions(slotslist[i, 0], start, end, item.item_name)); // start = end + 1; // } // } // else if (totalweight > slotspace && slotslist[i, 1] == 65) // { // //Call knapsack here // } // } //} //public void slottingB(int[,] slotslist, List<ItemsSlotter> itemsB) //{ // //partitioningItems(); // //List<Shelf> shelflist = getShelfs(); // //int[,] slotslist = getEmptySlots(shelflist); // int slotspace; // int totalweight = 0; // int start; // int end = -1; // foreach (ItemsSlotter item in itemsB) // { // totalweight = totalweight + item.weight; // } // for (int i = 0; i < slotslist.Length / 4; i++) // { // slotspace = slotslist[i, 3] - slotslist[i, 2] + 1; // if (totalweight <= slotspace && slotslist[i, 1] == 66) // { // start = slotslist[i, 2]; // foreach (ItemsSlotter item in itemsB) // { // if (!instructions.Exists(ins => ins.itemname.Equals(item.item_name))) // { // Console.Write("First B"); // end = start + item.weight - 1; // instructions.Add(new Instructions(slotslist[i, 0], start, end, item.item_name)); // start = end + 1; // } // else // { // } // } // slotslist[i, 2] = end + 1; // } // else if (totalweight > slotspace && slotslist[i, 1] == 66) // { // //Call knapsack here // } // //else if (totalweight <= slotspace) // //{ // // start = slotslist[i, 2]; // // foreach (ItemsSlotter item in itemsB) // // { // // if (instructions.Exists(ins => ins.itemname == item.item_name)) // // { // // } // // else // // { // // end = start + item.weight - 1; // // instructions.Add(new Instructions(slotslist[i, 0], start, end, item.item_name)); // // start = end + 1; // // } // // } // // slotslist[i, 2] = end + 1; // //} // else // { // // Console.WriteLine("No space in warehouse, items cannot be slotted"); // } // } //} //public void slottingC(int[,] slotslist, List<ItemsSlotter> itemsC) //{ // //partitioningItems(); // //List<Shelf> shelflist = getShelfs(); // //int[,] slotslist = getEmptySlots(shelflist); // int slotspace; // int totalweight = 0; // int start; // int end = -1; // foreach (ItemsSlotter item in itemsC) // { // totalweight = totalweight + item.weight; // } // for (int i = 0; i < slotslist.Length / 4; i++) // { // slotspace = slotslist[i, 3] - slotslist[i, 2] + 1; // if (totalweight <= slotspace && (char)slotslist[i, 1] == 'C') // { // start = slotslist[i, 2]; // foreach (ItemsSlotter item in itemsC) // { // if (instructions.Exists(ins => ins.itemname == item.item_name)) // { // } // else // { // end = start + item.weight - 1; // instructions.Add(new Instructions(slotslist[i, 0], start, end, item.item_name)); // start = end + 1; // } // } // slotslist[i, 2] = end + 1; // } // else if (totalweight > slotspace && (char)slotslist[i, 1] == 'C') // { // //Call knapsack here // } // //else if (totalweight <= slotspace) // //{ // // start = slotslist[i, 2]; // // foreach (ItemsSlotter item in itemsC) // // { // // if (instructions.Exists(ins => ins.itemname == item.item_name)) // // { // // } // // else // // { // // end = start + item.weight - 1; // // instructions.Add(new Instructions(slotslist[i, 0], start, end, item.item_name)); // // start = end + 1; // // } // // } // // slotslist[i, 2] = end + 1; // //} // else // { // // Console.WriteLine("No space in warehouse, items cannot be slotted"); // } // } //} //public void knapsack(int capacity, int[] weight, double[] value, String[] item) //{ // int count = 0;//count for item no. // int remaining = capacity; //space in knapsack remaining // int current_weight = 0; //selected weight // double current_value = 0.0; //selected value // String current_item; //selected item // List<ItemsSlotter> selected_items = new List<ItemsSlotter>(); // ItemsSlotter items = new ItemsSlotter(); // //sorting weight and value according to weight and value ratio // double[] x = Enumerable.Repeat(0.0, weight.Length).ToArray(); // for (int i = 0; i <= weight.Length - 1; i++) // { // for (int j = 0; j <= weight.Length - 2; j++) // { // if ((weight[i] / value[i]) < (weight[j] / value[j])) // { // int temp1 = weight[i]; // weight[i] = weight[j]; // weight[j] = temp1; // double temp2 = value[i]; // value[i] = value[j]; // value[j] = temp2; // } // } // } // for (count = 0; count < item.Length; count++) // { // current_weight = weight[count]; // current_value = value[count]; // current_item = item[count]; // if (current_weight > remaining) // { // break; // } // else // { // x[count] = 1.0; // items.weight = current_weight; // items.value = current_value; // items.item_name = current_item; // items.factor = x[count]; // selected_items.Add(items); // remaining = remaining - current_weight; // } // } // if (count < item.Length) // { // x[count] = remaining / weight[count]; // items.weight = weight[count]; // items.value = value[count]; // items.item_name = item[count]; // items.factor = x[count]; // selected_items.Add(items); // } //} public List <ItemsSlotter> knapsack(int slotspace, ref int start, int shelfid, List <ItemsSlotter> items) { int count = 0; //count for item no. int remaining = slotspace; //space in knapsack remaining int current_weight = 0; //selected weight double current_value = 0.0; //selected value String current_item; //selected item List <ItemsSlotter> itemscopy = new List <ItemsSlotter>(); List <ItemsSlotter> itemsremaining = new List <ItemsSlotter>(); //ItemsSlotter items = new ItemsSlotter(); //sorting weight and value according to weight and value ratio for (int i = 0; i < items.Count; i++) { for (int j = 1; j <= items.Count - 2; j++) { if (items[i].orders / items[i].quantity < items[j].orders / items[j].quantity) { ItemsSlotter tmp = items[j]; items[j] = items[i]; items[i] = items[j]; } } } itemscopy.AddRange(items); foreach (var item in items) { current_weight = item.quantity; current_value = item.orders; current_item = item.item_name; if (current_weight > remaining) { break; } else { //selected_items.Add(new ItemsSlotter(current_item,current_weight,current_value,1)); instructions.Add(new Instructions(shelfid, start, start + current_weight - 1, current_item, item.item_id)); itemscopy.Remove(item); start = start + current_weight; remaining = remaining - current_weight; } } if (itemscopy.Count != 0 && remaining > 0) { foreach (var item in itemscopy) { if (remaining > 0) { current_weight = remaining; current_item = item.item_name; instructions.Add(new Instructions(shelfid, start, start + current_weight - 1, current_item, item.item_id)); itemsremaining.Add(new ItemsSlotter(item.item_id, item.quantity - remaining, item.item_name, item.orders)); start = start + current_weight; remaining = remaining - current_weight; } } } return(itemsremaining); }
public ActionResult Save(List <Item> ware, List <Item_Consignment> itemcon, List <Consignment> con) { int conid = -1; List <ItemsSlotter> isl = new List <ItemsSlotter>(); foreach (Consignment conn in con) { WarehouseBusinessLayer wbl = new WarehouseBusinessLayer(); int warehouseIdd = wbl.getWarehouseId(Convert.ToInt32(Session["UserID"])); conn.warehouseId = warehouseIdd; conn.consignmentStatus = "Added"; try { conid = db.Consignments.Max(u => u.id) + 1; } catch (Exception ex) { conid = 1; } conn.id = conid; cbl.addConsignment(conn); int index = 0; foreach (Item_Consignment ic in itemcon) { Item items = ware.ElementAt(index); ic.itemId = getNewOrOldId(items); ItemsSlotter itemslot = new ItemsSlotter(); itemslot.item_id = ic.itemId; itemslot.quantity = (int)ic.quantity; // itemslot.expiry_date = ic.expiry.Value.ToShortDateString(); Item it = db.Items.FirstOrDefault(a => a.id == itemslot.item_id); itemslot.item_name = it.itemName; bool val = db.Item_Warehouse.Any(o => o.itemId == ic.itemId && o.warehouseId == warehouseIdd); if (val == true) { Item_Warehouse iw = db.Item_Warehouse.FirstOrDefault(a => a.itemId == ic.itemId && a.warehouseId == warehouseIdd); iw.quantity += ic.quantity; cbl.updateItemWarehouse(iw); itemslot.quantity = (int)iw.quantity; // found = true; //TODO: ADO.NET CODE } else { Item_Warehouse iw = new Item_Warehouse(); iw.warehouseId = warehouseIdd; iw.itemId = ic.itemId; iw.quantity = ic.quantity; iw.orders = 0; cbl.addItemWarehouse(iw); itemslot.quantity = (int)iw.quantity; } ic.consignmentId = conid; cbl.addItem_Consignment(ic); isl.Add(itemslot); index++; } //Yaha pe SLotting Horae ha List <Shelf> newShelf = db.Shelves.Where(a => a.warehouse_id == warehouseIdd).ToList(); Slotting slotting = new Slotting(); bool isError = slotting.slotting(newShelf, isl); if (isError) { ViewBag.throwError = "Unfortunately, there is no space in warehouse for the new items"; } List <String> instructionList = slotting.instructionsList; JavaScriptSerializer jss = new JavaScriptSerializer(); string inst = jss.Serialize(instructionList); SlottingBusinessLayer sbl = new SlottingBusinessLayer(); sbl.UpdateConsignmentInstruction(conid, inst); List <String> shelfInserted = slotting.shelfInserted.Distinct().ToList(); string insertedShelfJSON = jss.Serialize(shelfInserted); WarehouseDBEntities wdb = new WarehouseDBEntities(); Consignment consignment = wdb.Consignments.Find(conid); consignment.consignmentStatus = "Added"; consignment.shelfInserted = insertedShelfJSON; wdb.Entry(consignment).State = EntityState.Modified; wdb.SaveChanges(); Session["consignmentID"] = conid; break; } return(RedirectToAction("LoggedIn", "Account")); }