Java Code Examples for org.apache.giraph.graph.Vertex#getValue()

The following examples show how to use org.apache.giraph.graph.Vertex#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MaxComputation.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
@Override
public void compute(final Vertex<ObjectWritable<Integer>, VertexValueWritable<IntWritable>, EdgeValueWritable<NullWritable>> vertex,
                    final Iterable<IntWritable> messages) throws IOException {
    VertexValueWritable<IntWritable> vertexValue = vertex.getValue();
    HBaseVertex v = vertexValue.getVertex();
    if (vertexValue.getValue() == null) {
        vertexValue.setValue(new IntWritable(((Number) v.id()).intValue()));
    }
    int value = vertexValue.getValue().get();
    boolean changed = false;
    for (IntWritable message : messages) {
        if (value < message.get()) {
            value = message.get();
            vertexValue.setValue(new IntWritable(value));
            changed = true;
        }
    }
    if (getSuperstep() == 0 || changed) {
        sendMessageToAllEdges(vertex, new IntWritable(value));
    }
    vertex.voteToHalt();
}
 
Example 2
Source File: LouvainComputation.java    From distributed-graph-analytics with Apache License 2.0 6 votes vote down vote up
/**
 * Calculate this nodes contribution for the actual q value of the graph.
 */
private double calculateActualQ(Vertex<Text, LouvainNodeState, LongWritable> vertex, Iterable<LouvainMessage> messages) {
    // long start = System.currentTimeMillis();
    LouvainNodeState state = vertex.getValue();
    long k_i_in = state.getInternalWeight();
    for (LouvainMessage m : messages) {
        if (m.getCommunityId().equals(state.getCommunity())) {
            try {
                k_i_in += vertex.getEdgeValue(new Text(m.getSourceId())).get();
            } catch (NullPointerException e) {
                throw new IllegalStateException("Node: " + vertex.getId() + " does not have edge: " + m.getSourceId() + "  check that the graph is bi-directional.");
            }
        }
    }
    long sigma_tot = vertex.getValue().getCommunitySigmaTotal();
    long M = this.getTotalEdgeWeight();
    long k_i = vertex.getValue().getNodeWeight() + vertex.getValue().getInternalWeight();

    double q = ((((double) k_i_in) / M) - (((double) (sigma_tot * k_i)) / Math.pow(M, 2)));
    q = (q < 0) ? 0 : q;

    // long end = System.currentTimeMillis();
    // System.out.println("calculated actual q in :"+(end-start));
    return q;
}
 
Example 3
Source File: SimpleShortestPathsComputation.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Override
public void compute(
        Vertex<ObjectWritable<Long>, VertexValueWritable<DoubleWritable>, EdgeValueWritable<FloatWritable>> vertex,
        Iterable<DoubleWritable> messages) throws IOException {
    VertexValueWritable<DoubleWritable> vertexValue = vertex.getValue();
    if (getSuperstep() == 0) {
        vertexValue.setValue(new DoubleWritable(Double.MAX_VALUE));
    }
    double minDist = isSource(vertex) ? 0d : Double.MAX_VALUE;
    for (DoubleWritable message : messages) {
        minDist = Math.min(minDist, message.get());
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Vertex " + vertex.getId() + " got minDist = " + minDist +
                " vertex value = " + vertex.getValue());
    }
    if (minDist < vertexValue.getValue().get()) {
        vertexValue.setValue(new DoubleWritable(minDist));
        for (Edge<ObjectWritable<Long>, EdgeValueWritable<FloatWritable>> edge : vertex.getEdges()) {
            double distance = minDist + ((Number) edge.getValue().getEdge().property("weight").value()).doubleValue();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Vertex " + vertex.getId() + " sent to " +
                        edge.getTargetVertexId() + " = " + distance);
            }
            sendMessage(edge.getTargetVertexId(), new DoubleWritable(distance));
        }
    }
    vertex.voteToHalt();
}
 
Example 4
Source File: LouvainComputation.java    From distributed-graph-analytics with Apache License 2.0 5 votes vote down vote up
/**
 * Each vertex will recieve its own communities sigma_total (if updated),
 * and then send its currenty community info to each of its neighbors.
 *
 * @param messages
 */
