Ejemplo n.º 1
0
 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;
 }
Ejemplo n.º 2
0
 public static Counter ConstrainedEig(SparseMatrix mat, SparseMatrix orthogMat) {
   double trimEps = 0;
   int iterLimit = 10000;
   long start;
   long end;
   Counter vector = new Counter();
   int vecLen = mat.colDim;
   for (int i = 0; i < vecLen; i++) {
     //			vector.add(i, 1.0/Math.sqrt(vecLen));
     vector.add(i, Math.random() - 0.5);
   }
   Counter oldVector = new Counter();
   //		vector = mat.multiply(vector);
   //		for(int r: orthogMat.rows){
   //			Counter orthogRow = orthogMat.getRow(r);
   //			vector.orthogonalize(orthogRow);
   ////			System.out.println(vector.dot(orthogRow));
   //		}
   double norm = 0;
   double sim = 0;
   double diff = 1.0;
   double diffNeg = 1.0;
   int t = 0;
   start = System.currentTimeMillis();
   while (diff > Math.pow(10.0, -16.0) && diffNeg > Math.pow(10.0, -16.0) && t < iterLimit) {
     t += 1;
     //			vector.trimKeys(trimEps);
     oldVector = new Counter(vector);
     vector = mat.multiply(vector);
     for (int r : orthogMat.rows) {
       Counter orthogRow = orthogMat.getRow(r);
       //				System.out.println("before: "+vector.dot(orthogRow));
       vector.orthogonalize(orthogRow);
       //				System.out.println("after: "+vector.dot(orthogRow));
     }
     norm = 0;
     norm = vector.norm();
     vector.multiply(1.0 / norm);
     diff = 0;
     diffNeg = 0;
     Set<Integer> vecOldUnion = vector.concreteKeySet();
     vecOldUnion.addAll(oldVector.concreteKeySet());
     for (int i : vecOldUnion) {
       //			for(int i = 0; i < mat.colDim; i++){
       diff += (oldVector.get(i) - vector.get(i)) * (oldVector.get(i) - vector.get(i));
       diffNeg += (oldVector.get(i) + vector.get(i)) * (oldVector.get(i) + vector.get(i));
     }
     sim = vector.dot(oldVector);
     //			System.out.println(diff+" "+diffNeg+" "+sim+" "+norm+"
     // "+vector.dot(orthogMat.getRow(0)));
     //			System.out.println(vector.toString());
     // System.out.println(oldVector.toString());
   }
   System.out.println(norm + " " + orthogMat.rows.size() + " " + sim);
   //		System.out.println(mat.toStringValues());
   end = System.currentTimeMillis();
   System.out.println("Time: " + (end - start) + " iterations: " + t);
   return vector;
 }
Ejemplo n.º 3
0
 public static Counter TopEig(SparseMatrix mat) {
   double trimEps = 0.0;
   int iterLimit = 1000;
   long start;
   long end;
   Counter vector = new Counter();
   int vecLen = mat.colDim;
   //		for(int i = 0; i < vecLen; i++){
   //			vector.add(i, Math.random()-0.5);
   //		}
   for (int i = 0; i < vecLen; i++) {
     vector.add(i, 1.0 / (double) vecLen);
   }
   Counter oldVector = new Counter(vector);
   vector = mat.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 = mat.multiply(vector);
     double norm = 0;
     for (int i : vector.keySet()) {
       norm += vector.get(i) * vector.get(i);
       //				norm += Math.abs(vector.get(i));
     }
     norm = Math.sqrt(norm);
     vector.multiply(1.0 / 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(mat.toStringValues());
   end = System.currentTimeMillis();
   System.out.println("Time: " + (end - start) + " iterations: " + t);
   return vector;
 }
Ejemplo n.º 4
0
 public SparseMatrix getRedundant() {
   SparseMatrix redMat = new SparseMatrix(this).multiply(this);
   int oldCard = 0;
   int card = redMat.cardinality();
   int i = 0;
   // for(int r = 0; r < rowDim; r++){
   // if(true){//transMat.hasRow(r)){
   // transMat.add(r, r, 1.0);
   // }
   // if(true){//tcMat.hasRow(r)){
   // tcMat.add(r, r, 1.0);
   // }
   // }
   while (card > oldCard) {
     // System.out.println("iter: "+i);
     i++;
     oldCard = card;
     redMat = redMat.add(redMat.multiply(this));
     card = redMat.cardinality();
     // System.out.println(tcMat.toString());
   }
   return redMat;
 }
Ejemplo n.º 5
0
 public SparseMatrix transitiveClosure() {
   SparseMatrix tcMat = new SparseMatrix(this);
   SparseMatrix transMat = new SparseMatrix(this);
   int oldCard = 0;
   int card = tcMat.cardinality();
   int i = 0;
   // for(int r = 0; r < rowDim; r++){
   // if(true){//transMat.hasRow(r)){
   // transMat.add(r, r, 1.0);
   // }
   // if(true){//tcMat.hasRow(r)){
   // tcMat.add(r, r, 1.0);
   // }
   // }
   while (card > oldCard) {
     // System.out.println("iter: "+i);
     i++;
     oldCard = card;
     tcMat = tcMat.add(tcMat.multiply(transMat));
     card = tcMat.cardinality();
     // System.out.println(tcMat.toString());
   }
   return tcMat;
 }