Example #1
0
        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) { }
                        }

            }
        }
Example #2
0
 public void setDIST_Matrix(MatrixItem matrixItem)
 {
     Dictionary<string, object> parameters = new Dictionary<string, object>();
     parameters.Add("@ClusterID", matrixItem.ClusterID);
     parameters.Add("@MetaItemSource", matrixItem.Row);
     parameters.Add("@MetaItemDestination", matrixItem.Column);
     parameters.Add("@Distance", matrixItem.Cell);
     executeNonQuery("INSERT INTO [RS].[DISTANCE_MATRIX] ([ClusterID] ,[MetaItemSource] ,[MetaItemDestination] ,[Distance]) VALUES (@ClusterID ,@MetaItemSource ,@MetaItemDestination ,@Distance)", parameters);
 }
Example #3
0
        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
        }
Example #4
0
        public List<MatrixItem> getWeighMatrixByClusterID(string clusterID)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("@clusterID", clusterID);
            string strSelect = "SELECT [MetaItemSource],[MetaItemDestination],[WEIGHT] FROM [RS].[WEIGHT_MATRIX] WHERE [ClusterID] =@clusterID";
            SqlDataReader dr = executeReader(strSelect, parameters);

            List<MatrixItem> list = new List<MatrixItem>();
            while (dr.Read())
            {
                MatrixItem obj = new MatrixItem();
                obj.Row = dr.GetString(dr.GetOrdinal("MetaItemSource"));
                obj.Column = dr.GetString(dr.GetOrdinal("MetaItemDestination"));
                obj.Cell = dr.GetDouble(dr.GetOrdinal("WEIGHT"));
                list.Add(obj);
            }
            dr.Close();
            return list;
        }
Example #5
0
 public void setCONF_Matrix(MatrixItem matrixItem)
 {
     Dictionary<string, object> parameters = new Dictionary<string, object>();
     parameters.Add("@ClusterID", matrixItem.ClusterID);
     parameters.Add("@MetaItemSource", matrixItem.Row);
     parameters.Add("@MetaItemDestination", matrixItem.Column);
     parameters.Add("@Confident", matrixItem.Cell);
     executeNonQuery("INSERT INTO [RS].[CONFIDENT_MATRIX] ([ClusterID] ,[MetaItemSource] ,[MetaItemDestination] ,[Confident]) VALUES (@ClusterID ,@MetaItemSource ,@MetaItemDestination ,@Confident)", parameters);
 }
Example #6
0
        //------------------------------------------------------------------------------------------------------
        //This function is considered carefully by MC. NGUYEN, on 21.10.2014, I think it do not have any error.
        //------------------------------------------------------------------------------------------------------
        public List<MatrixItem> getRattingMatrixItem_ByClusterID(string ClusterID)
        {
            string strSelect = "SELECT [UserID], [MetaItemID], [Value] FROM [RS].[RATING_MATRIX] WHERE ClusterID = @ClusterID";
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("@ClusterID", ClusterID);
            SqlDataReader dr = executeReader(strSelect, parameters);

            List<MatrixItem> lst = new List<MatrixItem>();
            while (dr.Read())
            {
                MatrixItem obj = new MatrixItem();
                obj.Row = dr.GetString(dr.GetOrdinal("UserID"));
                obj.Column = dr.GetString(dr.GetOrdinal("MetaItemID"));
                obj.Cell = dr.GetDouble(dr.GetOrdinal("Value"));
                lst.Add(obj);
            }
            dr.Close();
            return lst;
        }
 public void setRattingMatrix(MatrixItem matrixItem)
 {
     Dictionary<string, object> parameters = new Dictionary<string, object>();
     parameters.Add("@ClusterID", matrixItem.ClusterID);
     parameters.Add("@UserID", matrixItem.Row);
     parameters.Add("@MetaItemID", matrixItem.Column);
     parameters.Add("@Value", matrixItem.Cell);
     executeNonQuery("INSERT INTO [RS].[RATING_MATRIX] ([ClusterID] ,[UserID] ,[MetaItemID] ,[Value]) VALUES (@ClusterID ,@UserID ,@MetaItemID ,@Value)", parameters);
 }