private void getAndSendCommunityInfo(Vertex<Text, LouvainNodeState, LongWritable> vertex, Iterable<LouvainMessage> messages) {
    LouvainNodeState state = vertex.getValue();
    // set new community information.
    if (getSuperstep() > 0) {
        Iterator<LouvainMessage> it = messages.iterator();
        if (!it.hasNext()) {
            throw new IllegalStateException("No community info recieved in getAndSendCommunityInfo! Superstep: " + getSuperstep() + " id: " + vertex.getId());
        }
        LouvainMessage inMess = it.next();
        if (it.hasNext()) {
            throw new IllegalStateException("More than one community info packets recieved in getAndSendCommunityInfo! Superstep: " + getSuperstep() + " id: " + vertex.getId());
        }
        state.setCommunity(inMess.getCommunityId());
        state.setCommunitySigmaTotal(inMess.getCommunitySigmaTotal());
    }

    // send community info to all neighbors
    for (Edge<Text, LongWritable> edge : vertex.getEdges()) {
        LouvainMessage outMess = new LouvainMessage();
        outMess.setCommunityId(state.getCommunity());
        outMess.setCommunitySigmaTotal(state.getCommunitySigmaTotal());
        outMess.setEdgeWeight(edge.getValue().get());
        outMess.setSourceId(vertex.getId().toString());
        this.sendMessage(edge.getTargetVertexId(), outMess);
    }

}
 
Example 5
Source File: LouvainComputation.java    From distributed-graph-analytics with Apache License 2.0 4 votes vote down vote up
/**
 * Based on community of each of its neighbors, each vertex determimnes if
 * it should retain its currenty community or swtich to a neighboring
 * communinity.
 * <p/>
 * At the end of this step a message is sent to the nodes community hub so a
 * new community sigma_total can be calculated.
 *
 * @param messages
 * @param iteration
 */
private void calculateBestCommunity(Vertex<Text, LouvainNodeState, LongWritable> vertex, Iterable<LouvainMessage> messages, int iteration) {

    LouvainNodeState state = vertex.getValue();

    // group messages by communities.
    HashMap<String, LouvainMessage> communityMap = new HashMap<String, LouvainMessage>();
    for (LouvainMessage message : messages) {

        String communityId = message.getCommunityId();
        long weight = message.getEdgeWeight();
        LouvainMessage newmess = new LouvainMessage(message);

        if (communityMap.containsKey(communityId)) {
            LouvainMessage m = communityMap.get(communityId);
            m.setEdgeWeight(m.getEdgeWeight() + weight);
        } else {
            communityMap.put(communityId, newmess);
        }
    }

    // calculate change in Q for each potential community
    String bestCommunityId = vertex.getValue().getCommunity();
    String startingCommunityId = bestCommunityId;
    BigDecimal maxDeltaQ = new BigDecimal("0.0");
    for (Map.Entry<String, LouvainMessage> entry : communityMap.entrySet()) {
        BigDecimal deltaQ = calculateQDelta(startingCommunityId, entry.getValue().getCommunityId(), entry.getValue().getCommunitySigmaTotal(), entry.getValue().getEdgeWeight(), state.getNodeWeight(), state.getInternalWeight());
        if (deltaQ.compareTo(maxDeltaQ) > 0 || (deltaQ.equals(maxDeltaQ) && entry.getValue().getCommunityId().compareTo(bestCommunityId) < 0)) {
            bestCommunityId = entry.getValue().getCommunityId();
            maxDeltaQ = deltaQ;
        }
    }

    // ignore switches based on iteration (prevent certain cycles)
    if ((state.getCommunity().compareTo(bestCommunityId) > 0 && iteration % 2 == 0) || (state.getCommunity().compareTo(bestCommunityId) < 0 && iteration % 2 != 0)) {
        bestCommunityId = state.getCommunity();
        // System.out.println("Iteration: "+iteration+" Node: "+getId()+" held stable to prevent cycle");
    }

    // update community and change count
    if (!state.getCommunity().equals(bestCommunityId)) {
        String old = state.getCommunity();
        LouvainMessage c = communityMap.get(bestCommunityId);
        if (!bestCommunityId.equals(c.getCommunityId())) {
            throw new IllegalStateException("Community mapping contains wrong Id");
        }
        state.setCommunity(c.getCommunityId());
        state.setCommunitySigmaTotal(c.getCommunitySigmaTotal());
        state.setChanged(1L);
        logger.debug("Iteration: {} Node: {} changed from {} -> {} dq: {}", iteration, vertex.getId(), old, state.getCommunity(), maxDeltaQ);
    }

    // send our node weight to the community hub to be summed in next
    // superstep
    this.sendMessage(new Text(state.getCommunity()), new LouvainMessage(state.getCommunity(), state.getNodeWeight() + state.getInternalWeight(), 0, vertex.getId().toString()));
}