Пример #1
0
 public void PrzepiszTempa(Zbior Tmp)
 {
     foreach (string item in Tmp.aZbior)
     {
         this.aZbior.Add(item);
     }
 }
Пример #2
0
        private List <Zbior> AlgoApriori()
        {
            List <Zbior> aZbioryF = new List <Zbior>();
            Zbior        F        = new Zbior();

            this.sLogi += "Zbiór F1: {";
            foreach (List <string> aRow in this.aZbior)
            {
                foreach (string item in aRow)
                {
                    if (!F.aZbior.Contains(item) && ZbiorSpelniaProgCZ(item))
                    {
                        F.aZbior.Add(item);
                        this.sLogi += "{" + item + "},";
                    }
                }
            }
            this.sLogi += "}\r\n";

            this.sLogi += "Sortujemy F1.\r\n\r\n";
            F.aZbior.Sort();
            aZbioryF.Add(F);

            // k = 2
            F           = new Zbior();
            this.sLogi += "Zbiór F2: {";
            foreach (string item1 in aZbioryF.Last().aZbior)
            {
                foreach (string item2 in aZbioryF.Last().aZbior)
                {
                    if (item1 != item2 && !F.aZbior.Contains(item1 + " " + item2) && !F.aZbior.Contains(item2 + " " + item1) && ZbiorSpelniaProgCZ(item1 + " " + item2))
                    {
                        F.aZbior.Add(item1 + " " + item2);
                        this.sLogi += "{" + item1 + ", " + item2 + "}, ";
                    }
                }
            }
            this.sLogi += "}\r\n\r\n";
            aZbioryF.Add(F);

            //k > 3 ++
            int k = 3;

            while (aZbioryF.Last().aZbior.Count > 1)
            {
                Zbior C = new Zbior(true);
                this.sLogi += "Zbiór kandydatow: {";
                foreach (string item1 in aZbioryF.Last().aZbior)
                {
                    foreach (string item2 in aZbioryF.Last().aZbior)
                    {
                        if (item1 == item2)
                        {
                            continue;
                        }

                        bool     lRowne     = true;
                        string   sArgumenty = "";
                        string[] aItem1     = item1.Split(' ');
                        string[] aItem2     = item2.Split(' ');

                        for (int i = 0; i < k - 2; i++)
                        {
                            if (aItem1[i] != aItem2[i])
                            {
                                lRowne = false;
                                break;
                            }

                            if (sArgumenty != "")
                            {
                                sArgumenty += ' ';
                            }

                            sArgumenty += aItem1[i];
                        }

                        if (lRowne)
                        {
                            for (int i = k - 2; i < k - 1; i++)         // k-1 bo liczymy od zera
                            {
                                sArgumenty += ' ' + aItem1[i];
                                sArgumenty += ' ' + aItem2[i];
                            }

                            //sprawdzamy czy juz nie mamy tych elementow
                            bool lJest = false;
                            foreach (string zbior in C.aZbior)
                            {
                                foreach (string elem in sArgumenty.Split(' '))
                                {
                                    lJest = true;
                                    if (!zbior.Contains(elem))
                                    {
                                        lJest = false;
                                        break;
                                    }
                                }
                                if (lJest)
                                {
                                    break;
                                }
                            }

                            if (!lJest)
                            {
                                this.sLogi += "{" + sArgumenty + "}, ";
                                C.aZbior.Add(sArgumenty);
                            }
                        }
                    }
                }
                this.sLogi += "}\r\n\r\n";

                // Przeciecie własnościa Apriori
                this.sLogi += "Przecinamy zbiór kandydatów własnością Apriori.\r\n";
                List <string> aNieCzeste = new List <string>();
                foreach (string zbior in C.aZbior)
                {
                    this.sLogi += "Sprawdzamy zbiór: {" + zbior + "}\r\n";

                    List <string> aElementy = zbior.Split(' ').ToList();

                    bool lZawiera   = true;
                    var  aPodzbiory = GetPermutations(aElementy, k - 1);
                    foreach (var perm in aPodzbiory)
                    {
                        lZawiera = true;

                        // konwersja na zbior przedstawiony w postaci "a b c" a nie {a} {b} {c}
                        string sPerm = "";
                        foreach (var item in perm)
                        {
                            if (sPerm != "")
                            {
                                sPerm += ' ';
                            }

                            sPerm += item;
                        }

                        if (!aZbioryF.Last().aZbior.Contains(sPerm))
                        {
                            lZawiera    = false;
                            this.sLogi += "- {" + sPerm + "} nie zawiera F" + (k - 1) + "\r\n";
                            break;
                        }

                        this.sLogi += "- {" + sPerm + "} zawiera F" + (k - 1) + "\r\n";
                    }

                    if (!lZawiera)
                    {
                        aNieCzeste.Add(zbior);
                    }
                }

                this.sLogi += "\r\n";

                //usuwamy ze zbioru kandydatow nie czeste
                foreach (string item in aNieCzeste)
                {
                    C.aZbior.Remove(item);
                    this.sLogi += "Zbiór (" + item + "} nie jest częsty usuwamy go ze zbioru kandydatów.\r\n";
                }

                this.sLogi += "\r\n";

                this.sLogi += "Sprawdzamu częstość pozostałych kandydatów.\r\n";
                List <string> aNieSpelnia = new List <string>();
                foreach (string zbior in C.aZbior)
                {
                    if (!ZbiorSpelniaProgCZ(zbior))
                    {
                        aNieCzeste.Add(zbior);
                        this.sLogi += "- Zbiór {" + zbior + "} nie spełnia progu częstości.\r\n";
                    }
                    this.sLogi += "- Zbiór {" + zbior + "} spełnia prog częstości.\r\n";
                }

                //usuwamy
                foreach (string item in aNieSpelnia)
                {
                    C.aZbior.Remove(item);
                }

                this.sLogi += "\r\n";

                //jezeli cos zostalo przepisujemy do F
                if (C.aZbior.Count > 0)
                {
                    F = new Zbior();
                    F.PrzepiszTempa(C);
                    aZbioryF.Add(F);
                    this.sLogi += "Zbiór F" + F.nNumer + ": {";
                    foreach (string elementy in F.aZbior)
                    {
                        string[] aElementy = elementy.Split(' ');
                        foreach (string element in aElementy)
                        {
                            this.sLogi += element;

                            if (element != aElementy.Last())
                            {
                                this.sLogi += ", ";
                            }
                        }
                    }
                    this.sLogi += "}\r\n\r\n";
                }
                else
                {
                    this.sLogi += "Ze zbioru kandydatów nie zostało żadnych elementów spełniające kryteria.";
                }

                k++;
            }

            this.sLogi += "KONIEC ALGORYTMU APRIORI\r\n\r\n";

            return(aZbioryF);
        }