/// <summary> /// Use prediction engine to create a series of purchase that are correlated by suggestions, not use all the users, but only a portion of them /// /// </summary> public void PurchaseModelInsertion() { using (var connection = new SqlConnection(conn_str)) { try { var predictionengine = TrainModelAction(); //foreach purchase already present select productID and check with all other ID using the prediction engine var purchaseList = connection.Query <CorePurchase>("SELECT * FROM CorePurchase").ToList(); //declare the adjunctive purchase list List <CorePurchase> purchase = new List <CorePurchase>(); Random dayRand = new Random(); Random qtaRand = new Random(); foreach (var elem in purchaseList) { //modify the span to be more accurate var productList = connection.Query <CoreProduct>("SELECT * FROM CoreProduct WHERE ProdID<@LastID AND ProdID>@FirstID", new { LastID = elem.ProductID + 20, FirstID = elem.ProductID - 20 }).ToList(); foreach (var prod in productList) { var prediction = predictionengine.Predict( new ProductEntry() { ProductID = Convert.ToUInt32(elem.ProductID), CoPurchaseProductID = Convert.ToUInt32(prod.ProdID) } ); //modify the percentage to be more or less precise while inserting if (prediction.Score > 0.2) { int qta = qtaRand.Next(0, 100); int day = dayRand.Next(0, 730); //create new purchase CorePurchase tmp = new CorePurchase() { ProductID = elem.ProductID, UserID = elem.UserID, PurchaseDate = DateTime.Now.AddDays(-day), Quantity = qta }; //insert inside the database the purchase purchase.Add(tmp); } } } connection.BulkInsert(purchase); }catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }
/// <summary> /// The function creates purchase for each client inside the product list, for optimization reasons /// a bins of 10000 users and 10000 elements is choosen each time to avoid problems with the /// number of records inside the tables /// </summary> public void InsertPurchase() { using (var connection = new SqlConnection(conn_str)) { try { //int prodNum = 262110; //int usrNum = 20000; int prodNum = connection.ExecuteScalar <int>("SELECT COUNT(*) FROM CoreProduct"); int usrNum = connection.ExecuteScalar <int>("SELECT COUNT(*) FROM CoreUser"); //create random purchase for each user Random usrRand = new Random(); Random prodRand = new Random(); Random dayRand = new Random(); Random qtaRand = new Random(); //set a number of purchase -> for base purchasing int purchaseNumber = 300000; List <CorePurchase> purchase = new List <CorePurchase>(); for (int i = 0; i < purchaseNumber; i++) { int ui = usrRand.Next(0, usrNum); int pi = prodRand.Next(1, prodNum); int qta = qtaRand.Next(0, 100); int day = dayRand.Next(0, 730); //CoreUser tmp_user = db_user.tblCoreUser.Find(ui); //CoreProduct tmp_prod = db_prod.tblCoreProduct.Find(pi); CoreUser tmp_user = connection.QueryFirst <CoreUser>("SELECT * FROM CoreUser WHERE ID=@UserID", new { UserID = ui }); CoreProduct tmp_prod = connection.QueryFirst <CoreProduct>("SELECT * FROM CoreProduct WHERE ProdID=@ProdID", new { ProdID = pi }); //create the association CorePurchase cp = new CorePurchase() { ProductID = tmp_prod.ProdID, UserID = tmp_user.ID, Quantity = qta, PurchaseDate = DateTime.Now.AddDays(-day) }; purchase.Add(cp); //wait for 1000 and insert if (i % 10000 == 0) { //db.AddRange(purchase); //db.SaveChanges(); connection.BulkInsert(purchase); purchase.Clear(); //db = new CorePurchaseContext(); } } if (purchase.Count > 0) { //db = new CorePurchaseContext(); //db.AddRange(purchase); //db.SaveChanges(); connection.BulkInsert(purchase); purchase.Clear(); } }catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }