From 1405da1d32a1e20a96539bb30b0538a23aa7ad18 Mon Sep 17 00:00:00 2001 From: Ricardo Ewert Date: Mon, 8 Jul 2024 14:05:34 +0200 Subject: [PATCH] differentiate vehicle selection for finite or infinite fleets --- .../commercial/CommercialVehicleSelector.java | 3 +- .../DefaultCommercialVehicleSelector.java | 30 ++++++++++++----- .../DefaultCommercialVehicleSelectorTest.java | 33 ++++++++++++++++--- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/matsim/prepare/commercial/CommercialVehicleSelector.java b/src/main/java/org/matsim/prepare/commercial/CommercialVehicleSelector.java index f822d1e..bdd5b1f 100644 --- a/src/main/java/org/matsim/prepare/commercial/CommercialVehicleSelector.java +++ b/src/main/java/org/matsim/prepare/commercial/CommercialVehicleSelector.java @@ -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 getPossibleVehicleTypes(Person freightDemandDataRelation, String carrierId); + List getPossibleVehicleTypes(Person freightDemandDataRelation, String carrierId, CarrierCapabilities.FleetSize fleetSize); String getModeForFTLTrip(Person freightDemandDataRelation); } diff --git a/src/main/java/org/matsim/prepare/commercial/DefaultCommercialVehicleSelector.java b/src/main/java/org/matsim/prepare/commercial/DefaultCommercialVehicleSelector.java index 726fe75..f760f81 100644 --- a/src/main/java/org/matsim/prepare/commercial/DefaultCommercialVehicleSelector.java +++ b/src/main/java/org/matsim/prepare/commercial/DefaultCommercialVehicleSelector.java @@ -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; @@ -69,28 +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 getPossibleVehicleTypes(Person freightDemandDataRelation, String carrierId) { + public List getPossibleVehicleTypes(Person freightDemandDataRelation, String carrierId, CarrierCapabilities.FleetSize fleetSize) { List 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; } diff --git a/src/test/java/org/matsim/prepare/commercial/DefaultCommercialVehicleSelectorTest.java b/src/test/java/org/matsim/prepare/commercial/DefaultCommercialVehicleSelectorTest.java index 2f4b781..4108d98 100644 --- a/src/test/java/org/matsim/prepare/commercial/DefaultCommercialVehicleSelectorTest.java +++ b/src/test/java/org/matsim/prepare/commercial/DefaultCommercialVehicleSelectorTest.java @@ -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; @@ -49,23 +50,45 @@ void testGetVehicleTypeForPlan() { @Test void testGetPossibleVehicleTypes() { - List vehicleTypesFTL = selector.getPossibleVehicleTypes(freightDemandDataRelationFTL, ""); + List vehicleTypesFTL = selector.getPossibleVehicleTypes(freightDemandDataRelationFTL, "", CarrierCapabilities.FleetSize.INFINITE); assertEquals(1, vehicleTypesFTL.size()); assertTrue(vehicleTypesFTL.contains("heavy40t")); - List vehicleTypesWaste = selector.getPossibleVehicleTypes(freightDemandDataRelationWaste, ""); + vehicleTypesFTL = selector.getPossibleVehicleTypes(freightDemandDataRelationFTL, "", CarrierCapabilities.FleetSize.FINITE); + assertEquals(1, vehicleTypesFTL.size()); + assertTrue(vehicleTypesFTL.contains("heavy40t")); + + List 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 vehicleTypesParcel = selector.getPossibleVehicleTypes(freightDemandDataRelationParcel, ""); + List vehicleTypesParcel = selector.getPossibleVehicleTypes(freightDemandDataRelationParcel, "", CarrierCapabilities.FleetSize.INFINITE); assertEquals(1, vehicleTypesParcel.size()); assertTrue(vehicleTypesParcel.contains("mercedes313_parcel")); - List vehicleTypesParcelTruck18t = selector.getPossibleVehicleTypes(freightDemandDataRelationParcelTruck18t, "_truck18t"); + vehicleTypesParcel = selector.getPossibleVehicleTypes(freightDemandDataRelationParcel, "", CarrierCapabilities.FleetSize.FINITE); + assertEquals(1, vehicleTypesParcel.size()); + assertTrue(vehicleTypesParcel.contains("mercedes313_parcel")); + + List 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 vehicleTypesRest = selector.getPossibleVehicleTypes(freightDemandDataRelationRest, ""); + List 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")); }