Exemplo n.º 1
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
        //---------------------------------------------------------------------------------------
        public void ComputeDIST(Predict_DAO_MCol 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);
                                }
                    }
                }
            }
        }