Beispiel #1
0
        static void Main(string[] args)
        {
            string         filePath = @"E:\HK IV\Công nghệ phần mềm\HPMR\SoftwareEngineering\ImplementationAS\demo.txt";
            Apriori        apriori  = new Apriori(filePath);
            List <ItemSet> ItemSets = new List <ItemSet>();
            List <List <AssociationRule> > AssociationRules = new List <List <AssociationRule> >();

            int k          = 1;
            int minSupport = 2;

            var firstCandidateItemSet = apriori.Get_First_Frequent_ItemSet(k, minSupport);

            if (firstCandidateItemSet.Count > 0)
            {
                ItemSets.Add(firstCandidateItemSet);
            }
            else
            {
                return;
            }

            Console.WriteLine("*************************");
            Console.WriteLine("{0} :", firstCandidateItemSet.Label);
            foreach (var item in firstCandidateItemSet.Keys)
            {
                Console.WriteLine("{0}, {1}", string.Join(" ", item), firstCandidateItemSet[item]);
            }

            var temp = firstCandidateItemSet;

            bool next;

            do
            {
                next = false;
                var frequent = apriori.Get_Frequent_ItemSet(k, temp, minSupport);

                Console.WriteLine("*************************");
                Console.WriteLine("{0} :", frequent.Label);
                foreach (var item in frequent.Keys)
                {
                    Console.WriteLine("{0}, {1}", string.Join(" ", item), frequent[item]);
                }

                if (frequent.Count > 0)
                {
                    ItemSets.Add(frequent);
                }
                else
                {
                    break;
                }
                ++k;
                temp = frequent;
                List <AssociationRule> rules = new List <AssociationRule>();
                rules = apriori.GetRules(frequent);
                AssociationRules.Add(rules);
                Console.WriteLine("Association Rule L{0}", k);
                Console.WriteLine(string.Format("{0, -10} | {1,-10} | {2,5}", "Rule", "Confidence", "Support"));
                foreach (var variable in rules)
                {
                    Console.WriteLine(string.Format("{0, -10} | {1,-10} | {2,5}", variable.Label, variable.Confidence, variable.Support));
                }
                next = true;
            } while (next);

            Console.ReadLine();
            //Console.WriteLine("***************************");
            //Console.WriteLine("Association Rule");
            //Console.WriteLine("***************************");

            //foreach (var item in AssociationRules)
            //{
            //    foreach (var variable in item)
            //    {
            //        Console.WriteLine(string.Format("{0, -10} | {1,-10} | {2,5}", variable.Label, variable.Confidence, variable.Support));
            //    }
            //}
        }
        private void ExecuteAssociationRule()
        {
            int Support = 2;

            if (trackBar1.InvokeRequired)
            {
                trackBar1.Invoke(new MethodInvoker(delegate
                {
                    Support           = trackBar1.Value + 1;
                    trackBar1.Enabled = false;
                }
                                                   ));
            }
            if (flowLayoutPanel1.InvokeRequired)
            {
                flowLayoutPanel1.Invoke(new MethodInvoker(delegate
                {
                    flowLayoutPanel1.Controls.Clear();
                    flowLayoutPanel1.Controls.Add(new TableResult(File.ReadAllLines(_fileName).ToList()));
                }
                                                          ));
            }

            ImplementationAS.Apriori apriori = new ImplementationAS.Apriori(_fileName);
            int k = 1;
            List <ImplementationAS.ItemSet> ItemSets = new List <ImplementationAS.ItemSet>();
            ItemSet L = new ItemSet();
            bool    next;

            do
            {
                next = false;
                if (k == 1)
                {
                    L = apriori.Get_First_Frequent_ItemSet(k, Support);
                }
                else
                {
                    L = apriori.Get_Frequent_ItemSet(k - 1, L, Support);
                }
                if (L.Count > 0)
                {
                    List <AssociationRule> rules = new List <AssociationRule>();
                    if (k != 1)
                    {
                        rules = apriori.GetRules(L);
                    }
                    TableResult tableL = new TableResult(L, rules);
                    next = true;
                    k++;
                    ItemSets.Add(L);
                    if (flowLayoutPanel1.InvokeRequired)
                    {
                        flowLayoutPanel1.Invoke(new MethodInvoker(delegate
                        {
                            flowLayoutPanel1.Controls.Add(tableL);
                            flowLayoutPanel1.VerticalScroll.Value = flowLayoutPanel1.VerticalScroll.Maximum;
                        }
                                                                  ));
                    }
                }
            } while (next);

            if (trackBar1.InvokeRequired)
            {
                trackBar1.Invoke(new MethodInvoker(delegate
                {
                    trackBar1.Enabled = true;
                }
                                                   ));
            }
        }