Search in sources :

Example 41 with DoubleValue

use of com.baidu.hugegraph.computer.core.graph.value.DoubleValue in project hugegraph-computer by hugegraph.

the class DefaultPropertiesTest method testEquals.

@Test
public void testEquals() {
    DefaultProperties props1 = new DefaultProperties(graphFactory());
    props1.put("p1", new LongValue(1L));
    props1.put("p2", new DoubleValue(2.0D));
    DefaultProperties props2 = new DefaultProperties(props1.get(), UnitTestBase.graphFactory());
    Assert.assertEquals(props1, props2);
}
Also used : DefaultProperties(com.baidu.hugegraph.computer.core.graph.properties.DefaultProperties) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) Test(org.junit.Test)

Example 42 with DoubleValue

use of com.baidu.hugegraph.computer.core.graph.value.DoubleValue in project hugegraph-computer by hugegraph.

the class StreamGraphOutputInputTest method testWriteReadMessage.

@Test
public void testWriteReadMessage() throws IOException {
    UnitTestBase.updateOptions(ComputerOptions.ALGORITHM_MESSAGE_CLASS, DoubleValue.class.getName());
    Id id = BytesId.of(999L);
    Value value = new DoubleValue(0.85D);
    byte[] bytes;
    try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
        StreamGraphOutput output = newStreamGraphOutput(bao);
        output.writeMessage(id, value);
        bytes = bao.toByteArray();
        System.out.println(Arrays.toString(bytes));
    }
    try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
        StreamGraphInput input = newStreamGraphInput(bai);
        Assert.assertEquals(Pair.of(id, value), input.readMessage());
    }
}
Also used : DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) Value(com.baidu.hugegraph.computer.core.graph.value.Value) LongValue(com.baidu.hugegraph.computer.core.graph.value.LongValue) Id(com.baidu.hugegraph.computer.core.graph.id.Id) BytesId(com.baidu.hugegraph.computer.core.graph.id.BytesId) Test(org.junit.Test)

Example 43 with DoubleValue

use of com.baidu.hugegraph.computer.core.graph.value.DoubleValue in project hugegraph-computer by hugegraph.

the class DegreeCentrality method compute0.

@Override
public void compute0(ComputationContext context, Vertex vertex) {
    if (!this.calculateByWeightProperty) {
        vertex.value(new DoubleValue(vertex.numEdges()));
    } else {
        /*
             *  TODO: Here we use doubleValue type now, we will use BigDecimal
             *  and output "BigDecimalValue" to resolve double type overflow
             *  int the future;
             */
        double totalWeight = 0.0;
        Iterator<Edge> edges = vertex.edges().iterator();
        while (edges.hasNext()) {
            Edge edge = edges.next();
            double weight = weightValue(edge.property(this.weightProperty));
            totalWeight += weight;
            if (Double.isInfinite(totalWeight)) {
                throw new ComputerException("Calculate weight overflow," + "current is %s, edge '%s' " + "is %s", totalWeight, edge, weight);
            }
        }
        vertex.value(new DoubleValue(totalWeight));
    }
    vertex.inactivate();
}
Also used : DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 44 with DoubleValue

use of com.baidu.hugegraph.computer.core.graph.value.DoubleValue in project hugegraph-computer by hugegraph.

the class PageRank method beforeSuperstep.

@Override
public void beforeSuperstep(WorkerContext context) {
    // Get aggregator values for computation
    DoubleValue danglingTotalRank = context.aggregatedValue(PageRank4Master.AGGR_COMULATIVE_DANGLING_PROBABILITY);
    DoubleValue cumulativeRank = context.aggregatedValue(PageRank4Master.AGGR_COMULATIVE_PROBABILITY);
    long totalVertex = context.totalVertexCount();
    this.danglingRank = danglingTotalRank.value() / totalVertex;
    this.initialRankInSuperstep = this.alpha / totalVertex;
    this.cumulativeRank = cumulativeRank.value();
    this.initialValue = new DoubleValue(1.0 / totalVertex);
    // Create aggregators
    this.l1DiffAggr = context.createAggregator(PageRank4Master.AGGR_L1_NORM_DIFFERENCE_KEY);
    this.cumulativeRankAggr = context.createAggregator(PageRank4Master.AGGR_COMULATIVE_PROBABILITY);
    this.danglingVertexNumAggr = context.createAggregator(PageRank4Master.AGGR_DANGLING_VERTICES_NUM);
    this.danglingCumulativeAggr = context.createAggregator(PageRank4Master.AGGR_COMULATIVE_DANGLING_PROBABILITY);
}
Also used : DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue)

Example 45 with DoubleValue

use of com.baidu.hugegraph.computer.core.graph.value.DoubleValue in project hugegraph-computer by hugegraph.

the class PageRank method compute.

@Override
public void compute(ComputationContext context, Vertex vertex, Iterator<DoubleValue> messages) {
    DoubleValue message = Combiner.combineAll(context.combiner(), messages);
    double rankFromNeighbors = 0.0;
    if (message != null) {
        rankFromNeighbors = message.value();
    }
    double rank = (this.danglingRank + rankFromNeighbors) * (1.0 - this.alpha) + this.initialRankInSuperstep;
    rank /= this.cumulativeRank;
    DoubleValue oldRank = vertex.value();
    vertex.value(new DoubleValue(rank));
    this.l1DiffAggr.aggregateValue(Math.abs(oldRank.value() - rank));
    this.cumulativeRankAggr.aggregateValue(rank);
    int edgeCount = vertex.numEdges();
    if (edgeCount == 0) {
        this.danglingVertexNumAggr.aggregateValue(1L);
        this.danglingCumulativeAggr.aggregateValue(rank);
    } else {
        this.contribValue.value(rank / edgeCount);
        context.sendMessageToAllEdges(vertex, this.contribValue);
    }
}
Also used : DoubleValue(com.baidu.hugegraph.computer.core.graph.value.DoubleValue)

Aggregations

DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)49 Test (org.junit.Test)28 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)21 IntValue (com.baidu.hugegraph.computer.core.graph.value.IntValue)14 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)13 FloatValue (com.baidu.hugegraph.computer.core.graph.value.FloatValue)12 Id (com.baidu.hugegraph.computer.core.graph.id.Id)10 GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)8 DefaultProperties (com.baidu.hugegraph.computer.core.graph.properties.DefaultProperties)7 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)6 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)5 BooleanValue (com.baidu.hugegraph.computer.core.graph.value.BooleanValue)4 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)3 Value (com.baidu.hugegraph.computer.core.graph.value.Value)3 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)2 DefaultEdge (com.baidu.hugegraph.computer.core.graph.edge.DefaultEdge)2 IdList (com.baidu.hugegraph.computer.core.graph.value.IdList)2 IdListList (com.baidu.hugegraph.computer.core.graph.value.IdListList)2 ListValue (com.baidu.hugegraph.computer.core.graph.value.ListValue)2 File (java.io.File)2