public static Counter PersonalizedPageRank(Counter seedVector, SparseMatrix transitionMat) { double trimEps = 0.0; int iterLimit = 200; transitionMat = transitionMat.stochasticizeRows(); double beta = 0.85; long start; long end; Counter vector = new Counter(seedVector); double svCard = 0; for (int i : seedVector.keySet()) { svCard += seedVector.get(i); } Counter oldVector = new Counter(vector); vector = transitionMat.multiply(vector); double diff = 1; int t = 0; start = System.currentTimeMillis(); while (diff > Math.pow(10.0, -10.0) && (diff < 3.99999 || diff > 4.00001) && t < iterLimit) { t += 1; vector.trimKeys(trimEps); vector = transitionMat.multiply(vector); Set<Integer> vecSeedUnion = vector.concreteKeySet(); vecSeedUnion.addAll(seedVector.concreteKeySet()); for (int i : vecSeedUnion) { // vector.set(i, // beta*vector.get(i)/norm+(1-beta)*seedVector.get(i));///Math.max(norm,1.0)); vector.set( i, beta * vector.get(i) + (1 - beta) * seedVector.get(i) / svCard); // /Math.max(norm,1.0)); } double norm = 0; for (int i : vector.keySet()) { // norm += vector.get(i)*vector.get(i); // norm += Math.abs(vector.get(i)); norm += vector.get(i); } // norm = Math.sqrt(norm); diff = 0; Set<Integer> vecOldUnion = vector.concreteKeySet(); vecOldUnion.addAll(oldVector.concreteKeySet()); for (int i : vecOldUnion) { diff += (oldVector.get(i) - vector.get(i)) * (oldVector.get(i) - vector.get(i)); } System.out.println(diff + " " + norm); // System.out.println(vector.toString()); // System.out.println(oldVector.toString()); oldVector = new Counter(vector); } // System.out.println(transitionMat.toStringValues()); end = System.currentTimeMillis(); System.out.println("Time: " + (end - start) + " iterations: " + t); return vector; }
public Counter multiply(Counter vector) { Counter resVec = new Counter(); // for(int r: vector.keySet()){ // Counter row = this.getRow(r); // double sum = 0; // for(int c: row.keySet()){ //// resVec.add(r, row.get(c)*vector.get(c)); // sum += row.get(c)*vector.get(c); // } // resVec.add(r, sum); // } for (int r : rows) { resVec.set(r, this.getRow(r).dot(vector)); } return resVec; }
/** Test method for {@link org.apache.niolex.commons.test.Counter#cnt()}. */ @Test public void testCnt() { Counter c = new Counter(); c.set(1123); assertEquals(1123, c.cnt()); }