public int WorkSchedule() //1 = success 0 = failure { try { List <LUT_Area> listOfArea = this.db.LUT_Area.ToList(); //List of area List <Truck> truckList = this.db.Trucks.ToList(); //List of available trucks List <TruckCleanUp> bestFit; //List to store the answer of TruckAllocationToRegion List <LUT_TruckType> currentTruckTypes; //List of available truckType Truck truckToUpdate; int indexOfbesttruck; foreach (LUT_Area area in listOfArea)//For each area we calculate the best truck and update its areaId { bestFit = TruckAllocationToRegion(area); currentTruckTypes = this.db.LUT_TruckType.Where(x => truckList.FindIndex(f => f.TruckTypeId == x.TruckTypeId) != -1).ToList(); TruckCleanUp bestTruck = bestFit.First(); foreach (TruckCleanUp truck in bestFit) //searching for the smallest truck with the lowest number of cleanups { if (truckList.FindIndex(x => x.TruckTypeId == truck.typeId) != -1) //Checking for the best truck available { if (truck.numOfCleanups < bestTruck.numOfCleanups) //Is available && better { bestTruck = truck; } else if (truckList.FindIndex(x => x.TruckTypeId == bestTruck.typeId) == -1) //If bestfit isn't available { bestTruck = truck; } } } if (bestTruck.typeId == bestFit.First().typeId&& truckList.FindIndex(x => x.TruckTypeId == bestTruck.typeId) == -1) { //If we are here its mean that we still have area to clean but not enough trucks available return(0); } indexOfbesttruck = truckList.FindIndex(x => x.TruckTypeId == bestTruck.typeId); //Getting the index of the truck we want to update truckToUpdate = truckList.ElementAt(indexOfbesttruck); truckList.RemoveAt(indexOfbesttruck); //Removing the best truck from the available list truckToUpdate.AreaId = area.AreaId; TruckData truckData = DbTruckToTruckData(truckToUpdate); this.UpdateTruck(truckData); //Adding the right areaId } return(1); } catch (Exception ex) { throw ErrorHandler.Handle(ex, this); } }
public List <TruckCleanUp> TruckAllocationToRegion(LUT_Area area)// return the smallest trcuk with the lowest number of cleanups { List <LUT_TruckType> listOfTruckType; DateTime lastThreeMonths = DateTime.Now.AddDays(-90); // Three months back List <Building> areaBuildings; List <Bin> buildingBins; List <WasteTransferLog> buildingWasteTransferLog; double areaAvg, sumOfTrash = 0; try { //ask lior about using the connections instead of line 269 areaBuildings = this.db.Buildings.Where(x => x.AreaId == area.AreaId).ToList(); //List of all the buildings in this area buildingBins = this.db.Bins.Where(x => areaBuildings.FindIndex(f => f.BuildingId == x.BuildingId) != -1).ToList(); //List of all the bins in this area buildingWasteTransferLog = this.db.WasteTransferLogs .Where(x => x.CreatedDate >= lastThreeMonths && buildingBins.FindIndex(f => f.BinId == x.BinId) != -1).ToList(); //buildingWasteTransferLog from the last three months //From this area foreach (WasteTransferLog wtl in buildingWasteTransferLog) { sumOfTrash += wtl.TransferedCapacity; } areaAvg = sumOfTrash / buildingWasteTransferLog.Count(); //sumOfTrash / number of cleanup(Log size) listOfTruckType = this.db.LUT_TruckType.ToList().OrderBy(x => x.Capacity).ToList(); //All truckType order by capacity List <TruckCleanUp> truckCleanupList = new List <TruckCleanUp>(); TruckCleanUp currentTrcuk = new TruckCleanUp(); foreach (LUT_TruckType ltt in listOfTruckType)//Calculating for each truck its numberofcleanups { currentTrcuk.typeId = ltt.TruckTypeId; currentTrcuk.numOfCleanups = (int)Math.Ceiling(areaAvg / ltt.Capacity);//Round up numOfCleanups truckCleanupList.Add(currentTrcuk); } return(truckCleanupList); } catch (Exception ex) { throw ErrorHandler.Handle(ex, this); } }