Пример #1
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();
            }
        }
Пример #2
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();
            }
        }
Пример #3
0
        /*******************************************************************************/
        /***************************** Primary Methods *********************************/
        /*******************************************************************************/
        public bool startPersonalizedPageRank(int nFold, int nIteration)
        {
            try
            {
                // Do experiments for each methodology
                foreach (Methodology methodology in Program.methodologies)
                {
                    // Get ego user's ID and his like count
                    long egoUser = long.Parse(Path.GetFileNameWithoutExtension(this.dbPath));

                    // Check if this experiment has ever been performed earlier
                    int m = (int)methodology;
                    if (Program.existingResults.ContainsKey(egoUser) && Program.existingResults[egoUser].Contains(m))
                    {
                        lock (Program.locker)
                        {
                            Console.WriteLine("Ego network(" + egoUser + "): done on experiment #" + m);
                        }
                        return(false);
                    }

                    // 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
                    List <Thread> threadList = 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();
                        Thread       thread     = new Thread(new ParameterizedThreadStart(runKfoldCrossValidation)); // Core Part
                        ThreadParams parameters = new ThreadParams(loader, methodology, fold, nIteration);           // 'ThreadParams': defined in 'Experiment.cs'
                        thread.Start(parameters);
                        threadList.Add(thread);
                    }
                    // Synchronization: Wait until threads be terminated
                    foreach (Thread thread in threadList)
                    {
                        thread.Join();
                    }

                    // Result: <Ego ID>\t<Experi Code>\t<N-fold>\t<iteration>\t<MAP>\t<Recall>\t<|LIKE|>\t<|HIT|>\t<|Friend|>
                    Program.logger.Write(egoUser + "\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;
                        }
                    }
                    Program.logger.Write("\t" + this.numOfFriend);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return(true);
        }
        /*******************************************************************************/
        /***************************** Primary Methods *********************************/
        /*******************************************************************************/
        public bool startPersonalizedPageRank(int nFold, int nIteration)
        {
            try
            {
                // Do experiments for each methodology
                foreach (Methodology methodology in Program.methodologies)
                {
                    // Get ego user's ID and his like count
                    long egoUser = long.Parse(Path.GetFileNameWithoutExtension(this.dbPath));

                    // Check if this experiment has ever been performed earlier
                    int m = (int)methodology;
                    if (Program.existingResults.ContainsKey(egoUser) && Program.existingResults[egoUser].Contains(m))
                    {
                        lock (Program.locker)
                        {
                            Console.WriteLine("Ego network(" + egoUser + "): done on experiment #" + m);
                        }
                        return false;
                    }

                    // 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
                    List<Thread> threadList = 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();
                        Thread thread = new Thread(new ParameterizedThreadStart(runKfoldCrossValidation)); // Core Part
                        ThreadParams parameters = new ThreadParams(loader, methodology, fold, nIteration); // 'ThreadParams': defined in 'Experiment.cs'
                        thread.Start(parameters);
                        threadList.Add(thread);
                    }
                    // Synchronization: Wait until threads be terminated
                    foreach (Thread thread in threadList)
                        thread.Join();

                    // Result: <Ego ID>\t<Experi Code>\t<N-fold>\t<iteration>\t<MAP>\t<Recall>\t<|LIKE|>\t<|HIT|>\t<|Friend|>
                    Program.logger.Write(egoUser + "\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;
                        }
                    }
                    Program.logger.Write("\t" + this.numOfFriend);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return true;
        }