コード例 #1
0
 /*
  * Should return path from initial node to terminal;
  * Means getting new path-backtracker
  */
 public Counter bfs(int initNode, int terminalNode) {
   Counter shortestPaths = new Counter();
   Counter currentNodes = new Counter();
   Counter newNodes = new Counter();
   shortestPaths.add(initNode, 0);
   newNodes.add(initNode, 0);
   while (!newNodes.isEmpty()) {
     currentNodes = new Counter(newNodes);
     newNodes = new Counter();
     for (int r : currentNodes.keySet()) {
       Counter row = this.getRow(r);
       for (int c : row.keySet()) {
         // Weighted case: later paths can be shorter than first path
         double pathLength = currentNodes.get(r) + row.get(c);
         if (pathLength < shortestPaths.getPath(c)) {
           newNodes.put(c, pathLength);
           shortestPaths.put(c, pathLength);
         }
       }
     }
     if (shortestPaths.getPath(terminalNode) < Double.MAX_VALUE / 2.0) {
       break;
     }
   }
   return shortestPaths;
 }
コード例 #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;
 }
コード例 #3
0
ファイル: StateMachineTest.java プロジェクト: fischey/ReactFX
  @Test
  public void countDownTest() {
    EventSource<Void> src1 = new EventSource<Void>();
    EventSource<Void> src2 = new EventSource<Void>();
    EventSource<Void> reset = new EventSource<Void>();

    BiFunction<Integer, Void, Tuple2<Integer, Optional<String>>> countdown =
        (s, i) -> s == 1 ? t(3, Optional.of("COUNTDOWN REACHED")) : t(s - 1, Optional.empty());

    EventStream<String> countdowns =
        StateMachine.init(3)
            .on(src1)
            .transmit(countdown)
            .on(src2)
            .transmit(countdown)
            .on(reset)
            .transition((s, i) -> 3)
            .toEventStream();

    Counter counter = new Counter();
    Subscription sub = countdowns.hook(x -> counter.inc()).pin();

    src1.push(null);
    src2.push(null);
    assertEquals(0, counter.get());

    src1.push(null);
    assertEquals(1, counter.getAndReset());

    src2.push(null);
    src2.push(null);
    reset.push(null);
    assertEquals(0, counter.get());
    src2.push(null);
    assertEquals(0, counter.get());
    src1.push(null);
    assertEquals(0, counter.get());
    src2.push(null);
    assertEquals(1, counter.getAndReset());

    sub.unsubscribe();
    src1.push(null);
    src1.push(null);
    src1.push(null);
    src1.push(null);
    src1.push(null);
    src1.push(null);
    assertEquals(0, counter.get());
  }
コード例 #4
0
 /*
  * Matrix mult but with min-plus, and iterative. Each min-plus operation
  * that changes the path inserts it into a new queue
  */
 public SparseMatrix apsp() {
   SparseMatrix shortestPaths = new SparseMatrix(this);
   SparseMatrix currentPairs = new SparseMatrix(this.rowDim, this.colDim);
   SparseMatrix newPairs = new SparseMatrix(this.rowDim, this.colDim);
   newPairs = new SparseMatrix(this);
   for (int d = 0; d < this.rowDim; d++) {
     shortestPaths.set(d, d, 0.0);
   }
   for (int d = 0; d < this.rowDim; d++) {
     newPairs.set(d, d, 0.0);
   }
   while (!newPairs.isEmpty()) {
     currentPairs = new SparseMatrix(newPairs);
     newPairs = new SparseMatrix(this.rowDim, this.colDim);
     for (int r : currentPairs.rows) {
       Counter row = currentPairs.getRow(r);
       for (int c : row.keySet()) {
         Counter oRow = this.getRow(c);
         for (int oc : oRow.keySet()) {
           double pathLength = currentPairs.get(r, c) + oRow.get(oc);
           if (pathLength < shortestPaths.getPath(r, oc)) {
             newPairs.set(r, oc, pathLength);
             shortestPaths.set(r, oc, pathLength);
           }
         }
       }
     }
   }
   return shortestPaths;
 }
