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 single source shortest path algorithm #285

Merged
merged 24 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
28691fd
feat: ListValue add clear() method
diaohancai Nov 21, 2023
d9c5c84
feat: Source Target Shortest Path
diaohancai Nov 21, 2023
3aff95d
test: test data is complicated
diaohancai Nov 21, 2023
b6c0f4c
test: ListValue.clear unit test
diaohancai Nov 21, 2023
f5a5de9
fix: ring loop
diaohancai Nov 21, 2023
4c37b2d
optimize: message combine
diaohancai Nov 21, 2023
15f95f6
Merge branch 'master' into pr/285
imbajin Dec 4, 2023
6630b82
refactor(algorithm): Single Source Shortest Path
diaohancai Dec 26, 2023
9524900
refactor(algorithm): output json
diaohancai Dec 26, 2023
2804bd7
feat(algorithm): multiple target optimization
diaohancai Dec 26, 2023
cdaba23
feat(core): IdList Merge Combiner
diaohancai Dec 26, 2023
125bba6
chore(algorithm): simple adjustments
diaohancai Dec 31, 2023
9587802
optimization(algorithm): change reachedTargets from IdList to IdSet
diaohancai Jan 1, 2024
01cde50
chore: json style key
diaohancai Jan 22, 2024
6b42bf5
improve: convert id from string to ID with type
diaohancai Jan 25, 2024
55b0ed3
chore: add IdUtilTest unit test
diaohancai Jan 25, 2024
2828bb7
fix: all targets reached
diaohancai Feb 6, 2024
a0a0103
improve: remove unnecessary member var
diaohancai Feb 6, 2024
c4ca8fe
improve: source vertex and target vertex specify idType
diaohancai Feb 6, 2024
174584e
chore: get properties
diaohancai Feb 7, 2024
aa8238d
improve: input vertex id parse
diaohancai Feb 29, 2024
0b1c720
chore: log improvement
diaohancai Feb 29, 2024
6510424
test: apply exception testing
diaohancai Mar 3, 2024
d55c7c7
test: parse empty id throws IllegalArgumentException
diaohancai Mar 3, 2024
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
@@ -0,0 +1,25 @@
/*
* 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.path.shortest;

public enum QuantityType {

SINGLE,
MULTIPLE,
ALL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
/*
* 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.path.shortest;

import java.util.Arrays;
import java.util.Iterator;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
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.id.Id;
import org.apache.hugegraph.computer.core.graph.value.DoubleValue;
import org.apache.hugegraph.computer.core.graph.value.IdSet;
import org.apache.hugegraph.computer.core.graph.value.Value;
import org.apache.hugegraph.computer.core.graph.vertex.Vertex;
import org.apache.hugegraph.computer.core.util.IdUtil;
import org.apache.hugegraph.computer.core.worker.Computation;
import org.apache.hugegraph.computer.core.worker.ComputationContext;
import org.apache.hugegraph.computer.core.worker.WorkerContext;
import org.apache.hugegraph.util.Log;
import org.slf4j.Logger;

public class SingleSourceShortestPath implements Computation<SingleSourceShortestPathValue> {

private static final Logger LOG = Log.logger(SingleSourceShortestPath.class);

public static final String OPTION_SOURCE_ID = "single_source_shortest_path.source_id";
public static final String OPTION_TARGET_ID = "single_source_shortest_path.target_id";
public static final String OPTION_WEIGHT_PROPERTY =
"single_source_shortest_path.weight_property";
public static final String OPTION_DEFAULT_WEIGHT =
"single_source_shortest_path.default_weight";

/**
* source vertex id.
*/
private Id sourceId;

/**
* target vertex id.
* 1. single target: A
* 2. multiple target: A, B, C
* 3. all: *
*/
private IdSet targetIdSet; // empty when targetId is all
/**
* target quantity type
*/
private QuantityType targetQuantityType;

/**
* weight property.
* weight value must be a positive number.
*/
private String weightProperty;

/**
* default weight.
* default 1
*/
private Double defaultWeight;

//****************** global data ******************//
/**
* reached targets
*/
private IdSet reachedTargets; // empty when targetId is all

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

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

