Commit c216e3f5 authored by Sebastian Hörl's avatar Sebastian Hörl
Browse files

Speed up link assignment

parent ac002c78
......@@ -8,9 +8,11 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import org.apache.log4j.Logger;
......@@ -161,7 +163,7 @@ public class AVLinkAssignment {
return false;
}
public Collection<Id<Link>> getLinkIds() {
public Collection<Id<Link>> getLinkIds() throws InterruptedException {
TravelTime travelTime = new FreeSpeedTravelTime();
TravelDisutility travelDisutility = new TravelDisutility() {
......@@ -250,30 +252,64 @@ public class AVLinkAssignment {
}
}
long newLinkCount = 0;
long numberOfIslands = islands.size();
long numberOfProcessedIslands = 0;
AtomicLong numberOfProcessedIslands = new AtomicLong(0);
AtomicLong newLinkCount = new AtomicLong(0);
for (Link link : islands) {
Path path1 = router.calcLeastCostPath(link.getToNode(), referenceLink.getFromNode(), 0.0, null, null);
Path path2 = router.calcLeastCostPath(referenceLink.getToNode(), link.getFromNode(), 0.0, null, null);
Iterator<Link> iterator = islands.iterator();
List<Thread> threads = new LinkedList<>();
for (Link pathLink : path1.links) {
result.add(pathLink);
newLinkCount++;
}
final Link finalReferenceLink = referenceLink;
for (Link pathLink : path2.links) {
result.add(pathLink);
newLinkCount++;
}
int batchSize = 40;
for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
Thread thread = new Thread(() -> {
LeastCostPathCalculator localRouter = new DijkstraFactory().createPathCalculator(this.network,
travelDisutility, travelTime);
Set<Link> localResult = new HashSet<>();
while (true) {
List<Link> localLinks = new LinkedList<>();
synchronized (iterator) {
while (iterator.hasNext() && localLinks.size() < batchSize) {
localLinks.add(iterator.next());
}
}
for (Link link : localLinks) {
Path path1 = localRouter.calcLeastCostPath(link.getToNode(), finalReferenceLink.getFromNode(),
0.0, null, null);
Path path2 = localRouter.calcLeastCostPath(finalReferenceLink.getToNode(), link.getFromNode(),
0.0, null, null);
localResult.addAll(path1.links);
localResult.addAll(path2.links);
}
long current = numberOfProcessedIslands.addAndGet(localLinks.size());
logger.info(String.format("Processed island %d/%d ...", current, numberOfIslands));
if (localLinks.size() == 0) {
break;
}
}
synchronized (result) {
result.addAll(localResult);
}
});
thread.start();
threads.add(thread);
}
numberOfProcessedIslands++;
logger.info(String.format("Processing island %d/%d ...", numberOfProcessedIslands, numberOfIslands));
for (Thread thread : threads) {
thread.join();
}
logger.info(String.format("Routing done! Added %d new links.", newLinkCount));
logger.info(String.format("Routing done! Added %d new links.", result.size()));
// Add tags
logger.info(String.format("Creating cleaned network ..."));
......@@ -307,7 +343,7 @@ public class AVLinkAssignment {
return remainingIds;
}
public void apply(Network network) {
public void apply(Network network) throws InterruptedException {
if (linkIdsCache == null) {
linkIdsCache = getLinkIds();
}
......@@ -320,7 +356,7 @@ public class AVLinkAssignment {
logger.info(String.format("Setting attribute done!"));
}
static public void main(String[] args) throws MalformedURLException, ConfigurationException {
static public void main(String[] args) throws MalformedURLException, ConfigurationException, InterruptedException {
CommandLine cmd = new CommandLine.Builder(args) //
.requireOptions("input-path", "output-path", "cities-shapefile", "year") //
.build();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment