Example usage for com.google.common.util.concurrent AtomicDouble addAndGet

List of usage examples for com.google.common.util.concurrent AtomicDouble addAndGet

Introduction

In this page you can find the example usage for com.google.common.util.concurrent AtomicDouble addAndGet.

Prototype

public final double addAndGet(double delta) 

Source Link

Document

Atomically adds the given value to the current value.

Usage

From source file:jurls.core.becca.DefaultZiptie.java

public static double getExponentialSum(RealMatrix c, double exponent, int rowStart, int rowEnd, int colStart,
        int colEnd) {
    AtomicDouble s = new AtomicDouble(0);
    c.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
        @Override//from  w w  w.jav  a2s .  co m
        public void visit(int row, int column, double value) {
            s.addAndGet(Math.pow(value, exponent));
        }
    }, rowStart, rowEnd, colStart, colEnd);
    return s.doubleValue();
}

From source file:jurls.core.becca.DefaultZiptie.java

public static double getGeneralizedMean(RealMatrix c, double exponent, int rowStart, int rowEnd, int colStart,
        int colEnd) {
    AtomicDouble s = new AtomicDouble(0);
    AtomicInteger n = new AtomicInteger(0);
    c.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
        @Override/*w  ww.  ja  v  a 2  s  .  c  om*/
        public void visit(int row, int column, double value) {
            s.addAndGet(Math.pow(value, exponent));
            n.incrementAndGet();
        }
    }, rowStart, rowEnd, colStart, colEnd);

    return (1.0 / n.doubleValue()) * Math.pow(s.doubleValue(), 1.0 / exponent);
}

From source file:com.qwazr.utils.WordCount.java

public static float compare(WordCount dic1, WordCount dic2) {
    HashSet<String> wordSet = new HashSet<String>(dic1.wordCount.keySet());
    wordSet.addAll(dic2.wordCount.keySet());
    AtomicDouble similarity = new AtomicDouble();
    wordSet.forEach(new Consumer<String>() {
        @Override/*from  w  ww.  jav  a2s.  c  o m*/
        public void accept(String word) {
            final AtomicInteger c1 = dic1.wordCount.get(word);
            final AtomicInteger c2 = dic2.wordCount.get(word);
            if (c1 == null || c2 == null)
                return;
            final double v1 = c1.doubleValue();
            final double v2 = c2.doubleValue();
            final double delta = v1 > v2 ? v2 / v1 : v1 / v2;
            similarity.addAndGet(delta);
        }
    });
    return similarity.floatValue() / (float) wordSet.size();
}

From source file:com.sri.ai.praise.model.v1.imports.uai.UAIMARSolver.java

private static void visitCompressedTableEntries(Expression compressedTableExpression, AtomicDouble count) {
    if (IfThenElse.isIfThenElse(compressedTableExpression)) {
        visitCompressedTableEntries(IfThenElse.thenBranch(compressedTableExpression), count);
        visitCompressedTableEntries(IfThenElse.elseBranch(compressedTableExpression), count);
    } else {//from  ww w.j  a v a 2s  .  co m
        // We are at a leaf node, therefore increment the count
        count.addAndGet(1);
    }
}

From source file:com.alibaba.jstorm.common.metric.operator.updater.DoubleAddUpdater.java

@Override
public AtomicDouble update(Number object, AtomicDouble cache, Object... others) {
    // TODO Auto-generated method stub
    if (cache == null) {
        cache = new AtomicDouble(0.0);
    }/*from   w  w w.  j av  a2 s  .c om*/
    if (object != null) {
        cache.addAndGet(object.doubleValue());
    }
    return cache;
}

From source file:syncleus.gremlann.train.Backprop.java

