public void testStrategy() { FastIDSet allItemIDs = new FastIDSet(); allItemIDs.AddAll(new long[] { 1L, 2L, 3L }); FastIDSet preferredItemIDs = new FastIDSet(1); preferredItemIDs.Add(2L); var dataModelMock = new DynamicMock( typeof( IDataModel )); dataModelMock.ExpectAndReturn("GetNumItems", 3); dataModelMock.ExpectAndReturn("GetItemIDs", allItemIDs.GetEnumerator()); IPreferenceArray prefArrayOfUser123 = new GenericUserPreferenceArray( new List<IPreference>() { new GenericPreference(123L, 2L, 1.0f) } ); ICandidateItemsStrategy strategy = new AllUnknownItemsCandidateItemsStrategy(); //EasyMock.replay(dataModel); FastIDSet candidateItems = strategy.GetCandidateItems(123L, prefArrayOfUser123, (IDataModel)dataModelMock.MockInstance); Assert.AreEqual(2, candidateItems.Count() ); Assert.True(candidateItems.Contains(1L)); Assert.True(candidateItems.Contains(3L)); dataModelMock.Verify(); //EasyMock.verify(dataModel); }
public void testStrategy() { List<IPreference> prefsOfUser123 = new List<IPreference>(); prefsOfUser123.Add(new GenericPreference(123L, 1L, 1.0f)); List<IPreference> prefsOfUser456 = new List<IPreference>(); prefsOfUser456.Add(new GenericPreference(456L, 1L, 1.0f)); prefsOfUser456.Add(new GenericPreference(456L, 2L, 1.0f)); List<IPreference> prefsOfUser789 = new List<IPreference>(); prefsOfUser789.Add(new GenericPreference(789L, 1L, 0.5f)); prefsOfUser789.Add(new GenericPreference(789L, 3L, 1.0f)); IPreferenceArray prefArrayOfUser123 = new GenericUserPreferenceArray(prefsOfUser123); FastByIDMap<IPreferenceArray> userData = new FastByIDMap<IPreferenceArray>(); userData.Put(123L, prefArrayOfUser123); userData.Put(456L, new GenericUserPreferenceArray(prefsOfUser456)); userData.Put(789L, new GenericUserPreferenceArray(prefsOfUser789)); IDataModel dataModel = new GenericDataModel(userData); ICandidateItemsStrategy strategy = new SamplingCandidateItemsStrategy(1, 1, 1, dataModel.GetNumUsers(), dataModel.GetNumItems()); FastIDSet candidateItems = strategy.GetCandidateItems(123L, prefArrayOfUser123, dataModel); Assert.True(candidateItems.Count() <= 1); Assert.False(candidateItems.Contains(1L)); }
public void testStrategy() { FastIDSet itemIDsFromUser123 = new FastIDSet(); itemIDsFromUser123.Add(1L); FastIDSet itemIDsFromUser456 = new FastIDSet(); itemIDsFromUser456.Add(1L); itemIDsFromUser456.Add(2L); List<IPreference> prefs = new List<IPreference>(); prefs.Add(new GenericPreference(123L, 1L, 1.0f)); prefs.Add(new GenericPreference(456L, 1L, 1.0f)); IPreferenceArray preferencesForItem1 = new GenericItemPreferenceArray(prefs); var dataModelMock = new DynamicMock(typeof(IDataModel)); dataModelMock.ExpectAndReturn("GetPreferencesForItem", preferencesForItem1, (1L)); dataModelMock.ExpectAndReturn("GetItemIDsFromUser", itemIDsFromUser123, (123L)); dataModelMock.ExpectAndReturn("GetItemIDsFromUser", itemIDsFromUser456, (456L)); IPreferenceArray prefArrayOfUser123 = new GenericUserPreferenceArray( new List<IPreference>() {new GenericPreference(123L, 1L, 1.0f)} ); ICandidateItemsStrategy strategy = new PreferredItemsNeighborhoodCandidateItemsStrategy(); //EasyMock.replay(dataModel); FastIDSet candidateItems = strategy.GetCandidateItems(123L, prefArrayOfUser123, (IDataModel)dataModelMock.MockInstance); Assert.AreEqual(1, candidateItems.Count()); Assert.True(candidateItems.Contains(2L)); dataModelMock.Verify(); // EasyMock.verify(dataModel); }
public ActionResult Recommend(string filmIdsJson) { var filmIds = (new JavaScriptSerializer()).Deserialize<long[]>(filmIdsJson); var pathToDataFile = Path.Combine(System.Web.HttpRuntime.AppDomainAppPath, "data/albums.dat"); if (dataModel == null) { try { dataModel = new FileDataModel(pathToDataFile, false, FileDataModel.DEFAULT_MIN_RELOAD_INTERVAL_MS, false); } catch (Exception e) { var exe = e.ToString(); } } var plusAnonymModel = new PlusAnonymousUserDataModel(dataModel); var prefArr = new GenericUserPreferenceArray(filmIds.Length); prefArr.SetUserID(0, PlusAnonymousUserDataModel.TEMP_USER_ID); for (int i = 0; i < filmIds.Length; i++) { prefArr.SetItemID(i, filmIds[i]); prefArr.SetValue(i, 5); // lets assume max rating } plusAnonymModel.SetTempPrefs(prefArr); var similarity = new LogLikelihoodSimilarity(plusAnonymModel); var neighborhood = new NearestNUserNeighborhood(15, similarity, plusAnonymModel); var recommender = new GenericBooleanPrefUserBasedRecommender(plusAnonymModel, neighborhood, similarity); var recommendedItems = recommender.Recommend(PlusAnonymousUserDataModel.TEMP_USER_ID, 5, null); return Json( recommendedItems.Select(ri => new Dictionary<string, object>() { {"id", ri.GetItemID() }, {"rating", ri.GetValue() }, }).ToArray() ); }
public void testHasPref() { IPreferenceArray prefs = new GenericUserPreferenceArray(3); prefs.Set(0, new GenericPreference(1L, 3L, 5.0f)); Assert.True(prefs.HasPrefWithItemID(3L)); Assert.True(prefs.HasPrefWithUserID(1L)); Assert.False(prefs.HasPrefWithItemID(2L)); Assert.False(prefs.HasPrefWithUserID(2L)); }
public void testUserID() { IPreferenceArray prefs = new GenericUserPreferenceArray(3); Assert.AreEqual(3, prefs.Length()); prefs.SetUserID(0, 1L); Assert.AreEqual(1L, prefs.GetUserID(0)); Assert.AreEqual(1L, prefs.GetUserID(1)); Assert.AreEqual(1L, prefs.GetUserID(2)); }
public void testItemID() { IPreferenceArray prefs = new GenericUserPreferenceArray(3); Assert.AreEqual(3, prefs.Length()); prefs.SetItemID(0, 1L); prefs.SetItemID(1, 2L); prefs.SetItemID(2, 3L); Assert.AreEqual(1L, prefs.GetItemID(0)); Assert.AreEqual(2L, prefs.GetItemID(1)); Assert.AreEqual(3L, prefs.GetItemID(2)); }
public void testClone() { GenericUserPreferenceArray prefs = new GenericUserPreferenceArray(3); prefs.Set(0, new GenericPreference(1L, 3L, 5.0f)); prefs.Set(1, new GenericPreference(1L, 1L, 4.0f)); prefs.Set(2, new GenericPreference(1L, 2L, 3.0f)); prefs = (GenericUserPreferenceArray)prefs.Clone(); Assert.AreEqual(3L, prefs.GetItemID(0)); Assert.AreEqual(1L, prefs.GetUserID(1)); Assert.AreEqual(3.0f, prefs.GetValue(2), EPSILON); }
public void testSetValue() { IPreferenceArray prefs = new GenericUserPreferenceArray(3); Assert.AreEqual(3, prefs.Length()); prefs.SetValue(0, 1.0f); prefs.SetValue(1, 2.0f); prefs.SetValue(2, 3.0f); Assert.AreEqual(1.0f, prefs.GetValue(0), EPSILON); Assert.AreEqual(2.0f, prefs.GetValue(1), EPSILON); Assert.AreEqual(3.0f, prefs.GetValue(2), EPSILON); }
public void testSetAndGetTempPreferences() { PlusAnonymousConcurrentUserDataModel instance = getTestableWithoutDelegateData(10); long anonymousUserID = instance.TakeAvailableUser().Value; IPreferenceArray tempPrefs = new GenericUserPreferenceArray(1); tempPrefs.SetUserID(0, anonymousUserID); tempPrefs.SetItemID(0, 1); instance.SetTempPrefs(tempPrefs, anonymousUserID); Assert.AreEqual(tempPrefs, instance.GetPreferencesFromUser(anonymousUserID)); instance.ReleaseUser(anonymousUserID); }
public void testSort() { IPreferenceArray prefs = new GenericUserPreferenceArray(3); prefs.Set(0, new GenericPreference(1L, 3L, 5.0f)); prefs.Set(1, new GenericPreference(1L, 1L, 5.0f)); prefs.Set(2, new GenericPreference(1L, 2L, 5.0f)); prefs.SortByItem(); Assert.AreEqual(1L, prefs.GetItemID(0)); Assert.AreEqual(2L, prefs.GetItemID(1)); Assert.AreEqual(3L, prefs.GetItemID(2)); }
/// Take the next available concurrent anonymous users from the pool. /// /// @return User ID or null if no more users are available public long?TakeAvailableUser() { long takenUserID; if (usersPool.TryDequeue(out takenUserID)) { // Initialize the preferences array to indicate that the user is taken. tempPrefs[takenUserID] = new GenericUserPreferenceArray(0); return(takenUserID); } return(null); }
public void testGetNumAnonymousUsers() { PlusAnonymousConcurrentUserDataModel instance = getTestableWithoutDelegateData(10); long anonymousUserID1 = instance.TakeAvailableUser().Value; IPreferenceArray tempPrefs1 = new GenericUserPreferenceArray(1); tempPrefs1.SetUserID(0, anonymousUserID1); tempPrefs1.SetItemID(0, 1); instance.SetTempPrefs(tempPrefs1, anonymousUserID1); // Anonymous users should not be included into the universe. Assert.AreEqual(0, instance.GetNumUsers()); }
public void testGetNumUsersWithDelegateUsersOnly() { IPreferenceArray prefs = new GenericUserPreferenceArray(1); long sampleUserID = 1; prefs.SetUserID(0, sampleUserID); long sampleItemID = 11; prefs.SetItemID(0, sampleItemID); FastByIDMap<IPreferenceArray> delegatePreferences = new FastByIDMap<IPreferenceArray>(); delegatePreferences.Put(sampleUserID, prefs); PlusAnonymousConcurrentUserDataModel instance = getTestableWithDelegateData(10, delegatePreferences); Assert.AreEqual(1, instance.GetNumUsers()); }
public void testGetPreferencesForNonAnonymousUser() { IPreferenceArray prefs = new GenericUserPreferenceArray(1); long sampleUserID = 1; prefs.SetUserID(0, sampleUserID); long sampleItemID = 11; prefs.SetItemID(0, sampleItemID); FastByIDMap <IPreferenceArray> delegatePreferences = new FastByIDMap <IPreferenceArray>(); delegatePreferences.Put(sampleUserID, prefs); PlusAnonymousConcurrentUserDataModel instance = getTestableWithDelegateData(10, delegatePreferences); Assert.AreEqual(prefs, instance.GetPreferencesFromUser(sampleUserID)); }
public void testGetPreferenceValue() { PlusAnonymousConcurrentUserDataModel instance = getTestableWithoutDelegateData(10); long anonymousUserID = instance.TakeAvailableUser().Value; IPreferenceArray tempPrefs = new GenericUserPreferenceArray(1); tempPrefs.SetUserID(0, anonymousUserID); long sampleItemID = 1; tempPrefs.SetItemID(0, sampleItemID); tempPrefs.SetValue(0, float.MaxValue); instance.SetTempPrefs(tempPrefs, anonymousUserID); Assert.AreEqual(float.MaxValue, instance.GetPreferenceValue(anonymousUserID, sampleItemID), EPSILON); }
public void testGetPreferencesForItem() { IPreferenceArray prefs = new GenericUserPreferenceArray(2); long sampleUserID = 4; prefs.SetUserID(0, sampleUserID); long sampleItemID = 11; prefs.SetItemID(0, sampleItemID); prefs.SetUserID(1, sampleUserID); long sampleItemID2 = 22; prefs.SetItemID(1, sampleItemID2); FastByIDMap <IPreferenceArray> delegatePreferences = new FastByIDMap <IPreferenceArray>(); delegatePreferences.Put(sampleUserID, prefs); PlusAnonymousConcurrentUserDataModel instance = getTestableWithDelegateData(10, delegatePreferences); long anonymousUserID = instance.TakeAvailableUser().Value; IPreferenceArray tempPrefs = new GenericUserPreferenceArray(2); tempPrefs.SetUserID(0, anonymousUserID); tempPrefs.SetItemID(0, sampleItemID); tempPrefs.SetUserID(1, anonymousUserID); long sampleItemID3 = 33; tempPrefs.SetItemID(1, sampleItemID3); instance.SetTempPrefs(tempPrefs, anonymousUserID); Assert.AreEqual(sampleUserID, instance.GetPreferencesForItem(sampleItemID).Get(0).GetUserID()); Assert.AreEqual(2, instance.GetPreferencesForItem(sampleItemID).Length()); Assert.AreEqual(1, instance.GetPreferencesForItem(sampleItemID2).Length()); Assert.AreEqual(1, instance.GetPreferencesForItem(sampleItemID3).Length()); Assert.AreEqual(2, instance.GetNumUsersWithPreferenceFor(sampleItemID)); Assert.AreEqual(1, instance.GetNumUsersWithPreferenceFor(sampleItemID, sampleItemID2)); Assert.AreEqual(1, instance.GetNumUsersWithPreferenceFor(sampleItemID, sampleItemID3)); }
public void preferencesFetchedOnlyOnce() { var dataModelMock = new DynamicMock( typeof( IDataModel) ); var itemSimilarityMock = new DynamicMock( typeof(IItemSimilarity) ); var candidateItemsStrategyMock = new DynamicMock( typeof (ICandidateItemsStrategy) ); var mostSimilarItemsCandidateItemsStrategyMock = new DynamicMock( typeof(IMostSimilarItemsCandidateItemsStrategy) ); IPreferenceArray preferencesFromUser = new GenericUserPreferenceArray( new List<IPreference>() {new GenericPreference(1L, 1L, 5.0f), new GenericPreference(1L, 2L, 4.0f)}); dataModelMock.ExpectAndReturn("GetMinPreference", float.NaN); dataModelMock.ExpectAndReturn("GetMaxPreference", float.NaN); dataModelMock.ExpectAndReturn("GetPreferencesFromUser", preferencesFromUser, 1L); var dataModel = (IDataModel)dataModelMock.MockInstance; candidateItemsStrategyMock.ExpectAndReturn("GetCandidateItems", new FastIDSet(new long[] { 3L, 4L }), 1L, preferencesFromUser, dataModel); itemSimilarityMock.ExpectAndReturn("ItemSimilarities", new double[] { 0.5, 0.3 }, 3L, preferencesFromUser.GetIDs()); itemSimilarityMock.ExpectAndReturn("ItemSimilarities", new double[] { 0.4, 0.1 }, 4L, preferencesFromUser.GetIDs()); //EasyMock.replay(dataModel, itemSimilarity, candidateItemsStrategy, mostSimilarItemsCandidateItemsStrategy); IRecommender recommender = new GenericItemBasedRecommender((IDataModel)dataModel, (IItemSimilarity)itemSimilarityMock.MockInstance, (ICandidateItemsStrategy)candidateItemsStrategyMock.MockInstance, (IMostSimilarItemsCandidateItemsStrategy)mostSimilarItemsCandidateItemsStrategyMock.MockInstance); recommender.Recommend(1L, 3); dataModelMock.Verify(); itemSimilarityMock.Verify(); candidateItemsStrategyMock.Verify(); mostSimilarItemsCandidateItemsStrategyMock.Verify(); //EasyMock.verify(dataModel, itemSimilarity, candidateItemsStrategy, mostSimilarItemsCandidateItemsStrategy); }
public void testGetPreferencesForItem() { IPreferenceArray prefs = new GenericUserPreferenceArray(2); long sampleUserID = 4; prefs.SetUserID(0, sampleUserID); long sampleItemID = 11; prefs.SetItemID(0, sampleItemID); prefs.SetUserID(1, sampleUserID); long sampleItemID2 = 22; prefs.SetItemID(1, sampleItemID2); FastByIDMap<IPreferenceArray> delegatePreferences = new FastByIDMap<IPreferenceArray>(); delegatePreferences.Put(sampleUserID, prefs); PlusAnonymousConcurrentUserDataModel instance = getTestableWithDelegateData(10, delegatePreferences); long anonymousUserID = instance.TakeAvailableUser().Value; IPreferenceArray tempPrefs = new GenericUserPreferenceArray(2); tempPrefs.SetUserID(0, anonymousUserID); tempPrefs.SetItemID(0, sampleItemID); tempPrefs.SetUserID(1, anonymousUserID); long sampleItemID3 = 33; tempPrefs.SetItemID(1, sampleItemID3); instance.SetTempPrefs(tempPrefs, anonymousUserID); Assert.AreEqual(sampleUserID, instance.GetPreferencesForItem(sampleItemID).Get(0).GetUserID()); Assert.AreEqual(2, instance.GetPreferencesForItem(sampleItemID).Length()); Assert.AreEqual(1, instance.GetPreferencesForItem(sampleItemID2).Length()); Assert.AreEqual(1, instance.GetPreferencesForItem(sampleItemID3).Length()); Assert.AreEqual(2, instance.GetNumUsersWithPreferenceFor(sampleItemID)); Assert.AreEqual(1, instance.GetNumUsersWithPreferenceFor(sampleItemID, sampleItemID2)); Assert.AreEqual(1, instance.GetNumUsersWithPreferenceFor(sampleItemID, sampleItemID3)); }
public void testGetUserIDs() { IPreferenceArray prefs = new GenericUserPreferenceArray(1); long sampleUserID = 1; prefs.SetUserID(0, sampleUserID); long sampleItemID = 11; prefs.SetItemID(0, sampleItemID); FastByIDMap<IPreferenceArray> delegatePreferences = new FastByIDMap<IPreferenceArray>(); delegatePreferences.Put(sampleUserID, prefs); PlusAnonymousConcurrentUserDataModel instance = getTestableWithDelegateData(10, delegatePreferences); long anonymousUserID = instance.TakeAvailableUser().Value; IPreferenceArray tempPrefs = new GenericUserPreferenceArray(1); tempPrefs.SetUserID(0, anonymousUserID); tempPrefs.SetItemID(0, 22); instance.SetTempPrefs(tempPrefs, anonymousUserID); var userIDs = instance.GetUserIDs(); userIDs.MoveNext(); Assert.AreEqual(sampleUserID, userIDs.Current); Assert.False(userIDs.MoveNext()); }
internal PreferenceView(GenericUserPreferenceArray arr, int i) { this.i = i; this.arr = arr; }
/// Take the next available concurrent anonymous users from the pool. /// /// @return User ID or null if no more users are available public long? TakeAvailableUser() { long takenUserID; if (usersPool.TryDequeue(out takenUserID)) { // Initialize the preferences array to indicate that the user is taken. tempPrefs[takenUserID] = new GenericUserPreferenceArray(0); return takenUserID; } return null; }
public void testSetMultipleTempPreferences() { PlusAnonymousConcurrentUserDataModel instance = getTestableWithoutDelegateData(10); long anonymousUserID1 = instance.TakeAvailableUser().Value; long anonymousUserID2 = instance.TakeAvailableUser().Value; IPreferenceArray tempPrefs1 = new GenericUserPreferenceArray(1); tempPrefs1.SetUserID(0, anonymousUserID1); tempPrefs1.SetItemID(0, 1); IPreferenceArray tempPrefs2 = new GenericUserPreferenceArray(2); tempPrefs2.SetUserID(0, anonymousUserID2); tempPrefs2.SetItemID(0, 2); tempPrefs2.SetUserID(1, anonymousUserID2); tempPrefs2.SetItemID(1, 3); instance.SetTempPrefs(tempPrefs1, anonymousUserID1); instance.SetTempPrefs(tempPrefs2, anonymousUserID2); Assert.AreEqual(tempPrefs1, instance.GetPreferencesFromUser(anonymousUserID1)); Assert.AreEqual(tempPrefs2, instance.GetPreferencesFromUser(anonymousUserID2)); }
public void testSortValue() { IPreferenceArray prefs = new GenericUserPreferenceArray(3); prefs.Set(0, new GenericPreference(1L, 3L, 5.0f)); prefs.Set(1, new GenericPreference(1L, 1L, 4.0f)); prefs.Set(2, new GenericPreference(1L, 2L, 3.0f)); prefs.SortByValue(); Assert.AreEqual(2L, prefs.GetItemID(0)); Assert.AreEqual(1L, prefs.GetItemID(1)); Assert.AreEqual(3L, prefs.GetItemID(2)); prefs.SortByValueReversed(); Assert.AreEqual(3L, prefs.GetItemID(0)); Assert.AreEqual(1L, prefs.GetItemID(1)); Assert.AreEqual(2L, prefs.GetItemID(2)); }