Beispiel #1
0
        /// <summary>
        /// Create a new BestCustomersTbl object.
        /// </summary>
        /// <param name="cid">Initial value of the Cid property.</param>
        /// <param name="clusterId">Initial value of the ClusterId property.</param>
        public static BestCustomersTbl CreateBestCustomersTbl(global::System.Int32 cid, global::System.Int32 clusterId)
        {
            BestCustomersTbl bestCustomersTbl = new BestCustomersTbl();

            bestCustomersTbl.Cid       = cid;
            bestCustomersTbl.ClusterId = clusterId;
            return(bestCustomersTbl);
        }
        public List <int>[] FindBestCustomers() // returns each customer in his/her cluster
        {
            try
            {
                List <int>[] results = new List <int> [3];
                // or 4 cluster because 4th cluster has some poor customers to recommend to purchase goods
                for (int i = 0; i < results.Length; i++)
                {
                    results[i] = new List <int>();
                }

                ECDBEntities entity        = new ECDBEntities();
                int          numberOfBests = (int)(SAMPLE_PERCENT * entity.CustomerTbls.Count());

                // ------------------------------------- FORMULAS -------------------------------------
                // Formula 1
                // (1) / (3)
                var idsFormula1 =
                    entity.CustomerTbls.OrderByDescending(
                        customer => (double)customer.NumberOfPurchases / (double)customer.NumberOfLogins).Select(
                        customer => customer.Cid).Take(numberOfBests);

                // Formula 2
                // 1 / (4) + (5) => max   or   (4) + (5) => min
                var idsFormula2 =
                    entity.CustomerTbls.OrderBy(
                        customer =>
                        (Math.Abs((int)EntityFunctions.DiffMinutes(DateTime.Now, customer.LastLoginDate.Value)) +
                         Math.Abs((int)EntityFunctions.DiffMinutes(DateTime.Now, customer.LastPurchaseDate.Value)))).
                    Select(
                        customer => customer.Cid).
                    Take(numberOfBests);

                // Formula 3
                // (4) / (5)  +  (1) / (2)
                var idsFormula3 = entity.CustomerTbls.OrderByDescending(
                    customer =>
                    Math.Abs((double)EntityFunctions.DiffMinutes(DateTime.Now, customer.LastLoginDate.Value)) /
                    Math.Abs((double)EntityFunctions.DiffMinutes(DateTime.Now, customer.LastPurchaseDate.Value)) +
                    (double)customer.NumberOfPurchases / (double)customer.LoggedInDuration).Select(
                    customer => customer.Cid)
                                  .Take(
                    numberOfBests);

                results[0].AddRange(idsFormula1.Intersect(idsFormula2).Intersect(idsFormula3));

                results[1].AddRange(
                    idsFormula1.Intersect(idsFormula2).Union(idsFormula1.Intersect(idsFormula3)).Union(
                        idsFormula2.Intersect(idsFormula3)).Except(results[0]));

                results[2].AddRange(
                    idsFormula1.Union(idsFormula2).Union(idsFormula3).Except(results[0]).Except(results[1]));


                // ------------------- Working With Database's Best Customers Table -------------------
                // Removing Last Data From Best Customers Table
                entity.ExecuteStoreCommand("DELETE BestCustomersTbls");

                // Adding Better Customers To Best Customers Table))
                for (int i = 0; i < results.Length; i++)
                {
                    foreach (var cid in results[i])
                    {
                        BestCustomersTbl bc = new BestCustomersTbl();
                        bc.ClusterId = i + 1;
                        bc.Cid       = cid;
                        entity.AddToBestCustomersTbls(bc);
                    }
                }
                entity.SaveChanges();

                return(results);
            }
            catch (Exception ex)
            {
                MySoapFault fault = new MySoapFault();
                fault.Operation   = "FindBestCustomers";
                fault.Reason      = "Error in finding best customers occurred .";
                fault.Details     = ex.Message;
                fault.MoreDetails = ex.StackTrace;
                throw new FaultException <MySoapFault>(fault);
            }
        }
Beispiel #3
0
 /// <summary>
 /// Deprecated Method for adding a new object to the BestCustomersTbls EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToBestCustomersTbls(BestCustomersTbl bestCustomersTbl)
 {
     base.AddObject("BestCustomersTbls", bestCustomersTbl);
 }