Skip to content

Commit

Permalink
allow freight as separate mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Dec 21, 2023
1 parent 3f15bad commit 568fd47
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
4 changes: 2 additions & 2 deletions input/v6.1/berlin-v6.1.config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<param name="inputFacilitiesFile" value="./berlin-v6.1-facilities.xml.gz"/>
</module>
<module name="planscalcroute">
<param name="networkModes" value="car,truck,ride"/>
<param name="networkModes" value="car,truck,freight,ride"/>
<param name="accessEgressType" value="accessEgressModeToLink"/>
<parameterset type="teleportedModeParameters">
<param name="beelineDistanceFactor" value="1.3"/>
Expand Down Expand Up @@ -170,7 +170,7 @@
<param name="marginalUtilityOfDistance_util_m" value="0.0" />
<param name="marginalUtilityOfTraveling_util_hr" value="0.0" />
<param name="monetaryDistanceRate" value="-4.0E-4" />
<param name="mode" value="truck" />
<param name="mode" value="freight" />
</parameterset>
</parameterset>
</module>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/matsim/prepare/ReprojectNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ public Integer call() throws Exception {
if (!remapModes.isEmpty()) {
for (Link link : scenario.getNetwork().getLinks().values()) {
Set<String> modes = new HashSet<>(link.getAllowedModes());

// Only add mode without removing
remapModes.forEach((oldMode, newMode) -> {
if (modes.contains(oldMode)) {
modes.remove(oldMode);
modes.add(newMode);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ public void install() {
}
});

controler.addOverridingModule(new RunOpenBerlinScenario.TravelTimeBinding());
controler.addOverridingModule(new SimWrapperModule());
}

Expand Down
29 changes: 16 additions & 13 deletions src/main/java/org/matsim/prepare/opt/RunCountOptimization.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.routes.NetworkRoute;
import org.matsim.counts.Count;
import org.matsim.counts.Counts;
import org.matsim.counts.MatsimCountsReader;
import org.matsim.counts.Volume;
import org.matsim.counts.*;
import org.matsim.prepare.RunOpenBerlinCalibration;
import org.optaplanner.core.api.solver.Solver;
import org.optaplanner.core.api.solver.SolverFactory;
Expand Down Expand Up @@ -49,6 +46,9 @@ public class RunCountOptimization implements MATSimAppCommand {
@CommandLine.Option(names = "--counts", description = "Path to counts", required = true)
private Path countsPath;

@CommandLine.Option(names = "--network-mode", description = "Path to vehicle types", defaultValue = TransportMode.car)
private String networkMode;

@CommandLine.Option(names = "--all-car", description = "Plans have been created with the all car option and counts should be scaled. ", defaultValue = "false")
private boolean allCar;

Expand Down Expand Up @@ -79,23 +79,27 @@ public Integer call() throws Exception {

new MatsimCountsReader(linkCounts).readFile(countsPath.toString());

int[] counts = new int[linkCounts.getCounts().size() * H];
Map<Id<Link>, MeasurementLocation<Link>> countStations = linkCounts.getMeasureLocations();

int[] counts = new int[countStations.size() * H];

linkMapping = new Object2IntLinkedOpenHashMap<>();

int k = 0;
for (Count<Link> value : linkCounts.getCounts().values()) {
Map<Integer, Volume> volumes = value.getVolumes();
for (MeasurementLocation<Link> station : countStations.values()){
// hard coded to car calibration
Measurable volumes = station.getVolumesForMode(networkMode);
for (int i = 0; i < H; i++) {
if (volumes.containsKey(i)) {
OptionalDouble v = volumes.getAtHour(i);
if (v.isPresent()) {
int idx = k * H + i;
counts[idx] = (int) volumes.get(i).getValue();
counts[idx] = (int) v.getAsDouble();
if (allCar)
counts[idx] = (int) (counts[idx] * RunOpenBerlinCalibration.CAR_FACTOR);
}
}

linkMapping.put(value.getId(), k++);
linkMapping.put(station.getRefId(), k++);
}

Network network = NetworkUtils.readNetwork(networkPath.toString());
Expand Down Expand Up @@ -140,7 +144,7 @@ private List<PlanPerson> processPopulation(Path input, Network network, Counts<L
Population population = PopulationUtils.readPopulation(input.toString());
List<PlanPerson> persons = new ArrayList<>();

Set<Id<Link>> links = linkCounts.getCounts().keySet();
Set<Id<Link>> links = linkCounts.getMeasureLocations().keySet();

SplittableRandom rnd = new SplittableRandom(0);

Expand All @@ -158,8 +162,6 @@ private List<PlanPerson> processPopulation(Path input, Network network, Counts<L
int offset = 0;
// Commercial traffic, which can be chosen to not be included at all
if (person.getId().toString().startsWith("commercialPersonTraffic")) {

offset = 1;
// if other trips have been scaled, these unscaled trips are scaled as well
if (allCar)
// scale with mean of CAR_FACTOR
Expand All @@ -176,6 +178,7 @@ private List<PlanPerson> processPopulation(Path input, Network network, Counts<L
for (PlanElement el : plan.getPlanElements()) {
if (el instanceof Leg leg) {

// TODO: other leg modes
if (!leg.getMode().equals(TransportMode.car))
continue;

Expand Down
27 changes: 20 additions & 7 deletions src/main/java/org/matsim/run/RunOpenBerlinScenario.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.matsim.run;

import com.google.inject.Key;
import com.google.inject.name.Names;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.TransportMode;
Expand All @@ -11,6 +13,8 @@
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.replanning.strategies.DefaultPlanStrategiesModule;
import org.matsim.core.router.costcalculators.TravelDisutilityFactory;
import org.matsim.core.router.util.TravelTime;
import org.matsim.prepare.RunOpenBerlinCalibration;
import org.matsim.simwrapper.SimWrapperConfigGroup;
import org.matsim.simwrapper.SimWrapperModule;
Expand Down Expand Up @@ -96,15 +100,24 @@ protected Config prepareConfig(Config config) {
protected void prepareControler(Controler controler) {

controler.addOverridingModule(new SimWrapperModule());
controler.addOverridingModule(new AbstractModule() {
@Override
public void install() {

addTravelTimeBinding(TransportMode.ride).to(networkTravelTime());
addTravelDisutilityFactoryBinding(TransportMode.ride).to(carTravelDisutilityFactoryKey());
controler.addOverridingModule(new TravelTimeBinding());

}
});
}

/**
* Add travel time bindings for ride and freight modes, which are not actually network modes.
*/
public static final class TravelTimeBinding extends AbstractModule {
@Override
public void install() {
addTravelTimeBinding(TransportMode.ride).to(networkTravelTime());
addTravelDisutilityFactoryBinding(TransportMode.ride).to(carTravelDisutilityFactoryKey());

addTravelTimeBinding("freight").to(Key.get(TravelTime.class, Names.named(TransportMode.truck)));
addTravelDisutilityFactoryBinding("freight").to(Key.get(TravelDisutilityFactory.class, Names.named(TransportMode.truck)));

}
}

}

0 comments on commit 568fd47

Please sign in to comment.