Skip to content
Permalink
Browse files
standardized algorithm package and name (#96)
  • Loading branch information
javeme committed Aug 30, 2021
1 parent 4f1a599 commit 2211db8e3b989aec490dc49c2f348923fa39052a
Showing 11 changed files with 46 additions and 50 deletions.
@@ -19,8 +19,6 @@

package com.baidu.hugegraph.computer.algorithm.centrality.degree;

import static jdk.nashorn.internal.objects.Global.Infinity;

import java.util.Iterator;

import org.apache.commons.lang.StringUtils;
@@ -39,14 +37,15 @@

public class DegreeCentrality implements Computation<NullValue> {

public static final String CONF_DEGREE_CENTRALITY_WEIGHT_PROPERTY =
"degree.centrality.weight.property";
public static final String OPTION_WEIGHT_PROPERTY =
"degree_centrality.weight_property";

private boolean calculateByWeightProperty;
private String weightProperty;

@Override
public String name() {
return "degreeCentrality";
return "degree_centrality";
}

@Override
@@ -66,26 +65,23 @@ public void compute0(ComputationContext context, Vertex vertex) {
* int the future;
*/
double totalWeight = 0.0;
double weight = 0.0;
Iterator<Edge> edges = vertex.edges().iterator();
while (edges.hasNext()) {
edge = edges.next();
weight = getWeightValue(
(Value) edge.properties()
.get(this.weightProperty));
if ((totalWeight + weight) == Infinity) {
double weight = weightValue(edge.property(this.weightProperty));
totalWeight += weight;
if (totalWeight == Double.POSITIVE_INFINITY) {
throw new ComputerException("Calculate weight overflow," +
"current is %s, edge '%s' is ",
totalWeight, edge, weight);
}
totalWeight += weight;
}
vertex.value(new DoubleValue(totalWeight));
}
vertex.inactivate();
}

private double getWeightValue(Value value) {
private static double weightValue(Value<?> value) {
if (value == null) {
return 1.0;
}
@@ -113,7 +109,7 @@ public void compute(ComputationContext context, Vertex vertex,
@Override
public void init(Config config) {
this.weightProperty = config.getString(
CONF_DEGREE_CENTRALITY_WEIGHT_PROPERTY, "");
OPTION_WEIGHT_PROPERTY, "");
this.calculateByWeightProperty = StringUtils.isNotEmpty(
this.weightProperty);
}
@@ -17,47 +17,48 @@
* under the License.
*/

package com.baidu.hugegraph.computer.algorithm.rank.pagerank;
package com.baidu.hugegraph.computer.algorithm.centrality.pagerank;

import java.util.Iterator;

import com.baidu.hugegraph.computer.core.aggregator.Aggregator;
import com.baidu.hugegraph.computer.core.combiner.Combiner;
import com.baidu.hugegraph.computer.core.config.Config;
import com.baidu.hugegraph.computer.core.graph.value.DoubleValue;
import com.baidu.hugegraph.computer.core.graph.value.LongValue;
import com.baidu.hugegraph.computer.core.graph.vertex.Vertex;
import com.baidu.hugegraph.computer.core.worker.Computation;
import com.baidu.hugegraph.computer.core.worker.ComputationContext;
import com.baidu.hugegraph.computer.core.worker.WorkerContext;

public class PageRank implements Computation<DoubleValue> {

public static final String CONF_ALPHA_KEY = "pagerank.alpha";
public static final String OPTION_ALPHA = "page_rank.alpha";

public static final double CONF_ALPHA_DEFAULT = 0.15D;
public static final double ALPHA_DEFAULT_VALUE = 0.15;

private double alpha;
private double rankFromDangling;
private double danglingRank;
private double initialRankInSuperstep;
private double cumulativeValue;
private double cumulativeRank;

private Aggregator l1DiffAggr;
private Aggregator cumulativeRankAggr;
private Aggregator danglingVertexNumAggr;
private Aggregator danglingCumulativeAggr;
private Aggregator<DoubleValue> l1DiffAggr;
private Aggregator<DoubleValue> cumulativeRankAggr;
private Aggregator<LongValue> danglingVertexNumAggr;
private Aggregator<DoubleValue> danglingCumulativeAggr;

// Initial value in superstep 0.
private DoubleValue initialValue;
private DoubleValue contribValue;

@Override
public String name() {
return "pageRank";
return "page_rank";
}

@Override
public String category() {
return "rank";
return "centrality";
}

@Override
@@ -79,13 +80,13 @@ public void compute0(ComputationContext context, Vertex vertex) {
public void compute(ComputationContext context, Vertex vertex,
Iterator<DoubleValue> messages) {
DoubleValue message = Combiner.combineAll(context.combiner(), messages);
double rankFromNeighbors = 0.0D;
double rankFromNeighbors = 0.0;
if (message != null) {
rankFromNeighbors = message.value();
}
double rank = (this.rankFromDangling + rankFromNeighbors) *
(1.0D - this.alpha) + this.initialRankInSuperstep;
rank /= this.cumulativeValue;
double rank = (this.danglingRank + rankFromNeighbors) *
(1.0 - this.alpha) + this.initialRankInSuperstep;
rank /= this.cumulativeRank;
DoubleValue oldRank = vertex.value();
vertex.value(new DoubleValue(rank));
this.l1DiffAggr.aggregateValue(Math.abs(oldRank.value() - rank));
@@ -102,7 +103,7 @@ public void compute(ComputationContext context, Vertex vertex,

@Override
public void init(Config config) {
this.alpha = config.getDouble(CONF_ALPHA_KEY, CONF_ALPHA_DEFAULT);
this.alpha = config.getDouble(OPTION_ALPHA, ALPHA_DEFAULT_VALUE);
this.contribValue = new DoubleValue();
}

@@ -114,16 +115,16 @@ public void close(Config config) {
@Override
public void beforeSuperstep(WorkerContext context) {
// Get aggregator values for computation
DoubleValue danglingContribution = context.aggregatedValue(
DoubleValue danglingTotalRank = context.aggregatedValue(
PageRank4Master.AGGR_COMULATIVE_DANGLING_PROBABILITY);

this.rankFromDangling = danglingContribution.value() /
context.totalVertexCount();
this.initialRankInSuperstep = this.alpha / context.totalVertexCount();
DoubleValue cumulativeProbability = context.aggregatedValue(
DoubleValue cumulativeRank = context.aggregatedValue(
PageRank4Master.AGGR_COMULATIVE_PROBABILITY);
this.cumulativeValue = cumulativeProbability.value();
this.initialValue = new DoubleValue(1.0D / context.totalVertexCount());
long totalVertex = context.totalVertexCount();

this.danglingRank = danglingTotalRank.value() / totalVertex;
this.initialRankInSuperstep = this.alpha / totalVertex;
this.cumulativeRank = cumulativeRank.value();
this.initialValue = new DoubleValue(1.0 / totalVertex);

// Create aggregators
this.l1DiffAggr = context.createAggregator(
@@ -17,7 +17,7 @@
* under the License.
*/

package com.baidu.hugegraph.computer.algorithm.rank.pagerank;
package com.baidu.hugegraph.computer.algorithm.centrality.pagerank;

import org.slf4j.Logger;

@@ -76,9 +76,8 @@ public void close(MasterContext context) {

@Override
public boolean compute(MasterComputationContext context) {

LongValue danglingVerticesNum = context.aggregatedValue(
AGGR_DANGLING_VERTICES_NUM);
AGGR_DANGLING_VERTICES_NUM);
DoubleValue danglingProbability = context.aggregatedValue(
AGGR_COMULATIVE_DANGLING_PROBABILITY);
DoubleValue cumulativeProbability = context.aggregatedValue(
@@ -17,7 +17,7 @@
* under the License.
*/

package com.baidu.hugegraph.computer.algorithm.rank.pagerank;
package com.baidu.hugegraph.computer.algorithm.centrality.pagerank;

import java.util.Map;

@@ -17,7 +17,7 @@
* under the License.
*/

package com.baidu.hugegraph.computer.algorithm.wcc;
package com.baidu.hugegraph.computer.algorithm.community.wcc;

import java.util.Iterator;

@@ -17,7 +17,7 @@
* under the License.
*/

package com.baidu.hugegraph.computer.algorithm.wcc;
package com.baidu.hugegraph.computer.algorithm.community.wcc;

import java.util.Map;

@@ -24,8 +24,8 @@
import org.junit.runners.Suite;

import com.baidu.hugegraph.computer.algorithm.centrality.degree.DegreeCentralityTest;
import com.baidu.hugegraph.computer.algorithm.rank.pagerank.PageRankTest;
import com.baidu.hugegraph.computer.algorithm.wcc.WccTest;
import com.baidu.hugegraph.computer.algorithm.centrality.pagerank.PageRankTest;
import com.baidu.hugegraph.computer.algorithm.community.wcc.WccTest;
import com.baidu.hugegraph.config.OptionSpace;

@RunWith(Suite.class)
@@ -40,7 +40,7 @@ public class DegreeCentralityTest extends AlgorithmTestBase {
@Test
public void testServiceWith1Worker() throws InterruptedException {
runAlgorithm(DegreeCentralityTestParams.class.getName(),
DegreeCentrality.CONF_DEGREE_CENTRALITY_WEIGHT_PROPERTY,
DegreeCentrality.OPTION_WEIGHT_PROPERTY,
"rate");
DegreeCentralityTestOutput.assertResult();
runAlgorithm(DegreeCentralityTestParams.class.getName());
@@ -72,7 +72,7 @@ public DegreeCentralityTestOutput() {
public void init(Config config, int partition) {
super.init(config, partition);
this.weight = config.getString(
DegreeCentrality.CONF_DEGREE_CENTRALITY_WEIGHT_PROPERTY, "");
DegreeCentrality.OPTION_WEIGHT_PROPERTY, "");
isRun = false;
}

@@ -17,7 +17,7 @@
* under the License.
*/

package com.baidu.hugegraph.computer.algorithm.rank.pagerank;
package com.baidu.hugegraph.computer.algorithm.centrality.pagerank;

import org.junit.Test;

@@ -17,7 +17,7 @@
* under the License.
*/

package com.baidu.hugegraph.computer.algorithm.wcc;
package com.baidu.hugegraph.computer.algorithm.community.wcc;

import org.junit.Test;

@@ -28,7 +28,7 @@
import org.junit.BeforeClass;
import org.junit.Test;

import com.baidu.hugegraph.computer.algorithm.rank.pagerank.PageRankParams;
import com.baidu.hugegraph.computer.algorithm.centrality.pagerank.PageRankParams;
import com.baidu.hugegraph.computer.core.common.ComputerContext;
import com.baidu.hugegraph.computer.core.common.exception.TransportException;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;

0 comments on commit 2211db8

Please sign in to comment.