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);
}
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());
}
}
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();
}
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);
}
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);
}
}
Aggregations