コード例 #1
0
    static void Main()
    {
        long    searchedSum = long.Parse(Console.ReadLine());
        int     n           = int.Parse(Console.ReadLine());
        int     counter     = 0;
        Itemset i1          = new Itemset();

        for (int i = 0; i < n; i++)
        {
            long number = long.Parse(Console.ReadLine());
            i1.Add(number);
        }

        foreach (var item in FindAllSubsets(i1))
        {
            long    currentSubsetSum = 0;
            long [] currentSubset    = item.ToArray();
            if (currentSubset.Length != 0)
            {
                foreach (var number in currentSubset)
                {
                    currentSubsetSum += number;
                }
                if (currentSubsetSum == searchedSum)
                {
                    counter++;
                }
            }
        }

        Console.WriteLine(counter);
    }
コード例 #2
0
ファイル: Program.cs プロジェクト: cesardv/dmproject
        /// <summary>
        /// Helper method for Apriori algorithm which just creates our itemset
        /// </summary>
        /// <returns></returns>
        private static Itemset CreateSetOfUniqueItems()
        {
            var itemsunique = new Itemset();
            var i           = 0;

            while (i < 50)
            {
                itemsunique.Add(i);
                i++;
            }
            return(itemsunique);
        }
コード例 #3
0
        private void ResetData()
        {
            //sample items
            Itemset items = new Itemset();

            items.Add(0);
            items.Add(1);
            items.Add(2);
            items.Add(3);
            items.Add(4);
            items.Add(5);
            items.Add(6);
            items.Add(7);
            items.Add(8);
            txtInputItem.Text = string.Join(",", items.ToArray());

            //sample database
            db = new ItemsetCollection();
            db.Add(new Itemset()
            {
                items[0], items[1], items[2], items[3], items[4]
            });
            db.Add(new Itemset()
            {
                items[1], items[2]
            });
            db.Add(new Itemset()
            {
                items[0], items[1], items[5]
            });
            db.Add(new Itemset()
            {
                items[1], items[0], items[6]
            });
            db.Add(new Itemset()
            {
                items[0], items[5], items[7]
            });
            richItems.Text      = db.ToString();
            txtTransaction.Text = db.Count.ToString();

            txtSupportThreshold.Text    = "40";
            txtConfidenceThreshold.Text = "70";
        }
コード例 #4
0
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            //get items specified by user
            Itemset items = new Itemset();

            //items.AddRange(txtInputItem.Text.Split(','));
            foreach (string it in txtInputItem.Text.Split(','))
            {
                items.Add(int.Parse(it));
            }
            //items.Remove("");
            int transactionCount = 5;

            int.TryParse(txtTransaction.Text, out transactionCount);
            Random rnd = new Random();

            //create random transactions
            db = new ItemsetCollection();
            for (int transactionIndex = 0; transactionIndex < transactionCount;)
            {
                int itemCount = rnd.Next(2, items.Count);

                Itemset transaction = new Itemset();
                //for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
                //{
                //    int randomItemIndex = rnd.Next(items.Count);
                //    if (!transaction.Contains(items[randomItemIndex]))
                //    {
                //        transaction.Add(items[randomItemIndex]);
                //    }
                //}
                while (itemCount > 0)
                {
                    int randomItemIndex = rnd.Next(items.Count);
                    if (!transaction.Contains(items[randomItemIndex]))
                    {
                        transaction.Add(items[randomItemIndex]);
                        itemCount--;
                    }
                }

                if (transaction.Count > 0)
                {
                    db.Add(transaction);
                    transactionIndex += 1;
                }
            }

            richItems.Text = db.ToString();
        }
