Ejemplo n.º 1
0
        private void UpdateFPTree(FPTree tree, IList <T> items, int count)
        {
            var treeNode = tree.Root;

            foreach (var item in items)
            {
                if (treeNode.Children.TryGetValue(item, out var nextTree))
                {
                    nextTree.Count += count;
                    treeNode        = nextTree;
                }
                else
                {
                    var headerItem = tree.HeaderTable[item];
                    var newNode    = new TreeNode()
                    {
                        Name     = item,
                        Count    = count,
                        Parent   = treeNode,
                        NodeLink = headerItem.Node,
                    };
                    headerItem.Node = newNode;
                    treeNode.Children.Add(item, newNode);
                    treeNode = newNode;
                }
            }
        }
Ejemplo n.º 2
0
        private void setEscenario4()
        {
            Dictionary <List <string>, int> transacc = new Dictionary <List <string>, int>();
            List <string> tranUniq = new List <string> {
                "a", "c", "b"
            };

            transacc.Add(tranUniq, 2);
            List <string> tranUniq2 = new List <string> {
                "d", "e"
            };

            transacc.Add(tranUniq2, 3);
            List <string> tranUniq3 = new List <string> {
                "b", "c"
            };

            transacc.Add(tranUniq3, 2);
            List <string> tranUniq4 = new List <string> {
                "b", "d"
            };

            transacc.Add(tranUniq4, 3);
            prueba = new FPTree();
            prueba.ConstructFPTree(transacc, 4);
        }
Ejemplo n.º 3
0
        public FPTree CreateFPTree(IEnumerable <IEnumerable <T> > trans, IEnumerable <int> count, int minSupport)
        {
            count = count ?? Enumerable.Repeat(1, int.MaxValue);
            var tree = new FPTree();

            tree.HeaderTable = this.InitHeaderTable(trans, count, minSupport);
            tree.Root        = new TreeNode()
            {
                Name  = default(T),
                Count = 0,
            };

            foreach (var pair in Enumerable.Zip(trans, count, (x, y) => Tuple.Create(x, y)))
            {
                var items = pair.Item1
                            .Where(x => tree.HeaderTable.ContainsKey(x))
                            .Select(x => Tuple.Create(x, tree.HeaderTable[x].Count))
                            .OrderByDescending(x => x.Item2)
                            .ThenBy(x => x.Item1)
                            .Select(x => x.Item1)
                            .ToList();
                this.UpdateFPTree(tree, items, pair.Item2);
            }

            return(tree);
        }
        public AssociationAnalyzerFPGrowth(DataManager data, double minSupport, double minConfidence)
        {
            List <List <String> > transactions = new List <List <string> >();

            this.minSupport    = minSupport;
            this.minConfidence = minConfidence;
            for (int i = 0; i < data.listOfAllTransactions.Count; i++)
            {
                if (data.listOfAllTransactions[i].MapFromItemToQuantity.Count > 0)
                {
                    List <string> items = new List <string>();
                    foreach (Item it in data.listOfAllTransactions[i].MapFromItemToQuantity.Keys)
                    {
                        if (it.minQuantity > data.listOfAllTransactions[i].MapFromItemToQuantity[it])
                        {
                            it.minQuantity = data.listOfAllTransactions[i].MapFromItemToQuantity[it];
                        }
                        if (it.maxQuantity < data.listOfAllTransactions[i].MapFromItemToQuantity[it])
                        {
                            it.maxQuantity = data.listOfAllTransactions[i].MapFromItemToQuantity[it];
                        }
                        items.Add(it.ItemCode);
                    }
                    transactions.Add(items);
                }
            }
            TransactionCodes = transactions;
            fptree           = new FPTree(transactions, minSupport);
        }
Ejemplo n.º 5
0
        private void setEscenario3()
        {
            List <List <string> > transacc = new List <List <string> >();
            List <string>         tranUniq = new List <string> {
                "b", "a", "c"
            };

            transacc.Add(tranUniq);
            List <string> tranUniq2 = new List <string> {
                "c", "a", "d"
            };

            transacc.Add(tranUniq2);
            List <string> tranUniq3 = new List <string> {
                "a", "d"
            };

            transacc.Add(tranUniq3);
            List <string> tranUniq4 = new List <string> {
                "c", "e"
            };

            transacc.Add(tranUniq4);
            List <string> tranUniq5 = new List <string> {
                "b", "f"
            };

            transacc.Add(tranUniq5);
            List <string> tranUniq6 = new List <string> {
                "d", "c"
            };

            transacc.Add(tranUniq6);
            prueba = new FPTree(transacc, 0.1);
        }