Example #8
0
        public List<MatrixItem> computeRattingMatrixItem(string U_SubCategoryID, double Alpha)
        {
            string strSelect = "SELECT x1.UserID, x2.MetaItemID, (@Alpha1*(x2.S1*1.0/x1.SU + x2.S1*1.0/x3.SG) + @Alpha2*(x2.F1*1.0/x1.FU + x2.F1*1.0/x3.FG))/2.0 As Rate FROM "+
                               "(SELECT [RS].[USER_TBL].UserID, SUM([Quantity]) as SU, count(*) as FU FROM [RS].[TRANSACTION_TBL], [RS].[USER_TBL] "+
                               "WHERE [RS].[TRANSACTION_TBL].[UserID] = [RS].[USER_TBL].[UserID] and [RS].[USER_TBL].U_SubCategoryID = @U_SubCategoryID " +
                               "GROUP BY [RS].[USER_TBL].UserID) X1, "+
                               "(SELECT [RS].[USER_TBL].UserID, [RS].[ITEM_TBL].[MetaItemID], SUM([Quantity]) as S1, count(*) as F1 FROM [RS].[TRANSACTION_TBL], [RS].[ITEM_TBL], [RS].[USER_TBL] "+
                               "WHERE [RS].[TRANSACTION_TBL].[UserID] = [RS].[USER_TBL].[UserID] and [RS].[TRANSACTION_TBL].ItemID = [RS].ITEM_TBL.ItemID and [RS].[USER_TBL].U_SubCategoryID = @U_SubCategoryID " +
                               "GROUP BY [RS].[USER_TBL].UserID, [RS].[ITEM_TBL].[MetaItemID]) X2,  "+
                               "(SELECT [RS].[ITEM_TBL].[MetaItemID], SUM([Quantity]) as SG, count(*) as FG FROM [RS].[TRANSACTION_TBL], [RS].[ITEM_TBL], [RS].[USER_TBL] "+
                               "WHERE [RS].[TRANSACTION_TBL].[UserID] = [RS].[USER_TBL].[UserID] and [RS].[TRANSACTION_TBL].ItemID = [RS].ITEM_TBL.ItemID and [RS].[USER_TBL].U_SubCategoryID = @U_SubCategoryID " +
                               "GROUP BY [RS].[ITEM_TBL].[MetaItemID]) X3 "+
                               "WHERE X1.UserID = X2.UserID AND X2.MetaItemID = X3.MetaItemID and  (@Alpha1*(x2.S1*1.0/x1.SU + x2.S1*1.0/x3.SG) + @Alpha2*(x2.F1*1.0/x1.FU + x2.F1*1.0/x3.FG))/2.0 >0";

            //string strSelect = "SELECT x2.[UserID], x2.MetaItemID, ((x2.QTY*1.0/x1.M*1.0) + (x2.FRE*1.0/x1.N*1.0))/2.0 as 'Rate' FROM (SELECT MAX([Quantity]) as M ,count(*) as N ,[RS].[USER_TBL].U_SubCategoryID ,[MetaItemID] FROM [RS].[TRANSACTION_TBL], [RS].[USER_TBL], [RS].ITEM_TBL WHERE [RS].[TRANSACTION_TBL].[UserID] = [RS].[USER_TBL].[UserID] and [RS].[TRANSACTION_TBL].ItemID = [RS].ITEM_TBL.ItemID and [RS].[USER_TBL].U_SubCategoryID = @U_SubCategoryID GROUP BY [MetaItemID], [RS].[USER_TBL].U_SubCategoryID ) x1 inner join (SELECT [RS].[TRANSACTION_TBL].[UserID] ,[MetaItemID] ,SUM([Quantity]) as QTY ,count(*) as FRE , [RS].[USER_TBL].U_SubCategoryID FROM [RS].[TRANSACTION_TBL], [RS].[USER_TBL], [RS].ITEM_TBL WHERE [RS].[TRANSACTION_TBL].[UserID] = [RS].[USER_TBL].[UserID] and [RS].[TRANSACTION_TBL].ItemID = [RS].ITEM_TBL.ItemID and [RS].[USER_TBL].U_SubCategoryID = @U_SubCategoryID GROUP BY [RS].[TRANSACTION_TBL].[UserID], [MetaItemID], [RS].[USER_TBL].U_SubCategoryID ) x2 on (x1.U_SubCategoryID = x2.U_SubCategoryID and x1.[MetaItemID] = x2.[MetaItemID]) WHERE ((x2.QTY*1.0/x1.M*1.0) + (x2.FRE*1.0/x1.N*1.0))/2.0 > 0 ";
            Dictionary<string, object> parameters = new Dictionary<string, object>();

            string str1 =Alpha.ToString(CultureInfo.CreateSpecificCulture("en-GB"));
            string str2 = (1-Alpha).ToString(CultureInfo.CreateSpecificCulture("en-GB"));

            parameters.Add("@Alpha1",str1);
            parameters.Add("@Alpha2",str2);
            parameters.Add("@U_SubCategoryID", U_SubCategoryID);
            SqlDataReader dr = executeReader(strSelect, parameters);

            List<MatrixItem> list = new List<MatrixItem>();
            while (dr.Read())
            {
                double rate = 0;
                try
                {
                    rate = (dr[1] == System.DBNull.Value) ? 0.0 : Convert.ToDouble(dr[2].ToString());
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                MatrixItem obj = new MatrixItem();
                obj.Row = dr.GetString(dr.GetOrdinal("UserID"));
                obj.Column = dr.GetString(dr.GetOrdinal("MetaItemID"));
                obj.Cell = rate;
                list.Add(obj);
            }
            dr.Close();
            return list;
        }
Example #9
0
        //---------------------------------------------------------------------------------------
        //This function computes the Confident Matrix and Weight Matrix, forllowing the formulars
        //Created by Vuong Minh Tuan
        //Corrected by MC. NGUYEN 21.10.2014.
        //---------------------------------------------------------------------------------------
        public void ComputeConfident(Predict_DAO_MC dao)
        {
            dao.CLEAN_CONF_Matrix();
            dao.CLEAN_WEIG_Matrix();

            List<string> list_ClusterID = dao.getAllClusterID();
            foreach (var item in list_ClusterID)
            {
                List<Transac> u_transacs = dao.getTransac_ForMatrix_ByClusterID_V2(item);
                if (u_transacs.Count > 0)
                {
                    // Get QuantityMatrix - MC OK
                    Dictionary<string, int> QM_dic_users = new Dictionary<string, int>();
                    Dictionary<string, int> QM_dic_items = new Dictionary<string, int>();
                    double[][] QuantityMatrix = Util.getQuantityMatrix_FromTransac_ByMetaItemID(u_transacs, out QM_dic_users, out QM_dic_items);

                    if (QuantityMatrix[0].Length > 1)
                    {
                        // Compute Support One Item Matrix - MC OK
                        double[] S = new double[QuantityMatrix[0].Length];
                        for (int j = 0; j < QuantityMatrix[0].Length; j++)
                        {
                            double count = 0.0;
                            for (int i = 0; i < QuantityMatrix.Length; i++)
                                if (QuantityMatrix[i][j] > 0)
                                    count++;
                            S[j] = count;
                        }

                        // Compute Support Matrix - MC OK
                        double[][] SupportMatrix = new double[QuantityMatrix[0].Length][];
                        for (int i = 0; i < QuantityMatrix[0].Length; i++)
                            SupportMatrix[i] = new double[QuantityMatrix[0].Length];

                        for (int i = 0; i < QuantityMatrix.Length; i++)
                        {
                            for (int j = 0; j < QuantityMatrix[0].Length; j++)
                                for (int j2 = 0; j2 < QuantityMatrix[0].Length; j2++)
                                    if (QuantityMatrix[i][j] > 0 & QuantityMatrix[i][j2] > 0 & j != j2)
                                        SupportMatrix[j][j2]++;
                        }

                        // Compute CONF Matrix - MC OK
                        double[][] CONF = new double[QuantityMatrix[0].Length][];
                        for (int i = 0; i < QuantityMatrix[0].Length; i++)
                            CONF[i] = new double[QuantityMatrix[0].Length];

                        for (int i = 0; i < QuantityMatrix[0].Length; i++)
                        {
                            for (int j = 0; j < QuantityMatrix[0].Length; j++)
                                if (i == j)
                                    CONF[i][j] = 1.0;
                                else if (S[i] > 0)
                                    CONF[i][j] = SupportMatrix[i][j] / S[i];
                        }

                        // Compute WEIG Matrix - MC OK
                        double[][] WEIG = new double[QuantityMatrix[0].Length][];
                        for (int i = 0; i < QuantityMatrix[0].Length; i++)
                            WEIG[i] = new double[QuantityMatrix[0].Length];

                        for (int i = 0; i < QuantityMatrix[0].Length; i++)
                        {
                            for (int j = 0; j < QuantityMatrix[0].Length; j++)
                                if (i != j)
                                    WEIG[i][j] = computeWEIG(i, j, SupportMatrix, QuantityMatrix);
                        }

                        // Save CONF Matrix - MC - OK
                        for (int i = 0; i < CONF.Length; i++)
                            for (int j = 0; j < CONF[0].Length; j++)
                                if (CONF[i][j] > 0)
                                {
                                    MatrixItem matrixItem = new MatrixItem();
                                    matrixItem.Row = Util.FindKeyByValue(QM_dic_items, i);
                                    matrixItem.Column = Util.FindKeyByValue(QM_dic_items, j);
                                    matrixItem.Cell = CONF[i][j];
                                    matrixItem.ClusterID = item;
                                    dao.setCONF_Matrix(matrixItem);
                                }

                        // Save WEIG Matrix
                        for (int i = 0; i < WEIG.Length; i++)
                            for (int j = 0; j < WEIG[0].Length; j++)
                                if (WEIG[i][j] > 0)
                                {
                                    MatrixItem matrixItem = new MatrixItem();
                                    matrixItem.Row = Util.FindKeyByValue(QM_dic_items, i);
                                    matrixItem.Column = Util.FindKeyByValue(QM_dic_items, j);
                                    matrixItem.Cell = WEIG[i][j];
                                    matrixItem.ClusterID = item;
                                    dao.setWEIG_Matrix(matrixItem);
                                }
                    }
                }
            }
        }
Example #10
0
        //---------------------------------------------------------------------------------------
        //This function computes the DISTANCE matrix and inserts it into the database
        //This is the distance between the two columns of the Rating Matrix
        //Created by Vuong Minh Tuan
        //Corrected all errors by MC. NGUYEN 21.10.2014.
        //---------------------------------------------------------------------------------------
        public void ComputeDIST(Predict_DAO_MC dao)
        {
            dao.CLEAN_DIST_Matrix();

            List<string> list_ClusterID = dao.getAllClusterID();
            foreach (var item in list_ClusterID)
            {
                Dictionary<string, int> RM_dic_users = new Dictionary<string, int>();
                Dictionary<string, int> RM_dic_items = new Dictionary<string, int>();
                // Compute ratting matrix
                List<MatrixItem> lstRMI = dao.getRattingMatrixItem_ByClusterID(item);
                double[][] RattingMatrix = Util.toMatrix_MatrixItem(lstRMI, out RM_dic_users, out RM_dic_items);

                if (RattingMatrix.Length > 0 & RM_dic_items.Count > 1)
                {
                    double[][] DIST = new double[RattingMatrix[0].Length][];
                    for (int i = 0; i < RattingMatrix[0].Length; i++)
                        DIST[i] = new double[RattingMatrix[0].Length];

                    // Compute DIST Matrix
                    for (int i1 = 0; i1 < RattingMatrix[0].Length - 1; i1++)
                        for (int i2 = i1 + 1; i2 < RattingMatrix[0].Length; i2++)
                        {
                            double value = 0.0;
                            for (int t = 0; t < RattingMatrix.Length; t++)
                                value += Math.Pow(RattingMatrix[t][i1] - RattingMatrix[t][i2], 2);
                            value = Math.Sqrt(value);
                            DIST[i1][i2] = value;
                            DIST[i2][i1] = value;
                        }

                    if (DIST.Length > 0)
                    {
                        double max = DIST[0][0];
                        // Find Max
                        for (int i = 0; i < DIST.Length; i++)
                        {
                            double localMax = DIST[i].Max();
                            if (localMax > max)
                                max = localMax;
                        }

                        // Standardization
                        if (max != 0)
                        {
                            for (int i = 0; i < DIST.Length; i++)
                                for (int j = 0; j < DIST[0].Length; j++)
                                    DIST[i][j] /= max;
                        }

                        // Save to Database
                        for (int i = 0; i < DIST.Length; i++)
                            for (int j = 0; j < DIST[0].Length; j++)
                                if (DIST[i][j] > 0)
                                {
                                    MatrixItem matrixItem = new MatrixItem();
                                    matrixItem.Row = Util.FindKeyByValue(RM_dic_items, i);
                                    matrixItem.Column = Util.FindKeyByValue(RM_dic_items, j);
                                    matrixItem.Cell = DIST[i][j];
                                    matrixItem.ClusterID = item;
                                    dao.setDIST_Matrix(matrixItem);
                                }
                    }
                }
            }
        }