コード例 #5
0
        private void btnAprioriNewSS_Click(object sender, EventArgs e)
        {
            richAssociationAprioriNewSS.Text = string.Empty;
            richLargeAprioriNewSS.Text       = string.Empty;
            // do FP-Growth
            double supportThreshold = double.Parse(txtSupportThreshold.Text);

            System.Diagnostics.Stopwatch calcTime = System.Diagnostics.Stopwatch.StartNew();
            ReturnLApriori();//FPGrowthMining.DoFPGrowthParallel(db, supportThreshold);
            txtTimesAprioriNewSS.Text = ((calcTime.ElapsedMilliseconds)) + " Milliseconds";

            //
            string[] database = null;
            try { database = System.IO.File.ReadAllLines("OutputAprioriNew.txt"); }
            catch { }
            ItemsetCollection L = new ItemsetCollection();
            Itemset           items;

            foreach (string item in database)
            {
                items = new Itemset();
                string[] itemsupport = item.Split(':');
                //items.AddRange(itemsupport[0].Split(','));
                foreach (string it in itemsupport[0].Split(','))
                {
                    items.Add(int.Parse(it));
                }
                //items.Remove("");
                items.Support = double.Parse(itemsupport[1]);
                L.Add(items);
            }
            //

            richLargeAprioriNewSS.Text = (L.Count + " Large Itemsets (by Apriori cải tiến song song)") + "\r\n";
            foreach (Itemset itemset in L)
            {
                richLargeAprioriNewSS.Text += itemset.ToString() + "\r\n";
            }

            //do mining
            double confidenceThreshold = double.Parse(txtConfidenceThreshold.Text);

            List <AssociationRule> allRules = AprioriMining.Mine(db, L, confidenceThreshold);

            richAssociationAprioriNewSS.Text = (allRules.Count + " Association Rules \n");
            foreach (AssociationRule rule in allRules)
            {
                richAssociationAprioriNewSS.Text += rule.ToString() + "\r\n";
            }
        }
コード例 #6
0
        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = "E:\\";
            openFileDialog.Filter           = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog.FilterIndex      = 2;
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                int i = 0;
                try
                {
                    if ((openFileDialog.OpenFile()) != null)
                    {
                        file = openFileDialog.FileName;
                        string[] database = System.IO.File.ReadAllLines(file);
                        db = new ItemsetCollection();
                        Itemset items;

                        foreach (string item in database)
                        {
                            items = new Itemset();
                            //items.AddRange(item.Split(','));
                            //items.Remove("");
                            foreach (string it in item.Split(','))
                            {
                                items.Add(int.Parse(it));
                            }
                            db.Add(items);
                            i++;
                        }

                        richItems.Text = db.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message + " " + i);
                }
            }
        }
コード例 #7
0
        private void TestApriori()
        {
            ItemsetCollection collection = new ItemsetCollection {
                list1, list2, list3, list4
            };
            ItemsetCollection L     = AprioriMining.Apriori(collection, 50);
            ItemsetCollection Check = GetItems();
            Itemset           i     = new Itemset();

            i.Add(1);
            i.Support = 10;
            i.Tid     = 0;
            Itemset j = new Itemset();

            j.Add(1);
            j.Support = 10;
            j.Tid     = 0;

            Assert.AreEqual(Check[0].Sum(), j.ToList());
        }
コード例 #8
0
ファイル: NUnitTest1.cs プロジェクト: Fiszer89/MovieFreak
        //support = 50%
        private ItemsetCollection GetItems()
        {
            ItemsetCollection allItems = new ItemsetCollection();
            Itemset           item1    = new Itemset();

            item1.Add(1);
            item1.Support = 1 / 2;
            Itemset item2 = new Itemset();

            item2.Add(2);
            item2.Support = 3 / 4;
            Itemset item3 = new Itemset();

            item3.Add(3);
            item3.Support = 3 / 4;
            Itemset item4 = new Itemset();

            item4.Add(5);
            item4.Support = 3 / 4;
            Itemset item5 = new Itemset {
                1, 3
            };

            item5.Support = 1 / 2;
            Itemset item6 = new Itemset {
                2, 3
            };

            item6.Support = 1 / 2;
            Itemset item7 = new Itemset {
                2, 5
            };

            item7.Support = 3 / 4;
            Itemset item8 = new Itemset {
                2, 3, 5
            };

            item8.Support = 1 / 2;
            return(allItems);
        }
コード例 #9
0
    public static ItemsetCollection FindAllSubsets(Itemset itemset)
    {
        ItemsetCollection allSubsets = new ItemsetCollection();

        int subsetCount = (int)Math.Pow(2, itemset.Count);

        for (int i = 0; i < subsetCount; i++)
        {
            Itemset subset = new Itemset();
            for (int bitIndex = 0; bitIndex < itemset.Count; bitIndex++)
            {
                if (GetBit(i, bitIndex) == 1)
                {
                    subset.Add(itemset[bitIndex]);
                }
            }

            allSubsets.Add(subset);
        }

        return(allSubsets);
    }
