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(); RoutesResultTextBox.Text += string.Format("Found {0} routes", routes.Count()) + Environment.NewLine + Environment.NewLine; foreach (var route in routes) { RoutesResultTextBox.Text += string.Join(" -> " + Environment.NewLine, route.GetTimeTable().Select(checkPoint => "[Ar = " + checkPoint.ArrivalTime + " Loc = " + checkPoint.Location.X + " " + checkPoint.Location.Y + "]")) + Environment.NewLine; RoutesResultTextBox.Text += "vehicle distance: " + route.GetTotalDistance() + Environment.NewLine; RoutesResultTextBox.Text += Environment.NewLine; } RoutesResultTextBox.Text += "Total distance: " + routes.Sum(r => r.GetTotalDistance()) + Environment.NewLine; RoutesResultTextBox.Text += Environment.NewLine; } catch (ImpossibleRouteException exception) { RoutesResultTextBox.Text = "Can not serve requests:" + Environment.NewLine; foreach (var request in exception.ImpossibleRequests) { RoutesResultTextBox.Text += "Loc = " + request.Location.X + " " + request.Location.Y + Environment.NewLine; } RoutesResultTextBox.Text += Environment.NewLine; RoutesResultTextBox.Text += "Routes without these requests:" + Environment.NewLine; TryServe(Depots, vehicleInfo, requests.Except(exception.ImpossibleRequests).ToList()); } catch (Exception exception) { RoutesResultTextBox.Text = exception.Message; } }
private void Button_Click(object sender, RoutedEventArgs e) { var DepotsRegex = new Regex("location_x: (?<location_x>.+); location_y: (?<location_y>.+); start: (?<start>.+); end: (?<end>.+); vehicles: (?<vehicles>.+);"); var DepotsRegexResults = DepotsRegex.Matches(DeportsTextBox.Text); var Depots = new List<Depot>(); for (var i = 0; i < DepotsRegexResults.Count; i++) { Depots.Add(new Depot { Name = "D_" + (i + 1), Location = new Point( double.Parse(DepotsRegexResults[i].Groups["location_x"].Value), double.Parse(DepotsRegexResults[i].Groups["location_y"].Value)), Start = double.Parse(DepotsRegexResults[i].Groups["start"].Value), End = double.Parse(DepotsRegexResults[i].Groups["end"].Value), Venicles = int.Parse(DepotsRegexResults[i].Groups["vehicles"].Value) }); } var vehicleInfoRegex = new Regex("Speed: (?<Speed>.+); Capacity: (?<Capacity>.+);"); var vehicleInfoRegexResult = vehicleInfoRegex.Match(VenicleInfoTextBox.Text); var vehicleInfo = new VehicleInfo { Speed = double.Parse(vehicleInfoRegexResult.Groups["Speed"].Value), Capacity = double.Parse(vehicleInfoRegexResult.Groups["Capacity"].Value) }; var requestsRegex = new Regex("location_x: (?<location_x>.+); location_y: (?<location_y>.+); start: (?<start>.+); end: (?<end>.+); unload: (?<unload>.+); Size: (?<Size>.+);"); var requestsRegexResults = requestsRegex.Matches(RequestsTextBox.Text); var requests = new List<Request>(); for (var i = 0; i < requestsRegexResults.Count; i++) { requests.Add(new Request { Id = i + 1, Location = new Point( double.Parse(requestsRegexResults[i].Groups["location_x"].Value), double.Parse(requestsRegexResults[i].Groups["location_y"].Value)), Start = double.Parse(requestsRegexResults[i].Groups["start"].Value), End = double.Parse(requestsRegexResults[i].Groups["end"].Value), Unload = int.Parse(requestsRegexResults[i].Groups["unload"].Value), Size = int.Parse(requestsRegexResults[i].Groups["Size"].Value) }); } RoutesResultTextBox.Text = string.Empty; TryServe(Depots, vehicleInfo, requests); }
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 RouteBuilder(VehicleInfo venicleInfo) { _venicleInfo = venicleInfo; }