private void LoadCluster(Clustering_Users_DAO dao, Settings st, ref bool existTransac, string U_SubCategoryID, string categoryName, Dictionary<string, int> dic_users, Dictionary<string, int> dic_items, double[][] x) { if (x.Length > 0) { existTransac = true; // Clustering Data Cluster cluster = new Cluster(); Dictionary<int, int> clustered_Data = new Dictionary<int, int>(); cluster.addSetting(st.k, st.maxLoop, x, st.epsilon, st.Alpha, st.T, st.U_M); clustered_Data = cluster.Clustering(st.cluster_type); //cuong add if //if (clustered_Data.Count > 0) //{ // Mapping UserID to clustered Data Dictionary<string, int> mapped_Data = map_ID_to_Index(dic_users, clustered_Data); // Get Destination V double[][] v = cluster.getV(); // Add new data #region C1 - Ratting Matrix // Add Ratting Matrix for (int i = 0; i < x.Length; i++) for (int j = 0; j < x[0].Length; j++) if (x[i][j] > 0) { try { MatrixItem matrixItem = new MatrixItem(); matrixItem.ClusterID = categoryName + (mapped_Data[Util.FindKeyByValue(dic_users, i)] + 1); matrixItem.Row = Util.FindKeyByValue(dic_users, i); matrixItem.Column = Util.FindKeyByValue(dic_items, j); matrixItem.Cell = x[i][j]; dao.setRattingMatrix(matrixItem); } catch (Exception) { } } #endregion // Add to USER_CLUSTER_TBL for (int i = 0; i < v.Length; i++) dao.addCluster(categoryName + (i + 1), U_SubCategoryID); // Add to PARTION_TBL foreach (var map in mapped_Data) { Partion detail = new Partion(); detail.UserID = map.Key; detail.ClusterID = categoryName + (map.Value + 1); dao.addPARTION_TBL(detail); } // Add to USER_CENTROID_TBL for (int i = 0; i < v.Length; i++) for (int j = 0; j < v[0].Length; j++) if (!Double.NaN.Equals(v[i][j])) { try { User_Centroid centroid = new User_Centroid(); centroid.Value = v[i][j]; centroid.ClusterID = categoryName + (i + 1); centroid.MetaItemID = Util.FindKeyByValue(dic_items, j); dao.add_User_Centroid(centroid); } catch (Exception) { } } } //}//cuong add }
private void LoadUpdateCluster(Clustering_Users_DAO dao, Settings st, ref bool existTransac, List<Partion> listPartion, string U_SubCategoryID, string categoryName, Dictionary<string, int> dic_users, Dictionary<string, int> dic_items, double[][] x) { if (x.Length > 0) { existTransac = true; List<string> listClusterID = getListClusterID(listPartion); // Get existed V double[][] v = new double[listClusterID.Count][]; Cluster cl = new Cluster(); cl.addSetting(v.Length, st.maxLoop, x, 0.0001, st.Alpha, st.T, st.U_M); Dictionary<int, int> clustered_Data; if (listPartion.Count > 0) { v = get_V_ByNewItems_ReComputeAll(x, dic_items, dic_users, listPartion, listClusterID); clustered_Data = cl.Clustering_Merge(st.cluster_type, v); } else clustered_Data = cl.Clustering_Merge(st.cluster_type); v = cl.getV(); // Mapping UserID to clustered Data Dictionary<string, int> mapped_Data = map_ID_to_Index(dic_users, clustered_Data); #region C1 - Ratting Matrix // Add Ratting Matrix for (int i = 0; i < x.Length; i++) for (int j = 0; j < x[0].Length; j++) if (x[i][j] > 0) { try { MatrixItem matrixItem = new MatrixItem(); matrixItem.ClusterID = categoryName + (mapped_Data[Util.FindKeyByValue(dic_users, i)] + 1); matrixItem.Row = Util.FindKeyByValue(dic_users, i); matrixItem.Column = Util.FindKeyByValue(dic_items, j); matrixItem.Cell = x[i][j]; dao.setRattingMatrix(matrixItem); } catch (Exception) { } } #endregion // Remove old USER_CLUSTER_TBL dao.delete_USER_CLUSTER_TBL(categoryName); // Add to USER_CLUSTER_TBL for (int i = 0; i < v.Length; i++) dao.addCluster(categoryName + (i + 1), U_SubCategoryID); // Remove old PARTION_TBL dao.delete_PARTION_TBL(categoryName); // Add to PARTION_TBL foreach (var map in mapped_Data) { Partion detail = new Partion(); detail.UserID = map.Key; detail.ClusterID = categoryName + (map.Value + 1); dao.addPARTION_TBL(detail); } // Remove old USER_CENTROID_TBL dao.delete_USER_CENTROID_TBL(categoryName); // Add to USER_CENTROID_TBL for (int i = 0; i < v.Length; i++) for (int j = 0; j < v[0].Length; j++) if (!Double.NaN.Equals(v[i][j])) { try { User_Centroid centroid = new User_Centroid(); centroid.Value = v[i][j]; centroid.ClusterID = categoryName + (i + 1); centroid.MetaItemID = Util.FindKeyByValue(dic_items, j); dao.add_User_Centroid(centroid); } catch (Exception) { } } } }
public Settings getSettings() { Settings st = new Settings(); try { List<Clustering_Setting> listCs = getListClusterSetting(); if (null != listCs && listCs.Count > 0) foreach (var item in listCs) { if (item.Key.Equals(ConstantValues.CST_U_K)) { foreach (var cs_detail in item.Values) if (cs_detail.isDedault.Equals(true)) { st.k = Convert.ToInt32(cs_detail.Value.Trim()); break; } } //else if (item.Key.Equals(ConstantValues.CST_U_INIT_V)) //{ // foreach (var cs_detail in item.Values) // if (cs_detail.isDedault.Equals(true)) // { // st.U_InitV = cs_detail.Value.Trim(); // break; // } //} else if (item.Key.Equals(ConstantValues.CST_U_MAX_LOOP)) { foreach (var cs_detail in item.Values) if (cs_detail.isDedault.Equals(true)) { st.maxLoop = Convert.ToInt32(cs_detail.Value.Trim()); break; } } else if (item.Key.Equals(ConstantValues.CST_U_TIMER)) { foreach (var cs_detail in item.Values) if (cs_detail.isDedault.Equals(true)) { st.U_Timer = cs_detail.Value.Trim(); break; } } else if (item.Key.Equals(ConstantValues.CST_U_CLUSTER)) { foreach (var cs_detail in item.Values) if (cs_detail.isDedault.Equals(true)) { st.cluster_type = cs_detail.Value.Trim(); break; } } //else if (item.Key.Equals(ConstantValues.CST_U_LOOP_UPDATE)) //{ // foreach (var cs_detail in item.Values) // if (cs_detail.isDedault.Equals(true)) // { // st.loopUpdate = Convert.ToInt32(cs_detail.Value.Trim()); // break; // } //} else if (item.Key.Equals(ConstantValues.CST_U_T)) { foreach (var cs_detail in item.Values) if (cs_detail.isDedault.Equals(true)) { try { st.T = Double.Parse(cs_detail.Value.Trim()); } catch (Exception) { st.T = Double.Parse(cs_detail.Value.Trim(), System.Globalization.CultureInfo.InvariantCulture); } break; } } else if (item.Key.Equals(ConstantValues.CST_U_EPSILON)) { foreach (var cs_detail in item.Values) if (cs_detail.isDedault.Equals(true)) { try { st.epsilon = Double.Parse(cs_detail.Value.Trim()); } catch (Exception) { st.epsilon = Double.Parse(cs_detail.Value.Trim(), System.Globalization.CultureInfo.InvariantCulture); } break; } } else if (item.Key.Equals(ConstantValues.CST_U_ALPHA)) { foreach (var cs_detail in item.Values) if (cs_detail.isDedault.Equals(true)) { try { st.Alpha = Double.Parse(cs_detail.Value.Trim()); } catch (Exception) { st.Alpha = Double.Parse(cs_detail.Value.Trim(), System.Globalization.CultureInfo.InvariantCulture); } break; } } else if (item.Key.Equals(ConstantValues.CST_U_M)) { foreach (var cs_detail in item.Values) if (cs_detail.isDedault.Equals(true)) { st.U_M = Convert.ToInt32(cs_detail.Value.Trim()); break; } } } } catch (Exception) { Cluster.setDefaultSettings(st.k, st.maxLoop, st.epsilon, st.Alpha, st.T, st.cluster_type); } return st; }