Skip to content

Commit

Permalink
Merge branch 'master' into noiseSubArea
Browse files Browse the repository at this point in the history
  • Loading branch information
tschlenther authored Nov 7, 2024
2 parents 2aef5f6 + 52efba4 commit 829972d
Show file tree
Hide file tree
Showing 68 changed files with 669 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.matsim.api.core.v01.events.Event;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.emissions.Pollutant;
import org.matsim.core.utils.io.XmlUtils;
import org.matsim.vehicles.Vehicle;

import java.util.Map;
Expand Down Expand Up @@ -67,4 +68,20 @@ public Map<String, String> getAttributes() {
}
return attributes;
}

@Override
public void writeAsXML(StringBuilder out) {
// Writes common attributes
writeXMLStart(out);

XmlUtils.writeEncodedAttributeKeyValue(out, ATTRIBUTE_LINK_ID, this.linkId.toString());
XmlUtils.writeEncodedAttributeKeyValue(out, ATTRIBUTE_VEHICLE_ID, this.vehicleId.toString());

for (Map.Entry<Pollutant, Double> entry : emissions.entrySet()) {
out.append(entry.getKey().name()).append("=\"");
out.append((double) entry.getValue()).append("\" ");
}

writeXMLEnd(out);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public void handleEvent(Event event) {
}

private VehicleType createVehicleType() {
VehicleType vehicleType = VehicleUtils.createVehicleType(Id.create("dieselCarFullSpecified", VehicleType.class));
VehicleType vehicleType = VehicleUtils.createVehicleType(Id.create("dieselCarFullSpecified", VehicleType.class), TransportMode.car);
EngineInformation engineInformation = vehicleType.getEngineInformation();
VehicleUtils.setHbefaVehicleCategory(engineInformation, "PASSENGER_CAR");
VehicleUtils.setHbefaTechnology(engineInformation, "diesel");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.core.gbl.Gbl;
import org.matsim.core.utils.io.MatsimXmlParser;
import org.matsim.vehicles.CostInformation;
Expand Down Expand Up @@ -54,6 +55,9 @@ public void startTag( String name, Attributes attributes, Stack<String> context
if(name.equals("vehicleType")){
Id<VehicleType> currentTypeId = Id.create( attributes.getValue( "id" ), VehicleType.class );
this.currentType = VehicleUtils.getFactory().createVehicleType( currentTypeId ) ;
// If no network mode is given, assume car, this is to be backwards compatible
// The v2 format will not make this assumption, and the network mode will be required
this.currentType.setNetworkMode(TransportMode.car);
}
if(name.equals("allowableWeight")){
// String weight = atts.getValue("weight");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.freight.carriers.CarrierVehicleTypes;
import org.matsim.testcases.MatsimTestUtils;
import org.matsim.vehicles.*;
Expand Down Expand Up @@ -53,6 +54,7 @@ public void setUp() {
VehicleCapacity vehicleCapacity = mediumType.getCapacity();
vehicleCapacity.setWeightInTons( 30 );
mediumType.setDescription( "Medium Vehicle" ).setMaximumVelocity( 13.89 );
mediumType.setNetworkMode(TransportMode.truck);
types = new CarrierVehicleTypes();
types.getVehicleTypes().put( mediumType.getId(), mediumType );
}
Expand All @@ -77,6 +79,7 @@ public void setUp() {
capacity.setWeightInTons( 16 ) ;
// VehicleType smallType = CarriersUtils.CarrierVehicleTypeBuilder.newInstance( smallTypeId, mediumType )
smallType.setDescription( "Small Vehicle" ).setMaximumVelocity( 10.0 ) ;
smallType.setNetworkMode(TransportMode.car);
types.getVehicleTypes().put( smallType.getId(), smallType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.contrib.minibus.PConfigGroup;
import org.matsim.pt.transitSchedule.api.Departure;
import org.matsim.pt.transitSchedule.api.TransitLine;
Expand All @@ -31,15 +32,15 @@

/**
* Generates vehicles for a whole transit schedule
*
*
* @author aneumann
*
*/
class PVehiclesFactory {

@SuppressWarnings("unused")
private final static Logger log = LogManager.getLogger(PVehiclesFactory.class);

private final PConfigGroup pConfig;

public PVehiclesFactory(PConfigGroup pConfig) {
Expand All @@ -48,11 +49,11 @@ public PVehiclesFactory(PConfigGroup pConfig) {

/**
* Create vehicles for each departure of the given transit schedule.
*
*
* @return Vehicles used by paratranit lines
*/
public Vehicles createVehicles(TransitSchedule pTransitSchedule){
Vehicles vehicles = VehicleUtils.createVehiclesContainer();
public Vehicles createVehicles(TransitSchedule pTransitSchedule){
Vehicles vehicles = VehicleUtils.createVehiclesContainer();
VehiclesFactory vehFactory = vehicles.getFactory();
VehicleType vehType = vehFactory.createVehicleType(Id.create(this.pConfig.getPIdentifier(), VehicleType.class));
// VehicleCapacity capacity = new VehicleCapacity();
Expand All @@ -61,11 +62,12 @@ public Vehicles createVehicles(TransitSchedule pTransitSchedule){
// vehType.setCapacity(capacity);
vehType.setPcuEquivalents(this.pConfig.getPassengerCarEquivalents());
vehType.setMaximumVelocity(this.pConfig.getVehicleMaximumVelocity());
vehType.setNetworkMode(TransportMode.car);
VehicleUtils.setAccessTime(vehType, this.pConfig.getDelayPerBoardingPassenger());
VehicleUtils.setEgressTime(vehType, this.pConfig.getDelayPerAlightingPassenger());
VehicleUtils.setDoorOperationMode(vehType, this.pConfig.getDoorOperationMode()) ;
vehicles.addVehicleType( vehType);

for (TransitLine line : pTransitSchedule.getTransitLines().values()) {
for (TransitRoute route : line.getRoutes().values()) {
for (Departure departure : route.getDepartures().values()) {
Expand All @@ -76,7 +78,7 @@ public Vehicles createVehicles(TransitSchedule pTransitSchedule){
}
}
}

return vehicles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
* Default dashboards suited for every run.
Expand All @@ -19,7 +20,7 @@ public List<Dashboard> getDashboards(Config config, SimWrapper simWrapper) {
List<Dashboard> result = new ArrayList<>(List.of(
new OverviewDashboard(),
new TripDashboard(),
new TrafficDashboard()
new TrafficDashboard(Set.copyOf(config.qsim().getMainModes()))
));

if (config.transit().isUseTransit()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,61 +34,12 @@ public List<Carrier> createListOfCarrierWithUnhandledJobs(Scenario scenario){
return carriersWithUnhandledJobs;
}

public int getServiceTimePerStop(Carrier carrier, GenerateSmallScaleCommercialTrafficDemand.CarrierAttributes carrierAttributes, int additionalTravelBufferPerIterationInMinutes) {
GenerateSmallScaleCommercialTrafficDemand.ServiceDurationPerCategoryKey key = null;
if (carrierAttributes.smallScaleCommercialTrafficType().equals(
GenerateSmallScaleCommercialTrafficDemand.SmallScaleCommercialTrafficType.commercialPersonTraffic.toString()))
key = GenerateSmallScaleCommercialTrafficDemand.makeServiceDurationPerCategoryKey(carrierAttributes.selectedStartCategory(), null, carrierAttributes.smallScaleCommercialTrafficType());
else if (carrierAttributes.smallScaleCommercialTrafficType().equals(
GenerateSmallScaleCommercialTrafficDemand.SmallScaleCommercialTrafficType.goodsTraffic.toString())) {
key = GenerateSmallScaleCommercialTrafficDemand.makeServiceDurationPerCategoryKey(carrierAttributes.selectedStartCategory(), carrierAttributes.modeORvehType(), carrierAttributes.smallScaleCommercialTrafficType());
}

//possible new Version by Ricardo
double maxVehicleAvailability = carrier.getCarrierCapabilities().getCarrierVehicles().values().stream().mapToDouble(vehicle -> vehicle.getLatestEndTime() - vehicle.getEarliestStartTime()).max().orElse(0);
int usedTravelTimeBuffer = additionalTravelBufferPerIterationInMinutes * 60; // buffer for the driving time; for unsolved carriers the buffer will be increased over time
for (int j = 0; j < 200; j++) {
GenerateSmallScaleCommercialTrafficDemand.DurationsBounds serviceDurationBounds = generator.getServiceDurationTimeSelector().get(key).sample();

for (int i = 0; i < 10; i++) {
int serviceDurationLowerBound = serviceDurationBounds.minDuration();
int serviceDurationUpperBound = serviceDurationBounds.maxDuration();
int possibleValue = rnd.nextInt(serviceDurationLowerBound * 60, serviceDurationUpperBound * 60);
// checks if the service duration will not exceed the vehicle availability including the buffer
if (possibleValue + usedTravelTimeBuffer <= maxVehicleAvailability)
return possibleValue;
}
if (j > 100){
CarrierVehicle carrierVehicleToChange = carrier.getCarrierCapabilities().getCarrierVehicles().values().stream().sorted(Comparator.comparingDouble(vehicle -> vehicle.getLatestEndTime() - vehicle.getEarliestStartTime())).toList().getFirst();
log.info("Changing vehicle availability for carrier {}. Old maxVehicleAvailability: {}", carrier.getId(), maxVehicleAvailability);
int tourDuration = 0;
int vehicleStartTime = 0;
int vehicleEndTime = 0;
while (tourDuration < maxVehicleAvailability) {
GenerateSmallScaleCommercialTrafficDemand.TourStartAndDuration t = generator.getTourDistribution().get(carrierAttributes.smallScaleCommercialTrafficType()).sample();
vehicleStartTime = t.getVehicleStartTime();
tourDuration = t.getVehicleTourDuration();
vehicleEndTime = vehicleStartTime + tourDuration;
}
CarrierVehicle newCarrierVehicle = CarrierVehicle.Builder.newInstance(carrierVehicleToChange.getId(), carrierVehicleToChange.getLinkId(),
carrierVehicleToChange.getType()).setEarliestStart(vehicleStartTime).setLatestEnd(vehicleEndTime).build();
carrier.getCarrierCapabilities().getCarrierVehicles().remove(carrierVehicleToChange.getId());
carrier.getCarrierCapabilities().getCarrierVehicles().put(newCarrierVehicle.getId(), newCarrierVehicle);
maxVehicleAvailability = carrier.getCarrierCapabilities().getCarrierVehicles().values().stream().mapToDouble(vehicle -> vehicle.getLatestEndTime() - vehicle.getEarliestStartTime()).max().orElse(0);
log.info("New maxVehicleAvailability: {}", maxVehicleAvailability);
}
}

throw new RuntimeException("No possible service duration found for employee category '" + carrierAttributes.selectedStartCategory() + "' and mode '"
+ carrierAttributes.modeORvehType() + "' in traffic type '" + carrierAttributes.smallScaleCommercialTrafficType() + "'");
}

/**
* Redraws the service-durations of all {@link CarrierService}s of the given {@link Carrier}.
*/
private void redrawAllServiceDurations(Carrier carrier, GenerateSmallScaleCommercialTrafficDemand.CarrierAttributes carrierAttributes, int additionalTravelBufferPerIterationInMinutes) {
for (CarrierService service : carrier.getServices().values()) {
double newServiceDuration = getServiceTimePerStop(carrier, carrierAttributes, additionalTravelBufferPerIterationInMinutes);
double newServiceDuration = generator.getServiceTimePerStop(carrier, carrierAttributes, additionalTravelBufferPerIterationInMinutes);
CarrierService redrawnService = CarrierService.Builder.newInstance(service.getId(), service.getLocationLinkId())
.setServiceDuration(newServiceDuration).setServiceStartTimeWindow(service.getServiceStartTimeWindow()).build();
carrier.getServices().put(redrawnService.getId(), redrawnService);
Expand All @@ -105,7 +56,6 @@ public void tryToSolveAllCarriersCompletely(Scenario scenario, List<Carrier> non
for (Carrier nonCompleteSolvedCarrier : nonCompleteSolvedCarriers) {
//Delete old plan of carrier
nonCompleteSolvedCarrier.clearPlans();
nonCompleteSolvedCarrier.setSelectedPlan(null);
GenerateSmallScaleCommercialTrafficDemand.CarrierAttributes carrierAttributes = generator.getCarrierId2carrierAttributes().get(nonCompleteSolvedCarrier.getId());

// Generate new services. The new service batch should have a smaller sum of serviceDurations than before (or otherwise it will not change anything)
Expand Down Expand Up @@ -133,4 +83,61 @@ public void tryToSolveAllCarriersCompletely(Scenario scenario, List<Carrier> non
}
}

/**
* Change the service duration for a given carrier, because the service could not be handled in the last solution.
*
* @param carrier The carrier for which we generate the serviceTime
* @param carrierAttributes attributes of the carrier to generate the service time for.
* @param key key for the service duration
* @param additionalTravelBufferPerIterationInMinutes additional buffer for the travel time
* @return new service duration
*/
@Override
public int changeServiceTimePerStop(Carrier carrier, GenerateSmallScaleCommercialTrafficDemand.CarrierAttributes carrierAttributes,
GenerateSmallScaleCommercialTrafficDemand.ServiceDurationPerCategoryKey key,
int additionalTravelBufferPerIterationInMinutes) {

double maxVehicleAvailability = carrier.getCarrierCapabilities().getCarrierVehicles().values().stream().mapToDouble(vehicle -> vehicle.getLatestEndTime() - vehicle.getEarliestStartTime()).max().orElse(0);
int usedTravelTimeBuffer = additionalTravelBufferPerIterationInMinutes * 60; // buffer for the driving time; for unsolved carriers the buffer will be increased over time
for (int j = 0; j < 200; j++) {
if (generator.getServiceDurationTimeSelector().get(key) == null) {
System.out.println("key: " + key);
System.out.println(generator.getServiceDurationTimeSelector().keySet());
throw new RuntimeException("No service duration found for employee category '" + carrierAttributes.selectedStartCategory() + "' and mode '"
+ carrierAttributes.modeORvehType() + "' in traffic type '" + carrierAttributes.smallScaleCommercialTrafficType() + "'");
}
GenerateSmallScaleCommercialTrafficDemand.DurationsBounds serviceDurationBounds = generator.getServiceDurationTimeSelector().get(key).sample();

for (int i = 0; i < 10; i++) {
int serviceDurationLowerBound = serviceDurationBounds.minDuration();
int serviceDurationUpperBound = serviceDurationBounds.maxDuration();
int possibleValue = rnd.nextInt(serviceDurationLowerBound * 60, serviceDurationUpperBound * 60);
// checks if the service duration will not exceed the vehicle availability including the buffer
if (possibleValue + usedTravelTimeBuffer <= maxVehicleAvailability)
return possibleValue;
}
if (j > 100){
CarrierVehicle carrierVehicleToChange = carrier.getCarrierCapabilities().getCarrierVehicles().values().stream().sorted(Comparator.comparingDouble(vehicle -> vehicle.getLatestEndTime() - vehicle.getEarliestStartTime())).toList().getFirst();
log.info("Changing vehicle availability for carrier {}. Old maxVehicleAvailability: {}", carrier.getId(), maxVehicleAvailability);
int tourDuration = 0;
int vehicleStartTime = 0;
int vehicleEndTime = 0;
while (tourDuration < maxVehicleAvailability) {
GenerateSmallScaleCommercialTrafficDemand.TourStartAndDuration t = generator.getTourDistribution().get(carrierAttributes.smallScaleCommercialTrafficType()).sample();
vehicleStartTime = t.getVehicleStartTime();
tourDuration = t.getVehicleTourDuration();
vehicleEndTime = vehicleStartTime + tourDuration;
}
CarrierVehicle newCarrierVehicle = CarrierVehicle.Builder.newInstance(carrierVehicleToChange.getId(), carrierVehicleToChange.getLinkId(),
carrierVehicleToChange.getType()).setEarliestStart(vehicleStartTime).setLatestEnd(vehicleEndTime).build();
carrier.getCarrierCapabilities().getCarrierVehicles().remove(carrierVehicleToChange.getId());
carrier.getCarrierCapabilities().getCarrierVehicles().put(newCarrierVehicle.getId(), newCarrierVehicle);
maxVehicleAvailability = carrier.getCarrierCapabilities().getCarrierVehicles().values().stream().mapToDouble(vehicle -> vehicle.getLatestEndTime() - vehicle.getEarliestStartTime()).max().orElse(0);
log.info("New maxVehicleAvailability: {}", maxVehicleAvailability);
}
}

throw new RuntimeException("No possible service duration found for employee category '" + carrierAttributes.selectedStartCategory() + "' and mode '"
+ carrierAttributes.modeORvehType() + "' in traffic type '" + carrierAttributes.smallScaleCommercialTrafficType() + "'");
}
}
Loading

0 comments on commit 829972d

Please sign in to comment.