コード例 #10
0
ファイル: Program.cs プロジェクト: cesardv/dmproject
        /// <summary>
        /// Connects to MySQL in order to run AprioriMiner
        /// </summary>
        private static void DataFromMysql()
        {
            double[] minsupminconf = PromptForMinsupAndConf();
            var      tablechosen   = PromptForDatasetSize();
            var      tableToMine   = string.Empty;

            switch (tablechosen)
            {
            case 1:
                tableToMine = "itemsets";
                break;

            case 5:
                tableToMine = "itemsets5k";
                break;

            case 20:
                tableToMine = "itemsets20k";
                break;

            case 75:
                tableToMine = "itemsets75k";
                break;

            default:
                Console.WriteLine("Whooops something very bad has happened... Try again.");
                return;
            }

            Console.WriteLine("Connecting to mysql server.... Trying to find table with {0}K transactions", tablechosen);
            var database = new ItemsetCollection();

            var timeStartQuery = DateTime.Now;

            Console.Write("Started querying DB at " + timeStartQuery.ToLocalTime());

            using (var conn = new MySqlConnection("server=localhost;user=dmuser;database=DataMiningDb;port=3306;password=data;"))
            {
                try
                {
                    conn.Open();
                    var rows = 0;

                    rows = CountTransactions(conn, tableToMine);

                    var cmd = conn.CreateCommand();

                    for (var i = 1; i <= rows; i++)
                    {
                        cmd.CommandText = i == 1
                                              ? (@"set profiling=1; SELECT itemID FROM dataminingdb." + tableToMine + " WHERE transId="
                                                 + i)
                                              : @"SELECT itemID FROM dataminingdb." + tableToMine + " WHERE transId=" + i;

                        var reader = cmd.ExecuteReader();

                        var listofItemsInRow = new Itemset();

                        while (reader.Read())
                        {
                            listofItemsInRow.Add(reader.GetInt32(0));
                        }

                        database.Add(listofItemsInRow);
                        reader.Close();
                        // ============== LOG PROFILER DATA
                        cmd.CommandText = "SHOW PROFILES";
                        var pfdata = cmd.ExecuteReader();
                        var rs     = 0;

                        var list = new List <object[]>();
                        while (pfdata.Read())
                        {
                            var rowArray = new object[pfdata.FieldCount];
                            rs = pfdata.GetValues(rowArray);
                            list.Add(rowArray);
                        }
                        using (var swrtr = new StreamWriter(@"..\..\..\MysqlProfilerLog.csv", true))
                        {
                            // swrtr.WriteLine(DateTime.Now + " =  LOG  Success = {0}--------------------\n", rs);
                            // Query Number,Time,SQLStatement
                            foreach (var stringse in list)
                            {
                                if (stringse[2].Equals("SHOW WARNINGS"))
                                {
                                    continue;
                                }

                                foreach (var s in stringse)
                                {
                                    swrtr.Write(s + ",");
                                    if (stringse.ElementAt(2) == s)
                                    {
                                        swrtr.Write("\n");
                                    }
                                }
                                // swrtr.WriteLine(Environment.NewLine);
                            }
                        }

                        // ==============

                        pfdata.Close();
                        // Writing profiler info to log file
                        // RecordLog(conn);
                    } // End of For Loop
                }
                catch (Exception e)
                {
                    Console.WriteLine("There was an error: {0}\n\n Consult the help menu.", e.Message);
                }
            }

            // database (of ItemsetCollection should now be populated and ready for mining...
            var endQueryT = DateTime.Now;

            Console.WriteLine("\aEnded queries at: {0}\nTime elapsed: {1}\n\nNow running Apriori on fetched data...", endQueryT.ToLocalTime(), endQueryT.Subtract(timeStartQuery));

            var large = AprioriMining.DoApriori(database, CreateSetOfUniqueItems(), minsupminconf[0]);

            var results = "Results: \n\n " + large.Count + " supported Itemsets obtained by Apriori\n\n";

            foreach (var itemset in large)
            {
                results += itemset.ToString() + "\n";
            }

            Console.WriteLine("DONE! Now mining association rules...");
            var allRules = AprioriMining.Mine(database, large, minsupminconf[1]);

            results += "\nAssociation Rules Found: \n";
            if (allRules.Count == 0)
            {
                results += "No rules were found over minconf of " + minsupminconf[1] + "%";
            }
            else
            {
                foreach (var associationRule in allRules)
                {
                    results += associationRule.ToString() + "\n";
                }
            }

            Console.WriteLine(results);
            var finaltimeEnd = DateTime.Now;

            Console.Write("Total Apriori time was: {0}", endQueryT.Subtract(finaltimeEnd));
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: cesardv/dmproject
        private static void DataFromMongo()
        {
            double[] minsupminconf    = PromptForMinsupAndConf();
            var      collneeded       = PromptForDatasetSize();
            var      collectionNeeded = string.Empty;

            switch (collneeded)
            {
            case 1:
                collectionNeeded = "receipts1k";
                break;

            case 5:
                collectionNeeded = "receipts5k";
                break;

            case 20:
                collectionNeeded = "receipts20k";
                break;

            case 75:
                collectionNeeded = "receipts75k";
                break;

            case 0:
                collectionNeeded = "receipts";
                break;

            default:
                Console.WriteLine("Whooops something very bad has happened... Try again.");
                return;
            }

            Console.WriteLine("\nConnecting to mongoDB...");

            var database = new ItemsetCollection();
            var start    = DateTime.Now;

            try
            {
                const string ConnStr = "mongodb://localhost:27017";
                var          clt     = new MongoClient(ConnStr);
                var          svr     = clt.GetServer();
                var          dblearn = svr.GetDatabase("learn");

                // var tlist = dblearn.GetCollection("receipts");

                var list = dblearn.GetCollection <Transaction>(collectionNeeded);
                Console.WriteLine("Found Data!\nCollection Name: {0}\nSize: {1}", collectionNeeded, list.Count());
                var all = list.FindAll();

                foreach (var transaction in all)
                {
                    var itemset = new Itemset();
                    foreach (var item in transaction.Items)
                    {
                        itemset.Add(item);
                    }

                    database.Add(itemset);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("There was an error, here are the details: \n=====================Error Stuff Here=====================\n" + e.Message + "\n==========================================================");
                Console.WriteLine("Is the mongodb server running?? See the Help/Info section for help...");
                return;
            }

            var endqueryT   = DateTime.Now;
            var totalQueryT = start.Subtract(endqueryT);

            var itemsunique = CreateSetOfUniqueItems();

            Console.WriteLine("Disconnected from MongoDB server at {0}.\n Total query time was: {1}", endqueryT.ToLocalTime(), totalQueryT);
            Console.WriteLine("Now running Apriori on fetched data...");

            var large = AprioriMining.DoApriori(database, itemsunique, minsupminconf[0]);

            var results = "Results: \n\n " + large.Count + " supported Itemsets obtained by Apriori\n\n";

            foreach (var itemset in large)
            {
                results += itemset.ToString() + "\n";
            }

            Console.WriteLine("DONE! Now mining association rules...");
            var allRules = AprioriMining.Mine(database, large, minsupminconf[1]);

            results += "\nAssociation Rules Found: \n";
            if (allRules.Count == 0)
            {
                results += "No rules were found over minconf of " + minsupminconf[1] + "%";
            }
            else
            {
                foreach (var associationRule in allRules)
                {
                    results += associationRule.ToString() + "\n";
                }
            }

            Console.WriteLine(results);
            var finaltimeEnd = DateTime.Now;

            Console.Write("It's now: {0}.\n\nTotal Apriori time was: {1}", finaltimeEnd.ToLocalTime(), endqueryT.Subtract(finaltimeEnd));
        }
コード例 #12
0
        private void PopulateChart()
        {
            try
            {
                var     filmList  = db.Movies.ToList();
                Itemset myItemset = new Itemset();
                foreach (var i in filmList)
                {
                    myItemset.Add(i.MyMovieID);
                }

                ItemsetCollection myItemsetCollection = new ItemsetCollection();
                var MovieList = db.UserMovieLists.Select(u => u);
                foreach (var u in MovieList)
                {
                    var query = from a in db.Ratings
                                join b in db.UserMovieLists
                                on a.UserMovieLIstID equals b.UserMovieListID
                                into temp
                                from b in temp.DefaultIfEmpty()
                                where a.UserMovieLIstID == u.UserMovieListID
                                where a.UserRatingID >= 5
                                select new
                    {
                        a.MyMovieID,
                    };
                    Itemset tempItemset = new Itemset();
                    ;
                    if (myItemset != null)
                    {
                        if (query.Count() > 1)
                        {
                            foreach (var r in query.Take(25))
                            {
                                tempItemset.Add(r.MyMovieID);
                            }
                            myItemsetCollection.Add(tempItemset);
                        }
                    }
                }

                List <double> supportList = new List <double>();
                double        support1    = 30;
                double        support2    = 27.5;
                double        support3    = 25;
                double        support4    = 22.5;
                double        support5    = 20;
                supportList.Add(support5);
                supportList.Add(support4);
                supportList.Add(support3);
                supportList.Add(support2);
                supportList.Add(support1);

                foreach (var ele in supportList)
                {
                    int timeStart, timeStop, timeElapsed;
                    timeStart = Environment.TickCount;
                    ItemsetCollection L = AprioriMining.Apriori(myItemsetCollection, ele);
                    timeStop    = Environment.TickCount;
                    timeElapsed = (timeStop - timeStart) / 1000;
                    this.chart1.Series["Apriori"].Points.AddXY(ele, timeElapsed);
                }

                foreach (var ele in supportList)
                {
                    int timeStart, timeStop, timeElapsed;
                    timeStart = Environment.TickCount;
                    ItemsetCollection L = AprioriMining.AprioriTid(myItemsetCollection, ele);
                    timeStop    = Environment.TickCount;
                    timeElapsed = (timeStop - timeStart) / 1000;
                    this.chart1.Series["AprioriTid"].Points.AddXY(ele, timeElapsed);
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }
        }
コード例 #13
0
        private void AddRules2()
        {
            try
            {
                var     filmList  = db.Movies.ToList();
                Itemset myItemset = new Itemset();
                foreach (var i in filmList)
                {
                    myItemset.Add(i.MyMovieID);
                }

                ItemsetCollection myItemsetCollection = new ItemsetCollection();
                var MovieList = db.UserMovieLists.Select(u => u);
                foreach (var u in MovieList)
                {
                    var query = from a in db.Ratings
                                join b in db.UserMovieLists
                                on a.UserMovieLIstID equals b.UserMovieListID
                                into temp
                                from b in temp.DefaultIfEmpty()
                                where a.UserMovieLIstID == u.UserMovieListID
                                where a.UserRatingID >= 5
                                select new
                    {
                        a.MyMovieID,
                    };
                    Itemset tempItemset = new Itemset();
                    ;
                    if (myItemset != null)
                    {
                        if (query.Count() > 1)
                        {
                            foreach (var r in query.Take(25))
                            {
                                tempItemset.Add(r.MyMovieID);
                            }
                            myItemsetCollection.Add(tempItemset);
                        }
                    }
                }

                ItemsetCollection      L        = AprioriMining.AprioriTid(myItemsetCollection, 20);
                List <AssociationRule> allRules = AprioriMining.Mine(myItemsetCollection, L, 20);

                var ruleX = db.RulesX.Select(x => x);
                db.RulesX.RemoveRange(ruleX);
                db.SaveChanges();
                var ruleY = db.RulesY.Select(x => x);
                db.RulesY.RemoveRange(ruleY);
                db.SaveChanges();
                var myRule = db.Rules.Select(x => x);
                db.Rules.RemoveRange(myRule);
                db.SaveChanges();
                var ruleXmovie = db.RuleXMovie.Select(x => x);
                db.RuleXMovie.RemoveRange(ruleXmovie);
                db.SaveChanges();
                var ruleYMovie = db.RuleYMovie.Select(x => x);
                db.RuleYMovie.RemoveRange(ruleYMovie);

                foreach (var r in allRules)
                {
                    var rule = new MyRule();
                    var X    = new RuleX();
                    var Y    = new RuleY();
                    db.RulesX.Add(X);
                    db.SaveChanges();
                    db.RulesY.Add(Y);
                    db.SaveChanges();
                    rule.Support   = r.Support;
                    rule.Confidene = r.Confidence;
                    rule.RuleXID   = X.RuleXID;
                    rule.RuleYID   = Y.RuleYID;
                    db.Rules.Add(rule);
                    db.SaveChanges();

                    foreach (var x in r.X)
                    {
                        var rm = new RuleXMyMovie();
                        rm.RuleX_RuleXID     = X.RuleXID;
                        rm.MyMovie_MyMovieID = x;
                        db.RuleXMovie.Add(rm);
                        db.SaveChanges();
                    }
                    foreach (var y in r.Y)
                    {
                        var rm = new RuleYMyMovie();
                        rm.RuleY_RuleYID     = Y.RuleYID;
                        rm.MyMovie_MyMovieID = y;
                        db.RuleYMovie.Add(rm);
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }
        }