Skip to content
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
merged 17 commits into from
Dec 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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, ",
Expand All @@ -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
Expand Down Expand Up @@ -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);
Copy link
Contributor

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

}

Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on a double value like:

  1. double weight = this.defaultWeight;
  2. weight = property..doubleValue() if checked ok
  3. do truncation
  4. return weight

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also keep double finalWeight and return double?

/*
* 3 types of vertices.
* 1. current vertex, called v
Expand All @@ -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;
}
Expand Down