-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(algorithm): support random walk in computer #274
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
af1a436
feat(algorithm): random walk
d59676e
chore: add licensed description
d6a23d2
fix: isolated vertex result type
9b5d8b2
fix: complete random walk unit test
435ac2b
chore: RandomWalkTest add to test suite
5019b9d
chore: modify comment
6e9c660
fix: walkPerNode、walkLength param can be 1
6abb578
refact: method rename
f330734
refact: method rename
4c83451
chore: blank line
f52acbd
chore: change Boolean to boolean
565d6e7
chore: wrap
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
189 changes: 189 additions & 0 deletions
189
...-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalk.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with this | ||
* work for additional information regarding copyright ownership. The ASF | ||
* licenses this file to You under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.hugegraph.computer.algorithm.sampling; | ||
|
||
import org.apache.hugegraph.computer.core.common.exception.ComputerException; | ||
import org.apache.hugegraph.computer.core.config.Config; | ||
import org.apache.hugegraph.computer.core.graph.edge.Edge; | ||
import org.apache.hugegraph.computer.core.graph.edge.Edges; | ||
import org.apache.hugegraph.computer.core.graph.id.Id; | ||
import org.apache.hugegraph.computer.core.graph.value.IdList; | ||
import org.apache.hugegraph.computer.core.graph.value.IdListList; | ||
import org.apache.hugegraph.computer.core.graph.vertex.Vertex; | ||
import org.apache.hugegraph.computer.core.worker.Computation; | ||
import org.apache.hugegraph.computer.core.worker.ComputationContext; | ||
import org.apache.hugegraph.util.Log; | ||
import org.slf4j.Logger; | ||
|
||
import java.util.Iterator; | ||
import java.util.Random; | ||
|
||
public class RandomWalk implements Computation<RandomWalkMessage> { | ||
|
||
private static final Logger LOG = Log.logger(RandomWalk.class); | ||
|
||
public static final String OPTION_WALK_PER_NODE = "randomwalk.walk_per_node"; | ||
public static final String OPTION_WALK_LENGTH = "randomwalk.walk_length"; | ||
|
||
/** | ||
* number of times per vertex(source vertex) walks | ||
*/ | ||
private Integer walkPerNode; | ||
|
||
/** | ||
* walk length | ||
*/ | ||
private Integer walkLength; | ||
|
||
/** | ||
* random | ||
*/ | ||
private Random random; | ||
|
||
@Override | ||
public String category() { | ||
return "sampling"; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return "random_walk"; | ||
} | ||
|
||
@Override | ||
public void init(Config config) { | ||
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); | ||
} | ||
LOG.info("[RandomWalk] algorithm param, {}: {}", OPTION_WALK_PER_NODE, 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); | ||
} | ||
LOG.info("[RandomWalk] algorithm param, {}: {}", OPTION_WALK_LENGTH, walkLength); | ||
|
||
this.random = new Random(); | ||
} | ||
|
||
@Override | ||
public void compute0(ComputationContext context, Vertex vertex) { | ||
vertex.value(new IdListList()); | ||
|
||
RandomWalkMessage message = new RandomWalkMessage(); | ||
message.addToPath(vertex); | ||
|
||
if (vertex.numEdges() <= 0) { | ||
// isolated vertex | ||
this.savePath(vertex, message.path()); // save result | ||
vertex.inactivate(); | ||
return; | ||
} | ||
|
||
for (int i = 0; i < walkPerNode; ++i) { | ||
// random select one edge and walk | ||
Edge selectedEdge = this.randomSelectEdge(vertex.edges()); | ||
context.sendMessage(selectedEdge.targetId(), message); | ||
} | ||
} | ||
|
||
@Override | ||
public void compute(ComputationContext context, Vertex vertex, Iterator<RandomWalkMessage> messages) { | ||
while (messages.hasNext()) { | ||
RandomWalkMessage message = messages.next(); | ||
|
||
if (message.isFinish()) { | ||
this.savePath(vertex, message.path()); // save result | ||
|
||
vertex.inactivate(); | ||
continue; | ||
} | ||
|
||
message.addToPath(vertex); | ||
|
||
if (vertex.numEdges() <= 0) { | ||
// there is nowhere to walk,finish eariler | ||
message.finish(); | ||
context.sendMessage(this.getSourceId(message.path()), message); | ||
|
||
vertex.inactivate(); | ||
continue; | ||
} | ||
|
||
if (message.path().size() >= this.walkLength + 1) { | ||
message.finish(); | ||
Id sourceId = this.getSourceId(message.path()); | ||
|
||
if (vertex.id().equals(sourceId)) { | ||
// current vertex is the source vertex,no need to send message once more | ||
this.savePath(vertex, message.path()); // save result | ||
} else { | ||
context.sendMessage(sourceId, message); | ||
} | ||
|
||
vertex.inactivate(); | ||
continue; | ||
} | ||
|
||
// random select one edge and walk | ||
Edge selectedEdge = this.randomSelectEdge(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()); | ||
|
||
int i = 0; | ||
Iterator<Edge> iterator = edges.iterator(); | ||
while (iterator.hasNext()) { | ||
selectedEdge = iterator.next(); | ||
if (i == randomNum) { | ||
break; | ||
} | ||
i++; | ||
} | ||
|
||
return selectedEdge; | ||
} | ||
|
||
/** | ||
* get source id of path | ||
*/ | ||
private Id getSourceId(IdList path) { | ||
// the first id of path is the source id | ||
return path.get(0); | ||
} | ||
|
||
/** | ||
* save path | ||
*/ | ||
private void savePath(Vertex sourceVertex, IdList path) { | ||
IdListList curValue = sourceVertex.value(); | ||
curValue.add(path.copy()); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...thm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with this | ||
* work for additional information regarding copyright ownership. The ASF | ||
* licenses this file to You under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.hugegraph.computer.algorithm.sampling; | ||
|
||
import org.apache.hugegraph.computer.core.graph.value.BooleanValue; | ||
import org.apache.hugegraph.computer.core.graph.value.IdList; | ||
import org.apache.hugegraph.computer.core.graph.value.Value; | ||
import org.apache.hugegraph.computer.core.graph.vertex.Vertex; | ||
import org.apache.hugegraph.computer.core.io.RandomAccessInput; | ||
import org.apache.hugegraph.computer.core.io.RandomAccessOutput; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class RandomWalkMessage implements Value.CustomizeValue<List<Object>> { | ||
|
||
/** | ||
* random walk path | ||
*/ | ||
private final IdList path; | ||
|
||
/** | ||
* finish flag | ||
*/ | ||
private BooleanValue isFinish; | ||
|
||
public RandomWalkMessage() { | ||
this.path = new IdList(); | ||
this.isFinish = new BooleanValue(false); | ||
} | ||
|
||
@Override | ||
public void read(RandomAccessInput in) throws IOException { | ||
this.path.read(in); | ||
this.isFinish.read(in); | ||
} | ||
|
||
@Override | ||
public void write(RandomAccessOutput out) throws IOException { | ||
this.path.write(out); | ||
this.isFinish.write(out); | ||
} | ||
|
||
@Override | ||
public List<Object> value() { | ||
return this.path.value(); | ||
} | ||
|
||
public IdList path() { | ||
return this.path; | ||
} | ||
|
||
public void addToPath(Vertex vertex) { | ||
this.path.add(vertex.id()); | ||
} | ||
|
||
public Boolean isFinish() { | ||
return isFinish.value(); | ||
} | ||
|
||
public void finish() { | ||
this.isFinish = new BooleanValue(true); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...ithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with this | ||
* work for additional information regarding copyright ownership. The ASF | ||
* licenses this file to You under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.hugegraph.computer.algorithm.sampling; | ||
|
||
import org.apache.hugegraph.computer.core.graph.value.IdListList; | ||
import org.apache.hugegraph.computer.core.graph.vertex.Vertex; | ||
import org.apache.hugegraph.computer.core.output.hg.HugeGraphOutput; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RandomWalkOutput extends HugeGraphOutput<List<String>> { | ||
|
||
@Override | ||
protected void prepareSchema() { | ||
this.client().schema().propertyKey(this.name()) | ||
.asText() | ||
.writeType(this.writeType()) | ||
.valueList() | ||
.ifNotExist() | ||
.create(); | ||
} | ||
|
||
@Override | ||
protected List<String> value(Vertex vertex) { | ||
IdListList value = vertex.value(); | ||
List<String> propValues = new ArrayList<>(); | ||
for (int i = 0; i < value.size(); i++) { | ||
propValues.add(value.get(i).toString()); | ||
} | ||
return propValues; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...ithm/src/main/java/org/apache/hugegraph/computer/algorithm/sampling/RandomWalkParams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with this | ||
* work for additional information regarding copyright ownership. The ASF | ||
* licenses this file to You under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.hugegraph.computer.algorithm.sampling; | ||
|
||
import org.apache.hugegraph.computer.algorithm.AlgorithmParams; | ||
import org.apache.hugegraph.computer.core.config.ComputerOptions; | ||
import org.apache.hugegraph.computer.core.graph.value.IdListList; | ||
|
||
import java.util.Map; | ||
|
||
public class RandomWalkParams implements AlgorithmParams { | ||
|
||
@Override | ||
public void setAlgorithmParameters(Map<String, String> params) { | ||
this.setIfAbsent(params, ComputerOptions.WORKER_COMPUTATION_CLASS, | ||
RandomWalk.class.getName()); | ||
this.setIfAbsent(params, ComputerOptions.ALGORITHM_MESSAGE_CLASS, | ||
RandomWalkMessage.class.getName()); | ||
this.setIfAbsent(params, ComputerOptions.ALGORITHM_RESULT_CLASS, | ||
IdListList.class.getName()); | ||
this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS, | ||
RandomWalkOutput.class.getName()); | ||
|
||
this.setIfAbsent(params, RandomWalk.OPTION_WALK_PER_NODE, | ||
"3"); | ||
this.setIfAbsent(params, RandomWalk.OPTION_WALK_LENGTH, | ||
"3"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep in one line? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok~ |
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefer to call
boolValue()
instead ofvalue()
here and let isFinish() return boolean type.and please also keep
this.isFinish
style.