-
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 biased second order random walk #280
Merged
imbajin
merged 17 commits into
apache:master
from
diaohancai:feat_second_order_random_walk
Dec 4, 2023
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9b7ec3e
chore: option rename
diaohancai 7d760ee
feat: define second order random walk algorithm param
diaohancai 5d8eb98
chore: code format
diaohancai 3e4da8d
feat: add defaultWeight param
diaohancai a2b6efc
feat: implement second order random walk algorithm
diaohancai 0c14381
feat: ListValue support getFirst
diaohancai 5914bff
bug: null point
77d3752
chore: supplementary test parameters
70d8f0f
optimize: change weightThreshold parameter to Double type
548148e
optimize: weight threshold truncation
b4b94b1
optimize: method optimize
diaohancai 7d6e8ca
chore: nullableKeys test
diaohancai 18f1260
refactor: double value simplification
diaohancai 9a93341
chore: add todo note
diaohancai 37d2aa7
fix: can not load edge properties
diaohancai 8cc1734
chore: remove TODO
diaohancai 94bd35b
Merge branch 'master' into pr/280
imbajin 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
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 |
---|---|---|
|
@@ -122,46 +122,36 @@ public void init(Config config) { | |
"actual got '%s'", | ||
OPTION_WALK_PER_NODE, this.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); | ||
} | ||
LOG.info("[RandomWalk] algorithm param, {}: {}", OPTION_WALK_LENGTH, this.walkLength); | ||
|
||
this.weightProperty = config.getString(OPTION_WEIGHT_PROPERTY, ""); | ||
LOG.info("[RandomWalk] algorithm param, {}: {}", | ||
OPTION_WEIGHT_PROPERTY, this.weightProperty); | ||
|
||
this.defaultWeight = config.getDouble(OPTION_DEFAULT_WEIGHT, 1); | ||
if (this.defaultWeight <= 0) { | ||
throw new ComputerException("The param %s must be greater than 0, " + | ||
"actual got '%s'", | ||
OPTION_DEFAULT_WEIGHT, this.defaultWeight); | ||
} | ||
LOG.info("[RandomWalk] algorithm param, {}: {}", | ||
OPTION_DEFAULT_WEIGHT, this.defaultWeight); | ||
|
||
this.minWeightThreshold = config.getDouble(OPTION_MIN_WEIGHT_THRESHOLD, 0.0); | ||
if (this.minWeightThreshold < 0) { | ||
throw new ComputerException("The param %s must be greater than or equal 0, " + | ||
"actual got '%s'", | ||
OPTION_MIN_WEIGHT_THRESHOLD, this.minWeightThreshold); | ||
} | ||
LOG.info("[RandomWalk] algorithm param, {}: {}", | ||
OPTION_MIN_WEIGHT_THRESHOLD, this.minWeightThreshold); | ||
|
||
this.maxWeightThreshold = config.getDouble(OPTION_MAX_WEIGHT_THRESHOLD, Double.MAX_VALUE); | ||
if (this.maxWeightThreshold < 0) { | ||
throw new ComputerException("The param %s must be greater than or equal 0, " + | ||
"actual got '%s'", | ||
OPTION_MAX_WEIGHT_THRESHOLD, this.maxWeightThreshold); | ||
} | ||
LOG.info("[RandomWalk] algorithm param, {}: {}", | ||
OPTION_MAX_WEIGHT_THRESHOLD, this.maxWeightThreshold); | ||
|
||
if (this.minWeightThreshold > this.maxWeightThreshold) { | ||
throw new ComputerException("%s must be greater than or equal %s, ", | ||
|
@@ -174,17 +164,13 @@ public void init(Config config) { | |
"actual got '%s'", | ||
OPTION_RETURN_FACTOR, this.returnFactor); | ||
} | ||
LOG.info("[RandomWalk] algorithm param, {}: {}", | ||
OPTION_RETURN_FACTOR, this.returnFactor); | ||
|
||
this.inOutFactor = config.getDouble(OPTION_INOUT_FACTOR, 1); | ||
if (this.inOutFactor <= 0) { | ||
throw new ComputerException("The param %s must be greater than 0, " + | ||
"actual got '%s'", | ||
OPTION_INOUT_FACTOR, this.inOutFactor); | ||
} | ||
LOG.info("[RandomWalk] algorithm param, {}: {}", | ||
OPTION_INOUT_FACTOR, this.inOutFactor); | ||
} | ||
|
||
@Override | ||
|
@@ -269,10 +255,10 @@ private Edge randomSelectEdge(Id preVertexId, IdList preVertexAdjacenceIdList, E | |
Iterator<Edge> iterator = edges.iterator(); | ||
while (iterator.hasNext()) { | ||
Edge edge = iterator.next(); | ||
// calculate weight | ||
Value weight = this.getWeight(edge); | ||
Double finalWeight = this.calculateWeight(preVertexId, preVertexAdjacenceIdList, | ||
edge.targetId(), weight); | ||
// calculate edge weight | ||
double weight = this.getEdgeWeight(edge); | ||
Double finalWeight = this.calculateEdgeWeight(preVertexId, preVertexAdjacenceIdList, | ||
edge.targetId(), weight); | ||
weightList.add(finalWeight); | ||
} | ||
|
||
|
@@ -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; | ||
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. based on a double value like:
|
||
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) { | ||
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. also keep |
||
/* | ||
* 3 types of vertices. | ||
* 1. current vertex, called v | ||
|
@@ -327,14 +314,14 @@ private Double calculateWeight(Id preVertexId, IdList preVertexAdjacenceIdList, | |
Double finalWeight = 0.0; | ||
if (preVertexId != null && preVertexId.equals(nextVertexId)) { | ||
// distance(t, x) = 0 | ||
finalWeight = 1.0 / this.returnFactor * (Double) weight.value(); | ||
finalWeight = 1.0 / this.returnFactor * weight; | ||
} else if (preVertexAdjacenceIdList != null && | ||
preVertexAdjacenceIdList.contains(nextVertexId)) { | ||
// distance(t, x) = 1 | ||
finalWeight = 1.0 * (Double) weight.value(); | ||
finalWeight = 1.0 * weight; | ||
} else { | ||
// distance(t, x) = 2 | ||
finalWeight = 1.0 / this.inOutFactor * (Double) weight.value(); | ||
finalWeight = 1.0 / this.inOutFactor * weight; | ||
} | ||
return finalWeight; | ||
} | ||
|
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.
can we add a mark here:
TODO: improve to avoid OOM