Search in sources :

Example 21 with AtomicDouble

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;
}
Also used : Relationship(org.neo4j.graphdb.Relationship) Log(org.neo4j.logging.Log) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Result(org.neo4j.graphdb.Result) Iterator(java.util.Iterator) LoggerFactory(com.graphaware.common.log.LoggerFactory) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) Map(java.util.Map) HashMap(java.util.HashMap) Transaction(org.neo4j.graphdb.Transaction) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) HashMap(java.util.HashMap) AtomicDouble(com.google.common.util.concurrent.AtomicDouble)

Example 22 with AtomicDouble

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 23 with AtomicDouble

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();
}
Also used : AtomicDouble(com.google.common.util.concurrent.AtomicDouble) Connection(blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection) MessageObstructedConnection(blusunrize.immersiveengineering.common.util.network.MessageObstructedConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Triple(org.apache.commons.lang3.tuple.Triple) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple)

Example 24 with AtomicDouble

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);
}
Also used : AtomicDouble(com.google.common.util.concurrent.AtomicDouble) Test(org.junit.Test)

Example 25 with AtomicDouble

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();
}
Also used : AtomicDouble(com.google.common.util.concurrent.AtomicDouble)

Aggregations

AtomicDouble (com.google.common.util.concurrent.AtomicDouble)28 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableList (com.google.common.collect.ImmutableList)2 LoggerFactory (com.graphaware.common.log.LoggerFactory)2 Resources (com.hubspot.mesos.Resources)2 MesosTaskMonitorObject (com.hubspot.mesos.json.MesosTaskMonitorObject)2 InvalidSingularityTaskIdException (com.hubspot.singularity.InvalidSingularityTaskIdException)2 SingularityAgentUsage (com.hubspot.singularity.SingularityAgentUsage)2 SingularityTask (com.hubspot.singularity.SingularityTask)2 SingularityTaskCurrentUsage (com.hubspot.singularity.SingularityTaskCurrentUsage)2 SingularityTaskHistoryUpdate (com.hubspot.singularity.SingularityTaskHistoryUpdate)2 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)2 SingularityTaskUsage (com.hubspot.singularity.SingularityTaskUsage)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2