Skip to content

Commit d1da935

Browse files
authored
feat: further parametrization and parallelization (#317)
* refactor parameter set for pt mapping Extracted the parameter set class => cleaner code some quality of life name changes * feat: parametrized pt modes now for each schedule mode, we can add a specific number of links and maximum search distance parameters we can also use strict rule: when number of links is reached no additional links are added backwards compatibility is ensured * parallelize creating facilities and links sequences Parallelize the generation of facilities and links from pseudo routes, speeding up the process considerably * make threads and chunk size configurable * fix typo * extract reused variable * remove duplicate exception * rename fields to match original names * update default pt mapper config * fix copy-paste * remove static * use local variables We now use local variables and do not adjust global ones if, for this mode, we need to use mode-specific rules * add a modeSpecificRules test * test to check the strict num of links rule
1 parent 1af2881 commit d1da935

17 files changed

+483
-238
lines changed

doc/defaultPTMapperConfig.xml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
between stops from the schedule. If travelCostType is
1919
linkLength minTravel cost is the beeline distance. -->
2020
<param name="maxTravelCostFactor" value="5.0" />
21+
<!-- Instead of using general number of links and maximum search distance rule, use the schedule mode specific rules to be defined within the parameter sets. For those that no information is provided the general values will be used. Options: [false, true]. Default: false. -->
22+
<param name="modeSpecificRules" value="false" />
2123
<!-- All links that do not have a transit route on them are removed, except the ones
2224
listed in this set (typically only car). Separated by comma. -->
2325
<param name="modesToKeepOnCleanUp" value="car" />
@@ -47,19 +49,24 @@
4749
transit routes passing using it. This is recommended for "artificial", additional
4850
modes (especially "rail", if used) can be added, separated by commas. -->
4951
<param name="scheduleFreespeedModes" value="artificial" />
52+
<!-- The size of the chunk that is sent to the pt mapper thread at the time to buildfacilities and links. Default: 100. -->
53+
<param name="threadChunkSize" value="100" />
5054
<!-- Defines which link attribute should be used for routing. Possible values "linkLength" (default)
5155
and "travelTime". -->
5256
<param name="travelCostType" value="linkLength" />
5357
<parameterset type="transportModeAssignment" >
54-
<!-- Transit Routes with the given scheduleMode can only use links with at least one of the network modes
55-
defined here. Separate multiple modes by comma. If no network modes are defined, the transit route will
56-
use artificial links. -->
58+
<param name="maxLinkCandidateDistance" value="90.0" />
59+
<param name="nLinkThreshold" value="6" />
5760
<param name="networkModes" value="car,bus" />
5861
<param name="scheduleMode" value="bus" />
62+
<param name="strictLinkRule" value="false" />
5963
</parameterset>
6064
<parameterset type="transportModeAssignment" >
65+
<param name="maxLinkCandidateDistance" value="90.0" />
66+
<param name="nLinkThreshold" value="6" />
6167
<param name="networkModes" value="rail,light_rail" />
6268
<param name="scheduleMode" value="rail" />
69+
<param name="strictLinkRule" value="false" />
6370
</parameterset>
6471
</module>
6572

src/main/java/org/matsim/pt2matsim/config/PublicTransitMappingConfigGroup.java

Lines changed: 51 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,23 @@
1919

2020
package org.matsim.pt2matsim.config;
2121

22-
import org.matsim.api.core.v01.network.Link;
23-
import org.matsim.core.api.internal.MatsimParameters;
24-
import org.matsim.core.config.*;
25-
import org.matsim.core.config.groups.ControllerConfigGroup;
26-
import org.matsim.core.config.groups.ControllerConfigGroup.RoutingAlgorithmType;
27-
import org.matsim.core.utils.collections.CollectionUtils;
28-
import org.matsim.pt.transitSchedule.api.TransitRoute;
29-
import org.matsim.pt2matsim.run.PublicTransitMapper;
22+
import static org.matsim.pt2matsim.config.PublicTransitMappingConfigGroup.TravelCostType.travelTime;
3023

3124
import java.util.HashMap;
3225
import java.util.HashSet;
3326
import java.util.Map;
3427
import java.util.Set;
3528
import java.util.stream.Collectors;
3629

37-
import static org.matsim.pt2matsim.config.PublicTransitMappingConfigGroup.TravelCostType.travelTime;
30+
import org.matsim.core.config.Config;
31+
import org.matsim.core.config.ConfigGroup;
32+
import org.matsim.core.config.ConfigUtils;
33+
import org.matsim.core.config.ConfigWriter;
34+
import org.matsim.core.config.ReflectiveConfigGroup;
35+
import org.matsim.core.config.groups.ControllerConfigGroup;
36+
import org.matsim.core.config.groups.ControllerConfigGroup.RoutingAlgorithmType;
37+
import org.matsim.core.utils.collections.CollectionUtils;
38+
import org.matsim.pt2matsim.run.PublicTransitMapper;
3839

3940

4041
/**
@@ -70,13 +71,19 @@ public enum TravelCostType { linkLength, travelTime }
7071
private static final String ROUTING_WITH_CANDIDATE_DISTANCE = "routingWithCandidateDistance";
7172

7273
private static final String NETWORK_ROUTER = "networkRouter";
74+
75+
private static final String USE_MODE_SPECIFIC_RULES = "modeSpecificRules";
76+
77+
private static final String THREAD_CHUNK_SIZE = "threadChunkSize";
7378

7479
// default values
7580
private Map<String, Set<String>> transportModeAssignment = new HashMap<>();
81+
private Map<String, TransportModeParameterSet> parameterSetsForMode = new HashMap<>();
7682
private Set<String> scheduleFreespeedModes = PublicTransitMappingStrings.ARTIFICIAL_LINK_MODE_AS_SET;
7783
private Set<String> modesToKeepOnCleanUp = new HashSet<>();
7884
private double maxTravelCostFactor = 5.0;
7985
private int numOfThreads = 2;
86+
private int chunkSize = 100;
8087
private boolean removeNotUsedStopFacilities = true;
8188

8289
private String inputNetworkFile = null;
@@ -91,6 +98,8 @@ public enum TravelCostType { linkLength, travelTime }
9198
private double maxLinkCandidateDistance = 90;
9299
private double candiateDistanceMulitplier = 1.6;
93100

101+
private boolean modeSpecificRules = false;
102+
94103
private RoutingAlgorithmType networkRouter = ControllerConfigGroup.RoutingAlgorithmType.SpeedyALT;
95104

96105
public PublicTransitMappingConfigGroup() {
@@ -104,9 +113,9 @@ public static PublicTransitMappingConfigGroup createDefaultConfig() {
104113
PublicTransitMappingConfigGroup config = new PublicTransitMappingConfigGroup();
105114
config.getModesToKeepOnCleanUp().add("car");
106115

107-
TransportModeAssignment tmaBus = new TransportModeAssignment("bus");
116+
TransportModeParameterSet tmaBus = new TransportModeParameterSet("bus");
108117
tmaBus.setNetworkModesStr("car,bus");
109-
TransportModeAssignment tmaRail = new TransportModeAssignment("rail");
118+
TransportModeParameterSet tmaRail = new TransportModeParameterSet("rail");
110119
tmaRail.setNetworkModesStr("rail,light_rail");
111120
config.addParameterSet(tmaBus);
112121
config.addParameterSet(tmaRail);
@@ -181,14 +190,18 @@ public final Map<String, String> getComments() {
181190
"\t\tNo link candidates beyond this distance are added.");
182191
map.put(NETWORK_ROUTER,
183192
"The router that should be used. Possible options are: [SpeedyALT, AStarLandmarks]");
193+
map.put(USE_MODE_SPECIFIC_RULES, "Instead of using general number of links and maximum search distance rule, use the schedule mode specific rules "
194+
+ "to be defined within the parameter sets. For those that no information is provided the general values will be used. Options: [false, true]. Default: false.");
195+
map.put(THREAD_CHUNK_SIZE, "The size of the chunk that is sent to the pt mapper thread at the time to build"
196+
+ "facilities and links. Default: 100.");
184197
return map;
185198
}
186199

187200
@Override
188201
public ConfigGroup createParameterSet(final String type) {
189202
switch(type) {
190-
case TransportModeAssignment.SET_NAME:
191-
return new TransportModeAssignment();
203+
case TransportModeParameterSet.GROUP_NAME:
204+
return new TransportModeParameterSet();
192205
default:
193206
throw new IllegalArgumentException("Unknown parameterset name!");
194207
}
@@ -204,9 +217,10 @@ public void addParameterSet(final ConfigGroup set) {
204217
* Loads the parameter set for TransportModeAssignment for easier access
205218
*/
206219
private void loadParameterSets() {
207-
for(ConfigGroup e : this.getParameterSets(TransportModeAssignment.SET_NAME)) {
208-
TransportModeAssignment mra = (TransportModeAssignment) e;
209-
transportModeAssignment.put(mra.getScheduleMode(), mra.getNetworkModes());
220+
for(ConfigGroup e : this.getParameterSets(TransportModeParameterSet.GROUP_NAME)) {
221+
TransportModeParameterSet tmps = (TransportModeParameterSet) e;
222+
transportModeAssignment.put(tmps.getScheduleMode(), tmps.getNetworkModes());
223+
parameterSetsForMode.put(tmps.getScheduleMode(), tmps);
210224
}
211225
}
212226

@@ -233,6 +247,10 @@ public Map<String, Set<String>> getTransportModeAssignment() {
233247
public void setTransportModeAssignment(Map<String, Set<String>> transportModeAssignment) {
234248
this.transportModeAssignment = transportModeAssignment;
235249
}
250+
251+
public TransportModeParameterSet getParameterSetForMode(String scheduleMode) {
252+
return this.parameterSetsForMode.get(scheduleMode);
253+
}
236254

237255
/**
238256
* All links that do not have a transit route on them are removed, except
@@ -485,71 +503,25 @@ public RoutingAlgorithmType getNetworkRouter() {
485503
public void setNetworkRouter(RoutingAlgorithmType networkRouter) {
486504
this.networkRouter = networkRouter;
487505
}
506+
507+
@StringGetter(USE_MODE_SPECIFIC_RULES)
508+
public boolean getModeSpecificRules() {
509+
return this.modeSpecificRules;
510+
}
488511

512+
@StringSetter(USE_MODE_SPECIFIC_RULES)
513+
public void setModeSpecificRules(String modeSpecificRules) {
514+
this.modeSpecificRules = Boolean.parseBoolean(modeSpecificRules);
515+
}
489516

490-
/**
491-
* Parameterset that define which network transport modes the router
492-
* can use for each schedule transport mode. If no networkModes are set, the
493-
* transit route is mapped artificially<p/>
494-
* <p>
495-
* Network transport modes are the ones in {@link Link#getAllowedModes()}, schedule
496-
* transport modes are from {@link TransitRoute#getTransportMode()}.
497-
*/
498-
public static class TransportModeAssignment extends ReflectiveConfigGroup implements MatsimParameters {
499-
500-
public static final String SET_NAME = "transportModeAssignment";
501-
502-
private static final String SCHEDULE_MODE = "scheduleMode";
503-
private static final String NETWORK_MODES = "networkModes";
504-
505-
private String scheduleMode;
506-
private Set<String> networkModes;
507-
508-
public TransportModeAssignment() {
509-
super(SET_NAME);
510-
}
511-
512-
public TransportModeAssignment(String scheduleMode) {
513-
super(SET_NAME);
514-
this.scheduleMode = scheduleMode;
515-
}
516-
517-
@Override
518-
public Map<String, String> getComments() {
519-
Map<String, String> map = super.getComments();
520-
map.put(NETWORK_MODES,
521-
"Transit Routes with the given scheduleMode can only use links with at least one of the network modes\n" +
522-
"\t\t\tdefined here. Separate multiple modes by comma. If no network modes are defined, the transit route will\n" +
523-
"\t\t\tuse artificial links.");
524-
return map;
525-
}
526-
527-
@StringGetter(SCHEDULE_MODE)
528-
public String getScheduleMode() {
529-
return scheduleMode;
530-
}
531-
532-
@StringSetter(SCHEDULE_MODE)
533-
public void setScheduleMode(String scheduleMode) {
534-
this.scheduleMode = scheduleMode;
535-
}
536-
537-
@StringGetter(NETWORK_MODES)
538-
public String getNetworkModesStr() {
539-
return CollectionUtils.setToString(networkModes);
540-
}
541-
542-
@StringSetter(NETWORK_MODES)
543-
public void setNetworkModesStr(String networkModesStr) {
544-
this.networkModes = CollectionUtils.stringToSet(networkModesStr);
545-
}
546-
547-
public Set<String> getNetworkModes() {
548-
return this.networkModes;
549-
}
517+
@StringGetter(THREAD_CHUNK_SIZE)
518+
public int getChunkSize() {
519+
return this.chunkSize;
520+
}
550521

551-
public void setNetworkModes(Set<String> networkModes) {
552-
this.networkModes = networkModes;
553-
}
522+
@StringSetter(THREAD_CHUNK_SIZE)
523+
public void setChunkSize(int chunkSize) {
524+
this.chunkSize = chunkSize;
554525
}
526+
555527
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.matsim.pt2matsim.config;
2+
3+
import java.util.Set;
4+
5+
import org.matsim.api.core.v01.network.Link;
6+
import org.matsim.core.config.ReflectiveConfigGroup;
7+
import org.matsim.core.utils.collections.CollectionUtils;
8+
import org.matsim.pt.transitSchedule.api.TransitRoute;
9+
10+
/**
11+
* Parameterset that define which network transport modes the router
12+
* can use for each schedule transport mode. If no networkModes are set, the
13+
* transit route is mapped artificially<p/>
14+
* <p>
15+
* Network transport modes are the ones in {@link Link#getAllowedModes()}, schedule
16+
* transport modes are from {@link TransitRoute#getTransportMode()}.
17+
**/
18+
public class TransportModeParameterSet extends ReflectiveConfigGroup{
19+
20+
static public final String GROUP_NAME = "transportModeAssignment";
21+
22+
private static final String SCHEDULE_MODE = "scheduleMode";
23+
private static final String NETWORK_MODES = "networkModes";
24+
private static final String NUMBER_LINK_CANDIDATES = "nLinkThreshold";
25+
private static final String MAXIMUM_SEARCH_DISTANCE = "maxLinkCandidateDistance";
26+
private static final String IMPOSE_STRICT_LINK_RULE = "strictLinkRule";
27+
28+
private String scheduleMode;
29+
private Set<String> networkModes;
30+
private int numberOfLinkCandidates = 6;
31+
private double maximumSearchDistance = 90;
32+
private boolean imposeStrictLinksRule = false;
33+
34+
public TransportModeParameterSet() {
35+
super(GROUP_NAME);
36+
}
37+
38+
public TransportModeParameterSet(String scheduleMode) {
39+
super(GROUP_NAME);
40+
this.scheduleMode = scheduleMode;
41+
}
42+
43+
@StringGetter(SCHEDULE_MODE)
44+
public String getScheduleMode() {
45+
return scheduleMode;
46+
}
47+
48+
@StringSetter(SCHEDULE_MODE)
49+
public void setScheduleMode(String scheduleMode) {
50+
this.scheduleMode = scheduleMode;
51+
}
52+
53+
@StringGetter(NETWORK_MODES)
54+
public String getNetworkModesStr() {
55+
return CollectionUtils.setToString(networkModes);
56+
}
57+
58+
@StringSetter(NETWORK_MODES)
59+
public void setNetworkModesStr(String networkModesStr) {
60+
this.networkModes = CollectionUtils.stringToSet(networkModesStr);
61+
}
62+
63+
public Set<String> getNetworkModes() {
64+
return this.networkModes;
65+
}
66+
67+
@StringGetter(NUMBER_LINK_CANDIDATES)
68+
public int getNumberOfLinkCandidates() {
69+
return numberOfLinkCandidates;
70+
}
71+
72+
@StringSetter(NUMBER_LINK_CANDIDATES)
73+
public void setNumberOfLinkCandidates(int numberOfLinkCandidates) {
74+
this.numberOfLinkCandidates = numberOfLinkCandidates;
75+
}
76+
77+
@StringGetter(MAXIMUM_SEARCH_DISTANCE)
78+
public double getMaximumSearchDistance() {
79+
return maximumSearchDistance;
80+
}
81+
82+
@StringSetter(MAXIMUM_SEARCH_DISTANCE)
83+
public void setMaximumSearchDistance(double maximumSearchDistance) {
84+
this.maximumSearchDistance = maximumSearchDistance;
85+
}
86+
@StringGetter(IMPOSE_STRICT_LINK_RULE)
87+
public boolean getImposeStrictLinksRule() {
88+
return imposeStrictLinksRule;
89+
}
90+
@StringSetter(IMPOSE_STRICT_LINK_RULE)
91+
public void setImposeStrictLinksRule(boolean imposeStrictLinksRule) {
92+
this.imposeStrictLinksRule = imposeStrictLinksRule;
93+
}
94+
95+
}

src/main/java/org/matsim/pt2matsim/examples/PT2MATSimExample.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import java.io.File;
3030
import java.io.IOException;
31+
import java.util.concurrent.ExecutionException;
3132

3233
/**
3334
* Usage test of PT2MATSim to document how the package can be used. The example region
@@ -45,7 +46,7 @@ public final class PT2MATSimExample {
4546

4647
private static final String addisonCountyEPSG = "EPSG:2032";
4748

48-
public static void main(String[] args) {
49+
public static void main(String[] args) throws InterruptedException, ExecutionException {
4950
prepare();
5051

5152
// 1. Convert a gtfs schedule to an unmapped transit schedule

0 commit comments

Comments
 (0)