コード例 #5
0
    public IotHubMessageResult execute(Message msg, Object context) {
      Counter counter = (Counter) context;
      System.out.println(
          "Received message "
              + counter.toString()
              + " with content: "
              + new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET));

      int switchVal = counter.get() % 3;
      IotHubMessageResult res;
      switch (switchVal) {
        case 0:
          res = IotHubMessageResult.COMPLETE;
          break;
        case 1:
          res = IotHubMessageResult.ABANDON;
          break;
        case 2:
          res = IotHubMessageResult.REJECT;
          break;
        default:
          // should never happen.
          throw new IllegalStateException("Invalid message result specified.");
      }

      System.out.println("Responding to message " + counter.toString() + " with " + res.name());

      counter.increment();

      return res;
    }
コード例 #6
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;
 }
コード例 #7
0
 public SparseMatrix makeLaplacian() {
   SparseMatrix laplacian = new SparseMatrix(this.rowDim, this.colDim);
   for (int r : this.getRows()) {
     Counter row = this.getRow(r);
     laplacian.set(r, r, row.sum());
     for (int c : row.keySet()) {
       laplacian.set(r, c, -1 * row.get(c));
     }
   }
   return laplacian;
 }
コード例 #8
0
 public SparseMatrix multiply(SparseMatrix other) {
   SparseMatrix mult = new SparseMatrix(this.rowDim, other.colDim);
   for (int r : rows) {
     // System.out.println("multiplying row: "+ r);
     Counter row = this.getRow(r);
     // for(int c: other.cols){
     // Counter col = other.getCol(c);
     // double dotProd = row.dot(col);
     // System.out.println(row.toString()+" "+col.toString()+" "+dotProd);
     // mult.set(r, c, dotProd);
     // }
     for (int c : row.keySet()) {
       Counter oRow = other.getRow(c);
       for (int oc : oRow.keySet()) {
         mult.add(r, oc, row.get(c) * oRow.get(oc));
       }
     }
   }
   return mult;
 }
コード例 #9
0
 public SparseMatrix transpose() {
   SparseMatrix transp = new SparseMatrix(this.rowDim, this.colDim);
   for (int r : rows) {
     Counter row = this.getRow(r);
     for (int c : row.keySet()) {
       double v = row.get(c);
       transp.set(c, r, v);
     }
   }
   return transp;
 }
コード例 #10
0
 /**
  * Removes the given number of int blocks from the buffer if possible.
  *
  * @param num the number of int blocks to remove
  * @return the number of actually removed buffers
  */
 public int freeBlocks(int num) {
   assert num >= 0 : "free blocks must be >= 0 but was: " + num;
   final int stop;
   final int count;
   if (num > freeBlocks) {
     stop = 0;
     count = freeBlocks;
   } else {
     stop = freeBlocks - num;
     count = num;
   }
   while (freeBlocks > stop) {
     freeByteBlocks[--freeBlocks] = null;
   }
   bytesUsed.addAndGet(-count * blockSize * RamUsageEstimator.NUM_BYTES_INT);
   assert bytesUsed.get() >= 0;
   return count;
 }
コード例 #11
0
ファイル: GridMergeIndex.java プロジェクト: ntikhonov/ignite
  /** @param page Page. */
  public final void addPage(GridResultPage page) {
    int pageRowsCnt = page.rowsInPage();

    if (pageRowsCnt != 0) addPage0(page);

    Counter cnt = remainingRows.get(page.source());

    int allRows = page.response().allRows();

    if (allRows
        != -1) { // Only the first page contains allRows count and is allowed to init counter.
      assert !cnt.initialized : "Counter is already initialized.";

      cnt.addAndGet(allRows);
      expRowsCnt.addAndGet(allRows);

      // We need this separate flag to handle case when the first source contains only one page
      // and it will signal that all remaining counters are zero and fetch is finished.
      cnt.initialized = true;
    }

    if (cnt.addAndGet(-pageRowsCnt)
        == 0) { // Result can be negative in case of race between messages, it is ok.
      boolean last = true;

      for (Counter c : remainingRows.values()) { // Check all the sources.
        if (c.get() != 0 || !c.initialized) {
          last = false;

          break;
        }
      }

      if (last && lastSubmitted.compareAndSet(false, true)) {
        addPage0(
            new GridResultPage(null, page.source(), null) {
              @Override
              public boolean isLast() {
                return true;
              }
            });
      }
    }
  }
