public void buildGraph() { for (int i = 0; i < nodes.Count; i++) { ForwardLink[] forwardLinks = null; if (edges.ContainsKey(i)) { // Count the number of explicit(defined) forwardlinks int nExplicitLinks = 0; foreach (ForwardLink forwardLink in edges[i]) // edges[i]: all forward links of 'i'th node { if (forwardLink.type != EdgeType.UNDEFINED) { nExplicitLinks += 1; } } // The graph should consider only explicit forward links if (nExplicitLinks > 0) { // Make ragged array of forward links for a node: To avoid 'Sparse Matrix' forwardLinks = new ForwardLink[nExplicitLinks]; // Calculate sum of weights of the given source node (entry.Key) int idx = 0; double sumWeights = 0; foreach (ForwardLink link in edges[i]) // edges[i]: all forward links of 'i'th node { if (link.type != EdgeType.UNDEFINED) { // Add link to array and its weight to summation forwardLinks[idx++] = link; sumWeights += link.weight; } } // Adjust weights whose sum is 1 for (int f = 0; f < nExplicitLinks; f++) { forwardLinks[f].weight /= sumWeights; } } } // Dangling node: Add virtual links to all nodes(include itself) else { forwardLinks = new ForwardLink[nodes.Count]; double weight = 1.0 / nodes.Count; for (int j = 0; j < nodes.Count; j++) { ForwardLink virtualLink = new ForwardLink(j, EdgeType.VIRTUAL, weight); forwardLinks[j] = virtualLink; } } // Add filterd forward links of 'i'th node matrix.Add(forwardLinks); } }
public void buildGraph() { for (int i = 0; i < nodes.Count; i++) { ForwardLink[] forwardLinks = null; if (edges.ContainsKey(i)) { // Count the number of explicit(defined) forwardlinks int nExplicitLinks = 0; foreach (ForwardLink forwardLink in edges[i]) // edges[i]: all forward links of 'i'th node { if (forwardLink.type != EdgeType.UNDEFINED) nExplicitLinks += 1; } // The graph should consider only explicit forward links if (nExplicitLinks > 0) { // Make ragged array of forward links for a node: To avoid 'Sparse Matrix' forwardLinks = new ForwardLink[nExplicitLinks]; // Calculate sum of weights of the given source node (entry.Key) int idx = 0; double sumWeights = 0; foreach (ForwardLink link in edges[i]) // edges[i]: all forward links of 'i'th node { if (link.type != EdgeType.UNDEFINED) { // Add link to array and its weight to summation forwardLinks[idx++] = link; sumWeights += link.weight; } } // Adjust weights whose sum is 1 for (int f = 0; f < nExplicitLinks; f++) forwardLinks[f].weight /= sumWeights; } } // Dangling node: Add virtual links to all nodes(include itself) else { forwardLinks = new ForwardLink[nodes.Count]; double weight = 1.0 / nodes.Count; for (int j = 0; j < nodes.Count; j++) { ForwardLink virtualLink = new ForwardLink(j, EdgeType.VIRTUAL, weight); forwardLinks[j] = virtualLink; } } // Add filterd forward links of 'i'th node matrix.Add(forwardLinks); } }
public void addLink(int indexSourceNode, int indexNewTargetNode, EdgeType type, double weight) { // Create new link structure for source node if (!allLinksFromNodes.ContainsKey(indexSourceNode)) { allLinksFromNodes.Add(indexSourceNode, new List <ForwardLink>()); } // Check already a link(equal type) between source and target nodes foreach (ForwardLink forwardLink in allLinksFromNodes[indexSourceNode]) { if (forwardLink.targetNode == indexNewTargetNode && forwardLink.type == type) { return; } } // Create a new link between source and target nodes ForwardLink link = new ForwardLink(indexNewTargetNode, type, weight); allLinksFromNodes[indexSourceNode].Add(link); nLinks += 1; }
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(); } }
public void addLink(int indexSourceNode, int indexNewTargetNode, EdgeType type, double weight) { // Create new link structure for source node if (!allLinksFromNodes.ContainsKey(indexSourceNode)) allLinksFromNodes.Add(indexSourceNode, new List<ForwardLink>()); // Check already a link(equal type) between source and target nodes foreach (ForwardLink forwardLink in allLinksFromNodes[indexSourceNode]) { if (forwardLink.targetNode == indexNewTargetNode && forwardLink.type == type) return; } // Create a new link between source and target nodes ForwardLink link = new ForwardLink(indexNewTargetNode, type, weight); allLinksFromNodes[indexSourceNode].Add(link); nLinks += 1; }
public void addLink(int idxSourceNode, int idxTargetNode, EdgeType type, double weight) { if (!allLinks.ContainsKey(idxSourceNode)) allLinks.Add(idxSourceNode, new List<ForwardLink>()); bool exist = false; foreach (ForwardLink forwardLink in allLinks[idxSourceNode]) { if (forwardLink.targetNode == idxTargetNode && forwardLink.type == type) { exist = true; break; } } if (exist == false) { ForwardLink link = new ForwardLink(idxTargetNode, type, weight); allLinks[idxSourceNode].Add(link); nLinks += 1; } }