示例#1
0
文件: Predict.cs 项目: vutiendung/RS
        //---------------------------------------------------------------------------------------
        //This function computes the Confident Matrix and Weight Matrix, forllowing the formulars
        //---------------------------------------------------------------------------------------

        public void ComputeConfident(Predict_DAO_MCol 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
                    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
                        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
                        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
                        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
                        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
                        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);
                                }
                            }
                        }
                    }
                }
            }
        }