//[ValidateAntiForgeryToken] public IActionResult Recommendations(float supportPercentage, int confidence, DateTime startDate, DateTime endDate) { IEnumerable <SequenceSupport> sequenceSupports = ReadFrequentSequences(); //List<FrequentSequentialRule> frequentSequentialRules = FindFrequentSequentialRules(sequenceSupports); List <UserSequence> userSequences = CreateUserSequences(startDate, endDate); int userCount = userSequences.Count(); int support = Convert.ToInt32(Math.Floor((supportPercentage / 100) * userCount)); IList <SequenceSupport> sequenceSupports2Items = sequenceSupports. Where(a => a.sequence.Count == 2 && a.sequence[0].Count == 1 && a.sequence[1].Count == 1 && a.support >= support).ToList(); IList <SequenceSupport> sequenceSupports1Items = sequenceSupports. Where(a => a.sequence.Count == 1 && a.sequence[0].Count == 1 && a.support >= support).ToList(); IList <SequenceSupport> sequenceSupports2ItemsRules = new List <SequenceSupport>(); foreach (SequenceSupport sequenceSupport in sequenceSupports2Items) { int ancestorSupport = sequenceSupports1Items.Single(a => a.sequence[0][0] == sequenceSupport.sequence[0][0]).support; if (((float)sequenceSupport.support / (float)ancestorSupport) >= ((float)confidence / 100.0)) { sequenceSupports2ItemsRules.Add(sequenceSupport); } } SpmRecommendation sequentialRecommendation = new SpmRecommendation(); System.Diagnostics.Debug.WriteLine("Step 0 - Read Data: " + DateTime.Now.TimeOfDay + " - Read data"); List <RecommendedItems> recommendedItemsList = sequentialRecommendation.Recommend(sequenceSupports2ItemsRules, userSequences, 50); SavetoDatabase(recommendedItemsList); System.Diagnostics.Debug.WriteLine("Step 5: " + DateTime.Now.TimeOfDay + " - Save results"); return(View(recommendedItemsList)); }
//[ValidateAntiForgeryToken] public IActionResult OptimumSupportConfidence(float supportFrom, float supportTo, float supportSteps, float confidenceFrom, float confidenceTo, float confidenceSteps, DateTime trainStartDate, DateTime trainEndDate, DateTime testStartDate, DateTime testEndDate) { List <SupportConfidenceAccuracyNrec> SupportConfidenceAccuracyNrecs = new List <SupportConfidenceAccuracyNrec>(); //Select purchases in test period IList <UserItemPurchase> purchasesAllTest = _dbContext.UkRetailOriginalSales .Where(a => a.InvoiceDate.Date >= testStartDate.Date && a.InvoiceDate.Date <= testEndDate.Date && a.CustomerID != null && a.CustomerID < 13000 && a.Quantity > 0 && string.Compare(a.StockCode, "a") == -1) .Select(a => new UserItemPurchase { User = (Int32)a.CustomerID, Item = a.StockCode }).ToList(); //Select purchases in train period IList <int> purchasesAllTrain = _dbContext.UkRetailOriginalSales .Where(a => a.InvoiceDate.Date >= trainStartDate.Date && a.InvoiceDate.Date <= trainEndDate.Date && a.CustomerID != null && a.CustomerID < 13000 && a.Quantity > 0 && string.Compare(a.StockCode, "a") == -1).Select(a => (Int32)a.CustomerID).Distinct().ToList(); //Select users which the user bought something both in test period and train priod List <int> purchaseUsers = purchasesAllTest.Select(a => a.User).Where(a => purchasesAllTrain.Contains(a)).Distinct().ToList(); //Select purchass where user has bought something both in training and testing IList <UserItemPurchase> purchases = purchasesAllTest.Where(a => purchaseUsers.Contains(a.User)).ToList(); IEnumerable <SequenceSupport> sequenceSupports = ReadFrequentSequences(); //List<FrequentSequentialRule> frequentSequentialRules = FindFrequentSequentialRules(sequenceSupports); List <UserSequence> userSequences = CreateUserSequences(trainStartDate, trainEndDate); int userCount = userSequences.Count(); //Parallel.ForEach(counter, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount - 1 }, supportPercentage => for (float supportPercentage = supportFrom; supportPercentage <= supportTo; supportPercentage += supportSteps) { var counter = new List <float>(); for (float i = confidenceFrom; i <= confidenceTo; i += confidenceSteps) { counter.Add(i); } Parallel.ForEach(counter, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, confidence => //for (float confidence = confidenceFrom; confidence <= confidenceTo; confidence += confidenceSteps) { //========================================================= //Recommend Items //========================================================= int support = Convert.ToInt32(Math.Floor((supportPercentage / 100) * userCount)); IList <SequenceSupport> sequenceSupports2Items = sequenceSupports. Where(a => a.sequence.Count == 2 && a.sequence[0].Count == 1 && a.sequence[1].Count == 1 && a.support >= support).ToList(); IList <SequenceSupport> sequenceSupports1Items = sequenceSupports. Where(a => a.sequence.Count == 1 && a.sequence[0].Count == 1 && a.support >= support).ToList(); IList <SequenceSupport> sequenceSupports2ItemsRules = new List <SequenceSupport>(); foreach (SequenceSupport sequenceSupport in sequenceSupports2Items) { int ancestorSupport = sequenceSupports1Items.Single(a => a.sequence[0][0] == sequenceSupport.sequence[0][0]).support; if (((float)sequenceSupport.support / (float)ancestorSupport) >= ((float)confidence / 100.0)) { sequenceSupports2ItemsRules.Add(sequenceSupport); } } SpmRecommendation sequentialRecommendation = new SpmRecommendation(); List <RecommendedItems> recommendedItemsList = sequentialRecommendation.Recommend(sequenceSupports2ItemsRules, userSequences, 50); System.Diagnostics.Debug.WriteLine("Step 0 - Recomendation finished: " + DateTime.Now.TimeOfDay + " - Support: " + supportPercentage + " - Confidence: " + confidence); //========================================================= //Evaluate recomendations //========================================================= //Select recomendations which user has purchased both in training and testing List <RecommendedItems> recommendations = recommendedItemsList.Where(a => purchaseUsers.Contains(a.User)).ToList(); AccuracyNrec accuracyNrec = CalculateAccuracy(recommendations, purchases); SupportConfidenceAccuracyNrec supportConfidenceAccuracyNrec = new SupportConfidenceAccuracyNrec(); supportConfidenceAccuracyNrec.Support = supportPercentage; supportConfidenceAccuracyNrec.Confidence = confidence; supportConfidenceAccuracyNrec.AccuracyNrec = accuracyNrec; SupportConfidenceAccuracyNrecs.Add(supportConfidenceAccuracyNrec); System.Diagnostics.Debug.WriteLine("Step 0 - Evaluation finished: " + DateTime.Now.TimeOfDay + " - Support: " + supportPercentage + " - Confidence: " + confidence); }); } return(View(SupportConfidenceAccuracyNrecs)); }