Skip to content

Commit

Permalink
change name
Browse files Browse the repository at this point in the history
  • Loading branch information
rewertvsp committed Feb 22, 2024
1 parent 0a28603 commit 7c8b142
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,18 @@ public Integer call() throws Exception {
.createInputDataDistribution(output, landuseCategoriesAndDataConnection, inputDataDirectory,
usedLanduseConfiguration.toString(), indexLanduse, indexZones,
indexBuildings, indexInvestigationAreaRegions, shapeFileZoneNameColumn, buildingsPerZone);
//TODO regionLinksMap umbenennen, da es alle Links einer Zone enthält
Map<String, Map<Id<Link>, Link>> regionLinksMap = filterLinksForZones(scenario, indexZones, buildingsPerZone);
Map<String, Map<Id<Link>, Link>> linksPerZone = filterLinksForZones(scenario, indexZones, buildingsPerZone);

switch (usedSmallScaleCommercialTrafficType) {
case commercialPersonTraffic, goodsTraffic ->
createCarriersAndDemand(output, scenario, resultingDataPerZone, regionLinksMap,
createCarriersAndDemand(output, scenario, resultingDataPerZone, linksPerZone,
usedSmallScaleCommercialTrafficType.toString(),
includeExistingModels);
case completeSmallScaleCommercialTraffic -> {
createCarriersAndDemand(output, scenario, resultingDataPerZone, regionLinksMap, "commercialPersonTraffic",
createCarriersAndDemand(output, scenario, resultingDataPerZone, linksPerZone, "commercialPersonTraffic",
includeExistingModels);
includeExistingModels = false; // because already included in the step before
createCarriersAndDemand(output, scenario, resultingDataPerZone, regionLinksMap, "goodsTraffic",
createCarriersAndDemand(output, scenario, resultingDataPerZone, linksPerZone, "goodsTraffic",
includeExistingModels);
}
default -> throw new RuntimeException("No traffic type selected.");
Expand All @@ -274,7 +273,7 @@ public Integer call() throws Exception {
new CarrierPlanWriter(CarriersUtils.addOrGetCarriers(scenario))
.write(scenario.getConfig().controller().getOutputDirectory() + "/"
+ scenario.getConfig().controller().getRunId() + ".output_CarrierDemand.xml");
solveSeparatedVRPs(scenario, regionLinksMap);
solveSeparatedVRPs(scenario, linksPerZone);
}
}
if (config.controller().getRunId() == null)
Expand Down Expand Up @@ -438,7 +437,7 @@ private void solveSeparatedVRPs(Scenario originalScenario, Map<String, Map<Id<Li

private void createCarriersAndDemand(Path output, Scenario scenario,
Map<String, Object2DoubleMap<String>> resultingDataPerZone,
Map<String, Map<Id<Link>, Link>> regionLinksMap, String smallScaleCommercialTrafficType,
Map<String, Map<Id<Link>, Link>> linksPerZone, String smallScaleCommercialTrafficType,
boolean includeExistingModels) throws Exception {

ArrayList<String> modesORvehTypes;
Expand All @@ -458,13 +457,13 @@ else if (smallScaleCommercialTrafficType.equals("commercialPersonTraffic"))
.createTrafficVolume_stop(resultingDataPerZone, output, sample, modesORvehTypes, smallScaleCommercialTrafficType);

if (includeExistingModels) {
SmallScaleCommercialTrafficUtils.readExistingModels(scenario, sample, regionLinksMap);
TrafficVolumeGeneration.reduceDemandBasedOnExistingCarriers(scenario, regionLinksMap, smallScaleCommercialTrafficType,
SmallScaleCommercialTrafficUtils.readExistingModels(scenario, sample, linksPerZone);
TrafficVolumeGeneration.reduceDemandBasedOnExistingCarriers(scenario, linksPerZone, smallScaleCommercialTrafficType,
trafficVolumePerTypeAndZone_start, trafficVolumePerTypeAndZone_stop);
}
final TripDistributionMatrix odMatrix = createTripDistribution(trafficVolumePerTypeAndZone_start,
trafficVolumePerTypeAndZone_stop, smallScaleCommercialTrafficType, scenario, output, regionLinksMap);
createCarriers(scenario, odMatrix, resultingDataPerZone, smallScaleCommercialTrafficType, regionLinksMap);
trafficVolumePerTypeAndZone_stop, smallScaleCommercialTrafficType, scenario, output, linksPerZone);
createCarriers(scenario, odMatrix, resultingDataPerZone, smallScaleCommercialTrafficType, linksPerZone);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ static void writeResultOfDataDistribution(Map<String, Object2DoubleMap<String>>
log.info("The data distribution is finished and written to: " + outputFileInOutputFolder);
}

static Id<Link> findNearestPossibleLink(String zone, List<String> noPossibleLinks, Map<String, Map<Id<Link>, Link>> regionLinksMap,
static Id<Link> findNearestPossibleLink(String zone, List<String> noPossibleLinks, Map<String, Map<Id<Link>, Link>> linksPerZone,
Id<Link> newLink, Coord centroidPointOfBuildingPolygon, int numberOfPossibleLinks) {
double minDistance = Double.MAX_VALUE;
searchLink:
for (Link possibleLink : regionLinksMap.get(zone).values()) {
for (Link possibleLink : linksPerZone.get(zone).values()) {
if (possibleLink.getToNode().getOutLinks() == null)
continue;
if (noPossibleLinks != null && numberOfPossibleLinks > noPossibleLinks.size())
Expand All @@ -157,7 +157,7 @@ static Id<Link> findNearestPossibleLink(String zone, List<String> noPossibleLink
}
}
if (newLink == null && numberOfPossibleLinks > 0) {
for (Link possibleLink : regionLinksMap.get(zone).values()) {
for (Link possibleLink : linksPerZone.get(zone).values()) {
double distance = NetworkUtils.getEuclideanDistance(centroidPointOfBuildingPolygon,
(Coord) possibleLink.getAttributes().getAttribute("newCoord"));
if (distance < minDistance) {
Expand Down Expand Up @@ -314,7 +314,7 @@ static String getSampleNameOfOutputFolder(double sample) {
* dispersedTraffic will be added additionally.
*/
static void readExistingModels(Scenario scenario, double sampleScenario,
Map<String, Map<Id<Link>, Link>> regionLinksMap) throws Exception {
Map<String, Map<Id<Link>, Link>> linksPerZone) throws Exception {

Path existingModelsFolder = Path.of(scenario.getConfig().getContext().toURI()).getParent().resolve("existingModels");
String locationOfExistingModels = existingModelsFolder.resolve("existingModels.csv").toString();
Expand Down Expand Up @@ -515,7 +515,7 @@ else if (!carrier.getShipments().isEmpty())

List<String> startAreas = new ArrayList<>();
for (ScheduledTour tour : newCarrier.getSelectedPlan().getScheduledTours()) {
String tourStartZone = findZoneOfLink(tour.getTour().getStartLinkId(), regionLinksMap);
String tourStartZone = findZoneOfLink(tour.getTour().getStartLinkId(), linksPerZone);
if (!startAreas.contains(tourStartZone))
startAreas.add(tourStartZone);
}
Expand Down Expand Up @@ -543,9 +543,9 @@ else if (!carrier.getShipments().isEmpty())
/**
* Find the zone where the link is located
*/
static String findZoneOfLink(Id<Link> linkId, Map<String, Map<Id<Link>, Link>> regionLinksMap) {
for (String area : regionLinksMap.keySet()) {
if (regionLinksMap.get(area).containsKey(linkId))
static String findZoneOfLink(Id<Link> linkId, Map<String, Map<Id<Link>, Link>> linksPerZone) {
for (String area : linksPerZone.keySet()) {
if (linksPerZone.get(area).containsKey(linkId))
return area;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ static void setInputParameters(String smallScaleCommercialTrafficType) {
* Reduces the traffic volumes based on the added existing models.
*
* @param scenario scenario
* @param regionLinksMap links for each zone
* @param linksPerZone links for each zone
* @param smallScaleCommercialTrafficType used trafficType (commercialPersonTraffic or goodsTraffic)
* @param trafficVolumePerTypeAndZone_start trafficVolume for start potentials for each zone
* @param trafficVolumePerTypeAndZone_stop trafficVolume for stop potentials for each zone
*/
static void reduceDemandBasedOnExistingCarriers(Scenario scenario,
Map<String, Map<Id<Link>, Link>> regionLinksMap, String smallScaleCommercialTrafficType,
Map<String, Map<Id<Link>, Link>> linksPerZone, String smallScaleCommercialTrafficType,
Map<TrafficVolumeKey, Object2DoubleMap<Integer>> trafficVolumePerTypeAndZone_start,
Map<TrafficVolumeKey, Object2DoubleMap<Integer>> trafficVolumePerTypeAndZone_stop) {

Expand All @@ -290,11 +290,11 @@ static void reduceDemandBasedOnExistingCarriers(Scenario scenario,
if (carrier.getSelectedPlan() != null) {
for (ScheduledTour tour : carrier.getSelectedPlan().getScheduledTours()) {
String startZone = SmallScaleCommercialTrafficUtils.findZoneOfLink(tour.getTour().getStartLinkId(),
regionLinksMap);
linksPerZone);
for (TourElement tourElement : tour.getTour().getTourElements()) {
if (tourElement instanceof ServiceActivity service) {
String stopZone = SmallScaleCommercialTrafficUtils.findZoneOfLink(service.getLocation(),
regionLinksMap);
linksPerZone);
try {
reduceVolumeForThisExistingJobElement(trafficVolumePerTypeAndZone_start,
trafficVolumePerTypeAndZone_stop, modeORvehType, purpose, startZone, stopZone);
Expand All @@ -306,9 +306,9 @@ static void reduceDemandBasedOnExistingCarriers(Scenario scenario,
}
if (tourElement instanceof Pickup pickup) {
startZone = SmallScaleCommercialTrafficUtils.findZoneOfLink(pickup.getShipment().getFrom(),
regionLinksMap);
linksPerZone);
String stopZone = SmallScaleCommercialTrafficUtils.findZoneOfLink(pickup.getShipment().getTo(),
regionLinksMap);
linksPerZone);
try {
reduceVolumeForThisExistingJobElement(trafficVolumePerTypeAndZone_start,
trafficVolumePerTypeAndZone_stop, modeORvehType, purpose, startZone, stopZone);
Expand All @@ -325,13 +325,13 @@ static void reduceDemandBasedOnExistingCarriers(Scenario scenario,
List<String> possibleStartAreas = new ArrayList<>();
for (CarrierVehicle vehicle : carrier.getCarrierCapabilities().getCarrierVehicles().values()) {
possibleStartAreas
.add(SmallScaleCommercialTrafficUtils.findZoneOfLink(vehicle.getLinkId(), regionLinksMap));
.add(SmallScaleCommercialTrafficUtils.findZoneOfLink(vehicle.getLinkId(), linksPerZone));
}
for (CarrierService service : carrier.getServices().values()) {
String startZone = (String) possibleStartAreas.toArray()[MatsimRandom.getRandom()
.nextInt(possibleStartAreas.size())];
String stopZone = SmallScaleCommercialTrafficUtils.findZoneOfLink(service.getLocationLinkId(),
regionLinksMap);
linksPerZone);
try {
reduceVolumeForThisExistingJobElement(trafficVolumePerTypeAndZone_start,
trafficVolumePerTypeAndZone_stop, modeORvehType, purpose, startZone, stopZone);
Expand All @@ -344,9 +344,9 @@ static void reduceDemandBasedOnExistingCarriers(Scenario scenario,
} else if (!carrier.getShipments().isEmpty()) {
for (CarrierShipment shipment : carrier.getShipments().values()) {
String startZone = SmallScaleCommercialTrafficUtils.findZoneOfLink(shipment.getFrom(),
regionLinksMap);
linksPerZone);
String stopZone = SmallScaleCommercialTrafficUtils.findZoneOfLink(shipment.getTo(),
regionLinksMap);
linksPerZone);
try {
reduceVolumeForThisExistingJobElement(trafficVolumePerTypeAndZone_start,
trafficVolumePerTypeAndZone_stop, modeORvehType, purpose, startZone, stopZone);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,18 @@ private TripDistributionMatrix(Builder builder) {
* @param modeORvehType selected mode or vehicle type
* @param purpose selected purpose
* @param smallScaleCommercialTrafficType goodsTraffic or commercialPersonTraffic
* @param regionLinksMap links in each zone
* @param shapeFileZoneNameColumn Name of the unique column of the name/Id of each zone in the zones shape file
* @param linksPerZone links in each zone
* @param shapeFileZoneNameColumn Name of the unique column of the name/Id of each zone in the zones shape file
*/
void setTripDistributionValue(String startZone, String stopZone, String modeORvehType, Integer purpose, String smallScaleCommercialTrafficType, Network network,
Map<String, Map<Id<Link>, Link>> regionLinksMap, double resistanceFactor, String shapeFileZoneNameColumn) {
Map<String, Map<Id<Link>, Link>> linksPerZone, double resistanceFactor, String shapeFileZoneNameColumn) {
double volumeStart = trafficVolume_start.get(TrafficVolumeGeneration.makeTrafficVolumeKey(startZone, modeORvehType)).getDouble(purpose);
double volumeStop = trafficVolume_stop.get(TrafficVolumeGeneration.makeTrafficVolumeKey(stopZone, modeORvehType)).getDouble(purpose);
int roundedVolume;
if (volumeStart != 0 && volumeStop != 0) {

double resistanceValue = getResistanceFunktionValue(startZone, stopZone, network, regionLinksMap, resistanceFactor, shapeFileZoneNameColumn);
double gravityConstantA = getGravityConstant(stopZone, trafficVolume_start, modeORvehType, purpose, network, regionLinksMap,
double resistanceValue = getResistanceFunktionValue(startZone, stopZone, network, linksPerZone, resistanceFactor, shapeFileZoneNameColumn);
double gravityConstantA = getGravityConstant(stopZone, trafficVolume_start, modeORvehType, purpose, network, linksPerZone,
resistanceFactor, shapeFileZoneNameColumn);
roundingError.computeIfAbsent(stopZone, (k) -> new Object2DoubleOpenHashMap<>());

Expand Down Expand Up @@ -339,10 +339,10 @@ Integer getTripDistributionValue(String startZone, String stopZone, String modeO
*
* @param startZone start zone
* @param stopZone stop zone
* @param regionLinksMap links for each zone
* @param linksPerZone links for each zone
* @param shapeFileZoneNameColumn Name of the unique column of the name/Id of each zone in the zones shape file
*/
private Double getResistanceFunktionValue(String startZone, String stopZone, Network network, Map<String, Map<Id<Link>, Link>> regionLinksMap,
private Double getResistanceFunktionValue(String startZone, String stopZone, Network network, Map<String, Map<Id<Link>, Link>> linksPerZone,
double resistanceFactor, String shapeFileZoneNameColumn) {

//if false the calculation is faster; e.g. for debugging
Expand Down Expand Up @@ -371,8 +371,8 @@ private Double getResistanceFunktionValue(String startZone, String stopZone, Net
} else {

if (useNetworkRoutesForResistanceFunction) {
Location startLocation = Location.newInstance(regionLinksMap.get(startZone).keySet().iterator().next().toString());
Location stopLocation = Location.newInstance(regionLinksMap.get(stopZone).keySet().iterator().next().toString());
Location startLocation = Location.newInstance(linksPerZone.get(startZone).keySet().iterator().next().toString());
Location stopLocation = Location.newInstance(linksPerZone.get(stopZone).keySet().iterator().next().toString());
Vehicle exampleVehicle = getExampleVehicle(startLocation);
// distance = netBasedCosts.getDistance(startLocation, stopLocation, 21600., exampleVehicle);
travelCosts = netBasedCosts.getTransportCost(startLocation, stopLocation, 21600., null, exampleVehicle);
Expand Down Expand Up @@ -450,13 +450,13 @@ private VehicleImpl getExampleVehicle(Location fromId) {
* @param trafficVolume volume of the traffic
* @param modeORvehType selected mode or vehicle type
* @param purpose selected purpose
* @param regionLinksMap links for each zone
* @param linksPerZone links for each zone
* @param shapeFileZoneNameColumn Name of the unique column of the name/Id of each zone in the zones shape file
* @return gravity constant
*/
private double getGravityConstant(String baseZone,
Map<TrafficVolumeKey, Object2DoubleMap<Integer>> trafficVolume, String modeORvehType,
Integer purpose, Network network, Map<String, Map<Id<Link>, Link>> regionLinksMap, double resistanceFactor, String shapeFileZoneNameColumn) {
Integer purpose, Network network, Map<String, Map<Id<Link>, Link>> linksPerZone, double resistanceFactor, String shapeFileZoneNameColumn) {

GravityConstantKey gravityKey = makeGravityKey(baseZone, modeORvehType, purpose);
if (!gravityConstantACache.containsKey(gravityKey)) {
Expand All @@ -468,7 +468,7 @@ private double getGravityConstant(String baseZone,
continue;
else {
double resistanceValue = getResistanceFunktionValue(baseZone, trafficVolumeKey.getZone(), network,
regionLinksMap, resistanceFactor, shapeFileZoneNameColumn);
linksPerZone, resistanceFactor, shapeFileZoneNameColumn);
sum = sum + (volume * resistanceValue);
}
}
Expand Down

0 comments on commit 7c8b142

Please sign in to comment.