public void backpropagate(final Vertex neuron) {

    //1. calculate deltaTrain based on all the destination synapses
    if (!isTrue(neuron, "output")) {

        final AtomicDouble newDeltaTrain = new AtomicDouble(0);

        neuron.outE("synapse").sideEffect(et -> {
            Edge synapse = et.get();//from   w  w  w  .java 2 s. co  m
            Vertex target = synapse.outV().next();

            newDeltaTrain.addAndGet(Synapse.weight(synapse) * deltaTrain(target));
        }).iterate();

        double ndt = newDeltaTrain.get() * getActivationFunction().activateDerivative(activity(neuron));
        set(neuron, "deltaTrain", ndt);

        //System.out.println(" delta=" + ndt + " " + newDeltaTrain.get());
    }

    //System.out.println("bp " + neuron.label() + " " + neuron.inE("synapse").toSet().size() + "|" +neuron.outE("synapse").toSet().size() + " d=" + real(neuron,"deltaTrain"));        

    //2. Back-propagates the training data to all the incoming synapses
    neuron.inE("synapse").sideEffect(et -> {
        Edge synapse = et.get();
        Vertex source = synapse.outV().next();

        double curWeight = Synapse.weight(synapse);
        double sourceDelta = deltaTrain(neuron);

        double newWeight = curWeight + (sourceDelta * learningRate(source) * signal(neuron));
        set(synapse, "weight", newWeight);

        //System.out.println("  " + synapse + " " + curWeight + " -> " + newWeight + " "+ sourceDelta + " " + deltaTrain(neuron));
    }).iterate();

}

From source file:org.loklak.susi.SusiTransfer.java

/**
 * A conclusion from choices is done by the application of a function on the choice set.
 * This may be done by i.e. counting the number of choices or extracting a maximum element.
 * @param choices the given set of json objects from the data object of a SusiThought
 * @returnan array of json objects which are the extraction of given choices according to the given mapping
 *///from  w ww  .  j a  va  2 s .c  o m
public JSONArray conclude(JSONArray choices) {
    JSONArray a = new JSONArray();
    if (this.selectionMapping != null && this.selectionMapping.size() == 1) {
        // test if this has an aggregation key: AVG, COUNT, MAX, MIN, SUM
        final String aggregator = this.selectionMapping.keySet().iterator().next();
        final String aggregator_as = this.selectionMapping.get(aggregator);
        if (aggregator.startsWith("COUNT(") && aggregator.endsWith(")")) { // TODO: there should be a special pattern for this to make it more efficient
            return a.put(new JSONObject().put(aggregator_as, choices.length()));
        }
        if (aggregator.startsWith("MAX(") && aggregator.endsWith(")")) {
            final AtomicDouble max = new AtomicDouble(Double.MIN_VALUE);
            String c = aggregator.substring(4, aggregator.length() - 1);
            choices.forEach(json -> max.set(Math.max(max.get(), ((JSONObject) json).getDouble(c))));
            return a.put(new JSONObject().put(aggregator_as, max.get()));
        }
        if (aggregator.startsWith("MIN(") && aggregator.endsWith(")")) {
            final AtomicDouble min = new AtomicDouble(Double.MAX_VALUE);
            String c = aggregator.substring(4, aggregator.length() - 1);
            choices.forEach(json -> min.set(Math.min(min.get(), ((JSONObject) json).getDouble(c))));
            return a.put(new JSONObject().put(aggregator_as, min.get()));
        }
        if (aggregator.startsWith("SUM(") && aggregator.endsWith(")")) {
            final AtomicDouble sum = new AtomicDouble(0.0d);
            String c = aggregator.substring(4, aggregator.length() - 1);
            choices.forEach(json -> sum.addAndGet(((JSONObject) json).getDouble(c)));
            return a.put(new JSONObject().put(aggregator_as, sum.get()));
        }
        if (aggregator.startsWith("AVG(") && aggregator.endsWith(")")) {
            final AtomicDouble sum = new AtomicDouble(0.0d);
            String c = aggregator.substring(4, aggregator.length() - 1);
            choices.forEach(json -> sum.addAndGet(((JSONObject) json).getDouble(c)));
            return a.put(new JSONObject().put(aggregator_as, sum.get() / choices.length()));
        }
    }
    if (this.selectionMapping != null && this.selectionMapping.size() == 2) {
        Iterator<String> ci = this.selectionMapping.keySet().iterator();
        String aggregator = ci.next();
        String column = ci.next();
        if (column.indexOf('(') >= 0) {
            String s = aggregator;
            aggregator = column;
            column = s;
        }
        final String aggregator_as = this.selectionMapping.get(aggregator);
        final String column_as = this.selectionMapping.get(column);
        final String column_final = column;
        if (aggregator.startsWith("PERCENT(") && aggregator.endsWith(")")) {
            final AtomicDouble sum = new AtomicDouble(0.0d);
            String c = aggregator.substring(8, aggregator.length() - 1);
            choices.forEach(json -> sum.addAndGet(((JSONObject) json).getDouble(c)));
            choices.forEach(json -> a.put(
                    new JSONObject().put(aggregator_as, 100.0d * ((JSONObject) json).getDouble(c) / sum.get())
                            .put(column_as, ((JSONObject) json).get(column_final))));
            return a;
        }
    }
    for (Object json : choices) {
        JSONObject extraction = this.extract((JSONObject) json);
        if (extraction.length() > 0)
            a.put(extraction);
    }
    return a;
}

