public async Task <IActionResult> GetForAll() { // get stores var stores = this.storeFinder.GetStoresToServe().ToList(); var fleets = this.droneService.GetDroneFleets().ToList(); var clientAddresses = await this.locationFinder.GetAllClientLocationAsync(); var result = new List <RouteSettings>(); if (!stores.AnySafe() || !fleets.AnySafe() || !clientAddresses.AnySafe()) { return(null); } var calc = new DistanceCalculator(); var fleetStoreDistanceArr = new List <List <double> >(); var line = new LinkedList <double>(); foreach (var fleet in fleets) { line.Clear(); foreach (var store in stores) { line.AddLast(calc.Calculate(fleet.Coordinates, store.Coordinates)); } fleetStoreDistanceArr.Add(line.ToList()); } var ways = new List <BestWay>(); var distance = new List <double>(); var bestWay = new BestWay(); foreach (var client in clientAddresses) { distance.Clear(); foreach (var store in stores) { distance.Add(calc.Calculate(client.Coordinates, store.Coordinates)); } bestWay = new BestWay { Distance = double.MaxValue, FleetIndex = 0, StoreIndex = 0, Client = client }; var availableDrones = await this.droneService.GetAvailableDronesAsync(); for (var i = 0; i < fleetStoreDistanceArr.Count; i++) { // for loop bellow fleetStoreDistanceArr line length will be the same for all lines, // so we don't need to calculate it for every line for (var j = 0; j < fleetStoreDistanceArr[0].Count; j++) { if (availableDrones.Any( x => x.AddressLine.Equals( fleets[i].AddressLine, StringComparison.InvariantCultureIgnoreCase))) { if (fleetStoreDistanceArr[i][j] + distance[j] < bestWay.Distance) { bestWay.Distance = fleetStoreDistanceArr[i][j] + distance[j]; bestWay.FleetIndex = i; bestWay.StoreIndex = j; } } } } ways.Add(bestWay); } foreach (var way in ways) { result.Add( BuildRouteHelper.BuildRouteSettings( calc, new RouteDistance { LocationFrom = fleets[way.FleetIndex].AddressLine, LocationFromAddress = fleets[way.FleetIndex], LocationTo = stores[way.StoreIndex].AddressLine, LocationToAddress = stores[way.StoreIndex] }, new RouteDistance { LocationFrom = stores[way.StoreIndex].AddressLine, LocationFromAddress = stores[way.StoreIndex], LocationTo = way.Client.AddressLine, LocationToAddress = way.Client }, way.Distance)); } return(this.Ok(result)); }
public async Task <IActionResult> Get(string address) { // get stores var stores = this.storeFinder.GetStoresToServe().ToList(); var fleets = this.droneService.GetAvailableDrones().ToList(); var clientAddress = await this.locationFinder.GetAddressCoordinatesAsync(address); if (!stores.AnySafe() || !fleets.AnySafe() || clientAddress == null) { return(null); } var calc = new DistanceCalculator(); var fleetStoreDistanceArr = new List <List <double> >(); var line = new LinkedList <double>(); foreach (var fleet in fleets) { line.Clear(); foreach (var store in stores) { line.AddLast(calc.Calculate(fleet.Coordinates, store.Coordinates)); } fleetStoreDistanceArr.Add(line.ToList()); } var distance = new List <double>(); foreach (var store in stores) { distance.Add(calc.Calculate(clientAddress.Coordinates, store.Coordinates)); } var bestWay = new BestWay { Distance = double.MaxValue, FleetIndex = 0, StoreIndex = 0, Client = clientAddress }; for (var i = 0; i < fleetStoreDistanceArr.Count; i++) { // for loop bellow fleetStoreDistanceArr line length will be the same for all lines, // so we don't need to calculate it for every line for (var j = 0; j < fleetStoreDistanceArr[0].Count; j++) { if (fleetStoreDistanceArr[i][j] + distance[j] < bestWay.Distance) { bestWay.Distance = fleetStoreDistanceArr[i][j] + distance[j]; bestWay.FleetIndex = i; bestWay.StoreIndex = j; } } } return(this.Ok(BuildRouteHelper.BuildRouteSettings( calc, new RouteDistance { LocationFrom = fleets[bestWay.FleetIndex].AddressLine, LocationFromAddress = fleets[bestWay.FleetIndex], LocationTo = stores[bestWay.StoreIndex].AddressLine, LocationToAddress = stores[bestWay.StoreIndex] }, new RouteDistance { LocationFrom = stores[bestWay.StoreIndex].AddressLine, LocationFromAddress = stores[bestWay.StoreIndex], LocationTo = bestWay.Client.AddressLine, LocationToAddress = bestWay.Client }, bestWay.Distance))); }