Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Jul 9, 2024
2 parents c5ada97 + 7214752 commit 4a4f317
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.matsim.api.core.v01.Coord;
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.application.options.ShpOptions;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.utils.geometry.CoordUtils;
Expand All @@ -20,20 +21,34 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TransferRestrictions {
public class ChangeNetworkModesForRestrictions {
/*
Transfers Restrictions from the shapefile to the given Network.
This class reads in a network and a shapefile containing restrictions for different network modes (e.g. car, truck8t, truck18t, truck26t, truck40t).
As a result, the network is updated, so that the links, which are restricted for certain vehicle categories, are not allowed to be used by these vehicles.
*/

private static final String ruhrNetworkPath = "D:\\Projects\\VSP\\RUHR\\commercialTraffic\\ruhr_network.xml.gz";
private static final String ruhrNetworkPath = "D:\\Projects\\VSP\\RUHR\\metropole-ruhr-v2.0.network_resolutionHigh-with-pt.xml";
private static final String restrictionPath = "D:\\Projects\\VSP\\RUHR\\commercialTraffic\\Restriktionen_RVR_20231121\\Restriktionen_RVR_20231121.shp";
private static final String outputPath = "scenarios/metropole-ruhr-v1.0/ruhr_network_with_restrictions.xml.gz";

public static void main(String[] args){
//Read in shp and xml files
Network network = NetworkUtils.readNetwork(ruhrNetworkPath);
Network network_no_pt = NetworkUtils.createNetwork();
ShpOptions shp = new ShpOptions(Path.of(restrictionPath), null, null);

//Create copy of network which only contains links, where car/freight-traffic is allowed (not pt, no bike-only-links, ...)
for(Node n : network.getNodes().values()){
if(!n.getId().toString().startsWith("pt") && !n.getId().toString().startsWith("bike")){
network_no_pt.addNode(n);
}
}
for(Link l : network.getLinks().values()){
if(l.getAllowedModes().contains("car") || l.getAllowedModes().contains("freight")){
network_no_pt.addLink(l);
}
}

// Add all freight-modes to all car-links
Collection<? extends Link> links = network.getLinks().values();
Set<String> all = new HashSet<>(Arrays.asList("truck8t", "truck18t", "truck26t", "truck40t"));
Expand All @@ -60,10 +75,17 @@ public static void main(String[] args){
//Finds the center of two nodes and searches for the link from there
if(j != 0){
Coord center = CoordUtils.getCenter(coord, coord);
Link nearestLink = NetworkUtils.getNearestLinkExactly(network, center);
Link nearestLink = NetworkUtils.getNearestLinkExactly(network_no_pt, center);
Link oppositeLink = NetworkUtils.findLinkInOppositeDirection(nearestLink);
Set<String> allowedModes = new HashSet<>(nearestLink.getAllowedModes()); // Makes a copy, because original is immutable

//Sort out links, that were assigned incorrectly
double distance = NetworkUtils.getEuclideanDistance(nearestLink.getFromNode().getCoord(), center);
double links_euclidean_length = NetworkUtils.getEuclideanDistance(nearestLink.getFromNode().getCoord(), nearestLink.getToNode().getCoord());
if(distance > links_euclidean_length+20){
continue;
}

//Get value if numeric
float value = -1;
try{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.matsim.prepare.commercial;

import org.matsim.api.core.v01.population.Person;
import org.matsim.freight.carriers.CarrierCapabilities;

import java.util.List;

public interface CommercialVehicleSelector {
String getVehicleTypeForPlan(Person freightDemandDataRelation, String carrierId);

List<String> getPossibleVehicleTypes(Person freightDemandDataRelation, String carrierId);
List<String> getPossibleVehicleTypes(Person freightDemandDataRelation, String carrierId, CarrierCapabilities.FleetSize fleetSize);

String getModeForFTLTrip(Person freightDemandDataRelation);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.commons.math3.random.RandomGenerator;
import org.apache.commons.math3.util.Pair;
import org.matsim.api.core.v01.population.Person;
import org.matsim.freight.carriers.CarrierCapabilities;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -56,7 +57,7 @@ private void createVehicleTypeDistribution() {
@Override
public String getVehicleTypeForPlan(Person freightDemandDataRelation, String carrierId) {

if (CommercialTrafficUtils.getTransportType(freightDemandDataRelation).equals("FTL"))
if (CommercialTrafficUtils.getTransportType(freightDemandDataRelation).equals("FTL") || CommercialTrafficUtils.getTransportType(freightDemandDataRelation).equals(CommercialTrafficUtils.TransportType.FTL_kv.toString()))
return vehicleDistributionFTL.sample().vehicleType;
else if (CommercialTrafficUtils.getGoodsType(freightDemandDataRelation) == 140) // waste collection
return vehicleDistributionWaste.sample().vehicleType;
Expand All @@ -69,27 +70,41 @@ else if (CommercialTrafficUtils.getGoodsType(freightDemandDataRelation) == 140)
}

/**
* Gets the possible vehicle types for the given carrier.
* Gets the possible vehicle types for the given carrier. If the fleet size is finite, only one vehicle type based on the distribution is returned.
* Otherwise, all possible vehicle types are returned.
* TODO perhaps add vehicleTypes file to this implementation to get the mode from the selected vehicle type
*
* @param freightDemandDataRelation the freight demand data relation
* @param carrierId the carrier id
* @param fleetSize the fleet size
* @return the possible vehicle types for this carrier
*/
@Override
public List<String> getPossibleVehicleTypes(Person freightDemandDataRelation, String carrierId) {
public List<String> getPossibleVehicleTypes(Person freightDemandDataRelation, String carrierId, CarrierCapabilities.FleetSize fleetSize) {
List<String> allVehicleTypes = new ArrayList<>();
if (CommercialTrafficUtils.getTransportType(freightDemandDataRelation).equals("FTL"))
allVehicleTypes.addAll(vehicleDistributionFTL.getPmf().stream().map(Pair::getFirst).map(VehicleSelection::vehicleType).toList());
else if (CommercialTrafficUtils.getGoodsType(freightDemandDataRelation) == 140) // waste collection
allVehicleTypes.addAll(vehicleDistributionWaste.getPmf().stream().map(Pair::getFirst).map(VehicleSelection::vehicleType).toList());
if (fleetSize == CarrierCapabilities.FleetSize.FINITE)
allVehicleTypes.add(vehicleDistributionWaste.sample().vehicleType);
else
allVehicleTypes.addAll(vehicleDistributionWaste.getPmf().stream().map(Pair::getFirst).map(VehicleSelection::vehicleType).toList());
else if (CommercialTrafficUtils.getGoodsType(freightDemandDataRelation) == 150) // parcel delivery
if (carrierId.contains("_truck18t"))
allVehicleTypes.addAll(
vehicleDistributionParcel_truck.getPmf().stream().map(Pair::getFirst).map(VehicleSelection::vehicleType).toList());
if (fleetSize == CarrierCapabilities.FleetSize.FINITE)
allVehicleTypes.add(vehicleDistributionParcel_truck.sample().vehicleType);
else
allVehicleTypes.addAll(vehicleDistributionParcel_truck.getPmf().stream().map(Pair::getFirst).map(VehicleSelection::vehicleType).toList());
else
allVehicleTypes.addAll(vehicleDistributionParcel.getPmf().stream().map(Pair::getFirst).map(VehicleSelection::vehicleType).toList());
else allVehicleTypes.addAll(vehicleDistributionRest.getPmf().stream().map(Pair::getFirst).map(VehicleSelection::vehicleType).toList());

if (fleetSize == CarrierCapabilities.FleetSize.FINITE)
allVehicleTypes.add(vehicleDistributionParcel.sample().vehicleType);
else
allVehicleTypes.addAll(vehicleDistributionParcel.getPmf().stream().map(Pair::getFirst).map(VehicleSelection::vehicleType).toList());
else
if (fleetSize == CarrierCapabilities.FleetSize.FINITE)
allVehicleTypes.add(vehicleDistributionRest.sample().vehicleType);
else
allVehicleTypes.addAll(vehicleDistributionRest.getPmf().stream().map(Pair::getFirst).map(VehicleSelection::vehicleType).toList());
return allVehicleTypes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public DefaultDemandPerDayCalculator(int workingDays, double sample) {

/**
* Calculate the kilograms per day.
* //TODO also move sampling behind jsprit to get more realistic tours
*
* @param tonsPerYear the tons per year
* @return the kilograms per day
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private void createFreightFTLAgentWithPlan(Person freightDemandDataRelation, Lis
}
person.addPlan(plan);
freightDemandDataRelation.getAttributes().getAsMap().forEach((k, v) -> person.getAttributes().putAttribute(k, v));
PopulationUtils.putSubpopulation(person, transportType + "_trip");
PopulationUtils.putSubpopulation(person, transportType + "_trips");
Id<VehicleType> vehicleTypeId = Id.create(vehicleType, VehicleType.class);
VehicleUtils.insertVehicleTypesIntoPersonAttributes(person, Map.of(FTL_mode, vehicleTypeId));
freightAgents.add(person);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void createFreightVehicles(Scenario scenario, Carrier newCarrier, Id<Lin
int earliestVehicleStartTime = (int) departureTimeCalculator.calculateDepartureTime(freightDemandDataRelation);
int latestVehicleEndTime = earliestVehicleStartTime + 9 * 3600; //assumption that working only max 9hours
List<String> possibleVehicleTypeIds = commercialVehicleSelector.getPossibleVehicleTypes(freightDemandDataRelation,
newCarrier.getId().toString());
newCarrier.getId().toString(), carrierCapabilities.getFleetSize());
possibleVehicleTypeIds.forEach(vehicleTypeId -> {
VehicleType vehicleType = carrierVehicleTypes.getVehicleTypes().get(Id.create(vehicleTypeId, VehicleType.class));
// Waste collection and parcel delivery tours we will not sample. That's why we adjust the pcu equivalents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.population.Person;
import org.matsim.core.population.PopulationUtils;
import org.matsim.freight.carriers.CarrierCapabilities;

import java.util.List;

Expand Down Expand Up @@ -49,23 +50,45 @@ void testGetVehicleTypeForPlan() {

@Test
void testGetPossibleVehicleTypes() {
List<String> vehicleTypesFTL = selector.getPossibleVehicleTypes(freightDemandDataRelationFTL, "");
List<String> vehicleTypesFTL = selector.getPossibleVehicleTypes(freightDemandDataRelationFTL, "", CarrierCapabilities.FleetSize.INFINITE);
assertEquals(1, vehicleTypesFTL.size());
assertTrue(vehicleTypesFTL.contains("heavy40t"));

List<String> vehicleTypesWaste = selector.getPossibleVehicleTypes(freightDemandDataRelationWaste, "");
vehicleTypesFTL = selector.getPossibleVehicleTypes(freightDemandDataRelationFTL, "", CarrierCapabilities.FleetSize.FINITE);
assertEquals(1, vehicleTypesFTL.size());
assertTrue(vehicleTypesFTL.contains("heavy40t"));

List<String> vehicleTypesWaste = selector.getPossibleVehicleTypes(freightDemandDataRelationWaste, "", CarrierCapabilities.FleetSize.INFINITE);
assertEquals(1, vehicleTypesWaste.size());
assertTrue(vehicleTypesWaste.contains("waste_collection_diesel"));

vehicleTypesWaste = selector.getPossibleVehicleTypes(freightDemandDataRelationWaste, "", CarrierCapabilities.FleetSize.FINITE);
assertEquals(1, vehicleTypesWaste.size());
assertTrue(vehicleTypesWaste.contains("waste_collection_diesel"));

List<String> vehicleTypesParcel = selector.getPossibleVehicleTypes(freightDemandDataRelationParcel, "");
List<String> vehicleTypesParcel = selector.getPossibleVehicleTypes(freightDemandDataRelationParcel, "", CarrierCapabilities.FleetSize.INFINITE);
assertEquals(1, vehicleTypesParcel.size());
assertTrue(vehicleTypesParcel.contains("mercedes313_parcel"));

List<String> vehicleTypesParcelTruck18t = selector.getPossibleVehicleTypes(freightDemandDataRelationParcelTruck18t, "_truck18t");
vehicleTypesParcel = selector.getPossibleVehicleTypes(freightDemandDataRelationParcel, "", CarrierCapabilities.FleetSize.FINITE);
assertEquals(1, vehicleTypesParcel.size());
assertTrue(vehicleTypesParcel.contains("mercedes313_parcel"));

List<String> vehicleTypesParcelTruck18t = selector.getPossibleVehicleTypes(freightDemandDataRelationParcelTruck18t, "_truck18t",
CarrierCapabilities.FleetSize.INFINITE);
assertEquals(1, vehicleTypesParcelTruck18t.size());
assertTrue(vehicleTypesParcelTruck18t.contains("medium18t_parcel"));

vehicleTypesParcelTruck18t = selector.getPossibleVehicleTypes(freightDemandDataRelationParcelTruck18t, "_truck18t",
CarrierCapabilities.FleetSize.FINITE);
assertEquals(1, vehicleTypesParcelTruck18t.size());
assertTrue(vehicleTypesParcelTruck18t.contains("medium18t_parcel"));

List<String> vehicleTypesRest = selector.getPossibleVehicleTypes(freightDemandDataRelationRest, "");
List<String> vehicleTypesRest = selector.getPossibleVehicleTypes(freightDemandDataRelationRest, "", CarrierCapabilities.FleetSize.INFINITE);
assertEquals(1, vehicleTypesRest.size());
assertTrue(vehicleTypesRest.contains("medium18t"));

vehicleTypesRest = selector.getPossibleVehicleTypes(freightDemandDataRelationRest, "", CarrierCapabilities.FleetSize.FINITE);
assertEquals(1, vehicleTypesRest.size());
assertTrue(vehicleTypesRest.contains("medium18t"));
}
Expand Down

0 comments on commit 4a4f317

Please sign in to comment.