Ejemplo n.º 1
0
        /// <summary>
        /// Gets the description for the enum.
        /// </summary>
        /// <returns>The description.</returns>
        /// <param name="value">Crime Type.</param>
        public static String GetDescription(this CrimeType value)
        {
            var info       = value.GetType().GetField(value.ToString());
            var attributes = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attributes == null || attributes.Length <= 0)
            {
                return(value.ToString());
            }

            return(attributes[0].Description);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates a Markov Model for the passed Crime Type and sets them to the internal dictionary or updates an existing entry with a cache.
        /// </summary>
        /// <param name="currentEnum">Current enum to generate the Markov Model for.</param>
        private void GenerateModel(CrimeType currentEnum)
        {
            this.logger.debug($"Generating Markov Model for {currentEnum.ToString()}");

            HashSet <Incident> currentIncidents;

            if (this.incidentCache.ContainsKey(currentEnum))
            {
                currentIncidents = this.incidentCache[currentEnum];
            }
            else
            {
                currentIncidents = this.incidentService.getAllForCrimeType(currentEnum)
                                   .Where(x => x.DateCreated >= this.start && x.DateCreated <= this.end)
                                   .ToHashSet();

                this.incidentCache[currentEnum] = currentIncidents;
            }

            double[][] dataSet = currentIncidents
                                 .Select(x => new double[] { x.Location.Latitude.Value, x.Location.Longitude.Value })
                                 .ToArray();

            this.logger.debug($"Clustering Data for {currentEnum.ToString()}");
            var rawClusters = this.clusteringService.Learn(dataSet);

            var clusters = new List <Cluster>();

            for (int i = 0; i < rawClusters.Count; i++)
            {
                clusters.Add(new Cluster(i, rawClusters[i]));
            }

            this.logger.debug($"Generating Transition matrix for {currentEnum.GetDescription()}");

            var model = new MarkovModel(currentEnum, this.logger);

            model.generateTransitionMatrix(currentIncidents, clusters);

            if (this.modelLookup.ContainsKey(currentEnum))
            {
                this.modelLookup[currentEnum] = model;
            }
            else
            {
                this.modelLookup.Add(currentEnum, model);
            }
        }
Ejemplo n.º 3
0
 // <inheritdoc>
 public IQueryable <Incident> getAllForCrimeType(CrimeType type)
 {
     return(this.repository.Query <Incident>()
            .Where(x => x.CrimeType.Equals(type.ToString()))
            .OrderBy(x => x.DateCreated)
            .AsQueryable());
 }
Ejemplo n.º 4
0
        // <inheritdoc>
        public double[] Predict(CrimeType type)
        {
            this.logger.debug($"Predicting on type {type.ToString()}");

            if (!this.modelLookup.ContainsKey(type))
            {
                string message = $"{type.ToString()} not in lookup";
                var    e       = new InvalidOperationException(message);
                this.logger.error(message, e);
                throw e;
            }

            this.modelLookup[type].predict();
            var predictionPoint = this.modelLookup[type].getPredictionPoint();

            if (predictionPoint != null && predictionPoint.Count() < 2)
            {
                this.logger.debug($"Generated Prediction Point, Lat: {predictionPoint[0]}, Long: {predictionPoint[1]}");
            }

            return(predictionPoint);
        }