use of com.google.common.util.concurrent.AtomicDouble in project neo4j-nlp by graphaware.
the class PageRank method run.
public Map<Long, Double> run(Map<Long, Map<Long, CoOccurrenceItem>> coOccurrences, int iter, double dampFactor, double threshold) {
nodeWeights = initializeNodeWeights(coOccurrences);
Map<Long, Double> pagerank = getInitializedPageRank(nodeWeights, dampFactor);
int nNodes = pagerank.size();
boolean thresholdHit = false;
Map<Long, Double> prTemp = new HashMap<>();
for (int iteration = 0; iteration < iter && !thresholdHit; iteration++) {
// Map<Long, Double> prTemp = new HashMap<>();
// calculate main part of the PR calculation, include weights of nodes and relationships
nodeWeights.entrySet().stream().forEach(enExt -> {
Long nodeIdExt = enExt.getKey();
Double nodeWExt = enExt.getValue();
AtomicDouble internalSum = new AtomicDouble(0.0);
// AtomicDouble internalNodeWSum = new AtomicDouble(0.0);
nodeWeights.entrySet().stream().filter(enInt -> coOccurrences.containsKey(enInt.getKey()) && coOccurrences.get(enInt.getKey()).containsKey(nodeIdExt)).forEach(enInt -> {
Long nodeIdInt = enInt.getKey();
Double nodeWInt = enInt.getValue();
// internalNodeWSum.addAndGet(nodeWInt);
Map<Long, CoOccurrenceItem> coOccurrentTags = coOccurrences.get(nodeIdInt);
// Can be optimized
double totalWeightSum = coOccurrentTags.values().stream().map(item -> item.getCount()).mapToDouble(Number::doubleValue).sum();
// internalSum.addAndGet(1.0d/coOccurrentTags.size() * pagerank.get(nodeIdInt)); // no relationship weights
// with relationship weights
internalSum.addAndGet(((1.0d * coOccurrentTags.get(nodeIdExt).getCount()) / totalWeightSum) * pagerank.get(nodeIdInt));
// internalSum.addAndGet(((1.0d * coOccurrentTags.get(nodeIdExt).getCount()) / totalWeightSum) * pagerank.get(nodeIdInt) * nodeWInt); // with relationship & node weights
});
// PR is a probability (PR values add up to 1)
double newPrValue = (1 - dampFactor) / nNodes + dampFactor * internalSum.get();
// PageRank with node weights
// long nInt = nodeWeights.entrySet().stream()
// .filter(enInt -> coOccurrences.containsKey(enInt.getKey()) && coOccurrences.get(enInt.getKey()).containsKey(nodeIdExt))
// .count();
// double newPrValue = (1 - dampFactor) / nNodes + dampFactor * internalSum.get() * (nInt / internalNodeWSum.get()); // PR is a probability (PR values add up to 1); WITH node weights
prTemp.put(nodeIdExt, newPrValue);
});
thresholdHit = checkThreshold(pagerank, prTemp, threshold);
if (thresholdHit) {
LOG.warn("Threshold hit after " + (iteration + 1) + " iterations");
}
// finish page rank computation and store it to the final list
nodeWeights.keySet().stream().forEach((nodeIdExt) -> {
pagerank.put(nodeIdExt, prTemp.get(nodeIdExt));
});
}
// iterations
return pagerank;
}
use of com.google.common.util.concurrent.AtomicDouble in project airlift by airlift.
the class QuantileDigest method validate.
@VisibleForTesting
void validate() {
AtomicDouble sum = new AtomicDouble();
AtomicInteger nodeCount = new AtomicInteger();
Set<Integer> freeSlots = computeFreeList();
checkState(freeSlots.size() == freeCount, "Free count (%s) doesn't match actual free slots: %s", freeCount, freeSlots.size());
if (root != -1) {
validateStructure(root, freeSlots);
postOrderTraversal(root, node -> {
sum.addAndGet(counts[node]);
nodeCount.incrementAndGet();
return true;
});
}
checkState(Math.abs(sum.get() - weightedCount) < ZERO_WEIGHT_THRESHOLD, "Computed weight (%s) doesn't match summary (%s)", sum.get(), weightedCount);
checkState(nodeCount.get() == getNodeCount(), "Actual node count (%s) doesn't match summary (%s)", nodeCount.get(), getNodeCount());
}
use of com.google.common.util.concurrent.AtomicDouble in project ImmersiveEngineering by BluSunrize.
the class ApiUtils method raytraceWires.
public static Connection raytraceWires(World world, Vec3d start, Vec3d end, @Nullable Connection ignored) {
Map<BlockPos, ImmersiveNetHandler.BlockWireInfo> inDim = ImmersiveNetHandler.INSTANCE.blockWireMap.lookup(world.provider.getDimension());
AtomicReference<Connection> ret = new AtomicReference<>();
AtomicDouble minDistSq = new AtomicDouble(Double.POSITIVE_INFINITY);
if (inDim != null) {
Utils.rayTrace(start, end, world, (pos) -> {
if (inDim.containsKey(pos)) {
ImmersiveNetHandler.BlockWireInfo info = inDim.get(pos);
for (int i = 0; i < 2; i++) {
Set<Triple<Connection, Vec3d, Vec3d>> conns = i == 0 ? info.in : info.near;
for (Triple<Connection, Vec3d, Vec3d> conn : conns) {
Connection c = conn.getLeft();
if (ignored == null || !c.hasSameConnectors(ignored)) {
Vec3d startRelative = start.add(-pos.getX(), -pos.getY(), -pos.getZ());
Vec3d across = conn.getRight().subtract(conn.getMiddle());
double t = Utils.getCoeffForMinDistance(startRelative, conn.getMiddle(), across);
t = MathHelper.clamp(0, t, 1);
Vec3d closest = conn.getMiddle().add(t * across.x, t * across.y, t * across.z);
double distSq = closest.squareDistanceTo(startRelative);
if (distSq < minDistSq.get()) {
ret.set(c);
minDistSq.set(distSq);
}
}
}
}
}
});
}
return ret.get();
}
use of com.google.common.util.concurrent.AtomicDouble in project commons by twitter.
the class StatsTest method testDoubleExport.
@Test
public void testDoubleExport() {
AtomicDouble var = Stats.exportDouble("test_double");
assertCounter("test_double", 0.0);
var.addAndGet(1.1);
assertCounter("test_double", 1.1);
var.addAndGet(5.55);
assertCounter("test_double", 6.65);
}
use of com.google.common.util.concurrent.AtomicDouble in project aic-praise by aic-sri-international.
the class UAIMARSolver method calculateCompressedEntries.
private static double calculateCompressedEntries(Expression compressedTableExpression) {
AtomicDouble count = new AtomicDouble(0);
visitCompressedTableEntries(compressedTableExpression, count);
return count.doubleValue();
}
Aggregations