コード例 #12
0
 @Override
 public void recycleIntBlocks(int[][] blocks, int start, int end) {
   final int numBlocks = Math.min(maxBufferedBlocks - freeBlocks, end - start);
   final int size = freeBlocks + numBlocks;
   if (size >= freeByteBlocks.length) {
     final int[][] newBlocks =
         new int[ArrayUtil.oversize(size, RamUsageEstimator.NUM_BYTES_OBJECT_REF)][];
     System.arraycopy(freeByteBlocks, 0, newBlocks, 0, freeBlocks);
     freeByteBlocks = newBlocks;
   }
   final int stop = start + numBlocks;
   for (int i = start; i < stop; i++) {
     freeByteBlocks[freeBlocks++] = blocks[i];
     blocks[i] = null;
   }
   for (int i = stop; i < end; i++) {
     blocks[i] = null;
   }
   bytesUsed.addAndGet(-(end - stop) * (blockSize * RamUsageEstimator.NUM_BYTES_INT));
   assert bytesUsed.get() >= 0;
 }
コード例 #13
0
ファイル: Ex1.java プロジェクト: rina-sleeping/java8_learning
  public static int countWords() throws IOException {
    String contents =
        new String(
            Files.readAllBytes(Paths.get("./src/ch2/ex01/alice.txt")), StandardCharsets.UTF_8);
    List<String> words = Arrays.asList(contents.split("[\\P{L}]+"));

    // 単一カウンターを用いるとincrement毎に排他制御が必要なため、個々の結果を合算する

    Counter counter = new Counter();
    final int GROUP_NUM = 4;
    List<Thread> threads = new ArrayList<Thread>(GROUP_NUM);

    for (int i = 0; i < GROUP_NUM; i++) {
      List<String> list = words.subList(words.size() / 4 * i, words.size() / 4 * (i + 1));

      threads.add(
          new Thread(
              () -> {
                int count = 0;
                for (String w : list) {
                  if (w.length() > 12) count++;
                }
                counter.add(count);
              },
              Integer.toString(i)));
    }

    for (Thread t : threads) t.start();

    for (Thread t : threads) {
      try {
        t.join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    return counter.get();
  }
コード例 #14
0
 public SparseMatrix stochasticizeRows() {
   SparseMatrix stochasticMat = new SparseMatrix(this.rowDim, this.colDim);
   double[] rowSums = new double[this.rowDim];
   for (int r : this.rows) {
     Counter row = this.getRow(r);
     for (int c : row.keySet()) {
       rowSums[r] += row.get(c);
     }
   }
   for (int r : this.rows) {
     Counter row = this.getRow(r);
     for (int c : row.keySet()) {
       double value = 0;
       if (rowSums[r] != 0) {
         //				if(true){
         value = this.get(r, c) / rowSums[r];
       }
       stochasticMat.set(r, c, value);
     }
   }
   return stochasticMat;
 }
コード例 #15
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;
 }
コード例 #16
0
 /** @return the number of bytes currently allocated by this {@link Allocator} */
 public long bytesUsed() {
   return bytesUsed.get();
 }
コード例 #17
0
  public double get(int r, int c) {
    Counter row = this.getRow(r);

    double v = row.get(c);
    return v;
  }
コード例 #18
0
ファイル: Shooter.java プロジェクト: Team1065/FRC-2013
 public double getEncoderRaw() {
   return shooterEncoder.get();
 }