public static void CalculateDeltaPopulationFromDummyData(TravelContext dbContext)
        {
            dbContext.DeltaPopulations.RemoveRange(dbContext.DeltaPopulations.Where(x => x.IsPredicted == false));
            dbContext.SaveChanges();

            List <DestinationAggregation> destinationList       = dbContext.DestinationAggregations.ToList();
            List <OrignAggregation>       originList            = dbContext.OrignAggregations.ToList();
            List <DeltaPopulation>        currentPopulationList = dbContext.DeltaPopulations.ToList();

            foreach (DestinationAggregation da in destinationList)
            {
                var match = originList.Find(oa => da.Destination == oa.Origin && da.Year == oa.Year && da.Month == oa.Month);
                if (match != null)
                {
                    DeltaPopulation delta = new DeltaPopulation();
                    delta.Place      = da.Destination;
                    delta.DeltaCount = da.Count - match.Count;
                    delta.Year       = da.Year;
                    delta.Month      = da.Month;
                    dbContext.Add(delta);
                }
                else
                {
                    DeltaPopulation delta = new DeltaPopulation();
                    delta.Place      = da.Destination;
                    delta.DeltaCount = da.Count;
                    delta.Year       = da.Year;
                    delta.Month      = da.Month;
                    dbContext.Add(delta);
                }
            }

            dbContext.SaveChanges();
        }