@Override
public void init(Config config) {
String sourceIdStr = config.getString(OPTION_SOURCE_ID, "");
if (StringUtils.isBlank(sourceIdStr)) {
throw new ComputerException("The param '%s' must not be blank", OPTION_SOURCE_ID);

Check warning on line 100 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L100

Added line #L100 was not covered by tests
}
this.sourceId = IdUtil.parseId(sourceIdStr);

String targetIdStr = config.getString(OPTION_TARGET_ID, "");
if (StringUtils.isBlank(targetIdStr)) {
throw new ComputerException("The param '%s' must not be blank", OPTION_TARGET_ID);

Check warning on line 106 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L106

Added line #L106 was not covered by tests
}
// remove spaces
targetIdStr = Arrays.stream(targetIdStr.split(","))
.map(e -> e.trim())
.collect(Collectors.joining(","));
this.targetQuantityType = this.getQuantityType(targetIdStr);
if (this.targetQuantityType != QuantityType.ALL) {
this.targetIdSet = new IdSet();
for (String targetId : targetIdStr.split(",")) {
targetIdSet.add(IdUtil.parseId(targetId));
}
}

this.weightProperty = config.getString(OPTION_WEIGHT_PROPERTY, "");

this.defaultWeight = config.getDouble(OPTION_DEFAULT_WEIGHT, 1);
if (this.defaultWeight <= 0) {
throw new ComputerException("The param '%s' must be greater than 0, " +

Check warning on line 124 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L124

Added line #L124 was not covered by tests
"actual got '%s'",
OPTION_DEFAULT_WEIGHT, this.defaultWeight);
}
}

@Override
public void compute0(ComputationContext context, Vertex vertex) {
SingleSourceShortestPathValue value = new SingleSourceShortestPathValue();
value.unreachable();
vertex.value(value);

// start from source vertex
if (!this.sourceId.equals(vertex.id())) {
vertex.inactivate();
return;
}
value.zeroDistance(); // source vertex

// single target && source == target
if (this.targetQuantityType == QuantityType.SINGLE &&
this.targetIdSet.contains(this.sourceId)) {
LOG.debug("source vertex equals target vertex: {}", this.sourceId);
vertex.inactivate();
return;

Check warning on line 148 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L146-L148

Added lines #L146 - L148 were not covered by tests
}

if (vertex.numEdges() <= 0) {
// isolated vertex
LOG.debug("The source vertex is isolated: {}", this.sourceId);
vertex.inactivate();
return;

Check warning on line 155 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L153-L155

Added lines #L153 - L155 were not covered by tests
}

vertex.edges().forEach(edge -> {
SingleSourceShortestPathValue message = new SingleSourceShortestPathValue();
message.addToPath(vertex, this.getEdgeWeight(edge));

context.sendMessage(edge.targetId(), message);
});

vertex.inactivate();
}

@Override
public void compute(ComputationContext context, Vertex vertex,
Iterator<SingleSourceShortestPathValue> messages) {
if (this.isTarget(vertex) && !this.reachedTargets.contains(vertex.id())) {
// reached targets
this.reachedTargets.add(vertex.id());
}

while (messages.hasNext()) {
SingleSourceShortestPathValue message = messages.next();
SingleSourceShortestPathValue value = vertex.value();

if (message.totalWeight() < value.totalWeight()) {
// find a shorter path
value.shorterPath(vertex, message.path(), message.totalWeight());
} else {
continue;
}

// target vertex finds all targets reached or nowhere to go
if ((this.isTarget(vertex) && this.isAllTargetsReached(vertex)) ||
vertex.numEdges() <= 0) {
continue;
}

vertex.edges().forEach(edge -> {
SingleSourceShortestPathValue forwardMessage = new SingleSourceShortestPathValue();
forwardMessage.addToPath(value.path(),
value.totalWeight() + this.getEdgeWeight(edge));

context.sendMessage(edge.targetId(), forwardMessage);
});
}

vertex.inactivate();
}

