internal bool HasItems(ItemKey itemKey)
        {
            int count = 0;

            foreach (string itemId in _itemIds)
            {
                if (itemKey.HasItem(itemId))
                {
                    count++;
                }
            }

            return(count == itemKey.Count);
        }
        public int GetSupport(ItemKey itemId)
        {
            int count = 0;

            foreach (ShoppingRecord record in _dataset.Values)
            {
                if (record.HasItems(itemId))
                {
                    count++;
                }
            }

            return(count);
        }
        private ItemKey GetKey(ItemKey itemKey1, ItemKey itemKey2)
        {
            SortedSet <string> itemKeys = new SortedSet <string>();

            foreach (string key in itemKey1.Ids)
            {
                itemKeys.Add(key);
            }

            foreach (string key in itemKey2.Ids)
            {
                itemKeys.Add(key);
            }
            return(itemKeys.Count != itemKey1.Count + 1 ? null : new ItemKey(itemKeys));
        }
        // https://stackoverflow.com/questions/7802822/all-possible-combinations-of-a-list-of-values
        // 取得組合後的 ItemKey
        public List <ItemKey> GetAllCombinationIds2()
        {
            var result = new SortedSet <ItemKey>(new ItemKeyComparar());

            for (int i = 0; i < _records.Count; i++)
            {
                for (int j = i + 1; j < _records.Count; j++)
                {
                    ItemKey key = GetKey(_records[i].ItemKey, _records[j].ItemKey);
                    if (key != null)
                    {
                        result.Add(key);
                    }
                }
            }

            return(result.ToList());
        }
        public double GetResult(double minSupport, List <string> boughtItemIds, List <string> forecastItemIds)
        {
            CalculateSupport(minSupport);

            _sw.Reset();
            _sw.Start();

            try
            {
                // todo: 排序項目, 計算機率
                var boughtKey   = new ItemKey(boughtItemIds);
                int boughtCount = _resultDict.ContainsKey(boughtKey.CombinationId) ?
                                  _resultDict[boughtKey.CombinationId].Count : 0;

                if (boughtCount == 0)
                {
                    return(0);
                }

                var allIds = boughtItemIds.Concat(forecastItemIds).ToList();
                var allKey = new ItemKey(allIds);

                if (_resultDict.ContainsKey(allKey.CombinationId) == false)
                {
                    return(0);
                }

                int allCount = _resultDict.ContainsKey(boughtKey.CombinationId) ?
                               _resultDict[allKey.CombinationId].Count : 0;

                return(allCount / (boughtCount / 1d));   // 轉成 double
            }
            finally
            {
                _sw.Stop();
                _timeDict[nameof(GetResult)] = _sw.Elapsed.TotalMilliseconds;
            }
        }
Exemple #6
0
        public double GetResult(double minSupport, List <string> boughtItemIds, List <string> forecastItemIds)
        {
            // Scan Data
            var    itemKeys   = _dataset.GetSortHeaderTable();
            var    supDataset = _dataset.GetFilter(itemKeys);
            FPNode tree       = CreateFPTree(supDataset);

            CalculateSupport(tree, minSupport);

            _sw.Reset();
            _sw.Start();

            try
            {
                // todo: 排序項目, 計算機率
                var boughtKey   = new ItemKey(boughtItemIds);
                int boughtCount = _resultDict.ContainsKey(boughtKey.CombinationId) ?
                                  _resultDict[boughtKey.CombinationId].Count : 0;

                if (boughtCount == 0)
                {
                    return(0);
                }

                var allIds   = boughtItemIds.Concat(forecastItemIds).ToList();
                var allKey   = new ItemKey(allIds);
                int allCount = _resultDict.ContainsKey(boughtKey.CombinationId) ?
                               _resultDict[allKey.CombinationId].Count : 0;

                return(allCount / (boughtCount / 1d));   // 轉成 double
            }
            finally
            {
                _sw.Stop();
                _timeDict[nameof(GetResult)] = _sw.Elapsed.TotalMilliseconds;
            }
        }
 public AprioriRecord(ItemKey itemKey, int count)
 {
     ItemKey = itemKey;
     Count   = count;
 }