Example #1
0
        private PurchaseData ConvertToPurchaseData(Order order)
        {
            var orderDetails = _orderDetails.Where(o => o.OrderId == order.OrderId).ToList();

            PurchaseData data = new PurchaseData();

            data.OrderID = order.OrderId;
            data.Data    = new int[_products.Count];

            for (int i = 0; i < PurchaseData.ProductIDs.Length; i++)
            {
                foreach (var orderDetail in orderDetails)
                {
                    if (orderDetail.ProductId == PurchaseData.ProductIDs[i])
                    {
                        data.Data[i] = 1;
                        break;
                    }
                }
            }

            return(data);
        }
Example #2
0
        private int FindRecommendationUserBased(List <PurchaseData> purchaseData, PurchaseData p, int kNearest)
        {
            List <PurchaseData> purchases;

            // Use k-nearest if possible
            if (purchaseData.Count >= kNearest)
            {
                purchases = purchaseData.OrderByDescending(purchase => purchase.Similarity).ToList().GetRange(0, kNearest);
            }
            // else as many as possible
            else
            {
                purchases = purchaseData.OrderByDescending(purchase => purchase.Similarity).ToList();
            }

            // Calculate similarity
            double similaritySum = purchases.Sum(purchase => purchase.Similarity);

            foreach (var purchase in purchases)
            {
                purchase.Influence = purchase.Similarity / similaritySum;
            }


            // Sum weighted similarity for each product
            var recommendations = new Dictionary <int, double>();
            var chosenInStore   = _recommendationResults;

            foreach (PurchaseData purchase in purchases)
            {
                foreach (var recommendationResult in chosenInStore.Where(r => r.OrderId == purchase.OrderID))
                {
                    if (recommendations.ContainsKey(recommendationResult.SelectedProductId))
                    {
                        recommendations[recommendationResult.SelectedProductId] += purchase.Influence;
                    }
                    else
                    {
                        recommendations.Add(recommendationResult.SelectedProductId, purchase.Influence);
                    }
                }
            }

            var sortedRecommendations = recommendations.OrderByDescending(product => product.Value).ToList();

            int recommendedProduct = -1;

            // TODO: Make this look if similarity is 0 instead
            if (sortedRecommendations.Count == 0)
            {
                Random random          = new Random();
                var    instoreProducts = _products.Where(x => x.IsInStore).ToList();

                recommendedProduct = instoreProducts[random.Next(instoreProducts.Count)].ProductId;
                LogResult("UserBased", kNearest, p.OrderID, recommendedProduct, true, sortedRecommendations);
                return(recommendedProduct);
            }

            LogResult("UserBased", kNearest, p.OrderID, sortedRecommendations[0].Key, false, sortedRecommendations);
            recommendedProduct = sortedRecommendations[0].Key;

            return(recommendedProduct);
        }