From source file:ai.susi.mind.SusiTransfer.java

/**
 * A conclusion from choices is done by the application of a function on the choice set.
 * This may be done by i.e. counting the number of choices or extracting a maximum element.
 * @param choices the given set of json objects from the data object of a SusiThought
 * @returnan array of json objects which are the extraction of given choices according to the given mapping
 *//*from   w ww. ja  v a  2  s . com*/
public JSONArray conclude(JSONArray choices) {
    JSONArray a = new JSONArray();
    if (this.selectionMapping != null && this.selectionMapping.size() == 1) {
        // test if this has an aggregation key: AVG, COUNT, MAX, MIN, SUM
        final String aggregator = this.selectionMapping.keySet().iterator().next();
        final String aggregator_as = this.selectionMapping.get(aggregator);
        if (aggregator.startsWith("COUNT(") && aggregator.endsWith(")")) { // TODO: there should be a special pattern for this to make it more efficient
            return a.put(new JSONObject().put(aggregator_as, choices.length()));
        }
        if (aggregator.startsWith("MAX(") && aggregator.endsWith(")")) {
            final AtomicDouble max = new AtomicDouble(Double.MIN_VALUE);
            String c = aggregator.substring(4, aggregator.length() - 1);
            choices.forEach(json -> max.set(Math.max(max.get(), ((JSONObject) json).getDouble(c))));
            return a.put(new JSONObject().put(aggregator_as, max.get()));
        }
        if (aggregator.startsWith("MIN(") && aggregator.endsWith(")")) {
            final AtomicDouble min = new AtomicDouble(Double.MAX_VALUE);
            String c = aggregator.substring(4, aggregator.length() - 1);
            choices.forEach(json -> min.set(Math.min(min.get(), ((JSONObject) json).getDouble(c))));
            return a.put(new JSONObject().put(aggregator_as, min.get()));
        }
        if (aggregator.startsWith("SUM(") && aggregator.endsWith(")")) {
            final AtomicDouble sum = new AtomicDouble(0.0d);
            String c = aggregator.substring(4, aggregator.length() - 1);
            choices.forEach(json -> sum.addAndGet(((JSONObject) json).getDouble(c)));
            return a.put(new JSONObject().put(aggregator_as, sum.get()));
        }
        if (aggregator.startsWith("AVG(") && aggregator.endsWith(")")) {
            final AtomicDouble sum = new AtomicDouble(0.0d);
            String c = aggregator.substring(4, aggregator.length() - 1);
            choices.forEach(json -> sum.addAndGet(((JSONObject) json).getDouble(c)));
            return a.put(new JSONObject().put(aggregator_as, sum.get() / choices.length()));
        }
    }
    if (this.selectionMapping != null && this.selectionMapping.size() == 2) {
        Iterator<String> ci = this.selectionMapping.keySet().iterator();
        String aggregator = ci.next();
        String column = ci.next();
        if (column.indexOf('(') >= 0) {
            String s = aggregator;
            aggregator = column;
            column = s;
        }
        final String aggregator_as = this.selectionMapping.get(aggregator);
        final String column_as = this.selectionMapping.get(column);
        final String column_final = column;
        if (aggregator.startsWith("PERCENT(") && aggregator.endsWith(")")) {
            final AtomicDouble sum = new AtomicDouble(0.0d);
            String c = aggregator.substring(8, aggregator.length() - 1);
            choices.forEach(json -> sum.addAndGet(((JSONObject) json).getDouble(c)));
            choices.forEach(json -> a.put(
                    new JSONObject().put(aggregator_as, 100.0d * ((JSONObject) json).getDouble(c) / sum.get())
                            .put(column_as, ((JSONObject) json).get(column_final))));
            return a;
        }
    }
    // this.selectionMapping == null -> extract everything
    for (Object json : choices) {
        JSONObject extraction = this.extract((JSONObject) json);
        if (extraction.length() > 0)
            a.put(extraction);
    }
    return a;
}

