/// <param name="TargetGraph"> /// Undirected web graph where the RWR will be calculated. /// </param> /// <param name="DampingFactor"> /// The damping factor alpha. /// </param> /// <param name="InitialNode"> /// List of initial nodes whose initial score set to be 1 and all others 0. /// </param> /// <param name="T"> /// Error tolerance. /// </param> public SignedRWR(WeightedUndirectedGraph TargetGraph, double DampingFactor, int InitialNode, double T, double beta, double gamma) { this.D = DampingFactor; this.graph = TargetGraph; this.T = T; this.beta = beta; this.gamma = gamma; score = new double[graph.Size]; nscore = new double[graph.Size]; pscore = new double[graph.Size]; restart = new double[graph.Size]; for (int i = 0; i < score.Length; i++) { if (InitialNode == i) { pscore[i] = 1.0d; nscore[i] = 0.0d; restart[i] = 1.0d; score[i] = pscore[i] - nscore[i]; } else { pscore[i] = 0.0d; nscore[i] = 0.0d; score[i] = pscore[i] - nscore[i]; restart[i] = 0.0d; } } }
public override void Recommend() { // RWR 점수 계산 recommend_user_item = new Dictionary <int, List <Score> >(); // 그래프 모델링 및 분석 WeightedUndirectedGraph model_graph = new WeightedUndirectedGraph(links, e); user_size = model_graph.user_size; for (int i = 1; i < user_size; i++) { // 해당 사용자가 test user가 아니면 스킵 if (!correct_user_item.ContainsKey(i)) { continue; } else if (i % 50 == 0) { Console.WriteLine("{0} Recommendation: user {1} / {2}", fold, i, (user_size - 1)); } List <Score> recommend_items = PerformRWR(model_graph, i); recommend_user_item.Add(i, recommend_items); } Console.WriteLine("Compute RWR scores of {0}: Complete", fold); }
public override void Recommend() { // RWR 점수 계산 recommend_user_item = new ConcurrentDictionary <int, List <Score> >(); Stopwatch sw = new Stopwatch(); sw.Start(); // 그래프 모델링 및 분석 WeightedUndirectedGraph model_graph = new WeightedUndirectedGraph(links, e, num_user, balance); sw.Stop(); Console.WriteLine("Complete 'links to graph'"); TimeSpan ts = sw.Elapsed; // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime : " + elapsedTime); user_size = model_graph.user_size; sw.Start(); model_graph.CreateAdjacencyMatrx(); sw.Stop(); Console.WriteLine("Complete 'graph to matrix'"); ts = sw.Elapsed; elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime : " + elapsedTime); Console.WriteLine("dangling node's Count: {0}", model_graph.dangling_node.Count); Console.WriteLine("global_p's count: {0}", model_graph.global_p.Count); Console.WriteLine("global_n's count: {0}", model_graph.global_n.Count); sw.Start(); int testCount = correct_user_item.Keys.Count(); int ff = 0; Parallel.For(0, user_size, (i) => { if (correct_user_item.ContainsKey(i)) { Interlocked.Increment(ref ff); Console.WriteLine("user id " + i + " 's rec started, (" + ff + "/" + testCount + ")"); List <Score> recommend_items = PerformRWR(model_graph, i); recommend_user_item.TryAdd(i, recommend_items); } }); Console.WriteLine("Compute RWR scores of {0}: Complete", fold); sw.Stop(); ts = sw.Elapsed; elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime : " + elapsedTime); }
/// <summary> /// user-item 형태로 모델링된 bipartite graph를 사용하여 모든 test user에 대해 RWR 수행 /// </summary> /// <param name="model_graph">user-item에 대한 bipartite graph</param> /// <param name="sw">추천 결과(사용자 id, 아이템 id, 점수)가 출력될 파일</param> public List <Score> PerformRWR(WeightedUndirectedGraph model_graph, int target) { // test user가 기존에 갖고 있는 이력 리스트를 저장 HashSet <int> i_items = new HashSet <int>(); if (model_graph.graph.ContainsKey(target)) { HashSet <Weight> i_weight = model_graph.graph[target]; foreach (Weight items in i_weight) { i_items.Add(items.id); } } // RWR 수행 RWR ranker = new RWR(model_graph, damping_factor, target); List <Weight> score = ranker.Calculate(); // RWR 점수를 기준으로 타겟 사용자가 선호할 만한 TOP N개의 아이템 출력 List <Score> recommend_items = new List <Score>(); //Dictionary<int, double> recommend_items = new Dictionary<int, double>(); foreach (Weight s in score) { // ID가 user ID 이거나, 추천 아이템이 타겟 사용자의 기존 이력에 포함되어 있으면 TOP N에 포함시키지 않음 if (s.id < user_size || i_items.Contains(s.id)) { continue; } else if (recommend_items.Count < num_recommend) { if (candidate_item == "Longtail_items") { if (Program.tophead_items.Contains(s.id)) { continue; } } Score user_score = new Score(s.id, s.w); recommend_items.Add(user_score); } else { break; } } return(recommend_items); }
public Dictionary <int, double> PerformRWR(WeightedUndirectedGraph model_graph, int target) { HashSet <int> i_items = new HashSet <int>(); if (model_graph.graph.ContainsKey(target)) { SortedSet <Weight> i_weight = model_graph.graph[target]; foreach (Weight items in i_weight) { i_items.Add(items.id); } } RWR ranker = new RWR(model_graph, damping_factor, target); List <Weight> score = ranker.Calculate(); double sum = 0d; Dictionary <int, double> recommend_items = new Dictionary <int, double>(); foreach (Weight s in score) { if (s.id >= user_size) { sum += s.w; } } foreach (Weight s in score) { if (s.id < user_size || i_items.Contains(s.id)) { continue; } if (candidate_item == "Longtail_items") { if (Program.tophead_items.Contains(s.id)) { continue; } } recommend_items.Add(s.id, s.w / sum); } return(recommend_items); }
/// <param name="TargetGraph"> /// Undirected web graph where the RWR will be calculated. /// </param> /// <param name="DampingFactor"> /// The damping factor alpha. /// </param> /// <param name="InitialNode"> /// List of initial nodes whose initial score set to be 1 and all others 0. /// </param> /// <param name="T"> /// Error tolerance. /// </param> public SignedRWR(WeightedUndirectedGraph TargetGraph, double DampingFactor, int InitialNode, double T, double beta, double gamma, double delta) { this.D = DampingFactor; this.graph = TargetGraph; this.T = T; this.beta = beta; this.gamma = gamma; this.delta = delta; this.user_size = TargetGraph.user_size; this.cs = new int[8]; for (int i = 0; i < 8; i++) { cs[i] = 0; } score = new double[graph.user_size]; pscore = new double[graph.user_size]; nscore = new double[graph.user_size]; //pscore = Vector.Create<double>(graph.Size); //nscore = Vector.Create<double>(graph.Size); init_node = InitialNode; for (int i = 0; i < score.Length; i++) { if (InitialNode == i) { pscore[i] = 1.0d; nscore[i] = 0.0d; score[i] = pscore[i] - nscore[i]; } else { pscore[i] = 0.0d; nscore[i] = 0.0d; score[i] = pscore[i] - nscore[i]; } } u_global_val = 1 / (double)(2 * graph.user_size); //i_global_val = 1 / (double)(2 * this.user_size); }
public RWR(WeightedUndirectedGraph TargetGraph, double DampingFactor, int InitialNode, double T) { this.D = DampingFactor; this.graph = TargetGraph; this.T = T; score = new double[graph.user_size]; restart = new double[graph.user_size]; for (int i = 0; i < score.Length; i++) { if (InitialNode == i) { score[i] = 1.0d; restart[i] = 1.0d; } else { score[i] = 0.0d; restart[i] = 0.0d; } } }
public override void Recommend() { // RWR scores recommend_user_item = new Dictionary <int, List <Score> >(); WeightedUndirectedGraph positive_graph = new WeightedUndirectedGraph(links_like, e); WeightedUndirectedGraph negative_graph = new WeightedUndirectedGraph(links_dislike, e); user_size = positive_graph.user_size; for (int i = 1; i < user_size; i++) { if (!correct_user_item.ContainsKey(i)) { continue; } else if (i % 50 == 0) { Console.WriteLine("{0} Recommendation: user {1} / {2}", fold, i, (user_size - 1)); } Dictionary <int, double> like_items = PerformRWR(positive_graph, i); Dictionary <int, double> dislike_items = PerformRWR(negative_graph, i); double dislike_min = 1d; foreach (int item_id in dislike_items.Keys) { if (dislike_items[item_id] == 0) { continue; } if (dislike_min > dislike_items[item_id]) { dislike_min = dislike_items[item_id]; } } List <Score> recommend_items = new List <Score>(); foreach (int item_id in like_items.Keys) { double final = 0d; if (!dislike_items.ContainsKey(item_id)) { continue; } else if (dislike_items[item_id] == 0) { final = like_items[item_id] - dislike_min; } else { final = like_items[item_id] - dislike_items[item_id]; } Score user_score = new Score(item_id, final); recommend_items.Add(user_score); } recommend_items.Sort(Score.CompareScore); recommend_user_item.Add(i, recommend_items); } Console.WriteLine("Compute RWR scores of {0}: Complete", fold); }
public RWR(WeightedUndirectedGraph TargetGraph, double DampingFactor, int InitialNode) : this(TargetGraph, DampingFactor, InitialNode, -1) { }
/// <param name="TargetGraph"> /// Undirected web graph where the RWR will be calculated. /// </param> /// <param name="DampingFactor"> /// The damping factor alpha. /// </param> /// <param name="InitialNode"> /// List of initial nodes whose initial score set to be 1 and all others 0. /// </param> public SignedRWR(WeightedUndirectedGraph TargetGraph, double DampingFactor, int InitialNode, double beta, double gamma, double delta) : this(TargetGraph, DampingFactor, InitialNode, -1, beta, gamma, delta) { }