Ejemplo n.º 6
0
        public void TestAddTransaction2()
        {
            arbol = new FPTree();

            List <string> lista = new List <string>();

            lista.Add("0");
            lista.Add("1");
            lista.Add("2");
            lista.Add("4");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("3");
            lista.Add("2");
            lista.Add("1");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("0");
            lista.Add("2");
            lista.Add("3");
            arbol.addTransaction(lista);
            //talba de header
            //Assert.AreEqual(headersTable.Count(), 5);
        }
Ejemplo n.º 7
0
        public void setupEscenario2()
        {
            arbol = new FPTree();
            List <string> lista = new List <string>();

            lista.Add("10");
            lista.Add("2");
            lista.Add("8");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("7");
            lista.Add("9");
            lista.Add("3");
            lista.Add("4");
            lista.Add("8");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("1");
            lista.Add("2");
            lista.Add("10");
            lista.Add("8");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("9");
            lista.Add("10");
            lista.Add("2");
            lista.Add("5");
            lista.Add("7");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("1");
            lista.Add("10");
            lista.Add("5");
            lista.Add("4");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("3");
            lista.Add("8");
            lista.Add("4");
            lista.Add("6");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("7");
            lista.Add("3");
            lista.Add("10");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("6");
            lista.Add("7");
            lista.Add("1");
            lista.Add("4");
            arbol.addTransaction(lista);
        }
Ejemplo n.º 8
0
        public void Add_FirstTransaction_ReturnOneTransactionsCount()
        {
            FPTree tree = new FPTree();

            tree.AddTransaction(Transaction.Create(1, 2, 3));

            Assert.AreEqual(1, tree.TransactionsCount);
        }
Ejemplo n.º 9
0
        public void GetItemPrefixPath_TwoTransactionWithTheSamePrefixAdded_ReturnsPathWithOneItem()
        {
            FPTree tree = new FPTree();

            tree.AddTransaction(Transaction.Create(2, 3));
            tree.AddTransaction(Transaction.Create(2, 3, 4));

            FPTreePrefixPathItem[] path = tree.GetItemPrefixPath(2).ToArray();

            Assert.AreEqual(1, path.Length);
            Assert.AreEqual(2, path[0].FPTreeNode.Support);
        }
Ejemplo n.º 10
0
        public void GetHeaderItemPath_TwoTransactionWithTheSameItemOnDifferentPositionAdded_ReturnsPathWithTwoItems()
        {
            FPTree tree = new FPTree();

            tree.AddTransaction(Transaction.Create(1, 2, 3));
            tree.AddTransaction(Transaction.Create(2, 3, 4));

            FPTreePrefixPathItem[] path = tree.GetItemPrefixPath(2).ToArray();

            Assert.AreEqual(2, path.Length);
            Assert.AreEqual(1, path[0].FPTreeNode.Support);
            Assert.AreEqual(1, path[1].FPTreeNode.Support);
        }
Ejemplo n.º 11
0
        public IActionResult Index()
        {
            var lists = new List <List <int> > {
                new List <int> {
                    1, 2, 5
                },
                new List <int> {
                    2, 4
                },
                new List <int> {
                    2, 3
                },
                new List <int> {
                    1, 2, 4
                },
                new List <int> {
                    1, 3
                },
                new List <int> {
                    2, 3
                },
                new List <int> {
                    1, 3
                },
                new List <int> {
                    1, 2, 3, 5
                },
                new List <int> {
                    1, 2, 3
                },
            };

            var fpGrowth = new FPGrowth(lists, 0.3);

            var listPattern = new List <Pattern> {
                new Pattern(new List <int> {
                    7, 6, 6, 2, 2
                }, 2)
            };


            var tree = fpGrowth.BuildTree(listPattern);

            var fpTree = new FPTree();
            var pat    = new Pattern();

            fpGrowth.GeneratePatterns(fpTree, pat);

            fpGrowth.GenerateL();
            return(View());
        }