From source file:org.apache.solr.cloud.autoscaling.SearchRateTrigger.java

@Override
public void run() {
    AutoScaling.TriggerEventProcessor processor = processorRef.get();
    if (processor == null) {
        return;//w  w w.  j  a v a2s.  com
    }

    Map<String, Map<String, List<ReplicaInfo>>> collectionRates = new HashMap<>();
    Map<String, AtomicDouble> nodeRates = new HashMap<>();

    for (String node : cloudManager.getClusterStateProvider().getLiveNodes()) {
        Map<String, ReplicaInfo> metricTags = new HashMap<>();
        // coll, shard, replica
        Map<String, Map<String, List<ReplicaInfo>>> infos = cloudManager.getNodeStateProvider()
                .getReplicaInfo(node, Collections.emptyList());
        infos.forEach((coll, shards) -> {
            shards.forEach((sh, replicas) -> {
                replicas.forEach(replica -> {
                    // we have to translate to the metrics registry name, which uses "_replica_nN" as suffix
                    String replicaName = Utils.parseMetricsReplicaName(coll, replica.getCore());
                    if (replicaName == null) { // should never happen???
                        replicaName = replica.getName(); // which is actually coreNode name...
                    }
                    String registry = SolrCoreMetricManager.createRegistryName(true, coll, sh, replicaName,
                            null);
                    String tag = "metrics:" + registry + ":QUERY." + handler + ".requestTimes:1minRate";
                    metricTags.put(tag, replica);
                });
            });
        });
        Map<String, Object> rates = cloudManager.getNodeStateProvider().getNodeValues(node,
                metricTags.keySet());
        rates.forEach((tag, rate) -> {
            ReplicaInfo info = metricTags.get(tag);
            if (info == null) {
                log.warn("Missing replica info for response tag " + tag);
            } else {
                Map<String, List<ReplicaInfo>> perCollection = collectionRates
                        .computeIfAbsent(info.getCollection(), s -> new HashMap<>());
                List<ReplicaInfo> perShard = perCollection.computeIfAbsent(info.getShard(),
                        s -> new ArrayList<>());
                info.getVariables().put(AutoScalingParams.RATE, rate);
                perShard.add(info);
                AtomicDouble perNode = nodeRates.computeIfAbsent(node, s -> new AtomicDouble());
                perNode.addAndGet((Double) rate);
            }
        });
    }

    long now = timeSource.getTime();
    // check for exceeded rates and filter out those with less than waitFor from previous events
    Map<String, Double> hotNodes = nodeRates.entrySet().stream()
            .filter(entry -> node.equals(Policy.ANY) || node.equals(entry.getKey()))
            .filter(entry -> waitForElapsed(entry.getKey(), now, lastNodeEvent))
            .filter(entry -> entry.getValue().get() > rate)
            .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue().get()));

    Map<String, Map<String, Double>> hotShards = new HashMap<>();
    List<ReplicaInfo> hotReplicas = new ArrayList<>();
    collectionRates.forEach((coll, shardRates) -> {
        shardRates.forEach((sh, replicaRates) -> {
            double shardRate = replicaRates.stream().map(r -> {
                if (waitForElapsed(r.getCollection() + "." + r.getCore(), now, lastReplicaEvent)
                        && ((Double) r.getVariable(AutoScalingParams.RATE) > rate)) {
                    hotReplicas.add(r);
                }
                return r;
            }).mapToDouble(r -> (Double) r.getVariable(AutoScalingParams.RATE)).sum();
            if (waitForElapsed(coll + "." + sh, now, lastShardEvent) && (shardRate > rate)
                    && (collection.equals(Policy.ANY) || collection.equals(coll))
                    && (shard.equals(Policy.ANY) || shard.equals(sh))) {
                hotShards.computeIfAbsent(coll, s -> new HashMap<>()).put(sh, shardRate);
            }
        });
    });

    Map<String, Double> hotCollections = new HashMap<>();
    collectionRates.forEach((coll, shardRates) -> {
        double total = shardRates.entrySet().stream().mapToDouble(e -> e.getValue().stream()
                .mapToDouble(r -> (Double) r.getVariable(AutoScalingParams.RATE)).sum()).sum();
        if (waitForElapsed(coll, now, lastCollectionEvent) && (total > rate)
                && (collection.equals(Policy.ANY) || collection.equals(coll))) {
            hotCollections.put(coll, total);
        }
    });

    if (hotCollections.isEmpty() && hotShards.isEmpty() && hotReplicas.isEmpty() && hotNodes.isEmpty()) {
        return;
    }

    // generate event

    // find the earliest time when a condition was exceeded
    final AtomicLong eventTime = new AtomicLong(now);
    hotCollections.forEach((c, r) -> {
        long time = lastCollectionEvent.get(c);
        if (eventTime.get() > time) {
            eventTime.set(time);
        }
    });
    hotShards.forEach((c, shards) -> {
        shards.forEach((s, r) -> {
            long time = lastShardEvent.get(c + "." + s);
            if (eventTime.get() > time) {
                eventTime.set(time);
            }
        });
    });
    hotReplicas.forEach(r -> {
        long time = lastReplicaEvent.get(r.getCollection() + "." + r.getCore());
        if (eventTime.get() > time) {
            eventTime.set(time);
        }
    });
    hotNodes.forEach((n, r) -> {
        long time = lastNodeEvent.get(n);
        if (eventTime.get() > time) {
            eventTime.set(time);
        }
    });

    if (processor.process(new SearchRateEvent(getName(), eventTime.get(), hotNodes, hotCollections, hotShards,
            hotReplicas))) {
        // update lastEvent times
        hotNodes.keySet().forEach(node -> lastNodeEvent.put(node, now));
        hotCollections.keySet().forEach(coll -> lastCollectionEvent.put(coll, now));
        hotShards.entrySet().forEach(
                e -> e.getValue().forEach((sh, rate) -> lastShardEvent.put(e.getKey() + "." + sh, now)));
        hotReplicas.forEach(r -> lastReplicaEvent.put(r.getCollection() + "." + r.getCore(), now));
    }
}

