javeme commented on code in PR #280:
URL:
https://github.com/apache/incubator-hugegraph-computer/pull/280#discussion_r1390129604
##########
computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java:
##########
@@ -146,29 +251,128 @@ public void compute(ComputationContext context, Vertex
vertex,
continue;
}
+ vertex.edges().forEach(edge ->
message.addToPreVertexAdjacence(edge.targetId()));
+
// random select one edge and walk
- Edge selectedEdge = this.randomSelectEdge(vertex.edges());
+ Edge selectedEdge = this.randomSelectEdge(preVertexId,
message.preVertexAdjacence(),
+ vertex.edges());
context.sendMessage(selectedEdge.targetId(), message);
}
}
/**
* random select one edge
*/
- private Edge randomSelectEdge(Edges edges) {
- Edge selectedEdge = null;
- int randomNum = random.nextInt(edges.size());
+ private Edge randomSelectEdge(Id preVertexId, IdList
preVertexAdjacenceIdList, Edges edges) {
+ List<Double> weightList = new ArrayList<>();
- int i = 0;
Iterator<Edge> iterator = edges.iterator();
while (iterator.hasNext()) {
- selectedEdge = iterator.next();
- if (i == randomNum) {
+ Edge edge = iterator.next();
+ // calculate weight
+ Value weight = this.getWeight(edge);
+ Double finalWeight = this.calculateWeight(preVertexId,
preVertexAdjacenceIdList,
+ edge.targetId(), weight);
+ weightList.add(finalWeight);
+ }
+
+ int selectedIndex = this.randomSelectIndex(weightList);
+ Edge selectedEdge = this.selectEdge(edges.iterator(), selectedIndex);
+ return selectedEdge;
+ }
+
+ /**
+ * get edge weight by weight property
+ */
+ private Value getWeight(Edge edge) {
+ Value weight = edge.property(this.weightProperty);
+ if (weight == null) {
+ weight = new DoubleValue(this.defaultWeight);
+ }
+
+ if (!weight.isNumber()) {
+ throw new ComputerException("The value of %s must be a numeric
value, " +
+ "actual got '%s'",
+ this.weightProperty, weight.string());
+ }
+
+ // weight threshold truncation
+ if ((Double) weight.value() < this.minWeightThreshold) {
Review Comment:
can we call weight.doubleValue() here?
##########
computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java:
##########
@@ -146,29 +251,128 @@ public void compute(ComputationContext context, Vertex
vertex,
continue;
}
+ vertex.edges().forEach(edge ->
message.addToPreVertexAdjacence(edge.targetId()));
+
// random select one edge and walk
- Edge selectedEdge = this.randomSelectEdge(vertex.edges());
+ Edge selectedEdge = this.randomSelectEdge(preVertexId,
message.preVertexAdjacence(),
+ vertex.edges());
context.sendMessage(selectedEdge.targetId(), message);
}
}
/**
* random select one edge
*/
- private Edge randomSelectEdge(Edges edges) {
- Edge selectedEdge = null;
- int randomNum = random.nextInt(edges.size());
+ private Edge randomSelectEdge(Id preVertexId, IdList
preVertexAdjacenceIdList, Edges edges) {
+ List<Double> weightList = new ArrayList<>();
- int i = 0;
Iterator<Edge> iterator = edges.iterator();
while (iterator.hasNext()) {
- selectedEdge = iterator.next();
- if (i == randomNum) {
+ Edge edge = iterator.next();
+ // calculate weight
+ Value weight = this.getWeight(edge);
+ Double finalWeight = this.calculateWeight(preVertexId,
preVertexAdjacenceIdList,
+ edge.targetId(), weight);
+ weightList.add(finalWeight);
+ }
+
+ int selectedIndex = this.randomSelectIndex(weightList);
+ Edge selectedEdge = this.selectEdge(edges.iterator(), selectedIndex);
+ return selectedEdge;
+ }
+
+ /**
+ * get edge weight by weight property
Review Comment:
"Get the weight of a edge by its weight property"
##########
computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java:
##########
@@ -146,29 +251,128 @@ public void compute(ComputationContext context, Vertex
vertex,
continue;
}
+ vertex.edges().forEach(edge ->
message.addToPreVertexAdjacence(edge.targetId()));
+
// random select one edge and walk
- Edge selectedEdge = this.randomSelectEdge(vertex.edges());
+ Edge selectedEdge = this.randomSelectEdge(preVertexId,
message.preVertexAdjacence(),
+ vertex.edges());
context.sendMessage(selectedEdge.targetId(), message);
}
}
/**
* random select one edge
*/
- private Edge randomSelectEdge(Edges edges) {
- Edge selectedEdge = null;
- int randomNum = random.nextInt(edges.size());
+ private Edge randomSelectEdge(Id preVertexId, IdList
preVertexAdjacenceIdList, Edges edges) {
+ List<Double> weightList = new ArrayList<>();
- int i = 0;
Iterator<Edge> iterator = edges.iterator();
while (iterator.hasNext()) {
- selectedEdge = iterator.next();
- if (i == randomNum) {
+ Edge edge = iterator.next();
+ // calculate weight
+ Value weight = this.getWeight(edge);
+ Double finalWeight = this.calculateWeight(preVertexId,
preVertexAdjacenceIdList,
+ edge.targetId(), weight);
+ weightList.add(finalWeight);
+ }
+
+ int selectedIndex = this.randomSelectIndex(weightList);
+ Edge selectedEdge = this.selectEdge(edges.iterator(), selectedIndex);
+ return selectedEdge;
+ }
+
+ /**
+ * get edge weight by weight property
+ */
+ private Value getWeight(Edge edge) {
Review Comment:
prefer getEdgeWeight()
##########
computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java:
##########
@@ -67,23 +114,77 @@ public String name() {
@Override
public void init(Config config) {
+ this.random = new Random();
+
this.walkPerNode = config.getInt(OPTION_WALK_PER_NODE, 3);
if (this.walkPerNode <= 0) {
throw new ComputerException("The param %s must be greater than 0,
" +
- "actual got '%s'",
- OPTION_WALK_PER_NODE, this.walkPerNode);
+ "actual got '%s'",
+ OPTION_WALK_PER_NODE,
this.walkPerNode);
}
- LOG.info("[RandomWalk] algorithm param, {}: {}", OPTION_WALK_PER_NODE,
walkPerNode);
+ LOG.info("[RandomWalk] algorithm param, {}: {}", OPTION_WALK_PER_NODE,
this.walkPerNode);
this.walkLength = config.getInt(OPTION_WALK_LENGTH, 3);
if (this.walkLength <= 0) {
throw new ComputerException("The param %s must be greater than 0,
" +
- "actual got '%s'",
- OPTION_WALK_LENGTH, this.walkLength);
+ "actual got '%s'",
+ OPTION_WALK_LENGTH, this.walkLength);
}
- LOG.info("[RandomWalk] algorithm param, {}: {}", OPTION_WALK_LENGTH,
walkLength);
+ LOG.info("[RandomWalk] algorithm param, {}: {}", OPTION_WALK_LENGTH,
this.walkLength);
Review Comment:
we can add a common method like `logAlgorithmParam(name, value)`, and just
use the `this.name()` as logged algorithm name
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]