Exemplo n.º 1
0
        public override void RemovePreference(long userID, long itemID)
        {
            IDataModel dataModel = GetDataModel();
            float?     oldPref   = dataModel.GetPreferenceValue(userID, itemID);

            base.RemovePreference(userID, itemID);
            if (oldPref.HasValue)
            {
                lock (this) {
                    //buildAveragesLock.writeLock().lock();
                    IRunningAverage itemAverage = itemAverages.Get(itemID);
                    if (itemAverage == null)
                    {
                        throw new InvalidOperationException("No preferences exist for item ID: " + itemID);
                    }
                    itemAverage.RemoveDatum(oldPref.Value);
                    IRunningAverage userAverage = userAverages.Get(userID);
                    if (userAverage == null)
                    {
                        throw new InvalidOperationException("No preferences exist for user ID: " + userID);
                    }
                    userAverage.RemoveDatum(oldPref.Value);
                    overallAveragePrefValue.RemoveDatum(oldPref.Value);
                }/* finally {
                  * buildAveragesLock.writeLock().unlock();
                  * }*/
            }
        }
        public static void Evaluate(IRecommender recommender,
                                    IDataModel model,
                                    int samples,
                                    IRunningAverage tracker,
                                    String tag)
        {
            printHeader();
            var users = recommender.GetDataModel().GetUserIDs();

            while (users.MoveNext())
            {
                long             userID = users.Current;
                var              recs1  = recommender.Recommend(userID, model.GetNumItems());
                IPreferenceArray prefs2 = model.GetPreferencesFromUser(userID);
                prefs2.SortByValueReversed();
                FastIDSet commonSet = new FastIDSet();
                long      maxItemID = setBits(commonSet, recs1, samples);
                FastIDSet otherSet  = new FastIDSet();
                maxItemID = Math.Max(maxItemID, setBits(otherSet, prefs2, samples));
                int max = mask(commonSet, otherSet, maxItemID);
                max = Math.Min(max, samples);
                if (max < 2)
                {
                    continue;
                }
                long[] items1   = getCommonItems(commonSet, recs1, max);
                long[] items2   = getCommonItems(commonSet, prefs2, max);
                double variance = scoreCommonSubset(tag, userID, samples, max, items1, items2);
                tracker.AddDatum(variance);
            }
        }
 private void buildAverageDiffs()
 {
     lock (this) {
         //buildAveragesLock.writeLock().lock();
         IDataModel dataModel = GetDataModel();
         var        it        = dataModel.GetUserIDs();
         while (it.MoveNext())
         {
             IPreferenceArray prefs = dataModel.GetPreferencesFromUser(it.Current);
             int size = prefs.Length();
             for (int i = 0; i < size; i++)
             {
                 long            itemID  = prefs.GetItemID(i);
                 IRunningAverage average = itemAverages.Get(itemID);
                 if (average == null)
                 {
                     average = new FullRunningAverage();
                     itemAverages.Put(itemID, average);
                 }
                 average.AddDatum(prefs.GetValue(i));
             }
         }
     }
     //finally {
     //buildAveragesLock.writeLock().unlock();
     //}
 }
 public static void Evaluate(IRecommender recommender,
                             IDataModel model,
                             int samples,
                             IRunningAverage tracker,
                             String tag) {
   printHeader();
   var users = recommender.GetDataModel().GetUserIDs();
   while (users.MoveNext()) {
     long userID = users.Current;
     var recs1 = recommender.Recommend(userID, model.GetNumItems());
     IPreferenceArray prefs2 = model.GetPreferencesFromUser(userID);
     prefs2.SortByValueReversed();
     FastIDSet commonSet = new FastIDSet();
     long maxItemID = setBits(commonSet, recs1, samples);
     FastIDSet otherSet = new FastIDSet();
     maxItemID = Math.Max(maxItemID, setBits(otherSet, prefs2, samples));
     int max = mask(commonSet, otherSet, maxItemID);
     max = Math.Min(max, samples);
     if (max < 2) {
       continue;
     }
     long[] items1 = getCommonItems(commonSet, recs1, max);
     long[] items2 = getCommonItems(commonSet, prefs2, max);
     double variance = scoreCommonSubset(tag, userID, samples, max, items1, items2);
     tracker.AddDatum(variance);
   }
 }
        public override void SetPreference(long userID, long itemID, float value)
        {
            IDataModel dataModel = GetDataModel();
            double     prefDelta;

            try {
                float?oldPref = dataModel.GetPreferenceValue(userID, itemID);
                prefDelta = !oldPref.HasValue ? value : value - oldPref.Value;
            } catch (NoSuchUserException nsee) {
                prefDelta = value;
            }
            base.SetPreference(userID, itemID, value);
            lock (this) {
                //buildAveragesLock.writeLock().lock();
                IRunningAverage average = itemAverages.Get(itemID);
                if (average == null)
                {
                    IRunningAverage newAverage = new FullRunningAverage();
                    newAverage.AddDatum(prefDelta);
                    itemAverages.Put(itemID, newAverage);
                }
                else
                {
                    average.ChangeDatum(prefDelta);
                }
            }
            //finally {
            //buildAveragesLock.writeLock().unlock();
            //}
        }