From source file:com.globocom.grou.report.ts.opentsdb.OpenTSDBClient.java

@SuppressWarnings("unchecked")
@Override/*  w  w w. j  a  v  a 2s  . co  m*/
public Map<String, Double> makeReport(Test test) {
    final TreeMap<String, Double> mapOfResult = new TreeMap<>();
    ArrayList<HashMap<String, Object>> metrics = Optional.ofNullable(metrics(test)).orElse(new ArrayList<>());
    metrics.stream().filter(metric -> Objects.nonNull(metric.get("metric"))).forEach(metric -> {
        String key = (String) metric.get("metric");
        String aggr = (String) metric.get("aggr");
        int durationTimeMillis = test.getDurationTimeMillis();
        Map<String, Double> dps = Optional.ofNullable((Map<String, Double>) metric.get("dps"))
                .orElse(Collections.emptyMap());
        final AtomicDouble reduceSum = new AtomicDouble(0.0);
        final AtomicDouble reduceMax = new AtomicDouble(0.0);
        dps.entrySet().stream().mapToDouble(Map.Entry::getValue).forEach(delta -> {
            reduceSum.addAndGet(delta);
            if (reduceMax.get() < delta)
                reduceMax.set(delta);
        });
        double value = reduceSum.get();
        double max = reduceMax.get();
        if (!Double.isNaN(value)) {
            if ("sum".equals(aggr)) {
                int durationTimeSecs = durationTimeMillis / 1000;
                double avg = value / (double) durationTimeSecs;
                mapOfResult.put(key + " (total)", formatValue(value));
                mapOfResult.put(key + " (avg tps)", formatValue(avg));
                mapOfResult.put(key + " (max tps)",
                        formatValue(max / Math.max(1.0, (double) durationTimeSecs / (double) NUM_SAMPLES)));
            } else {
                value = value / (double) dps.size();
                mapOfResult.put(key, formatValue(value));
            }
        }
    });
    if (mapOfResult.isEmpty())
        LOGGER.error("Test {}.{}: makeReport return NULL", test.getProject(), test.getName());
    return mapOfResult;
}