javeme commented on code in PR #280:
URL:
https://github.com/apache/incubator-hugegraph-computer/pull/280#discussion_r1391074395
##########
computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java:
##########
@@ -146,29 +237,129 @@ 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<>();
Review Comment:
can we add a mark here: `TODO: use primitive array instead, like
DoubleArray, in order to reduce memory fragmentation generated during
calculations`
the same to
https://github.com/search?q=repo%3Aapache%2Fincubator-hugegraph-computer+path%3A%2F%5Ecomputer-algorithm%5C%2F%2F++new+ArrayList&type=code
##########
computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java:
##########
@@ -282,35 +268,36 @@ private Edge randomSelectEdge(Id preVertexId, IdList
preVertexAdjacenceIdList, E
}
/**
- * get edge weight by weight property
+ * get the weight of an edge by its weight property
*/
- private Value getWeight(Edge edge) {
- Value weight = edge.property(this.weightProperty);
- if (weight == null) {
- weight = new DoubleValue(this.defaultWeight);
+ private double getEdgeWeight(Edge edge) {
+ Value property = edge.property(this.weightProperty);
+ if (property == null) {
+ property = new DoubleValue(this.defaultWeight);
}
- if (!weight.isNumber()) {
+ if (!property.isNumber()) {
throw new ComputerException("The value of %s must be a numeric
value, " +
"actual got '%s'",
- this.weightProperty, weight.string());
+ this.weightProperty,
property.string());
}
// weight threshold truncation
- if ((Double) weight.value() < this.minWeightThreshold) {
+ DoubleValue weight = (DoubleValue) property;
Review Comment:
based on a double value like:
1. double weight = this.defaultWeight;
2. weight = property..doubleValue() if checked ok
3. do truncation
4. return weight
##########
computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java:
##########
@@ -282,35 +268,36 @@ private Edge randomSelectEdge(Id preVertexId, IdList
preVertexAdjacenceIdList, E
}
/**
- * get edge weight by weight property
+ * get the weight of an edge by its weight property
*/
- private Value getWeight(Edge edge) {
- Value weight = edge.property(this.weightProperty);
- if (weight == null) {
- weight = new DoubleValue(this.defaultWeight);
+ private double getEdgeWeight(Edge edge) {
+ Value property = edge.property(this.weightProperty);
+ if (property == null) {
+ property = new DoubleValue(this.defaultWeight);
}
- if (!weight.isNumber()) {
+ if (!property.isNumber()) {
throw new ComputerException("The value of %s must be a numeric
value, " +
"actual got '%s'",
- this.weightProperty, weight.string());
+ this.weightProperty,
property.string());
}
// weight threshold truncation
- if ((Double) weight.value() < this.minWeightThreshold) {
+ DoubleValue weight = (DoubleValue) property;
+ if (weight.doubleValue() < this.minWeightThreshold) {
weight = new DoubleValue(this.minWeightThreshold);
}
- if ((Double) weight.value() > this.maxWeightThreshold) {
+ if (weight.doubleValue() > this.maxWeightThreshold) {
weight = new DoubleValue(this.maxWeightThreshold);
}
- return weight;
+ return weight.doubleValue();
}
/**
* calculate edge weight
*/
- private Double calculateWeight(Id preVertexId, IdList
preVertexAdjacenceIdList,
- Id nextVertexId, Value weight) {
+ private Double calculateEdgeWeight(Id preVertexId, IdList
preVertexAdjacenceIdList,
+ Id nextVertexId, double weight) {
Review Comment:
also keep `double finalWeight` and `return double`?
##########
computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java:
##########
@@ -146,29 +237,129 @@ 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 edge weight
+ double weight = this.getEdgeWeight(edge);
+ Double finalWeight = this.calculateEdgeWeight(preVertexId,
preVertexAdjacenceIdList,
+ edge.targetId(),
weight);
+ weightList.add(finalWeight);
Review Comment:
can we add a mark here: `TODO: improve to avoid OOM`
--
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]