@Override
public void beforeSuperstep(WorkerContext context) {
this.reachedTargets = context.aggregatedValue(
SingleSourceShortestPathMaster.SINGLE_SOURCE_SHORTEST_PATH_REACHED_TARGETS);
}

@Override
public void afterSuperstep(WorkerContext context) {
context.aggregateValue(
SingleSourceShortestPathMaster.SINGLE_SOURCE_SHORTEST_PATH_REACHED_TARGETS,
this.reachedTargets);
}

/**
* get quantityType by targetId
*/
private QuantityType getQuantityType(String targetIdStr) {
if (targetIdStr.equals("*")) {
return QuantityType.ALL;

Check warning on line 223 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L223

Added line #L223 was not covered by tests
} else if (targetIdStr.contains(",")) {
return QuantityType.MULTIPLE;

Check warning on line 225 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L225

Added line #L225 was not covered by tests
} else {
return QuantityType.SINGLE;
}
}

/**
* get the weight of an edge by its weight property
*/
private double getEdgeWeight(Edge edge) {
double weight = this.defaultWeight;

Value property = edge.property(this.weightProperty);
if (property != null) {
if (!property.isNumber()) {
throw new ComputerException("The value of %s must be a numeric value, " +

Check warning on line 240 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L240

Added line #L240 was not covered by tests
"actual got '%s'",
this.weightProperty, property.string());

Check warning on line 242 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L242

Added line #L242 was not covered by tests
}

weight = ((DoubleValue) property).doubleValue();
if (weight <= 0) {
throw new ComputerException("The value of %s must be greater than 0, " +

Check warning on line 247 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L247

Added line #L247 was not covered by tests
"actual got '%s'",
this.weightProperty, property.string());

Check warning on line 249 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L249

Added line #L249 was not covered by tests
}
}
return weight;
}

/**
* determine whether vertex is one of the target
*/
private boolean isTarget(Vertex vertex) {
return this.targetQuantityType != QuantityType.ALL &&
this.targetIdSet.contains(vertex.id());
}

/**
* determine whether all targets reached
*/
private boolean isAllTargetsReached(Vertex vertex) {
if (this.targetQuantityType == QuantityType.ALL) {
return false;

Check warning on line 268 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L268

Added line #L268 was not covered by tests
}

if (this.targetIdSet.size() == this.reachedTargets.size()) {
for (Id targetId : this.targetIdSet.value()) {
if (!this.reachedTargets.contains(targetId)) {
return false;

Check warning on line 274 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L274

Added line #L274 was not covered by tests
}
}
return true;
}
return false;

Check warning on line 279 in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java

View check run for this annotation

Codecov / codecov/patch

computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/path/shortest/SingleSourceShortestPath.java#L279

Added line #L279 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.path.shortest;

import org.apache.hugegraph.computer.core.combiner.Combiner;

public class SingleSourceShortestPathCombiner implements Combiner<SingleSourceShortestPathValue> {

@Override
public void combine(SingleSourceShortestPathValue v1, SingleSourceShortestPathValue v2,
SingleSourceShortestPathValue result) {
SingleSourceShortestPathValue shorter = v2.totalWeight() < v1.totalWeight() ? v2 : v1;
result.copy(shorter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.path.shortest;

import org.apache.hugegraph.computer.core.combiner.IdSetMergeCombiner;
import org.apache.hugegraph.computer.core.graph.value.ValueType;
import org.apache.hugegraph.computer.core.master.MasterComputation;
import org.apache.hugegraph.computer.core.master.MasterComputationContext;
import org.apache.hugegraph.computer.core.master.MasterContext;

public class SingleSourceShortestPathMaster implements MasterComputation {

public static final String SINGLE_SOURCE_SHORTEST_PATH_REACHED_TARGETS =
"single_source_shortest_path.reached_targets";

@Override
public void init(MasterContext context) {
context.registerAggregator(SINGLE_SOURCE_SHORTEST_PATH_REACHED_TARGETS,
ValueType.ID_SET,
IdSetMergeCombiner.class);
}

@Override
public void close(MasterContext context) {
// pass
}

@Override
public boolean compute(MasterComputationContext context) {
return true;
}
}
Loading
Loading