1

I'm adapting an open source code of Moletrust algorithm implementation from https://github.com/466152112/HappyResearch/blob/master/happyresearch/src/main/java/happy/research/utils/MoleTrust.java the change that I should make to calculate the trust value is adapted from this paper "Trust-aware Collaborative Filtering for Recommender Systems" written by Moletrust creators. They calculate the trust as follows "a user at distance n from source user will have a predicted trust value of (d − n + 1)/d" where d is the maximum propagation distance. The result that I get is either 1 or zero which is not correct. Hope that you can help me find the error.

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; 
}
Samar
  • 21
  • 2
  • Welcome to Stack Overflow, Samar! Can you edit your question to maybe include what values you are testing with to produce the 1 or 0 answer you talk about? And maybe what value you expect those test cases to produce? This might help answerers better find the error in your code. – MMAdams May 10 '18 at 17:33

1 Answers1

1

GOT IT

The reason is double trust_value = (horizon -dist +1) / horizon;

as horizon and dist are integers, I need to cast it before assigning the result to the double variable.
double trust_value = (double) (horizon -dist +1) / horizon;

Samar
  • 21
  • 2