diaohancai commented on code in PR #280:
URL:
https://github.com/apache/incubator-hugegraph-computer/pull/280#discussion_r1390673755
##########
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:
Now, I feel that the logs of algorithm param here should be placed in the
framework. I'll remove thoes logs.
--
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]