Пример #1
0
 private double[][] GetDataset(IEnumerable <Leg> legs)
 {
     return(legs.Select(leg => _geocodingDbSync.GetLegCoordinatesAsync(leg.LegID).Result)
            .Where(coordinates => coordinates != null)
            .Select(coordinates =>
     {
         return new double[] {
             Convert.ToDouble(coordinates.StartLatitude),
             Convert.ToDouble(coordinates.StartLongitude),
             Convert.ToDouble(coordinates.DestLatitude),
             Convert.ToDouble(coordinates.DestLongitude)
         };
     }).ToArray());
 }
Пример #2
0
        public async Task <IActionResult> Get(int id)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Challenge());
            }

            var driver     = (await _legRepository.Get(id)).Driver;
            var authResult = await _authorizationService.AuthorizeAsync(User, driver, "DriverInfoPolicy");

            if (!authResult.Succeeded)
            {
                return(Forbid());
            }

            return(Ok(await _dbSync.GetLegCoordinatesAsync(id)));
        }
Пример #3
0
        public async Task LearnFromDates(DateTime from, DateTime to)
        {
            int maxPickups = await GetMaxNumberOfPickups();

            // train clustering algorithm
            await _locationClustering.RetrainAsync(from, to);

            // initialize storage arrays
            int pickupElementSize = await GetMaxNumberOfPickups() + 1;

            InitStorageArray(ref clusterFareClassRegressions, pickupElementSize - 1, NumberOfFareClassIntervals - 1);
            InitStorageArray(ref clusterFareClassInputDensityKernels, pickupElementSize - 1, NumberOfFareClassIntervals);
            InitStorageArray(ref clusterFareClassDistributionsUnivariate, pickupElementSize - 1, NumberOfFareClassIntervals);
            InitStorageArray(ref clusterPickupFrequencies, pickupElementSize);
            InitStorageArray(ref clusterPickupInputDensityKernels, pickupElementSize);
            InitStorageArray(ref clusterPickupInputDensityKernelsUnivariate, pickupElementSize);

            // for each cluster
            for (int i = 0; i < _locationClustering.NumberOfClusters; i++)
            {
                // obtain data set
                IEnumerable <Task <Pair <Leg, bool> > > decisionTasks = (await _legRepository.ListAsync())
                                                                        .Where(leg => leg.StartTime.CompareTo(from) > 0 && leg.StartTime.CompareTo(to) < 0)
                                                                        .Select(async(leg) =>
                {
                    LegCoordinates coords = await _geocodingDbSync.GetLegCoordinatesAsync(leg.LegID);
                    double[] dp           = (new decimal[] { coords.StartLatitude, coords.StartLongitude, coords.DestLatitude, coords.DestLongitude })
                                            .Select(Convert.ToDouble).ToArray();
                    return(new Pair <Leg, bool>(leg, _locationClustering.ClusterCollection.Decide(dp) == i));
                });
                Pair <Leg, bool>[] decisions = await Task.WhenAll(decisionTasks);

                // Data input values (pickup delay, travel time) in this cluster
                IEnumerable <Leg> dataLegs = decisions.Where(pair => pair.Second).Select(pair => pair.First);
                double[][]        dataset  = dataLegs
                                             .Select(leg => new double[]
                {
                    leg.PickupRequestTime.HasValue
                        ? leg.StartTime.Subtract(leg.PickupRequestTime.Value).TotalMinutes
                        : 0,

                    leg.ArrivalTime.Subtract(leg.StartTime).TotalMinutes
                }).ToArray();
                // Fare classes in this cluster
                int[] fareClasses = dataLegs
                                    .Select(leg =>
                {
                    for (int j = 0; j < FareClassIntervals.Count(); j++)
                    {
                        if (j < FareClassIntervals.Count() &&
                            Convert.ToDecimal(FareClassIntervals.ElementAt(j)) > leg.Fare)
                        {
                            return(j);
                        }
                    }
                    return(FareClassIntervals.Count());
                }).ToArray();
                // Pickup numbers in this cluster
                int[] pickupNumbers = dataLegs.Select(leg => leg.NumOfPassengersPickedUp).ToArray();


                // for each possible number of pickups
                for (int n = 1; n <= maxPickups; n++)
                {
                    double[][] dataSubset        = dataset.Where((dp, k) => pickupNumbers[k] == n).ToArray();
                    int[]      fareClassesSubset = fareClasses.Where((fc, k) => pickupNumbers[k] == n).ToArray();

                    if (dataSubset.Length == 0)
                    {
                        throw new ApplicationException("Insufficient data to make a reliable prediction");
                    }

                    // for each fare class interval boundary
                    for (int j = 0; j < NumberOfFareClassIntervals; j++)
                    {
                        // train logistic regression
                        if (j > 0 && clusterFareClassRegressions[i][n - 1][j - 1] == null)
                        {
                            clusterFareClassRegressions[i][n - 1][j - 1] = _logisticRegressionAnalysis
                                                                           .Learn(dataSubset, fareClassesSubset.Select(fc => fc >= j ? 1.0 : 0.0).ToArray());
                        }

                        // train empirical density functions
                        if (fareClassesSubset.Count(fc => fc >= j) > 0.0)
                        {
                            double[][] dataSubsetSamples = dataSubset.Where((dp, k)
                                                                            => fareClassesSubset[k] >= j).ToArray();
                            MultivariateEmpiricalDistribution fareClassInputDistribution
                                = new MultivariateEmpiricalDistribution(dataSubsetSamples);

                            SetInputDistribution(fareClassInputDistribution, dataSubsetSamples,
                                                 out clusterFareClassInputDensityKernels[i][n - 1][j],
                                                 out clusterFareClassDistributionsUnivariate[i][n - 1][j]);
                        }
                    }
                }

                // compute pickup frequencies
                for (int l = 0; l < pickupElementSize; l++)
                {
                    clusterPickupFrequencies[i][l] = Convert.ToDouble(dataLegs.Count(leg => leg.NumOfPassengersPickedUp == l))
                                                     / to.Subtract(from).TotalMinutes;

                    if (pickupNumbers.Any(pn => pn == l))
                    {
                        double[][] samples = dataset.Where((dp, k) => pickupNumbers[k] == l).ToArray();
                        MultivariateEmpiricalDistribution pickupInputDistribution
                            = new MultivariateEmpiricalDistribution(samples);

                        SetInputDistribution(pickupInputDistribution, samples,
                                             out clusterPickupInputDensityKernels[i][l],
                                             out clusterPickupInputDensityKernelsUnivariate[i][l]);
                    }
                }
            }
        }