Example usage for org.apache.mahout.math Vector clone

List of usage examples for org.apache.mahout.math Vector clone

Introduction

In this page you can find the example usage for org.apache.mahout.math Vector clone.

Prototype

@SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException")
Vector clone();

Source Link

Document

Return a copy of the recipient

Usage

From source file:org.trustedanalytics.atk.giraph.algorithms.lp.LabelPropagationComputation.java

License:Apache License

@Override
public void compute(Vertex<LongWritable, VertexData4LPWritable, DoubleWritable> vertex,
        Iterable<IdWithVectorMessage> messages) throws IOException {
    long superStep = getSuperstep();

    if (superStep == 0) {
        initializeVertexEdges(vertex);/*  www. j a  v a2 s  . c o m*/
        vertex.voteToHalt();
    } else if (superStep <= maxSupersteps) {
        VertexData4LPWritable vertexValue = vertex.getValue();
        Vector prior = vertexValue.getPriorVector();
        Vector posterior = vertexValue.getPosteriorVector();
        double degree = vertexValue.getDegree();

        // collect messages sent to this vertex
        HashMap<Long, Vector> map = new HashMap();
        for (IdWithVectorMessage message : messages) {
            map.put(message.getData(), message.getVector());
        }

        // Update belief and calculate cost
        double hi = prior.getQuick(0);
        double fi = posterior.getQuick(0);
        double crossSum = 0d;
        Vector newBelief = posterior.clone().assign(0d);

        for (Edge<LongWritable, DoubleWritable> edge : vertex.getEdges()) {
            double weight = edge.getValue().get();
            if (weight <= 0d) {
                throw new IllegalArgumentException(
                        "Vertex ID: " + vertex.getId() + "has an edge with negative or zero value");
            }
            long targetVertex = edge.getTargetVertexId().get();
            if (map.containsKey(targetVertex)) {
                Vector tempVector = map.get(targetVertex);
                newBelief = newBelief.plus(tempVector.times(weight));
                double fj = tempVector.getQuick(0);
                crossSum += weight * fi * fj;
            }
        }

        double cost = degree
                * ((1 - lambda) * (Math.pow(fi, 2) - crossSum) + 0.5 * lambda * Math.pow((fi - hi), 2));
        aggregate(SUM_COST, new DoubleWritable(cost));

        // Update posterior if the vertex was not processed
        if (vertexValue.wasLabeled() == false) {
            newBelief = (newBelief.times(1 - lambda).plus(prior.times(lambda))).normalize(1d);
            vertexValue.setPosteriorVector(newBelief);
        }

        // Send out messages if not the last step
        if (superStep != maxSupersteps) {
            IdWithVectorMessage newMessage = new IdWithVectorMessage(vertex.getId().get(),
                    vertexValue.getPosteriorVector());
            sendMessageToAllEdges(vertex, newMessage);
        }
    }

    vertex.voteToHalt();
}