private long SaveRoute(CalculationManager.Node destination, forsendelse rute, pakke pakke) { if (destination.Previous == null) { rute.pakke = pakke; rute.StartBy = destination.By.CityId; return DataManager.OpretRute(rute); } if (rute.SlutBy == null) rute.SlutBy = destination.By.CityId; var sd = destination.Previous.Ruter.FirstOrDefault(p => p.From == destination.Previous && p.To == destination); var forsendelse = new forsendelsesRute(); forsendelse.StartBy = destination.Previous.By.CityId; forsendelse.SlutBy = destination.By.CityId; forsendelse.Price = Convert.ToUInt32(sd.Route.Pris); forsendelse.TransportTime = sd.Route.Rute.Time; forsendelse.TransportType = (Int64)sd.Route.TransportType; rute.forsendelsesRute.Add(forsendelse); return SaveRoute(destination.Previous, rute, pakke); }
public void TestGetRoutes() { var city = new @by() {Active = true, CityId = 2, Id = 2, Name = "Cairo"}; var package = new pakke() { SizeDepth = 30, SizeHight = 50, SizeWidth = 30, Weight = 4500, forsendelse = new List<forsendelse>() { new forsendelse() { forsendelsesType = new List<forsendelsesType>() { new forsendelsesType(){packetTypeId = 1} } } } }; foreach (var externalService in _externalServices) { var routes = externalService.GetRoute(city, package); Assert.IsTrue(routes != null); } }
public IEnumerable<Route> GetRoutes(by by, pakke pakke) { var result = new List<Route>(); foreach (var externalServicesApi in _externalApis) { result.AddRange(externalServicesApi.GetRoute(by, pakke)); } return result; }
public double BeregnPris(pakke sendtPakke, float multiplier) { var typer = FindType(sendtPakke.SizeDepth, sendtPakke.SizeHight, sendtPakke.SizeWidth); var priser = DataManager.HentPakkePriser(); var pris = priser.FindAll( p => typer.Contains(p.DimentionsNavn) && p.FromWeight <= sendtPakke.Weight && sendtPakke.Weight <= p.ToWeight).Select(t => t.Price).ToList(); pris.Sort(); return pris.First() * multiplier; }
public Node CalculateRouteTime(by source, by target, IList<IExternalServicesApi> externalServicesApis, pakke sendtPakke, float multiplier) { _externalServicesApis = externalServicesApis; _byliste = DataManager.HentAktiveredeByer().ToList(); foreach (var externalServicesApi in externalServicesApis) { _byliste.AddRange(externalServicesApi.GetCities().Select(e => _byliste.FirstOrDefault(p => p != null && p.CityId == e.CityId) == null ? e : null)); } _byliste.RemoveAll(p => p == null); var result = Dijstra(source, target, Politik.Tid, sendtPakke, multiplier); return result; }
public Tuple<CalculationManager.Node, long> CalculateRouteWeight(by fra, by til, int height, int depth, int width, int weight, List<long> pakkeTypeId) { var pakke = new pakke { SizeDepth = depth, SizeHight = height, SizeWidth = width, Weight = weight }; var result = _calculationManager.CalculateRouteWeight(fra, til, _externalApis, pakke, DataManager.HentPakkeType(pakkeTypeId)); long forsendelsesId = SaveRoute(result, new forsendelse(), pakke); return new Tuple<CalculationManager.Node, long> (result, forsendelsesId); }
public override IEnumerable<Route> GetRoute(by by, pakke pakke) { var result = new List<Route>() { new Route() { Pris = 20, Rute = new rute() { EndCity = 1, StartCity = 3, Id = 5, Time = 5000 }, TransportType = TransportType.TELSTAR }, new Route() { Pris = 20, Rute = new rute() { EndCity = 2, StartCity = 5, Id = 6, Time = 12000 }, TransportType = TransportType.EIC }, new Route() { Pris = 20, Rute = new rute() { EndCity = 2, StartCity = 8, Id = 9, Time = 12000 }, TransportType = TransportType.Oceanic }, }; return result; }
public void TestDijkstra() { var by1 = DataManager.HentAktiveredeByer().First(); var manager = ManagerFactory.GetRouteManager(); //List<IExternalServicesApi> w = new List<IExternalServicesApi>(); //w.Add(new TestThingie()); var pakke = new pakke() {SizeDepth = 30, SizeHight = 50, SizeWidth = 30, Weight = 4500}; for (int i = 1; i < 2 ; i++) { var by2 = DataManager.HentAktiveredeByer().ToArray()[i]; var result = manager.CalculateRouteWeight(by1, by2, pakke.SizeWidth, pakke.SizeDepth, pakke.SizeHight, pakke.Weight, new List<long>(){1}); if (result != null) { Debug.WriteLine(String.Format("Fra: {0} til {1}, det kostede i alt {2}", by1.Name, by2.Name, result.Item1.Distance)); printRute(result.Item1); } } }
public virtual IEnumerable<Route> GetRoute(by by, pakke pakke) { var routes = new List<Route>(); var queryString = HttpUtility.ParseQueryString(string.Empty); queryString["date"] = DateTime.Now.AddDays(1).ToString("o"); queryString["measurements"] = string.Format("{0}x{1}x{2}", pakke.SizeDepth, pakke.SizeHight, pakke.SizeWidth); queryString["weight"] = (pakke.Weight * 0.001).ToString(CultureInfo.InvariantCulture); queryString["requirements"] = GetRequirements(pakke.forsendelse); var queryParams = queryString.ToString(); //var result = HttpClient.GetAsync(uriBuilder.ToString()).Result; var url = string.Format("api/cities/{0}/routes?{1}", by.CityId, queryParams); var result = HttpClient.GetAsync(url).Result; if (result.IsSuccessStatusCode) { var jsonResponse = result.Content.ReadAsStringAsync().Result; var convertedResponse = JsonConvert.DeserializeObject<RouteResponseContract>(jsonResponse); routes = convertedResponse.routes.Select( route => new Route() { TransportType = GetTransportType(), Pris = (route.price / 100), Rute = new rute() { Time = route.duration, EndCity = route.destination, StartCity = by.CityId } }).ToList(); } // else // { // throw new WebException(string.Format("GetRoutes from {0} failed", HttpClient.BaseAddress)); // } return routes; }
public virtual IEnumerable<Route> GetRoute(by by, pakke pakke) { return new[] { new Route { TransportType = TransportType.EIC, Pris = 11, Rute = new rute { Time = 8, StartCity = 1, EndCity = 2 } } }; }
private Node Dijstra(by source, by target, Politik politik, pakke sendtPakke, float multiplier) { var queue = new HeapPriorityQueue<Node>(_byliste.Count * 2); _nodes = new List<Node>(); Node targetBy = null; foreach (var by in _byliste) { var node = new Node { By = by }; if (by.CityId == target.CityId) { targetBy = node; } node.Distance = by.CityId == source.CityId ? 0 : double.MaxValue; _nodes.Add(node); queue.Enqueue(node, node.Distance); } while (queue.Any()) { var node = queue.Dequeue(); if (node == targetBy && node.Distance != double.MaxValue) return node; GetRoutes(node, politik, sendtPakke, multiplier); foreach (var neighbour in getNeighbourghNodes(node, queue)) { if (neighbour == null || !queue.Contains(neighbour)) continue; var dist = node.Distance + DistanceBetween(node, neighbour, politik); if (dist < neighbour.Distance) { neighbour.Distance = dist; neighbour.Previous = node; queue.UpdatePriority(neighbour, dist); } } } return null; }
private void GetRoutes(Node node, Politik politik, pakke sendtPakke, float multiplier) { if (node.Ruter != null && node.Ruter.Any()) return; var ruter = new List<Edge>(); foreach (var rute in _externalServicesApis.Select(enemy => enemy.GetRoute(node.By, sendtPakke))) { ruter.AddRange(rute.Select(route => new Edge { From = _nodes.FirstOrDefault(p => p.By.CityId == route.Rute.StartCity), To = _nodes.FirstOrDefault(p => p.By.CityId == route.Rute.EndCity), Weight = Politik.Pris == politik ? route.Pris : route.Rute.Time, Route = route })); } var ownRoutes = DataManager.HentRuter(node.By).Select(r => new Edge { From = _nodes.FirstOrDefault(p => p.By.CityId == r.Rute.StartCity), To = _nodes.FirstOrDefault(p => p.By.CityId == r.Rute.EndCity), Route = r, Weight = politik == Politik.Pris ? BeregnPris(sendtPakke, multiplier) : r.Rute.Time }).ToList(); ruter.AddRange(ownRoutes); var prunedList = new List<Edge>(); foreach (var outerRoute in ruter) { if (outerRoute.From.By.CityId != node.By.CityId) { continue; } Boolean update = true; double minWeight = outerRoute.Weight; foreach (var innerRoute in ruter) { if (update && outerRoute.From == innerRoute.From && outerRoute.To == innerRoute.To && innerRoute.Weight < minWeight) { update = false; } } if (update) prunedList.Add(outerRoute); } prunedList.FindAll(r => r.Route.TransportType == TransportType.Oceanic) .ForEach(rout => rout.Route.Pris = BeregnPris(sendtPakke, multiplier)); node.Ruter = prunedList; }
public GetRoutesContract GetRoute( [FromUri] int id, DateTime date, string measurements, double weight, string requirements = null) { var routesResult = new GetRoutesContract(); List<RouteDTO> routes = new List<RouteDTO>(); try { var splittedMeasurements = measurements.Split('x'); var depth = (int) Convert.ToDouble(splittedMeasurements[0]); var width = (int) Convert.ToDouble(splittedMeasurements[1]); var height = (int) Convert.ToDouble(splittedMeasurements[2]); var package = new pakke { SizeDepth = depth, SizeWidth = width, SizeHight = height, Weight = (int)(weight * 1000), forsendelse = new List<forsendelse> { new forsendelse { AfsendelsesDato = date, forsendelsesType = GetForsendelsesType(requirements) } } }; var ruter = DataManager.HentRuter(new @by {CityId = id}); var calculationManager = new CalculationManager(); var forsendelsesTyper = GetForsendelsesType(requirements).Select(x => x.packetTypeId.Value).ToList(); if (!forsendelsesTyper.Any()) { forsendelsesTyper = new List<long>() { Int32.MaxValue }; } var multiplier = DataManager.HentPakkeType(forsendelsesTyper); routes = new List<RouteDTO>(); foreach (var route in ruter) { routes.Add(new RouteDTO { destination = route.Rute.EndCity, duration = route.Rute.Time, price = 100 * Convert.ToInt32(calculationManager.BeregnPris(package, multiplier)) }); } } catch (Exception) { } routesResult.Routes = routes; return routesResult; }