我正在改编来自https://github.com/466152112/HappyResearch/blob/master/happyresearch/src/main/java/happy/research/utils/MoleTrust.java 的Moletrust 算法实现的开源代码应该使计算信任值改编自 Moletrust 创建者撰写的这篇论文“Trust-aware Collaborative Filtering for Recommender Systems”。他们按如下方式计算信任度:“距离源用户为 n 的用户将具有 (d - n + 1)/d 的预测信任值”,其中 d 是最大传播距离。我得到的结果是 1 或零,这是不正确的。希望你能帮我找出错误。
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;
}