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: Control car departure access #154

Merged
merged 6 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -3,24 +3,28 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import org.eqasim.core.misc.ParallelProgress;
import org.eqasim.core.scenario.cutter.network.RoadNetwork;
import org.matsim.api.core.v01.network.Link;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.utils.collections.QuadTree;
import org.matsim.core.utils.collections.QuadTrees;
import org.matsim.facilities.ActivityFacilities;
import org.matsim.facilities.ActivityFacility;
import org.matsim.facilities.ActivityFacilityImpl;

public class FacilityPlacement {
private final int numberOfThreads;
private final int batchSize;
private final RoadNetwork network;
private final QuadTree<Link> spatialIndex;

public FacilityPlacement(int numberOfThreads, int batchSize, RoadNetwork network) {
this.network = network;
public FacilityPlacement(int numberOfThreads, int batchSize, RoadNetwork network, FacilityPlacementVoter voter) {
this.numberOfThreads = numberOfThreads;
this.batchSize = batchSize;

this.spatialIndex = QuadTrees.createQuadTree(
network.getLinks().values().stream().filter(voter::canPlaceFacility).collect(Collectors.toList()));
}

public void run(ActivityFacilities facilities) throws InterruptedException {
Expand Down Expand Up @@ -69,12 +73,56 @@ public void run() {
}

for (ActivityFacility facility : localTasks) {
Link link = NetworkUtils.getNearestLink(network, facility.getCoord());
Link link = spatialIndex.getClosest(facility.getCoord().getX(), facility.getCoord().getY());
((ActivityFacilityImpl) facility).setLinkId(link.getId());
}

progress.update(localTasks.size());
} while (localTasks.size() > 0);
}
}

static public interface FacilityPlacementVoter {
boolean canPlaceFacility(Link link);
}

static public class OSMFacilityPlacementVoter implements FacilityPlacementVoter {
private final static String HIGHWAY_TAG = "osm:way:highway";

public OSMFacilityPlacementVoter(RoadNetwork network) {
boolean foundAttribute = false;

for (Link link : network.getLinks().values()) {
if (link.getAttributes().getAttribute(HIGHWAY_TAG) != null) {
foundAttribute = true;
break;
}
}

if (!foundAttribute) {
throw new IllegalStateException("Did not find osm:highway attribute in network");
}
}

@Override
public boolean canPlaceFacility(Link link) {
String highway = (String) link.getAttributes().getAttribute(HIGHWAY_TAG);

if (highway != null) {
if (highway.contains("motorway")) {
return false;
}

if (highway.contains("trunk")) {
return false;
}

if (highway.contains("_link")) {
return false;
}
}

return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.eqasim.core.scenario.preparation;

import org.eqasim.core.scenario.cutter.network.RoadNetwork;
import org.eqasim.core.scenario.preparation.FacilityPlacement.OSMFacilityPlacementVoter;
import org.matsim.api.core.v01.Scenario;
import org.matsim.core.config.CommandLine;
import org.matsim.core.config.CommandLine.ConfigurationException;
Expand Down Expand Up @@ -33,7 +34,8 @@ static public void main(String[] args) throws ConfigurationException, Interrupte
int batchSize = cmd.getOption("batch-size").map(Integer::parseInt).orElse(10);

RoadNetwork roadNetwork = new RoadNetwork(scenario.getNetwork());
FacilityPlacement facilityPlacement = new FacilityPlacement(numberOfThreads, batchSize, roadNetwork);
OSMFacilityPlacementVoter voter = new OSMFacilityPlacementVoter(roadNetwork);
FacilityPlacement facilityPlacement = new FacilityPlacement(numberOfThreads, batchSize, roadNetwork, voter);
facilityPlacement.run(scenario.getActivityFacilities());

// Fix freight activities (TODO: should go to the pipeline)
Expand Down
Loading