Skip to content

Commit

Permalink
Add run script to test new DRT zonal system
Browse files Browse the repository at this point in the history
  • Loading branch information
luchengqi7 committed Jan 26, 2024
1 parent 9d254ca commit aa149bf
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public static void main(String[] args) {
heuristicRebalancingZoneGenerator.compute();
}

public void compute() {
public DrtZonalSystem compute() {
analyzeNetwork();
selectInitialCentroids();
generateZones();
writeOutputNetworkWithZones();
//exportDrtZonalSystems();
return exportDrtZonalSystems();
}

public static class ZoneGeneratorBuilder {
Expand Down Expand Up @@ -291,11 +291,10 @@ private Map<Id<Node>, Set<Id<Link>>> createReachableLInksMapCopy() {
}

private void removeRedundantCentroid() {
log.info("Begin removing redundant centroids");
// Find all redundant centroids
log.info("Checking for redundant centroids");
Set<Id<Node>> redundantCentroids = identifyRedundantCentroids();

log.info("Initial number of redundant centroids (i.e., zones) identified = " + redundantCentroids.size());
log.info("Number of redundant centroids identified = " + redundantCentroids.size());

// Remove the redundant centroid that covers the minimum number of links
while (!redundantCentroids.isEmpty()) {
Expand All @@ -314,7 +313,7 @@ private void removeRedundantCentroid() {
redundantCentroids = identifyRedundantCentroids();
log.info("Removing in progress: " + redundantCentroids.size() + " redundant centroids (i.e., zones) left");
}
log.info("Removal of redundant centroids complete. There are " + zonalSystemData.size() + " centroids (i.e., zones) remaining");
log.info("After removal, there are " + zonalSystemData.size() + " centroids (i.e., zones) remaining");
}

protected Set<Id<Node>> identifyRedundantCentroids() {
Expand Down Expand Up @@ -410,7 +409,7 @@ private void assignLinksToNearestZone() {
zonalSystemData.get(closestCentralNodeId).add(linkBeingAssigned);
}
}

private void writeOutputNetworkWithZones() {
// Identify the neighbours for each zone, such that we can color the neighboring zones in different colors
Map<String, Set<String>> zoneNeighborsMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.matsim.project.drt_zone_generation.run;

import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.population.Population;
import org.matsim.application.MATSimApplication;
import org.matsim.contrib.drt.analysis.zonal.DrtZonalSystem;
import org.matsim.contrib.drt.routing.DrtRoute;
import org.matsim.contrib.drt.routing.DrtRouteFactory;
import org.matsim.contrib.drt.run.*;
import org.matsim.contrib.dvrp.run.AbstractDvrpModeModule;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.contrib.dvrp.run.DvrpModule;
import org.matsim.contrib.dvrp.run.DvrpQSimComponents;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.Controler;
import org.matsim.project.drt_zone_generation.HeuristicRebalancingZoneGenerator;
import picocli.CommandLine;

import javax.annotation.Nullable;

public class RunDrtSimulations extends MATSimApplication {

@CommandLine.Option(names = "--new-zone", defaultValue = "false", description = "enable new zonal system")
private boolean improvedZones;

public static void main(String[] args) {
MATSimApplication.run(RunDrtSimulations.class, args);
}

@Nullable
@Override
protected Config prepareConfig(Config config) {
config.addModule(new MultiModeDrtConfigGroup());
config.addModule(new DvrpConfigGroup());
MultiModeDrtConfigGroup multiModeDrtConfig = ConfigUtils.addOrGetModule(config, MultiModeDrtConfigGroup.class);
DrtConfigs.adjustMultiModeDrtConfig(multiModeDrtConfig, config.planCalcScore(), config.plansCalcRoute());
return config;
}

@Override
protected void prepareScenario(Scenario scenario) {
scenario.getPopulation()
.getFactory()
.getRouteFactories()
.setRouteFactory(DrtRoute.class, new DrtRouteFactory());
}

@Override
protected void prepareControler(Controler controler) {
Config config = controler.getConfig();
MultiModeDrtConfigGroup multiModeDrtConfig = ConfigUtils.addOrGetModule(config, MultiModeDrtConfigGroup.class);
controler.addOverridingModule(new DvrpModule());
controler.addOverridingModule(new MultiModeDrtModule());
controler.configureQSimComponents(DvrpQSimComponents.activateAllModes(multiModeDrtConfig));

if (improvedZones) {
DrtConfigGroup drtConfigGroup = DrtConfigGroup.getSingleModeDrtConfig(config);
String outputPathForNetworkWithZones = config.controler().getOutputDirectory() + "/output-network-with-zones.xml.gz";
controler.addOverridingModule(new AbstractDvrpModeModule(drtConfigGroup.mode) {
@Override
public void install() {
bindModal(DrtZonalSystem.class).toProvider(modalProvider(getter ->
new HeuristicRebalancingZoneGenerator.ZoneGeneratorBuilder(getter.getModal(Network.class), outputPathForNetworkWithZones)
.setInputPlans(getter.get(Population.class)).setZoneIterations(10)
.build().compute())).asEagerSingleton();
}
});
}

}
}

0 comments on commit aa149bf

Please sign in to comment.