public Dictionary <int, double> PerformBP(BeliefPropagation BP, int target_user, double target_prior) { List <Score> belief_score_list = new List <Score>(); BP.AssignNodePotential(target_user, target_prior); for (int iter = 0; iter < num_iter; iter++) { // message passing BP.PassMessage(target_user, iter); belief_score_list = BP.ComputeBeliefScore(target_user, test_items, num_recommend, correct_user_item); } Dictionary <int, double> recommend_items = new Dictionary <int, double>(); for (int i = 0; i < belief_score_list.Count; i++) { recommend_items.Add(belief_score_list[i].item_id, belief_score_list[i].score); } return(recommend_items); }
public override void Recommend() { BeliefPropagation BP_like = new BeliefPropagation(like_edges, node_potential_like, propagation_matrix, max_user_id, max_item_id); BeliefPropagation BP_dislike = new BeliefPropagation(dislike_edges, node_potential_dislike, propagation_matrix, max_user_id, max_item_id); recommend_user_item = new Dictionary <int, List <Score> >(); int num = 0; foreach (int target_user in correct_user_item.Keys) { if (!node_potential_like.ContainsKey(target_user) && !node_potential_dislike.ContainsKey(target_user)) { continue; } if (num != 0) { like_edges.Clear(); dislike_edges.Clear(); } Dictionary <int, double> like_items = PerformBP(BP_like, target_user, 0.9); Dictionary <int, double> dislike_items = PerformBP(BP_dislike, target_user, 0.7); 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 (candidate_item == "Longtail_items") { if (Program.tophead_items.Contains(item_id)) { continue; } } if (node_potential_like[target_user].linkednode_list.Contains(item_id)) { continue; } else if (node_potential_dislike.ContainsKey(target_user)) { if (node_potential_dislike[target_user].linkednode_list.Contains(item_id)) { continue; } } if (like_items[item_id] == 0) { continue; } 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(target_user, recommend_items); num++; if (num % 50 == 0) { Console.WriteLine("Compute BP score for {0} user / {1} users (fold{2}): Complete", num, correct_user_item.Count(), fold); } } Console.WriteLine("Compute BP scores: Complete"); }