Beispiel #1
0
 public void AssignNodePotential(int node_id, double target_prior)
 {
     foreach (KeyValuePair <int, Dictionary <int, Edge> > row in edge_list.bp_matrix)
     {
         if (!node_potential.ContainsKey(node_id))
         {
             NodePotential temp = new NodePotential();
             node_potential.Add(node_id, temp);
             node_potential[node_id].SetPriorScore(row.Key, 0.5);
         }
         else
         {
             if (node_potential[node_id].linkednode_list.Contains(row.Key))
             {
                 node_potential[node_id].SetPriorScore(row.Key, target_prior);
             }
             else if (row.Key == node_id)
             {
                 node_potential[node_id].SetPriorScore(row.Key, target_prior);
             }
             else
             {
                 node_potential[node_id].SetPriorScore(row.Key, 0.5);
             }
         }
     }
 }
        public override void ReadTrainingSet(StreamReader sr)
        {
            max_user_id = int.MinValue;
            max_item_id = int.MinValue;

            like_edges             = new BPMatrix();
            dislike_edges          = new BPMatrix();
            node_potential_like    = new Dictionary <int, NodePotential>();
            node_potential_dislike = new Dictionary <int, NodePotential>();

            while (sr.EndOfStream == false)
            {
                line = sr.ReadLine().Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                int user_id = int.Parse(line[0].ToString());
                int item_id = int.Parse(line[1].ToString()) + Program.num_user;
                int rating  = int.Parse(line[2].ToString());

                if (max_user_id < user_id)
                {
                    max_user_id = user_id;
                }
                if (max_item_id < item_id)
                {
                    max_item_id = item_id;
                }

                if (rating == 1)
                {
                    Edge forward_edge = new Edge();
                    like_edges.SetAt(user_id, item_id, forward_edge);
                    Edge backward_edge = new Edge();
                    like_edges.SetAt(item_id, user_id, backward_edge);

                    if (node_potential_like.ContainsKey(user_id))
                    {
                        node_potential_like[user_id].AddNode(item_id);
                    }
                    else
                    {
                        NodePotential temp_node = new NodePotential();
                        temp_node.AddNode(item_id);
                        node_potential_like.Add(user_id, temp_node);
                    }
                }
                else
                {
                    Edge forward_edge = new Edge();
                    dislike_edges.SetAt(user_id, item_id, forward_edge);
                    Edge backward_edge = new Edge();
                    dislike_edges.SetAt(item_id, user_id, backward_edge);

                    if (node_potential_dislike.ContainsKey(user_id))
                    {
                        node_potential_dislike[user_id].AddNode(item_id);
                    }
                    else
                    {
                        NodePotential temp_node = new NodePotential();
                        temp_node.AddNode(item_id);
                        node_potential_dislike.Add(user_id, temp_node);
                    }
                }
            }
        }