use of org.apache.tinkerpop.gremlin.process.computer.KeyValue in project janusgraph by JanusGraph.
the class OLAPTest method testPageRank.
@Test
public void testPageRank() throws ExecutionException, InterruptedException {
mgmt.makePropertyKey("distance").dataType(Integer.class).cardinality(Cardinality.SINGLE).make();
mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.MULTI).make();
mgmt.makeEdgeLabel("likes").multiplicity(Multiplicity.MULTI).make();
finishSchema();
final int branch = 6;
final int diameter = 5;
final double alpha = 0.85d;
int numV = (int) ((Math.pow(branch, diameter + 1) - 1) / (branch - 1));
JanusGraphVertex v = tx.addVertex();
expand(v, 0, diameter, branch);
clopen();
assertCount(numV, tx.query().vertices());
log.debug("PR test numV: {}", numV);
newTx();
// Precompute correct PR results:
double[] correctPR = new double[diameter + 1];
for (int i = diameter; i >= 0; i--) {
double pr = (1.0D - alpha) / numV;
if (i < diameter)
pr += alpha * branch * correctPR[i + 1];
log.debug("diameter={} pr={}", diameter, pr);
correctPR[i] = pr;
}
double correctPRSum = 0;
for (final JanusGraphVertex janusGraphVertex : tx.query().vertices()) {
correctPRSum += correctPR[janusGraphVertex.<Integer>value("distance")];
}
final JanusGraphComputer computer = graph.compute();
computer.resultMode(JanusGraphComputer.ResultMode.NONE);
computer.workers(4);
computer.program(PageRankVertexProgram.build().iterations(10).vertexCount(numV).dampingFactor(alpha).create(graph));
computer.mapReduce(PageRankMapReduce.build().create());
ComputerResult result = computer.submit().get();
Iterator<KeyValue<Long, Double>> ranks = result.memory().get(PageRankMapReduce.DEFAULT_MEMORY_KEY);
assertNotNull(ranks);
int vertexCounter = 0;
double computedPRSum = 0;
correctPRSum = 0;
final Set<Long> vertexIDs = new HashSet<>(numV);
while (ranks.hasNext()) {
final KeyValue<Long, Double> rank = ranks.next();
final Long vertexID = rank.getKey();
final Double computedPR = rank.getValue();
assertNotNull(vertexID);
assertNotNull(computedPR);
final JanusGraphVertex u = getV(tx, vertexID);
final int distance = u.<Integer>value("distance");
vertexCounter++;
// assertEquals("Incorrect PR on vertex #" + vertexCounter, correctPR[distance], computedPR, EPSILON);
computedPRSum += computedPR;
correctPRSum += correctPR[distance];
assertFalse(vertexIDs.contains(vertexID));
vertexIDs.add(vertexID);
log.debug("vertexID={} computedPR={}", vertexID, computedPR);
}
assertEquals(numV, vertexCounter);
assertEquals(correctPRSum, computedPRSum, 0.001);
}
use of org.apache.tinkerpop.gremlin.process.computer.KeyValue in project janusgraph by JanusGraph.
the class OLAPTest method testShortestDistance.
@Test
public void testShortestDistance() throws Exception {
PropertyKey distance = mgmt.makePropertyKey("distance").dataType(Integer.class).cardinality(Cardinality.SINGLE).make();
mgmt.makeEdgeLabel("connect").signature(distance).multiplicity(Multiplicity.MULTI).make();
finishSchema();
int maxDepth = 16;
int maxBranch = 5;
JanusGraphVertex vertex = tx.addVertex();
// Grow a star-shaped graph around vertex which will be the single-source for this shortest path computation
final int numV = growVertex(vertex, 0, maxDepth, maxBranch);
final int numE = numV - 1;
assertCount(numV, tx.query().vertices());
assertCount(numE, tx.query().edges());
log.debug("seed inE count: {}", vertex.query().direction(Direction.IN).edgeCount());
log.debug("seed outE count: {}", vertex.query().direction(Direction.OUT).edgeCount());
clopen();
final JanusGraphComputer computer = graph.compute();
computer.resultMode(JanusGraphComputer.ResultMode.NONE);
computer.workers(4);
computer.program(ShortestDistanceVertexProgram.build().seed((long) vertex.id()).maxDepth(maxDepth + 4).create(graph));
computer.mapReduce(ShortestDistanceMapReduce.build().create());
ComputerResult result = computer.submit().get();
Iterator<KeyValue<Long, Long>> distances = result.memory().get(ShortestDistanceMapReduce.DEFAULT_MEMORY_KEY);
int vertexCount = 0;
while (distances.hasNext()) {
final KeyValue<Long, Long> kv = distances.next();
final long dist = kv.getValue();
assertTrue(dist >= 0 && dist < Integer.MAX_VALUE, "Invalid distance: " + dist);
JanusGraphVertex v = getV(tx, kv.getKey());
assertEquals(v.<Integer>value("distance").intValue(), dist);
vertexCount++;
}
assertEquals(numV, vertexCount);
assertTrue(0 < vertexCount);
}
Aggregations