Exemplo n.º 6
0
        private static void addDatumAndCreateIfNeeded(long itemID, float value, FastByIDMap <IRunningAverage> averages)
        {
            IRunningAverage itemAverage = averages.Get(itemID);

            if (itemAverage == null)
            {
                itemAverage = new FullRunningAverage();
                averages.Put(itemID, itemAverage);
            }
            itemAverage.AddDatum(value);
        }
 private float doEstimatePreference(long itemID)
 {
     lock (this) { // buildAveragesLock.readLock().lock();
                   //try {
         IRunningAverage average = itemAverages.Get(itemID);
         return(average == null ? float.NaN : (float)average.GetAverage());
         //} finally {
         //buildAveragesLock.readLock().unlock();
         //}
     }
 }
Exemplo n.º 8
0
 public ItemUserAverageRecommender(IDataModel dataModel) : base(dataModel)
 {
     this.itemAverages            = new FastByIDMap <IRunningAverage>();
     this.userAverages            = new FastByIDMap <IRunningAverage>();
     this.overallAveragePrefValue = new FullRunningAverage();
     //this.buildAveragesLock = new ReentrantReadWriteLock();
     this.refreshHelper = new RefreshHelper(() => {
         buildAverageDiffs();
     });
     refreshHelper.AddDependency(dataModel);
     buildAverageDiffs();
 }
Exemplo n.º 9
0
 private float doEstimatePreference(long userID, long itemID)
 {
     //buildAveragesLock.readLock().lock();
     lock (this) {
         IRunningAverage itemAverage = itemAverages.Get(itemID);
         if (itemAverage == null)
         {
             return(float.NaN);
         }
         IRunningAverage userAverage = userAverages.Get(userID);
         if (userAverage == null)
         {
             return(float.NaN);
         }
         double userDiff = userAverage.GetAverage() - overallAveragePrefValue.GetAverage();
         return((float)(itemAverage.GetAverage() + userDiff));
     } /*finally {
        * buildAveragesLock.readLock().unlock();
        * }*/
 }
Exemplo n.º 10
0
 public LoadStatistics(IRunningAverage timing) {
   this.timing = timing;
 }
Exemplo n.º 11
0
 public LoadStatistics(IRunningAverage timing)
 {
     this.timing = timing;
 }
Exemplo n.º 12
0
 protected override void reset()
 {
     average = new FullRunningAverage();
 }
 protected override void reset() {
   average = new FullRunningAverage();
 }
 public InvertedRunningAverage(IRunningAverage deleg) {
   this._Delegate = deleg;
 }
 public InvertedRunningAverage(IRunningAverage deleg)
 {
     this._Delegate = deleg;
 }