public IList<Route> Build(IList<Depot> deports, VehicleInfo venicleInfo, List<Request> requests) { BruteForceCounter.MaximumOption = 0; BruteForceCounter.CurrentOption = 0; BruteForceCounter.MinPathDistance = 0; BruteForceCounter.MinPath = null; while (true) { var routes = new List<Route>(); var unservicedRequests = requests; var venicles = CreateVenicles(deports); foreach (var venicle in venicles) { var route = BuildRoute(venicle, unservicedRequests); if (route == null) continue; unservicedRequests = unservicedRequests.Except(route.Requests).ToList(); routes.Add(route); } BruteForceCounter.PossibleRequests = BruteForceCounter.PossibleRequests.Concat(routes.SelectMany(r => r.Requests)).ToList(); if (unservicedRequests.Count() == requests.Count()) if (BruteForceCounter.MinPath == null) throw new ImpossibleRouteException("Can service all requests", requests.Except(BruteForceCounter.PossibleRequests).ToList()); else return BruteForceCounter.MinPath; BruteForceCounter.MaximumOption++; BruteForceCounter.CurrentOption = 0; var totalDistance = routes.Sum(r => r.GetTotalDistance()); if (unservicedRequests.Any() || !(BruteForceCounter.MinPathDistance > totalDistance || BruteForceCounter.MinPath == null)) continue; BruteForceCounter.MinPathDistance = totalDistance; BruteForceCounter.MinPath = routes; } }
public Route(VehicleInfo venicleInfo) { _venicleInfo = venicleInfo; Requests = new List<Request>(); }
public static ExampleObject LoadProblem(string filename) { String _name = ""; int _num_depots = 0; int num_capacities = 0; int _num_visits = 0; int _num_locations = 0; int _num_vehicles = 0; int _capacities = 0; double CUT_OFF_TIME = 0.5; int requestEnd = 0; String[] linesOfFile = {}; try { linesOfFile = File.ReadAllLines(filename + ".vrp"); } catch (Exception e) { Console.WriteLine("EXCEPTION: " + e.Message); } String tempValue = ""; String[] aboveData; String[] separators = { ":" }; for (int i = 0; i < linesOfFile.Count(); i++) { aboveData = linesOfFile[i].Split(separators, StringSplitOptions.RemoveEmptyEntries); if(aboveData.Count() > 1) tempValue = aboveData[1]; switch (aboveData[0]) { case "NAME": _name = tempValue; break; case "NUM_DEPOTS": _num_depots = Int32.Parse(tempValue); break; case "NUM_CAPACITIES": num_capacities = Int32.Parse(tempValue); break; case "NUM_VISITS": _num_visits = Int32.Parse(tempValue); break; case "NUM_LOCATIONS": _num_locations = Int32.Parse(tempValue); break; case "NUM_VEHICLES": _num_vehicles = Int32.Parse(tempValue); break; case "CAPACITIES": _capacities = Int32.Parse(tempValue); break; default: continue; } } //Parsing DATA SECTION IList<Depot> listOfDepots = new List<Depot>(); List<Request> listOfRequests = new List<Request>(); for (int j = 0; j < linesOfFile.Count(); j++) { if (linesOfFile[j] == "DATA_SECTION") { for(int z = 1; z <= linesOfFile.Count() - (j+1); z++){ switch(linesOfFile[j+z].ToString()){ case "DEPOTS": for (int i = 1; i <= _num_depots; i++) { Depot newDepot = new Depot(); newDepot.Name = linesOfFile[j + z + i].ToString(); newDepot.Vehicles = _num_vehicles; listOfDepots.Add(newDepot); } break; case "DEMAND_SECTION": String[] demandSectionLine; String[] demandSectionSeparator = {" "}; for (int i = 1; i <= _num_visits; i++) { Request newRequest = new Request(); demandSectionLine = linesOfFile[j + z + i].Split(demandSectionSeparator, StringSplitOptions.RemoveEmptyEntries); newRequest.Id = Int32.Parse(demandSectionLine[0]); newRequest.Size = -Int32.Parse(demandSectionLine[1]); listOfRequests.Add(newRequest); } break; case "LOCATION_COORD_SECTION": String[] locationCoordSectionLine; String[] locationCoordSectionSeparator = { " " }; for (int i = 1; i <= _num_depots; i++) { locationCoordSectionLine = linesOfFile[j + z + i].Split(locationCoordSectionSeparator, StringSplitOptions.RemoveEmptyEntries); Point depoLocation = new Point(double.Parse(locationCoordSectionLine[1]), double.Parse(locationCoordSectionLine[2])); listOfDepots.ElementAt(Int32.Parse(locationCoordSectionLine[0])).Location = depoLocation; } for (int i = (_num_depots+1); i <= _num_visits+1; i++) { locationCoordSectionLine = linesOfFile[j + z + i].Split(locationCoordSectionSeparator, StringSplitOptions.RemoveEmptyEntries); Point requestLocation = new Point(double.Parse(locationCoordSectionLine[1]), double.Parse(locationCoordSectionLine[2])); listOfRequests.ElementAt(i - 2).Location = requestLocation; } break; case "DEPOT_LOCATION_SECTION": //f*****g skip it break; case "VISIT_LOCATION_SECTION": //no f*****g clue dude break; case "DURATION_SECTION": String[] durationSectionLine; String[] durationSectionSeparator = { " " }; for (int i = 1; i <= _num_visits; i++) { durationSectionLine = linesOfFile[j + z + i].Split(durationSectionSeparator, StringSplitOptions.RemoveEmptyEntries); listOfRequests.ElementAt(i - 1).Unload = Int32.Parse(durationSectionLine[1]); } break; case "DEPOT_TIME_WINDOW_SECTION": String[] depotTimeWidnowSectionLine; String[] depotTimeWidnowSectionSeparator = { " " }; requestEnd = 0; for (int i = 1; i <= _num_depots; i++) { depotTimeWidnowSectionLine = linesOfFile[j + z + i].Split(depotTimeWidnowSectionSeparator, StringSplitOptions.RemoveEmptyEntries); listOfDepots.ElementAt(i - 1).Start = 0; listOfDepots.ElementAt(i - 1).End = Int32.Parse(depotTimeWidnowSectionLine[2]); requestEnd = Int32.Parse(depotTimeWidnowSectionLine[2]); } for (int i = 1; i <= _num_visits; i++) { listOfRequests.ElementAt(i - 1).End = requestEnd; } break; case "TIME_AVAIL_SECTION": String[] timeAvailSectionLine; String[] timeAvailSectionSeparator = { " " }; for (int i = 1; i <= _num_visits; i++) { timeAvailSectionLine = linesOfFile[j + z + i].Split(timeAvailSectionSeparator, StringSplitOptions.RemoveEmptyEntries); listOfRequests.ElementAt(i - 1).Start = Int32.Parse(timeAvailSectionLine[1]); // ZMIANA TIME AVAILABLE if (listOfRequests.ElementAt(i - 1).Start > (CUT_OFF_TIME * listOfRequests.ElementAt(i - 1).End)) listOfRequests.ElementAt(i - 1).Start = 0; } break; default: continue; } } } } VehicleInfo vehicleInfo = new VehicleInfo(); vehicleInfo.Speed = 1; vehicleInfo.Capacity = _capacities; ExampleObject Example = new ExampleObject(); Example.Depots = listOfDepots; Example.vehicleInfo = vehicleInfo; Example.Requests = listOfRequests; return Example; }
public static ExampleObject LoadProblemFromString(string file) { String _name = ""; int _num_depots = 0; int num_capacities = 0; int _num_visits = 0; int _num_locations = 0; int _num_vehicles = 0; int _capacities = 0; double CUT_OFF_TIME = 0.5; int requestEnd = 0; String[] linesOfFile = { }; linesOfFile = file.Split('\n'); String tempValue = ""; String[] aboveData; String[] separators = { ":" }; for (int i = 0; i < linesOfFile.Count(); i++) { aboveData = linesOfFile[i].Split(separators, StringSplitOptions.RemoveEmptyEntries); if (aboveData.Count() > 1) { tempValue = aboveData[1]; } switch (aboveData[0]) { case "NAME": _name = tempValue; break; case "NUM_DEPOTS": _num_depots = Int32.Parse(tempValue); break; case "NUM_CAPACITIES": num_capacities = Int32.Parse(tempValue); break; case "NUM_VISITS": _num_visits = Int32.Parse(tempValue); break; case "NUM_LOCATIONS": _num_locations = Int32.Parse(tempValue); break; case "NUM_VEHICLES": _num_vehicles = Int32.Parse(tempValue); break; case "CAPACITIES": _capacities = Int32.Parse(tempValue); break; default: continue; } } //Parsing DATA SECTION IList <Depot> listOfDepots = new List <Depot>(); List <Request> listOfRequests = new List <Request>(); for (int j = 0; j < linesOfFile.Count(); j++) { if (linesOfFile[j] == "DATA_SECTION") { for (int z = 1; z <= linesOfFile.Count() - (j + 1); z++) { switch (linesOfFile[j + z].ToString()) { case "DEPOTS": for (int i = 1; i <= _num_depots; i++) { Depot newDepot = new Depot(); newDepot.Name = linesOfFile[j + z + i].ToString(); newDepot.Vehicles = _num_vehicles; listOfDepots.Add(newDepot); } break; case "DEMAND_SECTION": String[] demandSectionLine; String[] demandSectionSeparator = { " " }; for (int i = 1; i <= _num_visits; i++) { Request newRequest = new Request(); demandSectionLine = linesOfFile[j + z + i].Split(demandSectionSeparator, StringSplitOptions.RemoveEmptyEntries); newRequest.Id = Int32.Parse(demandSectionLine[0]); newRequest.Size = -Int32.Parse(demandSectionLine[1]); listOfRequests.Add(newRequest); } break; case "LOCATION_COORD_SECTION": String[] locationCoordSectionLine; String[] locationCoordSectionSeparator = { " " }; for (int i = 1; i <= _num_depots; i++) { locationCoordSectionLine = linesOfFile[j + z + i].Split(locationCoordSectionSeparator, StringSplitOptions.RemoveEmptyEntries); Point depoLocation = new Point(double.Parse(locationCoordSectionLine[1]), double.Parse(locationCoordSectionLine[2])); listOfDepots.ElementAt(Int32.Parse(locationCoordSectionLine[0])).Location = depoLocation; } for (int i = (_num_depots + 1); i <= _num_visits + 1; i++) { locationCoordSectionLine = linesOfFile[j + z + i].Split(locationCoordSectionSeparator, StringSplitOptions.RemoveEmptyEntries); Point requestLocation = new Point(double.Parse(locationCoordSectionLine[1]), double.Parse(locationCoordSectionLine[2])); listOfRequests.ElementAt(i - 2).Location = requestLocation; } break; case "DEPOT_LOCATION_SECTION": //f*****g skip it break; case "VISIT_LOCATION_SECTION": //no f*****g clue dude break; case "DURATION_SECTION": String[] durationSectionLine; String[] durationSectionSeparator = { " " }; for (int i = 1; i <= _num_visits; i++) { durationSectionLine = linesOfFile[j + z + i].Split(durationSectionSeparator, StringSplitOptions.RemoveEmptyEntries); listOfRequests.ElementAt(i - 1).Unload = Int32.Parse(durationSectionLine[1]); } break; case "DEPOT_TIME_WINDOW_SECTION": String[] depotTimeWidnowSectionLine; String[] depotTimeWidnowSectionSeparator = { " " }; requestEnd = 0; for (int i = 1; i <= _num_depots; i++) { depotTimeWidnowSectionLine = linesOfFile[j + z + i].Split(depotTimeWidnowSectionSeparator, StringSplitOptions.RemoveEmptyEntries); listOfDepots.ElementAt(i - 1).Start = 0; listOfDepots.ElementAt(i - 1).End = Int32.Parse(depotTimeWidnowSectionLine[2]); requestEnd = Int32.Parse(depotTimeWidnowSectionLine[2]); } for (int i = 1; i <= _num_visits; i++) { listOfRequests.ElementAt(i - 1).End = requestEnd; } break; case "TIME_AVAIL_SECTION": String[] timeAvailSectionLine; String[] timeAvailSectionSeparator = { " " }; for (int i = 1; i <= _num_visits; i++) { timeAvailSectionLine = linesOfFile[j + z + i].Split(timeAvailSectionSeparator, StringSplitOptions.RemoveEmptyEntries); listOfRequests.ElementAt(i - 1).Start = Int32.Parse(timeAvailSectionLine[1]); //MADAFAKA ZMIANA TIME AVAILABLE if (listOfRequests.ElementAt(i - 1).Start > (CUT_OFF_TIME * listOfRequests.ElementAt(i - 1).End)) { listOfRequests.ElementAt(i - 1).Start = 0; } } break; default: continue; } } } } VehicleInfo vehicleInfo = new VehicleInfo(); vehicleInfo.Speed = 1; vehicleInfo.Capacity = _capacities; ExampleObject Example = new ExampleObject(); Example.Depots = listOfDepots; Example.vehicleInfo = vehicleInfo; Example.Requests = listOfRequests; return(Example); }
private void TryServe(IList<Depot> Depots, VehicleInfo vehicleInfo, List<Request> requests) { try { var builder = new RouteBuilder(vehicleInfo); var routes = builder.Build(Depots, vehicleInfo, requests).ToList(); String foundRoutes = ""; String totalDistance = ""; foundRoutes += string.Format("Found {0} routes", routes.Count()) + Environment.NewLine + Environment.NewLine; foreach (var route in routes) { foundRoutes += string.Join(" -> " + Environment.NewLine, route.GetTimeTable().Select(checkPoint => "[Ar = " + checkPoint.ArrivalTime + " Loc = " + checkPoint.Location.X + " " + checkPoint.Location.Y + "[" + checkPoint.LocationID + "]" + " Route ID = " + route.Venicle.Id + "]")) + Environment.NewLine; foundRoutes += "vehicle distance: " + route.GetTotalDistance() + Environment.NewLine; foundRoutes += Environment.NewLine; } Console.WriteLine(foundRoutes); totalDistance += "Total distance: " + routes.Sum(r => r.GetTotalDistance()) + Environment.NewLine; totalDistance += Environment.NewLine; Console.WriteLine(totalDistance); } catch (ImpossibleRouteException exception) { String failedServeRequest = ""; String routesWithoutRequests = ""; ; failedServeRequest = "Can not serve requests:" + Environment.NewLine; foreach (var request in exception.ImpossibleRequests) { failedServeRequest += "Loc = " + request.Location.X + " " + request.Location.Y + Environment.NewLine; } failedServeRequest += Environment.NewLine; Console.WriteLine(failedServeRequest); routesWithoutRequests += "Routes without these requests:" + Environment.NewLine; Console.WriteLine(routesWithoutRequests); TryServe(Depots, vehicleInfo, requests.Except(exception.ImpossibleRequests).ToList()); } catch (Exception exception) { Console.WriteLine(exception.Message); } }
public RouteBuilder(VehicleInfo venicleInfo) { _venicleInfo = venicleInfo; }