Ejemplo n.º 12
0
 private void MineFPTree(FPTree tree, int minSupport, IEnumerable <T> prefix, IList <IEnumerable <T> > freqItems, IList <int> count)
 {
     foreach (var kvp in tree.HeaderTable)
     {
         var newFreqSet = prefix.Concat(new T[] { kvp.Key }).ToList();
         freqItems.Add(newFreqSet);
         count.Add(kvp.Value.Count);
         var condPats = this.FindPrefixPath(tree, kvp.Key, out var condCount);
         var condTree = this.CreateFPTree(condPats, condCount, minSupport);
         if (condTree.HeaderTable.Any())
         {
             this.MineFPTree(condTree, minSupport, newFreqSet, freqItems, count);
         }
     }
 }
Ejemplo n.º 13
0
        public void GetItemPrefixPath_OneTransactionAdded_CanTraverseTransaction()
        {
            FPTree tree = new FPTree();

            UInt32[] items = { 1, 2, 3, 4, 5 };
            tree.AddTransaction(Transaction.Create(items));

            FPTreeNode leafNode = tree.GetItemPrefixPath(5).ToArray()[0].FPTreeNode;

            Assert.AreEqual(5, leafNode.Item);
            Assert.AreEqual(4, leafNode.ParentNode.Item);
            Assert.AreEqual(3, leafNode.ParentNode.ParentNode.Item);
            Assert.AreEqual(2, leafNode.ParentNode.ParentNode.ParentNode.Item);
            Assert.AreEqual(1, leafNode.ParentNode.ParentNode.ParentNode.ParentNode.Item);
            Assert.AreEqual(FPTreeNodeType.Root, leafNode.ParentNode.ParentNode.ParentNode.ParentNode.ParentNode.Type);
        }
Ejemplo n.º 14
0
        public void GetHeaderItemPath_TwoSameTransactionAdded_AllPathHaveOneItem()
        {
            FPTree tree = new FPTree();

            UInt32[] items = { 1, 2, 3, 4 };

            tree.AddTransaction(Transaction.Create(items));
            tree.AddTransaction(Transaction.Create(items));

            foreach (var item in items)
            {
                FPTreePrefixPathItem[] itemPath = tree.GetItemPrefixPath(item).ToArray();

                Assert.AreEqual(1, itemPath.Length);
                Assert.AreEqual(2, itemPath[0].FPTreeNode.Support);
            }
        }
Ejemplo n.º 15
0
        private static FPTree CreateTree(List <List <int> > T)
        {
            FPTree tree = new FPTree();

            List[] list_frequencyItems_TID;

            int i = 0;

            list_frequencyItems_TID = ToList(T);
            for (i = 0; i < T.Count; i++)
            {
                List list = new List();
                list = list_frequencyItems_TID[i];
                tree = tree.InsertNode(tree, list);
            }
            return(tree);
        }
Ejemplo n.º 16
0
        public void GetRootNode_OneTransactionAdded_RootNodeContainsItemWithHigherSupport()
        {
            FrequentItemsCollection frequentItems = new FrequentItemsCollection(new[]
            {
                new KeyValuePair <uint, Itemset>(1, Itemset.CreateWithSupport(1, 40)),
                new KeyValuePair <uint, Itemset>(2, Itemset.CreateWithSupport(2, 100)),
                new KeyValuePair <uint, Itemset>(3, Itemset.CreateWithSupport(3, 30)),
                new KeyValuePair <uint, Itemset>(4, Itemset.CreateWithSupport(4, 30))
            });

            FPTree tree = new FPTree(frequentItems);

            tree.AddTransaction(Transaction.Create(1, 2, 3, 4));

            FPTreeNode rootNode = tree.GetRootNode();

            Assert.IsNotNull(rootNode.GetSubNode(2));
        }
Ejemplo n.º 17
0
        public void TestAddTransaction1()
        {
            arbol = new FPTree();

            List <string> lista = new List <string>();

            lista.Add("a");
            lista.Add("b");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("b");
            lista.Add("c");
            lista.Add("d");
            arbol.addTransaction(lista);

            //talba de header
            //Assert.AreEqual(headersTable.Count(), 4);
        }