Example #2
0
        public static void GenerateDummyTravelData(TravelContext dbContext)
        {
            List <PassengerInfo> passengerInfoList = new List <PassengerInfo>();
            int           reqCount = Int32.Parse(ConfigurationManager.AppSettings["NoOfTravelRecordsToGenerate"]);
            List <string> destList = (ConfigurationManager.AppSettings["Destinations"].Split(",".ToCharArray())).ToList <string>();

            Parallel.ForEach(destList, desti =>
            {
                Parallel.For(0, reqCount, i =>
                {
                    PassengerInfo passenger = new PassengerInfo();
                    Random randomNumber     = new Random(2);
                    var age                     = GetRandomDate(1945);
                    var travelDate              = GetRandomDate(2015).Item1.Date;
                    passenger.Gender            = randomNumber.Next(1, 2);
                    passenger.StringDateOfBirth =
                        (age.Item1 < DateTime.MinValue) ? String.Format("{0:MM/dd/yyyy}", DateTime.Today.AddMonths(-4).Date) : String.Format("{0:MM/dd/yyyy}", age.Item1.Date);
                    passenger.Mode             = (TravelMode)(new Random(1)).Next(1, 3);
                    passenger.StringTravelDate =
                        (travelDate < DateTime.MinValue) ? String.Format("{0:MM/dd/yyyy}", DateTime.Today.AddDays(-30)) : String.Format("{0:MM/dd/yyyy}", travelDate);
                    passenger.Origin      = GetRandomPlaces().Item1;
                    passenger.Destination = desti;
                    Console.WriteLine(passenger.DateOfBirth + "--" + passenger.Origin + "--" + passenger.Destination + "--" + passenger.Age + "--" + passenger.TravelDate);
                    passengerInfoList.Add(passenger);
                });
            });

            Thread.Sleep(30000);
            int counter = 1;

            foreach (PassengerInfo passenger in passengerInfoList)
            {
                TravelRawData travelRecord = new TravelRawData()
                {
                    DateOfBirth = passenger.StringDateOfBirth,
                    Destination = passenger.Destination,
                    Gender      = passenger.Gender == 1 ? true : false,
                    Mode        = (int)passenger.Mode,
                    Origin      = passenger.Origin,
                    TravelDate  = passenger.StringTravelDate,
                };

                dbContext.Add(travelRecord);
                counter += 1;
                if (counter >= 100)
                {
                    dbContext.SaveChanges();
                    counter = 1;
                }
            }

            dbContext.SaveChanges();

            //string fileNameSuffix = DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".csv";
            //ExportToFile.CreateCSV<PassengerInfo>(passengerInfoList, "travelInfoSampe_" + fileNameSuffix);
        }
        public static void CalculateDeltaPopulationFromDummyData(TravelContext dbContext)
        {
            dbContext.DeltaPopulations.RemoveRange(dbContext.DeltaPopulations.Where(x => x.IsPredicted == false));
            dbContext.SaveChanges();

            List <DestinationAggregation> destinationList       = dbContext.DestinationAggregations.ToList();
            List <OrignAggregation>       originList            = dbContext.OrignAggregations.ToList();
            List <DeltaPopulation>        currentPopulationList = dbContext.DeltaPopulations.ToList();

            foreach (DestinationAggregation da in destinationList)
            {
                var             match = originList.Find(oa => da.Destination == oa.Origin && da.Year == oa.Year && da.Month == oa.Month);
                DeltaPopulation delta = null;
                if (match != null)
                {
                    delta            = new DeltaPopulation();
                    delta.Place      = da.Destination;
                    delta.DeltaCount = da.Count - match.Count;
                    delta.Year       = da.Year;
                    delta.Month      = da.Month;
                }
                else
                {
                    delta            = new DeltaPopulation();
                    delta.Place      = da.Destination;
                    delta.DeltaCount = da.Count;
                    delta.Year       = da.Year;
                    delta.Month      = da.Month;
                }

                var cityPopulation = avgPopulations.Where(i => i.City == da.Destination).SingleOrDefault();
                if (cityPopulation != null)
                {
                    var yearAvg = cityPopulation.Avg.Where(i => i.Year == da.Year).FirstOrDefault();
                    if (yearAvg != null)
                    {
                        delta.DeltaCount += yearAvg?.Month;
                    }
                }
                dbContext.Add(delta);
            }

            dbContext.SaveChanges();
        }
        public static async Task PredictMigrationPopulation(TravelContext dbContext)
        {
            List <DeltaPopulation> currentPopulationList = dbContext.DeltaPopulations.ToList();

            List <string> places = (from deltaRecord in currentPopulationList
                                    select deltaRecord.Place).Distinct().ToList();

            StringTable inpuToMLService = new StringTable();

            inpuToMLService.Values = new string[places.Count * 36, 3];
            int rowIndex = 0;

            foreach (string place in places)
            {
                for (int i = 1; i <= 36; i++)
                {
                    inpuToMLService.Values.SetValue(place.TrimStart(), rowIndex, 0);
                    inpuToMLService.Values.SetValue(DateTime.Today.AddMonths(i).Year.ToString(), rowIndex, 1);
                    inpuToMLService.Values.SetValue(DateTime.Today.AddMonths(i).Month.ToString(), rowIndex, 2);
                    rowIndex += 1;
                }
            }

            using (var client = new HttpClient())
            {
                var scoreRequest = new
                {
                    Inputs = new Dictionary <string, StringTable>()
                    {
                        {
                            "input1",
                            new StringTable()
                            {
                                ColumnNames = new string[] { "Place", "Year", "Month" },
                                Values      = inpuToMLService.Values
                            }
                        },
                    },
                    GlobalParameters = new Dictionary <string, string>()
                    {
                        { "Append score columns to output", "True" },
                    }
                };

                string apiUrl = null;
                string apiKey = null;
                apiUrl = ConfigurationManager.AppSettings["LinerRegressionAPI"];
                apiKey = ConfigurationManager.AppSettings["LinerRegressionKey"];

                if (!bool.Parse(ConfigurationManager.AppSettings["UseLinerRegressionAlgorithm"]))
                {
                    apiUrl = ConfigurationManager.AppSettings["BoostedDecisionTreeRegressionAPI"];
                    apiKey = ConfigurationManager.AppSettings["BoostedDecisionTreeRegressionKey"];
                }


                client.BaseAddress = new Uri(apiUrl + "&details=true");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
                HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);

                if (response.IsSuccessStatusCode)
                {
                    dbContext.DeltaPopulations.RemoveRange(dbContext.DeltaPopulations.Where(x => x.IsPredicted == true));
                    dbContext.SaveChanges();

                    string jsonRequest = await response.Content.ReadAsStringAsync();

                    RootObject            mlResponse       = JsonConvert.DeserializeObject <RootObject>(jsonRequest);
                    List <List <string> > predictedrecords = mlResponse.Results.output1.value.Values;
                    foreach (List <string> predictedRecord in predictedrecords)
                    {
                        DeltaPopulation prediction = new DeltaPopulation();
                        prediction.Place       = predictedRecord[0].TrimStart();
                        prediction.Year        = Int32.Parse(predictedRecord[1]);
                        prediction.Month       = Int32.Parse(predictedRecord[2]);
                        prediction.DeltaCount  = (int)decimal.Parse(predictedRecord[3]);
                        prediction.IsPredicted = true;
                        dbContext.Add(prediction);
                    }
                    dbContext.SaveChanges();
                }
                else
                {
                    Console.WriteLine(string.Format("The request failed with status code: {0}", response.StatusCode));

                    // Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
                    Console.WriteLine(response.Headers.ToString());

                    string responseContent = await response.Content.ReadAsStringAsync();

                    Console.WriteLine(responseContent);
                }
            }
        }