Beispiel #1
0
        private void runKfoldCrossValidation(object parameters)
        {
            try
            {
                // Setting environment for experiments
                kFoldThreadParameter p           = (kFoldThreadParameter)parameters;
                DataLoader           loader      = p.loader;
                Methodology          methodology = p.methodology;
                int fold       = p.fold;
                int nIteration = p.nIteration;

                // #1 Core Part: 'EgoNetwork DB' --> 'Graph Strctures(Node, Edge)'
                loader.setTrainTestSet(fold);
                if (Program.isValidTrainSet == false)
                {
                    return;
                }
                List <Feature> features = loader.getFeaturesOnMethodology(methodology);
                loader.setGraphConfiguration(features);

                // Graph Elements: Nodes and edges
                Dictionary <int, Node> nodes = loader.allNodes;
                Dictionary <int, List <ForwardLink> > edges = loader.allLinksFromNodes;

                // Incase: Mention Count(O), Friendship(X)
                if (methodology == Methodology.INCL_MENTIONCOUNT ||
                    methodology == Methodology.INCL_AUTHORSHIP_AND_MENTIONCOUNT ||
                    methodology == Methodology.INCL_FOLLOWSHIP_ON_THIRDPARTY_AND_MENTIONCOUNT ||
                    methodology == Methodology.EXCL_FRIENDSHIP)
                {
                    foreach (List <ForwardLink> forwardLinks in edges.Values)
                    {
                        for (int i = 0; i < forwardLinks.Count; i++)
                        {
                            if (forwardLinks[i].type == EdgeType.FRIENDSHIP)
                            {
                                ForwardLink revisedForwardLink = forwardLinks[i];
                                revisedForwardLink.type = EdgeType.UNDEFINED; // FRIENDSHIP --> UNDEFINED
                                forwardLinks[i]         = revisedForwardLink;
                            }
                        }
                    }
                }

                // #2 Core Part: 'Graph Strcture(Nodes, Edges)' --> 'PageRank Matrix(2D-ragged array)'
                Graph graph = new Graph(nodes, edges);
                graph.buildGraph();

                // #3 Core Part: Recommendation list(Personalized PageRanking Algorithm)
                Recommender recommender    = new Recommender(graph);
                var         recommendation = recommender.Recommendation(0, 0.15f, nIteration); // '0': Ego Node's Index, '0.15f': Damping Factor

                // #4 Core Part: Validation - AP(Average Precision)
                DataSet        testSet = loader.getTestSet();
                HashSet <long> egoLikedTweets = testSet.getEgoLikedTweets();
                int            hit = 0, like = 0;
                double         AP = 0.0, recall = 0.0; // Average Precision
                for (int i = 0; i < recommendation.Count; i++)
                {
                    if (egoLikedTweets.Contains(((Tweet)recommendation[i]).ID))
                    {
                        hit += 1;
                        AP  += (double)hit / (i + 1);
                    }
                }
                // LIKE
                like = (int)egoLikedTweets.Count;
                // Average Precision & Recall
                if (hit != 0)
                {
                    AP    /= hit;
                    recall = (double)hit / like;
                }
                else
                {
                    AP     = 0.0;
                    recall = 0.0;
                }

                // Add current result to final one
                lock (kFoldLocker)
                {
                    foreach (EvaluationMetric metric in this.metrics)
                    {
                        switch (metric)
                        {
                        case EvaluationMetric.MAP:
                            this.finalResult[metric] += AP;
                            break;

                        case EvaluationMetric.RECALL:
                            this.finalResult[metric] += recall;
                            break;

                        case EvaluationMetric.LIKE:
                            this.finalResult[metric] += like;
                            break;

                        case EvaluationMetric.HIT:
                            this.finalResult[metric] += hit;
                            break;
                        }
                    }
                }
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                kFoldSemaphore.Release();
            }
        }
        private void runKfoldCrossValidation(object parameters)
        {
            try
            {
                // Setting environment for experiments
                kFoldThreadParameter p = (kFoldThreadParameter)parameters;
                DataLoader loader = p.loader;
                Methodology methodology = p.methodology;
                int fold = p.fold;
                int nIteration = p.nIteration;

                // #1 Core Part: 'EgoNetwork DB' --> 'Graph Strctures(Node, Edge)'
                loader.setTrainTestSet(fold);
                if (Program.isValidTrainSet == false)
                    return;
                List<Feature> features = loader.getFeaturesOnMethodology(methodology);
                loader.setGraphConfiguration(features);

                // Graph Elements: Nodes and edges
                Dictionary<int, Node> nodes = loader.allNodes;
                Dictionary<int, List<ForwardLink>> edges = loader.allLinksFromNodes;

                // Incase: Mention Count(O), Friendship(X)
                if (methodology == Methodology.INCL_MENTIONCOUNT
                    || methodology == Methodology.INCL_AUTHORSHIP_AND_MENTIONCOUNT
                    || methodology == Methodology.INCL_FOLLOWSHIP_ON_THIRDPARTY_AND_MENTIONCOUNT
                    || methodology == Methodology.EXCL_FRIENDSHIP)
                {
                    foreach (List<ForwardLink> forwardLinks in edges.Values)
                    {
                        for (int i = 0; i < forwardLinks.Count; i++)
                        {
                            if (forwardLinks[i].type == EdgeType.FRIENDSHIP)
                            {
                                ForwardLink revisedForwardLink = forwardLinks[i];
                                revisedForwardLink.type = EdgeType.UNDEFINED; // FRIENDSHIP --> UNDEFINED
                                forwardLinks[i] = revisedForwardLink;
                            }
                        }
                    }
                }

                // #2 Core Part: 'Graph Strcture(Nodes, Edges)' --> 'PageRank Matrix(2D-ragged array)'
                Graph graph = new Graph(nodes, edges);
                graph.buildGraph();

                // #3 Core Part: Recommendation list(Personalized PageRanking Algorithm)
                Recommender recommender = new Recommender(graph);
                var recommendation = recommender.Recommendation(0, 0.15f, nIteration); // '0': Ego Node's Index, '0.15f': Damping Factor

                // #4 Core Part: Validation - AP(Average Precision)
                DataSet testSet = loader.getTestSet();
                HashSet<long> egoLikedTweets = testSet.getEgoLikedTweets();
                int hit = 0, like = 0;
                double AP = 0.0, recall = 0.0; // Average Precision
                for (int i = 0; i < recommendation.Count; i++)
                {
                    if (egoLikedTweets.Contains(((Tweet)recommendation[i]).ID))
                    {
                        hit += 1;
                        AP += (double)hit / (i + 1);
                    }
                }
                // LIKE
                like = (int)egoLikedTweets.Count;
                // Average Precision & Recall
                if (hit != 0)
                {
                    AP /= hit;
                    recall = (double)hit / like;
                }
                else
                {
                    AP = 0.0;
                    recall = 0.0;
                }

                // Add current result to final one
                lock (kFoldLocker)
                {
                    foreach (EvaluationMetric metric in this.metrics)
                    {
                        switch (metric)
                        {
                            case EvaluationMetric.MAP:
                                this.finalResult[metric] += AP;
                                break;
                            case EvaluationMetric.RECALL:
                                this.finalResult[metric] += recall;
                                break;
                            case EvaluationMetric.LIKE:
                                this.finalResult[metric] += like;
                                break;
                            case EvaluationMetric.HIT:
                                this.finalResult[metric] += hit;
                                break;
                        }
                    }
                }
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                kFoldSemaphore.Release();
            }
        }