使用Epinions数据集与Moletrust推荐系统

时间:2018-05-10 16:55:48

标签: java search trust recommender-systems

我正在调整https://github.com/466152112/HappyResearch/blob/master/happyresearch/src/main/java/happy/research/utils/MoleTrust.java的Moletrust算法实现的开源代码 我应该为计算信任值而做出的改变改编自本文由Moletrust创建者编写的“推荐系统的信任感知协同过滤”。他们按如下方式计算信任 “距离源用户距离为n的用户将具有预测的信任值(d-n + 1)/ d”,其中d是最大传播距离。 我得到的结果是1或0,这是不正确的。希望你能帮我找到错误。

public static HashMap<String ,Double> MoleTrustAlg ( HashMap<String,HashMap<String,Double>> trust_data,String sourceUser , int horizon)
{
    // all the visited nodes 
    List<String> nodes = new ArrayList<>(40163); 
    // source user - edges[target users - trust value] 
    Map<String, Map<String, Double>> edges = new HashMap<>(40163); 

    /* Step 1: construct directed graphic and remove cyclic */ 
    int dist = 0; 
    List<String>[] users = new List[horizon + 1]; 
    users[dist] = new ArrayList<>(); 
    users[dist].add(sourceUser); 
    nodes.add(sourceUser); 

    // Denote su: source user; tu: target user 
    while (dist < horizon) 
    { 
        dist++; 
        users[dist] = new ArrayList<>(); 
        for (String su : users[dist - 1]) 
        { 
            Map<String, Double> tns = trust_data.get(su); 
            if (tns == null) continue; // no trusted neighbours 
            for (String tn : tns.keySet()) 
            { 
                if (!nodes.contains(tn) && !users[dist].contains(tn) && !users[dist - 1].contains(tn)) 
                { 
                    users[dist].add(tn); 
                } 
            } 
        } 

        for (String su : users[dist - 1]) 
        { 
            Map<String, Double> tns = trust_data.get(su); 
            if (tns == null) continue; 
            for (String tu : tns.keySet()) 
            { 
                if (!nodes.contains(tu) && users[dist].contains(tu)) 
                { 
                    Map<String, Double> tuTrusts; 
                    if (edges.containsKey(su)) tuTrusts = edges.get(su); 
                    else tuTrusts = new HashMap<>(); 

                    double trustValue = tns.get(tu); 
                    tuTrusts.put(tu, trustValue); 
                    edges.put(su, tuTrusts); 
                } 
            } 
        } 
    } 

    /* Step 2: Evaluate trust score */ 
    dist = 0; 
    //double threashold = 0.5; 
    // trusted neighbours - trust score map 
    HashMap<String, Double> trustScores = new HashMap<>(); 
    trustScores.put(sourceUser, 1.0); 
    while (dist < horizon) 
    { 
        dist++;
        for (String su : users[dist - 1]) 
        { 
            Map<String, Double> tns = trust_data.get(su); 
            if (tns == null) continue; 
            for (String tu : tns.keySet()) 
            {

                double trust_value = (horizon -dist +1) / horizon;
                trustScores.put(tu, trust_value);
            }
        }

    } 

    trustScores.remove(sourceUser); 
    return trustScores; 
}

1 个答案:

答案 0 :(得分:1)

GOT IT

原因是     double trust_value =(horizo​​n -dist +1)/ horizo​​n;

因为horizo​​n和dist是整数,我需要在将结果赋值给double变量之前进行转换     double trust_value =(double)(horizo​​n -dist +1)/ horizo​​n;