Beispiel #1
0
            public void CreateDiscrete()
            {
                /*
                 * 5  3  0  1
                 * 4  0  0  1
                 * 1  1  0  5
                 * 1  0  0  4
                 * 0  1  5  4
                 */
                DataMatrix R = GetSampleRatingMatrix();

                // act
                PrefRelations PR = PrefRelations.CreateDiscrete(R);

                // assert
                foreach (KeyValuePair <int, SparseMatrix> user in PR.PreferenceRelationsByUser)
                {
                    int          indexOfUser       = user.Key;
                    SparseMatrix preferencesOfUser = user.Value;

                    // Note that the diagonal (item compares to itsself) is elft empty
                    Debug.Assert(preferencesOfUser.Trace() == 0);

                    // Check if the correct number of preference relations have been created
                    Debug.Assert((Math.Pow(R.GetNonZerosCountOfRow(indexOfUser), 2)
                                  - R.GetNonZerosCountOfRow(indexOfUser))
                                 == preferencesOfUser.NonZerosCount);
                }

                // Check if the first user's preferences are correct
                Debug.WriteLine("PR[0][0, 0]=" + PR[0][0, 0]);
                Debug.Assert(PR[0][0, 0] == SparseMatrix.Zero);
                Debug.Assert(PR[0][0, 1] == Config.Preferences.Preferred);
                Debug.Assert(PR[0][1, 0] == Config.Preferences.LessPreferred);
                Debug.Assert(PR[0][0, 2] == SparseMatrix.Zero);
                Debug.Assert(PR[0][2, 0] == SparseMatrix.Zero);
                Debug.Assert(PR[0][1, 2] == SparseMatrix.Zero);
                Debug.Assert(PR[0][2, 1] == SparseMatrix.Zero);
                Debug.Assert(PR[0][1, 3] == Config.Preferences.Preferred);
                Debug.Assert(PR[0][3, 1] == Config.Preferences.LessPreferred);

                // Check if the last user's preferences are correct
                Debug.Assert(PR[4][1, 1] == SparseMatrix.Zero);
                Debug.Assert(PR[4][0, 1] == SparseMatrix.Zero);
                Debug.Assert(PR[4][1, 0] == SparseMatrix.Zero);
                Debug.Assert(PR[4][0, 2] == SparseMatrix.Zero);
                Debug.Assert(PR[4][2, 0] == SparseMatrix.Zero);
                Debug.Assert(PR[4][1, 2] == Config.Preferences.LessPreferred);
                Debug.Assert(PR[4][2, 1] == Config.Preferences.Preferred);
                Debug.Assert(PR[4][1, 3] == Config.Preferences.LessPreferred);
                Debug.Assert(PR[4][3, 1] == Config.Preferences.Preferred);
            }
Beispiel #2
0
            public void PreferencesToPositions()
            {
                /*
                 * 5  3  0  1
                 * 4  0  0  1
                 * 1  1  0  5
                 * 1  0  0  4
                 * 0  1  5  4
                 */
                DataMatrix    R  = GetSampleRatingMatrix();
                PrefRelations PR = PrefRelations.CreateDiscrete(R);

                // act
                // Convert first, Third, and last users' preferences to positions
                Vector <double> positionsOfUserFirst = PR.PreferencesToPositions(PR[0]);
                Vector <double> positionsOfUserThird = PR.PreferencesToPositions(PR[2]);
                Vector <double> positionsOfUserLast  = PR.PreferencesToPositions(PR[4]);

                // assert
                // Check first user
                Debug.Assert(positionsOfUserFirst[0] == 1 + Config.Preferences.PositionShift);
                Debug.Assert(positionsOfUserFirst[1] == 0 + Config.Preferences.PositionShift);
                Debug.Assert(positionsOfUserFirst[2] == SparseMatrix.Zero);
                Debug.Assert(positionsOfUserFirst[3] == -1 + Config.Preferences.PositionShift);

                // Check third user
                Debug.Assert(positionsOfUserThird[0] == -0.5 + Config.Preferences.PositionShift);
                Debug.Assert(positionsOfUserThird[1] == -0.5 + Config.Preferences.PositionShift);
                Debug.Assert(positionsOfUserThird[2] == SparseMatrix.Zero);
                Debug.Assert(positionsOfUserThird[3] == 1 + Config.Preferences.PositionShift);

                // Check second last user
                Debug.Assert(positionsOfUserLast[0] == SparseMatrix.Zero);
                Debug.Assert(positionsOfUserLast[1] == -1 + Config.Preferences.PositionShift);
                Debug.Assert(positionsOfUserLast[2] == 1 + Config.Preferences.PositionShift);
                Debug.Assert(positionsOfUserLast[3] == 0 + Config.Preferences.PositionShift);

                // The number of positions should match the number of ratings by each user
                Debug.Assert(positionsOfUserFirst.GetNonZerosCount()
                             == R.GetNonZerosCountOfRow(0), String.Format("{0}=={1}",
                                                                          positionsOfUserFirst.GetNonZerosCount(), R.GetNonZerosCountOfRow(0)));

                Debug.Assert(positionsOfUserThird.GetNonZerosCount()
                             == R.GetNonZerosCountOfRow(2), String.Format("{0}=={1}",
                                                                          positionsOfUserThird.GetNonZerosCount(), R.GetNonZerosCountOfRow(2)));

                Debug.Assert(positionsOfUserLast.GetNonZerosCount()
                             == R.GetNonZerosCountOfRow(4), String.Format("{0}=={1}",
                                                                          positionsOfUserLast.GetNonZerosCount(), R.GetNonZerosCountOfRow(4)));
            }