コード例 #1
0
        // Command line argument: C:\Users\dilet\Desktop\TwitterDB 0 10 15 0.1 1 1
        public static void Main(string[] args) 
        {
            Console.WriteLine("RWR-based Recommendation (" + DateTime.Now.ToString() + ")\n");
            Stopwatch programStopwatch = Stopwatch.StartNew();

            // Program arguments
            string dirPath = args[0] + Path.DirectorySeparatorChar;     // Path of directory that containes SQLite DB files
            string[] methodologyList = args[1].Split(',');              // The list of experimental codes (csv format; for example: 0,1,8,9,10,11,12 )
            int kFolds = int.Parse(args[2]);                            // Number of folds
            int nIterations = int.Parse(args[3]);                       // Number of iterations for RWR
            egoLikeThresholdRatioInTestSet = double.Parse(args[4]);
            isOnlyFriendInEgoNetwork = (int.Parse(args[5]) == 1) ? true : false;
            isGenericFriendship = (int.Parse(args[6]) == 1) ? true : false;

            // <Ego, dbPath(.sqlite)> Sorted by Ego ID(Ascending Order)
            string[] dbCollection = Directory.GetFiles(dirPath, "*.sqlite");
            SortedDictionary<long, string> egoList = new SortedDictionary<long, string>();
            foreach (string dbPath in dbCollection)
            {
                long egoID = long.Parse(Path.GetFileNameWithoutExtension(dbPath));
                egoList.Add(egoID, dbPath);
            }          

            // Methodology list(Experiment Codes)
            List<Methodology> methodologies = new List<Methodology>();
            foreach (string methodology in methodologyList)
                methodologies.Add((Methodology) int.Parse(methodology));

            // #Core Part: One .sqlite to One thread
            int cntSemaphore = 1;
            Program.dbSemaphore = new Semaphore(cntSemaphore, cntSemaphore);
            foreach (Methodology methodology in methodologies)
            {
                // Outfile Setting
                string outFilePath = args[0] + Path.DirectorySeparatorChar + "RWR_MAP_10Split_Friend_Domain1_" + (int)methodology + "_Friendship0.txt";
                
                // Load existing experimental results: SKIP already performed experiments
                HashSet<long> alreadyPerformedEgoList = new HashSet<long>(); // (<ego ID>, <{Experiments Codes}>)
                if (File.Exists(outFilePath))
                {
                    using (StreamReader reader = new StreamReader(outFilePath))
                    {
                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            string[] tokens = line.Split('\t');
                            long egoUser = long.Parse(tokens[0]);
                            alreadyPerformedEgoList.Add(egoUser);
                        }
                    }
                }
                Program.logger = new StreamWriter(outFilePath, true);

                // Personalized PageRank: Multi-threading
                Experiment experiment;
                personalizedPageRankThreadParamters pagaRankParameters;
                List<Thread> threadList = new List<Thread>();
                foreach (KeyValuePair<long, string> egoDBpair in egoList)
                {
                    long egoID = egoDBpair.Key;
                    string dbPath = egoDBpair.Value;
                    if (alreadyPerformedEgoList.Contains(egoID) == true)
                    {
                        Console.WriteLine("Ego {0} Already Performend", egoID);
                        continue;
                    }                     
                    try
                    {
                        // one Thread to one DB
                        Program.dbSemaphore.WaitOne();
                        Console.WriteLine("Ego {0} Start", egoID);
                        experiment = new Experiment(dbPath);
                        Thread thread = new Thread(experiment.startPersonalizedPageRank);
                        pagaRankParameters = new personalizedPageRankThreadParamters(kFolds, nIterations, methodology);
                        thread.Start(pagaRankParameters);
                        threadList.Add(thread);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
                foreach (Thread thread in threadList)
                    thread.Join();
                // Close Output file
                Program.logger.Close();
            }
            // Execution Time
            programStopwatch.Stop();
            Console.WriteLine("Execution Time: " + Tools.getExecutionTime(programStopwatch));
            Console.WriteLine("Finished!");
        }
コード例 #2
0
        // Command line argument: C:\Users\dilet\Desktop\TwitterDB 0 10 15 0.1 1 1
        public static void Main(string[] args)
        {
            Console.WriteLine("RWR-based Recommendation (" + DateTime.Now.ToString() + ")\n");
            Stopwatch programStopwatch = Stopwatch.StartNew();

            // Program arguments
            string dirPath = args[0] + Path.DirectorySeparatorChar;     // Path of directory that containes SQLite DB files

            string[] methodologyList = args[1].Split(',');              // The list of experimental codes (csv format; for example: 0,1,8,9,10,11,12 )
            int      kFolds          = int.Parse(args[2]);              // Number of folds
            int      nIterations     = int.Parse(args[3]);              // Number of iterations for RWR

            egoLikeThresholdRatioInTestSet = double.Parse(args[4]);
            isOnlyFriendInEgoNetwork       = (int.Parse(args[5]) == 1) ? true : false;
            isGenericFriendship            = (int.Parse(args[6]) == 1) ? true : false;

            // <Ego, dbPath(.sqlite)> Sorted by Ego ID(Ascending Order)
            string[] dbCollection = Directory.GetFiles(dirPath, "*.sqlite");
            SortedDictionary <long, string> egoList = new SortedDictionary <long, string>();

            foreach (string dbPath in dbCollection)
            {
                long egoID = long.Parse(Path.GetFileNameWithoutExtension(dbPath));
                egoList.Add(egoID, dbPath);
            }

            // Methodology list(Experiment Codes)
            List <Methodology> methodologies = new List <Methodology>();

            foreach (string methodology in methodologyList)
            {
                methodologies.Add((Methodology)int.Parse(methodology));
            }

            // #Core Part: One .sqlite to One thread
            int cntSemaphore = 1;

            Program.dbSemaphore = new Semaphore(cntSemaphore, cntSemaphore);
            foreach (Methodology methodology in methodologies)
            {
                // Outfile Setting
                string outFilePath = args[0] + Path.DirectorySeparatorChar + "RWR_MAP_10Split_Friend_Domain1_" + (int)methodology + "_Friendship0.txt";

                // Load existing experimental results: SKIP already performed experiments
                HashSet <long> alreadyPerformedEgoList = new HashSet <long>(); // (<ego ID>, <{Experiments Codes}>)
                if (File.Exists(outFilePath))
                {
                    using (StreamReader reader = new StreamReader(outFilePath))
                    {
                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            string[] tokens  = line.Split('\t');
                            long     egoUser = long.Parse(tokens[0]);
                            alreadyPerformedEgoList.Add(egoUser);
                        }
                    }
                }
                Program.logger = new StreamWriter(outFilePath, true);

                // Personalized PageRank: Multi-threading
                Experiment experiment;
                personalizedPageRankThreadParamters pagaRankParameters;
                List <Thread> threadList = new List <Thread>();
                foreach (KeyValuePair <long, string> egoDBpair in egoList)
                {
                    long   egoID  = egoDBpair.Key;
                    string dbPath = egoDBpair.Value;
                    if (alreadyPerformedEgoList.Contains(egoID) == true)
                    {
                        Console.WriteLine("Ego {0} Already Performend", egoID);
                        continue;
                    }
                    try
                    {
                        // one Thread to one DB
                        Program.dbSemaphore.WaitOne();
                        Console.WriteLine("Ego {0} Start", egoID);
                        experiment = new Experiment(dbPath);
                        Thread thread = new Thread(experiment.startPersonalizedPageRank);
                        pagaRankParameters = new personalizedPageRankThreadParamters(kFolds, nIterations, methodology);
                        thread.Start(pagaRankParameters);
                        threadList.Add(thread);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
                foreach (Thread thread in threadList)
                {
                    thread.Join();
                }
                // Close Output file
                Program.logger.Close();
            }
            // Execution Time
            programStopwatch.Stop();
            Console.WriteLine("Execution Time: " + Tools.getExecutionTime(programStopwatch));
            Console.WriteLine("Finished!");
        }
コード例 #3
0
        // Primary Methods
        public void startPersonalizedPageRank(object parameterObject)
        {
            Stopwatch pageRankStopwatch = Stopwatch.StartNew();
            // PageRank Environment Setting
            personalizedPageRankThreadParamters parameters = (personalizedPageRankThreadParamters)parameterObject;
            int         nFold       = parameters.kFold;
            int         nIteration  = parameters.nIteration;
            Methodology methodology = parameters.methodology;

            try
            {
                // Get ego user's ID and his like count
                long egoID = long.Parse(Path.GetFileNameWithoutExtension(this.dbPath));

                // Final result to put the experimental result per fold together
                this.finalResult = new Dictionary <EvaluationMetric, double>(); // <'HIT(0)', double> or <'AVGPRECISION(1)', double>
                foreach (EvaluationMetric metric in Enum.GetValues(typeof(EvaluationMetric)))
                {
                    this.finalResult.Add(metric, 0d); // Initialization
                }
                // Need to avoid the following error: "Collection was modified; enumeration operation may not execute"
                metrics = new List <EvaluationMetric>(this.finalResult.Keys);

                // K-Fold Cross Validation
                kFoldSemaphore = new Semaphore(nFold, nFold);
                List <Thread> kFoldThreadList = new List <Thread>(); // 'Thread': Standard Library Class
                for (int fold = 0; fold < 1; fold++)                 // One fold to One thread
                {
                    // Load graph information from database and then configurate the graph
                    DataLoader loader = new DataLoader(this.dbPath);
                    loader.setEgoNetwork();
                    loader.setEgoTimeline();
                    loader.splitTimelineToKfolds(nFold);
                    // |Friend|
                    this.numOfFriend = loader.getNumOfFriend();
                    // Multi-Threading
                    kFoldSemaphore.WaitOne();                                                                               // Wait until Semaphore released
                    Thread thread = new Thread(new ParameterizedThreadStart(runKfoldCrossValidation));
                    kFoldThreadParameter kFoldparameters = new kFoldThreadParameter(loader, methodology, fold, nIteration); // 'ThreadParams': defined in 'Experiment.cs'
                    thread.Start(kFoldparameters);
                    kFoldThreadList.Add(thread);
                }
                // Synchronization: Wait until threads be terminated
                foreach (Thread thread in kFoldThreadList)
                {
                    thread.Join();
                }

                if (Program.isValidTrainSet == false)
                {
                    return;
                }
                // Result: <Ego ID>\t<Experi Code>\t<N-fold>\t<iteration>\t<MAP>\t<Recall>\t<|LIKE|>\t<|HIT|>\t<|Friend|>
                pageRankStopwatch.Stop();
                lock (Program.outFileLocker)
                {
                    Program.logger.Write(egoID + "\t" + (int)methodology + "\t" + nFold + "\t" + nIteration);
                    foreach (EvaluationMetric metric in metrics)
                    {
                        switch (metric)
                        {
                        case EvaluationMetric.MAP:
                            Program.logger.Write("\t{0:F15}", (finalResult[metric] / 1));
                            break;

                        case EvaluationMetric.RECALL:
                            Program.logger.Write("\t{0:F15}", (finalResult[metric] / 1));
                            break;

                        case EvaluationMetric.LIKE:
                            Program.logger.Write("\t" + (finalResult[metric]));
                            break;

                        case EvaluationMetric.HIT:
                            Program.logger.Write("\t" + (finalResult[metric]));
                            break;
                        }
                    }
                    // |Friend|
                    Program.logger.Write("\t" + this.numOfFriend);
                    // Output Execution Time
                    Program.logger.WriteLine("\t" + Tools.getExecutionTime(pageRankStopwatch));
                    Program.logger.Flush();
                    // Console Output
                    Console.WriteLine("|Friend|: " + this.numOfFriend);
                    Console.WriteLine("Excution Time: " + Tools.getExecutionTime(pageRankStopwatch));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                Program.dbSemaphore.Release();
            }
        }