/// <summary> /// 计算所有用户的兴趣程度矩阵 /// </summary> public void GetUserInterestMatrix() { Array.Clear(UserInterestMatrix, 0, itemTotal * userTotal); for (int i = 0; i < userTotal; i++) { DataTable Tb_UserInterest = new DataTable(); Tb_UserInterest.Columns.Add("userID", System.Type.GetType("System.Int32")); Tb_UserInterest.Columns.Add("itemID", System.Type.GetType("System.Int32")); Tb_UserInterest.Columns.Add("interest", System.Type.GetType("System.Double")); string tableName = "UserInterest"; for (int j = 0; j < itemTotal; j++) { if (TrainSet[j, i] == 0) { UserInterestMatrix[i, j] = CalculateUserInterest(i, j, 10); DataRow row = Tb_UserInterest.NewRow(); row[0] = i + 1; row[1] = j + 1; row[2] = UserInterestMatrix[i, j]; Tb_UserInterest.Rows.Add(row); } } SqlSeverProvider.ExecuteSqlBulkCopy(Tb_UserInterest, tableName); } }
/// <summary> /// 推荐物品 /// </summary> public void GetRecommend() { int mostInterestNum; //当前最感兴趣物品 for (int i = 0; i < userTotal; i++) { int [] orderFlag = new int[itemTotal]; for (int j = 0; j < recommendNum; j++) { mostInterestNum = 0; while (orderFlag[mostInterestNum] != 0) { mostInterestNum++; } for (int k = 0; k < itemTotal; k++) { if (UserInterestMatrix[i, mostInterestNum] < UserInterestMatrix[j, k] && orderFlag[k] == 0) { mostInterestNum = k; } } orderFlag[mostInterestNum] = 1; RecommendSet[i, j] = mostInterestNum; } } Console.WriteLine("推荐完成..."); for (int i = 0; i < 6040; i++) { DataTable Tb_Recommended = new DataTable(); Tb_Recommended.Columns.Add("userID", System.Type.GetType("System.Int32")); Tb_Recommended.Columns.Add("itemID", System.Type.GetType("System.Int32")); string tableName = "Recommended"; for (int j = 0; j < 10; j++) { var item = (int)RecommendSet[i, j]; //string str = string.Format("insert into Recommended values({0},{1})",i+1,item); //SqlSeverProvider.ExecuteNonQuery(str); DataRow row = Tb_Recommended.NewRow(); row[0] = i + 1; row[1] = item; Tb_Recommended.Rows.Add(row); } SqlSeverProvider.ExecuteSqlBulkCopy(Tb_Recommended, tableName); } Console.WriteLine("推荐完成22222..."); }
/// <summary> /// 计算所有物品之间的相似性,得到相似性矩阵 /// </summary> public void GetSimilarityMatrix() { Array.Clear(SimilarityMatrix, 0, itemTotal * itemTotal); for (int i = 0; i < itemTotal; i++) { DataTable Tb_SimilarityMatrix = new DataTable(); Tb_SimilarityMatrix.Columns.Add("itemOne", System.Type.GetType("System.Int32")); Tb_SimilarityMatrix.Columns.Add("itemTwo", System.Type.GetType("System.Int32")); Tb_SimilarityMatrix.Columns.Add("similarity", System.Type.GetType("System.Double")); string tableName = "SimilarityMatrix"; for (int j = 0; j < itemTotal; j++) { SimilarityMatrix[i, j] = CalculateSimilarityForA_B(i, j); DataRow row = Tb_SimilarityMatrix.NewRow(); row[0] = i + 1; row[1] = j + 1; row[2] = SimilarityMatrix[i, j]; Tb_SimilarityMatrix.Rows.Add(row); } SqlSeverProvider.ExecuteSqlBulkCopy(Tb_SimilarityMatrix, tableName); } }