Skip to content

Commit

Permalink
Consolidate dvrp/drt/taxi zones (matsim-org#3224)
Browse files Browse the repository at this point in the history
* WIP: consolidate drt and dvrp zones

* minor refactorings

* minor refactorings

* further steps towards consolidation of dvrp and drt zone systems

* WIP: consolidate drt and dvrp zones

* minor refactorings

* minor refactorings

* further steps towards consolidation of dvrp and drt zone systems

* update zone systems

* finalize zone system consolidation

* finalize zone system consolidation

* fix tests

* fix tests

* fix another test

* change population comparison method

* move pairwise plan elements comparison from test utils to population comparison
  • Loading branch information
nkuehnel authored Apr 30, 2024
1 parent d5db30a commit 65b49fb
Show file tree
Hide file tree
Showing 133 changed files with 1,854 additions and 1,673 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.URL;

import org.matsim.api.core.v01.Scenario;
import org.matsim.contrib.common.zones.systems.grid.square.SquareGridZoneSystemParams;
import org.matsim.contrib.drt.run.DrtControlerCreator;
import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup;
import org.matsim.contrib.drt.run.MultiModeDrtModule;
Expand All @@ -33,6 +34,7 @@
import org.matsim.contrib.taxi.run.MultiModeTaxiConfigGroup;
import org.matsim.contrib.taxi.run.MultiModeTaxiModule;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.OutputDirectoryHierarchy;
Expand All @@ -44,8 +46,13 @@
*/
public class RunDrtAndTaxiExample {
public static void run(URL configUrl, boolean otfvis) {
DvrpConfigGroup dvrpConfigGroup = new DvrpConfigGroup();
ConfigGroup zoneParams = dvrpConfigGroup.getTravelTimeMatrixParams().createParameterSet(SquareGridZoneSystemParams.SET_NAME);
dvrpConfigGroup.getTravelTimeMatrixParams().addParameterSet(zoneParams);


Config config = ConfigUtils.loadConfig(configUrl, new MultiModeDrtConfigGroup(), new MultiModeTaxiConfigGroup(),
new DvrpConfigGroup(), new OTFVisConfigGroup());
dvrpConfigGroup, new OTFVisConfigGroup());
Scenario scenario = DrtControlerCreator.createScenarioWithDrtRouteFactory(config);
ScenarioUtils.loadScenario(scenario);
config.controller()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
import org.matsim.contrib.commercialTrafficApplications.jointDemand.ChangeCommercialJobOperator;
import org.matsim.contrib.commercialTrafficApplications.jointDemand.JointDemandConfigGroup;
import org.matsim.contrib.commercialTrafficApplications.jointDemand.JointDemandModule;
import org.matsim.contrib.common.zones.systems.grid.square.SquareGridZoneSystemParams;
import org.matsim.contrib.drt.optimizer.insertion.extensive.ExtensiveInsertionSearchParams;
import org.matsim.contrib.drt.run.DrtConfigGroup;
import org.matsim.contrib.drt.run.DrtConfigs;
import org.matsim.contrib.drt.run.DrtControlerCreator;
import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup;
import org.matsim.contrib.dvrp.run.DvrpConfigGroup;
import org.matsim.contrib.dvrp.run.DvrpQSimComponents;
import org.matsim.core.config.ConfigGroup;
import org.matsim.freight.carriers.FreightCarriersConfigGroup;
import org.matsim.freight.carriers.CarriersUtils;
import org.matsim.core.config.Config;
Expand Down Expand Up @@ -132,8 +134,11 @@ private static void prepareConfig(Config config) {
}

private static void loadConfigGroups(Config config) {
ConfigUtils.addOrGetModule(config, DvrpConfigGroup.class);
MultiModeDrtConfigGroup multiModeDrtConfigGroup = ConfigUtils.addOrGetModule(config, MultiModeDrtConfigGroup.class);
DvrpConfigGroup dvrpConfigGroup = ConfigUtils.addOrGetModule(config, DvrpConfigGroup.class);
ConfigGroup zoneParams = dvrpConfigGroup.getTravelTimeMatrixParams().createParameterSet(SquareGridZoneSystemParams.SET_NAME);
dvrpConfigGroup.getTravelTimeMatrixParams().addParameterSet(zoneParams);

MultiModeDrtConfigGroup multiModeDrtConfigGroup = ConfigUtils.addOrGetModule(config, MultiModeDrtConfigGroup.class);

DrtConfigGroup drtCfg = new DrtConfigGroup();
drtCfg.maxWaitTime = 2 * 3600;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@
* *********************************************************************** *
*/

package org.matsim.contrib.util;
package org.matsim.contrib.common.util;

import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.config.ReflectiveConfigGroup;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.matsim.core.config.ConfigGroup;
import org.matsim.core.config.ReflectiveConfigGroup;

import com.google.common.base.Preconditions;
import com.google.common.base.Verify;

/**
* Provides additional functionality for handling parameter sets according to definitions.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.matsim.contrib.common.zones;

import org.matsim.api.core.v01.Coord;

import java.util.Optional;

public interface GridZoneSystem extends ZoneSystem {

Optional<Zone> getZoneForCoord(Coord coord);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.matsim.contrib.common.zones;

import org.locationtech.jts.geom.prep.PreparedGeometry;
import org.locationtech.jts.geom.prep.PreparedPolygon;
import org.matsim.api.core.v01.BasicLocation;
import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Identifiable;
Expand All @@ -11,9 +11,9 @@

public interface Zone extends BasicLocation, Identifiable<Zone> {
@Nullable
PreparedGeometry getPreparedGeometry();
PreparedPolygon getPreparedGeometry();

Coord getCentroid();
String getType();

List<Link> getLinks();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.matsim.contrib.common.zones;

import org.locationtech.jts.geom.prep.PreparedGeometry;
import org.locationtech.jts.geom.prep.PreparedPolygon;
import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.network.Link;
Expand All @@ -13,20 +13,20 @@ public class ZoneImpl implements Zone {

private final Id<Zone> id;
@Nullable
private final PreparedGeometry preparedGeometry; //null for virtual/dummy zones
private final List<Link> links;
private PreparedPolygon preparedGeometry; //null for virtual/dummy zones
private final Coord centroid;
private String type;

public ZoneImpl(Id<Zone> id, PreparedGeometry preparedGeometry, List<Link> links) {
this(id, preparedGeometry, links, MGC.point2Coord(preparedGeometry.getGeometry().getCentroid()));
public ZoneImpl(Id<Zone> id, PreparedPolygon preparedGeometry, @Nullable String type) {
this(id, preparedGeometry, MGC.point2Coord(preparedGeometry.getGeometry().getCentroid()), type);
}

private ZoneImpl(Id<Zone> id, @Nullable PreparedGeometry preparedGeometry, List<Link> links, Coord centroid) {
public ZoneImpl(Id<Zone> id, @Nullable PreparedPolygon preparedGeometry, Coord centroid, @Nullable String type) {
this.id = id;
this.preparedGeometry = preparedGeometry;
this.links = links;
this.centroid = centroid;
}
this.type = type;
}

@Override
public Id<Zone> getId() {
Expand All @@ -40,7 +40,7 @@ public Coord getCoord() {

@Override
@Nullable
public PreparedGeometry getPreparedGeometry() {
public PreparedPolygon getPreparedGeometry() {
return preparedGeometry;
}

Expand All @@ -50,16 +50,21 @@ public Coord getCentroid() {
}

@Override
public List<Link> getLinks() {
return links;
public String getType() {
return type;
}

boolean isDummy() {
return preparedGeometry == null;
}

public static ZoneImpl createDummyZone(Id<Zone> id, List<Link> links, Coord centroid) {
return new ZoneImpl(id, null, links, centroid);
public void setGeometry(PreparedPolygon preparedPolygon) {
this.preparedGeometry = preparedPolygon;
}


public static ZoneImpl createDummyZone(Id<Zone> id, Coord centroid) {
return new ZoneImpl(id, null, centroid, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Node;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public interface ZoneSystem {
@Nullable
Zone getZoneForLinkId(Id<Link> linkId);
Optional<Zone> getZoneForLinkId(Id<Link> link);

Optional<Zone> getZoneForNodeId(Id<Node> nodeId);

List<Link> getLinksForZoneId(Id<Zone> zone);


Map<Id<Zone>, Zone> getZones();
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,61 @@
package org.matsim.contrib.common.zones;

import org.apache.commons.lang3.tuple.Pair;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.IdMap;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.network.Node;
import org.matsim.contrib.common.zones.util.ZoneFinder;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.*;

public class ZoneSystemImpl implements ZoneSystem {

private final Map<Id<Zone>, Zone> zones = new IdMap<>(Zone.class);
private final IdMap<Link, Zone> link2zone = new IdMap<>(Link.class);

private final IdMap<Node, Zone> nodeToZoneMap = new IdMap<>(Node.class);

private final IdMap<Zone, List<Link>> zoneToLinksMap = new IdMap<>(Zone.class);

public ZoneSystemImpl(Collection<Zone> zones) {
zones.forEach(zone -> this.zones.put(zone.getId(), zone));
zones.stream()
.flatMap(zone -> zone.getLinks().stream().map(link -> Pair.of(link.getId(), zone)))
.forEach(idZonePair -> link2zone.put(idZonePair.getKey(), idZonePair.getValue()));
}

public ZoneSystemImpl(Collection<Zone> zones, ZoneFinder zoneFinder, Network network) {
zones.forEach(zone -> this.zones.put(zone.getId(), zone));

IdMap<Node, Zone> nodeToZoneMap = ZoneSystemUtils.createNodeToZoneMap(network, zoneFinder);
IdMap<Link, Zone> linkToZoneMap = ZoneSystemUtils.createLinkToZoneMap(network, zoneFinder);
this.nodeToZoneMap.putAll(nodeToZoneMap);
this.link2zone.putAll(linkToZoneMap);

for (Link link : network.getLinks().values()) {
zoneFinder.findZone(link.getToNode().getCoord()).ifPresent(zone -> {
List<Link> links = zoneToLinksMap.computeIfAbsent(zone.getId(), zoneId1 -> new ArrayList<>());
links.add(link);
});
}
}

/**
* @param linkId
* @param link
* @return the the {@code DrtZone} that contains the {@code linkId}. If the given link's {@code Coord} borders two or more cells, the allocation to a cell is random.
* Result may be null in case the given link is outside of the service area.
*/
@Override
@Nullable
public Zone getZoneForLinkId(Id<Link> linkId) {
return link2zone.get(linkId);
public Optional<Zone> getZoneForLinkId(Id<Link> link) {
return Optional.ofNullable(link2zone.get(link));
}

@Override
public Optional<Zone> getZoneForNodeId(Id<Node> nodeId) {
return Optional.ofNullable(nodeToZoneMap.get(nodeId));
}

@Override
public List<Link> getLinksForZoneId(Id<Zone> zone) {
return zoneToLinksMap.getOrDefault(zone, Collections.emptyList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.matsim.contrib.common.zones;

import org.matsim.core.config.ReflectiveConfigGroup;

/**
* @author nkuehnel / MOIA
*/
public abstract class ZoneSystemParams extends ReflectiveConfigGroup {
public ZoneSystemParams(String paramSetName) {
super(paramSetName);
}
}
Loading

0 comments on commit 65b49fb

Please sign in to comment.