Ejemplo n.º 18
0
        private IList <IEnumerable <T> > FindPrefixPath(FPTree tree, T item, out IList <int> count)
        {
            var condPats = new List <IEnumerable <T> >();

            count = new List <int>();
            for (var treeNode = tree.HeaderTable[item].Node; treeNode != null; treeNode = treeNode.NodeLink)
            {
                var prefixPath = new List <T>();
                for (var node = treeNode.Parent; node != null && node.Parent != null; node = node.Parent)
                {
                    prefixPath.Add(node.Name);
                }
                if (prefixPath.Any())
                {
                    condPats.Add(prefixPath);
                    count.Add(treeNode.Count);
                }
            }
            return(condPats);
        }
Ejemplo n.º 19
0
        private void setEscenario2()
        {
            Dictionary <List <string>, int> transacc = new Dictionary <List <string>, int>();
            List <string> tranUniq = new List <string> {
                "b", "a", "c"
            };

            transacc.Add(tranUniq, 1);
            List <string> tranUniq2 = new List <string> {
                "c", "a", "d"
            };

            transacc.Add(tranUniq2, 1);
            List <string> tranUniq3 = new List <string> {
                "a", "d"
            };

            transacc.Add(tranUniq3, 1);
            List <string> tranUniq4 = new List <string> {
                "c", "e"
            };

            transacc.Add(tranUniq4, 1);
            List <string> tranUniq5 = new List <string> {
                "b", "f"
            };

            transacc.Add(tranUniq5, 1);
            List <string> tranUniq6 = new List <string> {
                "d", "c"
            };

            transacc.Add(tranUniq6, 1);

            prueba = new FPTree();
            prueba.ConstructFPTree(transacc, 3);
        }
Ejemplo n.º 20
0
        public void New_ZeroTransactionsInTree()
        {
            FPTree tree = new FPTree();

            Assert.AreEqual(0, tree.TransactionsCount);
        }
