コード例 #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 void add(int r, int c, double v) {
   Counter newRow = new Counter();
   newRow.add(c, v);
   this.addRow(r, newRow);
   rows.add(r);
   cols.add(c);
 }
コード例 #3
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;
 }
コード例 #4
0
 public void run() {
   for (int i = 0; i < NUM; i++) {
     Counter.add(value);
     try {
       Thread.sleep(1);
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
   }
 }
コード例 #5
0
  @Verify
  public void globalVerify() {
    Counter totalCounter = new Counter();
    for (Counter counter : results) {
      totalCounter.add(counter);
    }
    LOGGER.info(basename + " " + totalCounter + " from " + results.size() + " Worker threads");

    ICacheEntryListener totalEvents = new ICacheEntryListener();
    for (ICacheEntryListener entryListener : listeners) {
      totalEvents.add(entryListener);
    }
    LOGGER.info(basename + " totalEvents: " + totalEvents);
    assertEquals(basename + " unexpected events found", 0, totalEvents.getUnexpected());
  }
コード例 #6
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;
 }
コード例 #7
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();
  }
コード例 #8
0
ファイル: Magic4.java プロジェクト: hiuchida/exam
 /*
 void solve() {
 	map = new int[4][4];
 	for (int i=1; i<=16; i++) {
 		set.add(i);
 	}
 	start = System.currentTimeMillis();
 	int[] sumcol = new int[6];
 	dfs(0, 0, 0, sumcol);
 	for (Object o : counter.map.keySet()) {
 		pln(""+o+","+counter.get(o));
 	}
 }
 */
 void dfs(int x, int y, int sumrow, int[] sumcol) {
   if (sumrow > 34) return;
   if (x >= 4) {
     if (sumrow != 34) return;
     sumrow = 0;
     x = 0;
     y++;
   }
   if (y >= 4) {
     if (check(sumcol)) {
       String k1 = keyrow(true);
       String k2 = keycol(true);
       String k3 = keyrow(false);
       String k4 = keycol(false);
       String key1 = k1 + k2 + k3 + k4;
       String key2 = k2 + k1 + k4 + k3;
       String key3 = k3 + k4 + k1 + k2;
       String key4 = k4 + k3 + k2 + k1;
       int s = score();
       Integer sc1 = score.get(key1);
       Integer sc2 = score.get(key2);
       Integer sc3 = score.get(key3);
       Integer sc4 = score.get(key4);
       if (sc1 == null && sc2 == null && sc3 == null && sc4 == null) {
         cnt++;
         score.put(key1, s);
         score.put(key2, s);
         score.put(key3, s);
         score.put(key4, s);
         print(s, key1, key2);
         counter.add(s);
       } else if (sc1 != null) {
         if (s != sc1) System.err.println("!!!" + cnt);
         counter.add(s);
       } else if (sc2 != null) {
         if (s != sc2) System.err.println("!!!" + cnt);
         counter.add(s);
       } else if (sc3 != null) {
         if (s != sc3) System.err.println("!!!" + cnt);
         counter.add(s);
       } else if (sc4 != null) {
         if (s != sc4) System.err.println("!!!" + cnt);
         counter.add(s);
       }
     }
     return;
   }
   int reserve = 0;
   int h = set.last();
   int l = set.first();
   if (x == 3) {
     if (!set.contains(34 - sumrow)) return;
     reserve = 34 - sumrow;
   }
   if (x == 2) {
     if (sumrow + h + set.lower(h) < 34) return;
     if (sumrow + l + set.higher(l) > 34) return;
   }
   if (y == 3) {
     if (!set.contains(34 - sumcol[x])) return;
     reserve = 34 - sumcol[x];
     if (x == 0) {
       if (!set.contains(34 - sumcol[5])) return;
     } else if (x == 3) {
       if (!set.contains(34 - sumcol[4])) return;
     }
   }
   if (y == 2) {
     if (sumcol[x] + h + set.lower(h) < 34) return;
     if (sumcol[x] + l + set.higher(l) > 34) return;
   }
   Set<Integer> nset = new TreeSet<>(set);
   for (int v : nset) {
     if (reserve > 0 && reserve != v) continue;
     map[y][x] = v;
     set.remove(v);
     sumcol[x] += v;
     if (x == y) sumcol[4] += v;
     if (x == 3 - y) sumcol[5] += v;
     dfs(x + 1, y, sumrow + v, sumcol);
     map[y][x] = 0;
     set.add(v);
     sumcol[x] -= v;
     if (x == y) sumcol[4] -= v;
     if (x == 3 - y) sumcol[5] -= v;
   }
 }