public async Task <IActionResult> GetForDriver(int id) { if (!User.Identity.IsAuthenticated) { return(Challenge()); } Driver driver = await _driverRepository.GetAsync(id); AuthorizationResult authResult = await _authorizationService.AuthorizeAsync(User, driver, "DriverInfoPolicy"); if (!authResult.Succeeded) { return(Forbid()); } IEnumerable <Leg> legs = await _legRepository.ListForDriverAsync(id); foreach (Leg leg in legs) { leg.Driver = null; // to prevent self-referencing loops during serialization } return(Ok(legs)); }
public async Task ComputeDriverStatistics(int id) { if (driverStats == null) { driverStats = new Dictionary <int, DriverStatisticResults>(); } Driver driver = await _driverRepository.GetAsync(id); if (driver == null) { return; } IEnumerable <Leg> legs = await _legRepository.ListForDriverAsync(id); DriverStatisticResults results = new DriverStatisticResults(); results.DriverID = id; results.Pickups = legs.Select(leg => leg.NumOfPassengersPickedUp).Sum(); results.MilesDriven = legs.Select(leg => leg.Distance).Sum(); if (await _legRepository.CountDriverLegsAsync(id) > 0) { results.AveragePickupDelay = legs.Select(leg => leg.StartTime.Subtract(leg.PickupRequestTime.GetValueOrDefault(leg.StartTime)).TotalMinutes).Average(); } results.TotalFares = legs.Select(leg => leg.Fare * leg.NumOfPassengersAboard).Sum(); results.TotalCosts = legs.Select(leg => leg.GetTotalFuelCost()).Sum(); driverStats[id] = results; }
// GET: Drivers/Details/5 public async Task <IActionResult> Details(int?id) { var driver = await _driverRepository.GetAsync(id.Value); if (driver == null) { return(NotFound()); } if (!User.Identity.IsAuthenticated) { return(Challenge()); } var authResult = await _authorizationService.AuthorizeAsync(User, driver, "DriverInfoPolicy"); if (!authResult.Succeeded) { return(Forbid()); } await _driverStatisticsService.ComputeDriverStatistics(id.Value); // total pickups int pickups = _driverStatisticsService.GetPickupsBy(id.Value); ViewData["Pickups"] = pickups + " passenger pickup" + (pickups == 1 ? "" : "s"); // total miles driven decimal milesDriven = _driverStatisticsService.GetMilesDrivenBy(id.Value); ViewData["MilesDriven"] = milesDriven + " mile" + ((milesDriven > 0 && milesDriven < 1) ? "" : "s") + " driven"; // average pickup delay in minutes if (_driverStatisticsService.GetAveragePickupDelayBy(id.Value).HasValue) { double avgPickupDelay = _driverStatisticsService.GetAveragePickupDelayBy(id.Value).Value; ViewData["AveragePickupDelay"] = avgPickupDelay + " minute" + ((avgPickupDelay > 0 && avgPickupDelay < 1) ? "" : "s"); } decimal totalFares = _driverStatisticsService.GetTotalFaresBy(id.Value); ViewData["TotalFares"] = "$" + totalFares; decimal totalCosts = _driverStatisticsService.GetTotalCostsBy(id.Value); ViewData["TotalCosts"] = "$" + totalCosts; if (id == null) { return(NotFound()); } await _legRepository.ListForDriverAsync(id.Value); return(View(driver)); }
/* Learn from legs with specified request times in a given date range */ public async Task LearnFromDates(DateTime from, DateTime to) { IEnumerable <Leg> legs = await _legRepository.ListForDriverAsync(_DriverID, leg => leg.StartTime.CompareTo(from) >= 0 && leg.StartTime.CompareTo(to) < 0 && leg.PickupRequestTime.HasValue); double[][] trainingInputs = legs.Select(leg => { return(new double[] { leg.StartTime.Subtract(leg.PickupRequestTime.Value).TotalMinutes, leg.ArrivalTime.Subtract(leg.StartTime).TotalMinutes, decimal.ToDouble(leg.Fare) }); }).ToArray(); _logisticRegressions.Clear(); _logisticRegressions.AddRange(_logisticRegressionAnalyses.Select((lra, i) => { double[] trainingOutputs = legs.Select(leg => leg.NumOfPassengersPickedUp > i + 1 ? 1.0 : 0.0).ToArray(); return(lra.Learn(trainingInputs, trainingOutputs)); })); }
/// <summary> /// Train on number of clusters using gap statistic /// </summary> private async Task ComputeK(int maxK = 100, int B = 10, int driverID = 0, DateTime?startDate = null, DateTime?endDate = null) { double[] Wk = new double[maxK]; double[][] Wref_kb = new double[maxK][]; double[] Gap = new double[maxK]; double[] sd = new double[maxK]; KMeansClusterCollection[] clusterCollections = new KMeansClusterCollection[maxK]; // obtain dataset IEnumerable <Leg> legs = driverID == 0 ? await _legRepository.ListAsync() : await _legRepository.ListForDriverAsync(driverID); if (startDate == null) { startDate = DateTime.MinValue; } if (endDate == null) { endDate = DateTime.MaxValue; } legs = legs.Where(leg => leg.StartTime.CompareTo(startDate) >= 0 && leg.StartTime.CompareTo(endDate) < 0); double[][] dataset = GetDataset(legs); // first cluster the dataset varying K for (int k = 1; k <= maxK; k++) { KMeans kMeans = new KMeans(k) { // distance function for geographic coordinates Distance = new GeographicDistance() }; clusterCollections[k - 1] = kMeans.Learn(dataset); double[][][] clusterData = ClusterPoints(dataset, k, clusterCollections[k - 1]); // sum of pairwise distances Wk[k - 1] = ComputeWk(clusterData, clusterCollections[k - 1]); } // then generate the reference data sets double[] lowerBounds = new double[4]; double[] boxDimensions = new double[4]; for (int i = 0; i < 4; i++) { lowerBounds[i] = dataset.Select(l => l[i]).Min(); boxDimensions[i] = dataset.Select(l => l[i]).Max() - lowerBounds[i]; } CorrectLongitudeBounds(lowerBounds, boxDimensions, 1); CorrectLongitudeBounds(lowerBounds, boxDimensions, 3); Random random = new Random(); for (int k = 1; k <= maxK; k++) { Wref_kb[k - 1] = new double[B]; for (int c = 0; c < B; c++) { double[][] refDataset = new double[dataset.Length][]; for (int i = 0; i < refDataset.Length; i++) { double[] dataPoint = new double[4]; for (int j = 0; j < 4; j++) { dataPoint[j] = random.NextDouble() * boxDimensions[j] + lowerBounds[j]; if ((j == 1 || j == 3) && dataPoint[j] > 180) { dataPoint[j] -= 360; } } refDataset[i] = dataPoint; } // cluster reference dataset KMeans refKmeans = new KMeans(k); refKmeans.Distance = new GeographicDistance(); KMeansClusterCollection refClusters = refKmeans.Learn(refDataset); // points in each cluster double[][][] refClusterData = ClusterPoints(refDataset, k, refClusters); // compute pairwise distance sum for refDataset Wref_kb[k - 1][c] = ComputeWk(refClusterData, refClusters); } // compute gap statistic double l_avg = Wref_kb[k - 1].Select(x => Log(x)).Average(); Gap[k - 1] = l_avg - Log(Wk[k - 1]); sd[k - 1] = Sqrt(Wref_kb[k - 1].Select(x => (Log(x) - l_avg) * (Log(x) - l_avg)).Average()); // decide optimal k if (k > 1 && Gap[k - 2] >= Gap[k - 1] - sd[k - 1]) { ClusterCollection = clusterCollections[k - 2]; NumberOfClustersLastChanged = DateTime.Now; return; } } }