Ejemplo n.º 21
0
        //Install-Package MPI.NET -Version 1.3.0

        // MPIEXEC -n 3 Mpi.NET1.exe "data3.txt" "50"
        static void Main(string[] args)
        {
            using (MPI.Environment environment = new MPI.Environment(ref args))
            {
                if (args.Length < 2)
                {
                    Console.WriteLine("At least two Arguments are needed");
                    return;
                }

                Intracommunicator comm = MPI.Communicator.world;

                if (comm.Size < 2)
                {
                    Console.WriteLine("At least two processes are needed");
                    return;
                }

                float support = float.Parse(args[1]);
                if (comm.Rank == 0) // It's the root
                {
                    // đọc dữ liệu đầu vào
                    string             inputFile = args[0];
                    string[]           database  = System.IO.File.ReadAllLines(inputFile);
                    List <List <int> > db        = new List <List <int> >();
                    List <int>         items;
                    foreach (string item in database)
                    {
                        items = new List <int>();
                        foreach (string it in item.Split(','))
                        {
                            items.Add(int.Parse(it));
                        }
                        db.Add(items);
                    }

                    for (int i = 1; i < comm.Size; i++)
                    {
                        comm.Send(db, i, 0);// gửi tới các tiến trình i, tag 0
                    }
                    Dictionary <int, int> tongHop = new Dictionary <int, int>();
                    for (int i = 1; i < comm.Size; i++)
                    {
                        Dictionary <int, int> demItem;
                        comm.Receive(i, 1, out demItem);// nhận từ i, tag 1
                        foreach (var item in demItem)
                        {
                            if (tongHop.ContainsKey(item.Key))
                            {
                                tongHop[item.Key] += item.Value;
                            }
                            else
                            {
                                tongHop.Add(item.Key, item.Value);
                            }
                        }
                    }
                    int len = db.Count;
                    //var ordered = tongHop.OrderByDescending(x => x.Value);
                    List <int> keys = new List <int>(tongHop.Keys);
                    foreach (var key in keys)
                    {
                        //Console.WriteLine("Item {0}", key);
                        if (((float)tongHop[key] / (float)len * 100.0) < support)
                        {
                            tongHop.Remove(key);
                        }
                    }
                    tongHop = tongHop.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, y => y.Value);
                    //foreach (var item in tongHop)
                    //{
                    //    Console.WriteLine("Item {0} count {1}", item.Key, item.Value);
                    //}
                    //tongHop.OrderByDescending(x => x.Value)
                    // gửi 1-item phổ biến
                    for (int i = 1; i < comm.Size; i++)
                    {
                        comm.Send(tongHop, i, 2);// gửi tới các tiến trình i, tag 2
                    }
                    //
                    Dictionary <int, Dictionary <int, Dictionary <int, int> > > Pall =
                        new Dictionary <int, Dictionary <int, Dictionary <int, int> > >();
                    for (int i = 1; i < comm.Size; i++)
                    {
                        Dictionary <int, Dictionary <int, Dictionary <int, int> > > P;
                        comm.Receive(i, 3, out P);// nhận từ i, tag 1
                        foreach (var itemset in P)
                        {
                            if (Pall.ContainsKey(itemset.Key))
                            {
                                foreach (var item in itemset.Value)
                                {
                                    Dictionary <int, Dictionary <int, int> > keyValues = Pall[itemset.Key];
                                    if (keyValues.ContainsKey(item.Key))
                                    {
                                        Dictionary <int, int> kv = keyValues[item.Key];
                                        //keyValues[item.Key] += item.Value;
                                        foreach (var k in item.Value)
                                        {
                                            if (kv.ContainsKey(k.Key))
                                            {
                                                kv[k.Key] += k.Value;
                                            }
                                            else
                                            {
                                                kv.Add(k.Key, k.Value);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        keyValues.Add(item.Key, item.Value);
                                    }
                                    Pall[itemset.Key] = keyValues;
                                }
                            }
                            else
                            {
                                Pall.Add(itemset.Key, itemset.Value);
                            }
                        }
                    }

                    List <int> itemkey = new List <int>(Pall.Keys);
                    foreach (var itemset in itemkey)
                    {
                        Dictionary <int, Dictionary <int, int> > keyValues = Pall[itemset];
                        Dictionary <int, int> coutValue = CountValue(keyValues);
                        //PrintDictionary(coutValue);
                        List <int> kv = new List <int>(keyValues.Keys);
                        foreach (var item in kv)
                        {
                            Dictionary <int, int> keyValue = keyValues[item];
                            List <int>            ikey     = new List <int>(keyValue.Keys);
                            foreach (var k in ikey)
                            {
                                if ((float)coutValue[k] / len * 100.0 < support)
                                {
                                    keyValue.Remove(k);
                                }
                            }
                            keyValues[item] = keyValue;
                        }
                        //keyValues.Add(itemset, tongHop[itemset]);
                        Pall[itemset] = keyValues;
                    }
                    //foreach (var itemset in Pall)
                    //{
                    //    Console.WriteLine("FPTree: {0}", itemset.Key);
                    //    foreach (var item in itemset.Value)
                    //    {
                    //        Console.WriteLine("Node: {0}", item.Key);
                    //        foreach (var key in item.Value)
                    //            Console.Write("Item {0} count {1} -> ", key.Key, key.Value);
                    //        Console.WriteLine();
                    //    }
                    //    Console.WriteLine();
                    //}
                    //foreach (var itemset in Pall)
                    //{
                    //    Console.WriteLine("Item {0}: ", itemset.Key);
                    //    foreach (var item in itemset.Value)
                    //        Console.WriteLine("Item {0} count {1}", item.Key, item.Value);
                    //}
                    Dictionary <List <int>, float> FPTreeCon = new Dictionary <List <int>, float>();
                    foreach (var itemset in Pall)
                    {
                        FPTreeCon.Add(new List <int> {
                            itemset.Key
                        }, (float)tongHop[itemset.Key] / len * 100f);
                        foreach (var item in itemset.Value)
                        {
                            List <int> keyValues = new List <int>(item.Value.Keys);
                            //Console.WriteLine("keyValues {0}", string.Join(",", keyValues.ToArray()));
                            List <List <int> > subsets = Bit.FindSubsets(keyValues, 0); //get all subsets
                            foreach (var itms in subsets)
                            {
                                if (itms.Count > 0)
                                {
                                    //Console.WriteLine("subsets {0}", string.Join(",", itms.ToArray()));
                                    float itemsupport = FindSupport(len, Pall[itemset.Key], itms);
                                    itms.Add(itemset.Key);
                                    if (itemsupport >= support && !ContainsKey(new List <List <int> >(FPTreeCon.Keys), itms))
                                    {
                                        FPTreeCon.Add(itms, itemsupport);
                                    }
                                }
                            }
                        }
                    }
                    //foreach (var itemset in FPTreeCon)
                    //{
                    //    Console.WriteLine("Item {0}  - support {1}", String.Join(", ", itemset.Key.ToArray()), itemset.Value);
                    //}
                    using (StreamWriter outputFile = new StreamWriter("OutputFPGrowth.txt"))
                    {
                        foreach (var itemset in FPTreeCon)
                        {
                            outputFile.WriteLine("{0}:{1}", string.Join(",", itemset.Key.ToArray()), itemset.Value);
                        }
                    }
                }
                else
                {
                    List <List <int> > db;
                    comm.Receive(0, 0, out db);// nhận từ 0, tag 0
                    Dictionary <int, int> demItem = new Dictionary <int, int>();
                    // tính chia db
                    int size  = comm.Size - 1;
                    int len   = db.Count;
                    int n     = len / size;
                    int start = (comm.Rank - 1) * n;
                    int end   = comm.Rank * n;
                    if (comm.Rank == size)
                    {
                        end = len;
                    }
                    //
                    for (int i = start; i < end; i++)
                    {
                        foreach (int item in db[i])
                        {
                            if (demItem.ContainsKey(item))
                            {
                                demItem[item]++;
                            }
                            else
                            {
                                demItem.Add(item, 1);
                            }
                        }
                    }
                    // item cout cục bộ
                    comm.Send(demItem, 0, 1);// gửi tới các tiến trình 0, tag 1
                    // nhận 1-itemset phổ biến
                    Dictionary <int, int> Fre;
                    comm.Receive(0, 2, out Fre);// nhận từ 0, tag 2

                    // T itemset cục bộ
                    List <List <int> > Tcucbo = new List <List <int> >();
                    for (int i = start; i < end; i++)
                    {
                        List <int> itemset = new List <int>();
                        foreach (var item in Fre)
                        {
                            if (db[i].Contains(item.Key))
                            {
                                itemset.Add(item.Key);
                            }
                        }
                        if (itemset.Count > 0)
                        {
                            Tcucbo.Add(itemset);
                        }
                    }
                    //if(comm.Rank == 2)
                    //foreach (var items in Tcucbo)
                    //{
                    //    foreach (var item in items)
                    //        Console.Write(item + " ");
                    //    Console.WriteLine();
                    //}
                    // xây dựng FP-Tree cục bộ
                    FPTree tree = CreateTree(Tcucbo);
                    // Conditional Patern Bases
                    Dictionary <int, Dictionary <int, Dictionary <int, int> > >
                    P = new Dictionary <int, Dictionary <int, Dictionary <int, int> > >();
                    foreach (var itemset in Fre)
                    {
                        Dictionary <int, Dictionary <int, int> > nodeParent = new Dictionary <int, Dictionary <int, int> >();
                        //List<int> itemsetCollectionKey = new List<int>();
                        //int itemsetCollectionValue = 0;
                        //Console.WriteLine("Node: {0}", itemset.Key);
                        for (int j = 0; j < tree.countNode; j++)
                        {
                            Dictionary <int, int> itemsetCollection = new Dictionary <int, int>();
                            var node = tree.arrayNode[j];
                            if (node.itemName == itemset.Key && !node.visited)
                            {
                                node.visited = true;
                                var nodeparent = node.nodeParent;
                                while (nodeparent.itemName > -1)
                                {
                                    //bool index = itemsetCollection.Key.Contains(nodeparent.itemName);
                                    if (itemsetCollection.ContainsKey(nodeparent.itemName))
                                    {
                                        itemsetCollection[nodeparent.itemName] += node.count;
                                    }
                                    else
                                    {
                                        itemsetCollection.Add(nodeparent.itemName, node.count);
                                    }
                                    //Console.Write("Item:{0}({1})->", nodeparent.itemName, node.count);
                                    nodeparent = nodeparent.nodeParent;
                                }
                                if (itemsetCollection.Count > 0)
                                {
                                    if (nodeParent.ContainsKey(itemsetCollection.Keys.Last()))
                                    {
                                        Dictionary <int, int> keyValues = nodeParent[itemsetCollection.Keys.Last()];
                                        foreach (var item in itemsetCollection)
                                        {
                                            if (keyValues.ContainsKey(item.Key))
                                            {
                                                keyValues[item.Key] += item.Value;
                                            }
                                            else
                                            {
                                                keyValues.Add(item.Key, item.Value);
                                            }
                                        }
                                        nodeParent[itemsetCollection.Keys.Last()] = keyValues;
                                    }
                                    else
                                    {
                                        nodeParent.Add(itemsetCollection.Keys.Last(), itemsetCollection);
                                    }
                                }
                            }
                        }
                        P.Add(itemset.Key, nodeParent);
                    }
                    comm.Send(P, 0, 3);// gửi tới các tiến trình 0, tag 3
                }
            }
        }
Ejemplo n.º 22
0
        public void setupEscenario3()
        {
            arbol = new FPTree();

            List <string> lista = new List <string>();

            lista.Add("a");
            lista.Add("b");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("b");
            lista.Add("c");
            lista.Add("d");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("a");
            lista.Add("c");
            lista.Add("d");
            lista.Add("e");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("a");
            lista.Add("d");
            lista.Add("e");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("a");
            lista.Add("b");
            lista.Add("c");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("a");
            lista.Add("b");
            lista.Add("c");
            lista.Add("d");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("a");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("a");
            lista.Add("b");
            lista.Add("c");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("a");
            lista.Add("b");
            lista.Add("d");
            arbol.addTransaction(lista);

            lista = new List <string>();
            lista.Add("b");
            lista.Add("c");
            lista.Add("e");
            arbol.addTransaction(lista);
        }
        public ItemSets <MultiLevelItem <T> > MinePatterns(IEnumerable <TransactionWithMultiLevelItems <T> > database, IList <MultiLevelItem <T> > domain, GetMinSupportHandle getMinItemSetSupport)
        {
            Dictionary <MultiLevelItem <T>, int> counts = new Dictionary <MultiLevelItem <T>, int>();

            for (int i = 0; i < domain.Count; ++i)
            {
                counts[domain[i]] = 0;
            }

            foreach (TransactionWithMultiLevelItems <T> transaction in database)
            {
                for (int i = 0; i < domain.Count; ++i)
                {
                    if (transaction.ContainsItem(domain[i]))
                    {
                        counts[domain[i]]++;
                    }
                }
            }

            List <MultiLevelItem <T> > freqItems = new List <MultiLevelItem <T> >();

            for (int i = 0; i < domain.Count; ++i)
            {
                if (counts[domain[i]] >= getMinItemSetSupport(new ItemSet <MultiLevelItem <T> >()
                {
                    domain[i]
                }))
                {
                    freqItems.Add(domain[i]);
                }
            }

            MultiLevelItem <T>[] FList = freqItems.ToArray();
            Array.Sort(FList, (i1, i2) =>
            {
                int comp = counts[i1].CompareTo(counts[i2]);
                return(-comp);
            });

            FPTree <MultiLevelItem <T> > fpTree = new FPTree <MultiLevelItem <T> >();

            int dbSize = 0;

            foreach (TransactionWithMultiLevelItems <T> transaction in database)
            {
                dbSize++;

                List <MultiLevelItem <T> > orderedFreqItems = new List <MultiLevelItem <T> >();
                for (int i = 0; i < FList.Length; ++i)
                {
                    if (transaction.ContainsItem(FList[i]))
                    {
                        orderedFreqItems.Add(FList[i]);
                    }
                }

                fpTree.AddOrderedFreqItems(orderedFreqItems);
            }

            fpTree.DbSize = dbSize;

            ItemSets <MultiLevelItem <T> > allItemSets = new ItemSets <MultiLevelItem <T> >();

            for (int i = FList.Length - 1; i >= 0; i--)
            {
                MultiLevelItem <T> item = FList[i];
                List <ItemSet <MultiLevelItem <T> > > fis = fpTree.MinePatternsContaining(item, (candidate) => { return(getMinItemSetSupport(candidate)); });
                fpTree.RemoveFromLeaves(item);
                allItemSets.AddRange(fis);
            }

            return(allItemSets);
        }