Skip to content

Commit

Permalink
Merge branch 'master' into rejectionAnalysis
Browse files Browse the repository at this point in the history
  • Loading branch information
tschlenther committed Sep 28, 2023
2 parents 789ea36 + 5120823 commit f4ad924
Show file tree
Hide file tree
Showing 255 changed files with 35,833 additions and 983 deletions.
1 change: 1 addition & 0 deletions .github/workflows/verify-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
- contribs/socnetsim
- contribs/sumo
- contribs/pseudosimulation
- contribs/railsim
- contribs/roadpricing
- contribs/analysis
- contribs/eventsBasedPTRouter
Expand Down
1 change: 1 addition & 0 deletions contribs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The MATSim core development team cannot make any guarantee that these extensions
| [parking](parking/README.md) | Parking infrastructure and supply constraints
| [protobuf](protobuf/README.md) | Protocol buffer implementation and converter for the MATSim event infrastructure
| [pseudosimulation](pseudosimulation/README.md) | Pseudo-simulation to speed-up simulation times
| [railsim](railsim/README.md) | A large-scale hybrid micro- and mesoscopic simulation approach for railway operation
| [roadpricing](roadpricing/README.md) | Functionality to simulate different road-pricing scenarios in MATSim
| [shared_mobility](shared_mobility/README.md) | Simulate human-driven shared mobility (i.e., micromobility)
| [signals](signals/README.md) | Simulate traffic lights microscopically
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public Integer call() throws Exception {
v = new Coord(coord.getX() - y * m, coord.getY() + x * m);
}

v = CoordUtils.round(v);

mapping.put(act.getCoord(), v);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
import org.matsim.application.MATSimAppCommand;
import org.matsim.application.options.CsvOptions;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.utils.geometry.CoordUtils;
import picocli.CommandLine;

import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;

@CommandLine.Command(
name = "extract-home-coordinates",
description = "Extract the home coordinates of a person"
name = "extract-home-coordinates",
description = "Extract the home coordinates of a person"
)
public class ExtractHomeCoordinates implements MATSimAppCommand {
public final class ExtractHomeCoordinates implements MATSimAppCommand {

private static final Logger log = LogManager.getLogger(ExtractHomeCoordinates.class);

Expand All @@ -34,6 +35,29 @@ public class ExtractHomeCoordinates implements MATSimAppCommand {
@CommandLine.Mixin
private CsvOptions options = new CsvOptions();

/**
* Set and return home coordinate of this person. Can be null if no home activity is known.
*/
public static Coord setHomeCoordinate(Person person) {
for (Plan plan : person.getPlans()) {
for (PlanElement planElement : plan.getPlanElements()) {
if (planElement instanceof Activity) {
String actType = ((Activity) planElement).getType();
if (actType.startsWith("home")) {
Coord homeCoord = CoordUtils.round(((Activity) planElement).getCoord());

person.getAttributes().putAttribute("home_x", homeCoord.getX());
person.getAttributes().putAttribute("home_y", homeCoord.getY());

return homeCoord;
}
}
}
}

return null;
}

@Override
public Integer call() throws Exception {

Expand All @@ -42,24 +66,9 @@ public Integer call() throws Exception {
Map<Person, Coord> coords = new LinkedHashMap<>();

for (Person person : population.getPersons().values()) {
outer:
for (Plan plan : person.getPlans()) {
for (PlanElement planElement : plan.getPlanElements()) {
if (planElement instanceof Activity) {
String actType = ((Activity) planElement).getType();
if (actType.startsWith("home")) {
Coord homeCoord = ((Activity) planElement).getCoord();
coords.put(person, homeCoord);

person.getAttributes().putAttribute("home_x", homeCoord.getX());
person.getAttributes().putAttribute("home_y", homeCoord.getY());

break outer;
}
}
}

}
Coord coord = setHomeCoordinate(person);
if (coord != null)
coords.put(person, coord);
}

if (csv != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.matsim.application.options.ShpOptions;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.utils.geometry.CoordUtils;
import org.matsim.core.utils.geometry.geotools.MGC;
import picocli.CommandLine;

Expand Down Expand Up @@ -93,7 +94,7 @@ public Integer call() throws Exception {

Coord newCoord = mapping.getOrDefault(coord, null);
if (newCoord == null) {
newCoord = landuse.select(crs.getInputCRS(),
newCoord = CoordUtils.round(landuse.select(crs.getInputCRS(),
() -> {
double x = rnd.nextDouble(-gridResolution / 2, gridResolution / 2);
double y = rnd.nextDouble(-gridResolution / 2, gridResolution / 2);
Expand All @@ -103,7 +104,7 @@ public Integer call() throws Exception {
else
return new Coord(coord.getX() + x, coord.getY() + y);
}
);
));
mapping.put(coord, newCoord);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ private enum SmallScaleCommercialTrafficType {
commercialPersonTraffic, goodsTraffic, completeSmallScaleCommercialTraffic
}

@CommandLine.Parameters(arity = "1", paramLabel = "INPUT", description = "Path to the freight data directory")
private Path inputDataDirectory;
@CommandLine.Parameters(arity = "1", paramLabel = "INPUT", description = "Path to the config for small scale commercial generation")
private Path configPath;

@CommandLine.Option(names = "--sample", description = "Scaling factor of the small scale commercial traffic (0, 1)", required = true)
private double sample;
Expand Down Expand Up @@ -166,11 +166,11 @@ public static void main(String[] args) {
public Integer call() throws Exception {
Configurator.setLevel("org.matsim.core.utils.geometry.geotools.MGC", Level.ERROR);

String modelName = inputDataDirectory.getFileName().toString();
String modelName = configPath.getParent().getFileName().toString();

String sampleName = SmallScaleCommercialTrafficUtils.getSampleNameOfOutputFolder(sample);

Config config = readAndCheckConfig(inputDataDirectory, modelName, sampleName, output);
Config config = readAndCheckConfig(configPath, modelName, sampleName, output);

output = Path.of(config.controler().getOutputDirectory());

Expand All @@ -185,6 +185,8 @@ public Integer call() throws Exception {
throw new Exception(
"You set that existing models should included to the new model. This is only possible for a creation of the new carrier file and not by using an existing.");
freightConfigGroup = ConfigUtils.addOrGetModule(config, FreightConfigGroup.class);
if (config.vehicles() != null && freightConfigGroup.getCarriersVehicleTypesFile() == null)
freightConfigGroup.setCarriersVehicleTypesFile(config.vehicles().getVehiclesFile());
log.info("Load carriers from: " + freightConfigGroup.getCarriersFile());
FreightUtils.loadCarriersAccordingToFreightConfig(scenario);
}
Expand All @@ -194,6 +196,8 @@ public Integer call() throws Exception {
throw new Exception(
"You set that existing models should included to the new model. This is only possible for a creation of the new carrier file and not by using an existing.");
freightConfigGroup = ConfigUtils.addOrGetModule(config, FreightConfigGroup.class);
if (config.vehicles() != null && freightConfigGroup.getCarriersVehicleTypesFile() == null)
freightConfigGroup.setCarriersVehicleTypesFile(config.vehicles().getVehiclesFile());
log.info("Load carriers from: " + freightConfigGroup.getCarriersFile());
FreightUtils.loadCarriersAccordingToFreightConfig(scenario);
solveSeparatedVRPs(scenario, null);
Expand All @@ -209,6 +213,7 @@ public Integer call() throws Exception {
if (!Files.exists(shapeFileZonePath)) {
throw new Exception("Required districts shape file {} not found" + shapeFileZonePath.toString());
}
Path inputDataDirectory = Path.of(config.getContext().toURI()).getParent();
HashMap<String, Object2DoubleMap<String>> resultingDataPerZone = LanduseBuildingAnalysis
.createInputDataDistribution(output, landuseCategoriesAndDataConnection, inputDataDirectory,
usedLanduseConfiguration.toString(), shapeFileLandusePath, shapeFileZonePath,
Expand All @@ -221,13 +226,13 @@ public Integer call() throws Exception {
case commercialPersonTraffic, goodsTraffic ->
createCarriersAndDemand(output, scenario, shpZones, resultingDataPerZone, regionLinksMap,
usedSmallScaleCommercialTrafficType.toString(),
inputDataDirectory, includeExistingModels);
includeExistingModels);
case completeSmallScaleCommercialTraffic -> {
createCarriersAndDemand(output, scenario, shpZones, resultingDataPerZone, regionLinksMap, "commercialPersonTraffic",
inputDataDirectory, includeExistingModels);
includeExistingModels);
includeExistingModels = false; // because already included in the step before
createCarriersAndDemand(output, scenario, shpZones, resultingDataPerZone, regionLinksMap, "goodsTraffic",
inputDataDirectory, includeExistingModels);
includeExistingModels);
}
default -> throw new RuntimeException("No traffic type selected.");
}
Expand Down Expand Up @@ -393,7 +398,6 @@ private void solveSeparatedVRPs(Scenario originalScenario, Map<String, HashMap<I
private void createCarriersAndDemand(Path output, Scenario scenario, ShpOptions shpZones,
HashMap<String, Object2DoubleMap<String>> resultingDataPerZone,
Map<String, HashMap<Id<Link>, Link>> regionLinksMap, String smallScaleCommercialTrafficType,
Path inputDataDirectory,
boolean includeExistingModels) throws Exception {

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

if (includeExistingModels) {
SmallScaleCommercialTrafficUtils.readExistingModels(scenario, sample, inputDataDirectory, regionLinksMap);
SmallScaleCommercialTrafficUtils.readExistingModels(scenario, sample, regionLinksMap);
TrafficVolumeGeneration.reduceDemandBasedOnExistingCarriers(scenario, regionLinksMap, smallScaleCommercialTrafficType,
trafficVolumePerTypeAndZone_start, trafficVolumePerTypeAndZone_stop);
}
Expand All @@ -425,9 +429,9 @@ else if (smallScaleCommercialTrafficType.equals("commercialPersonTraffic"))
/**
* Reads and checks config if all necessary parameter are set.
*/
private Config readAndCheckConfig(Path inputDataDirectory, String modelName, String sampleName, Path output) throws Exception {
private Config readAndCheckConfig(Path configPath, String modelName, String sampleName, Path output) throws Exception {

Config config = ConfigUtils.loadConfig(inputDataDirectory.resolve("config_demand.xml").toString());
Config config = ConfigUtils.loadConfig(configPath.toString());
if (output == null || output.toString().isEmpty())
config.controler().setOutputDirectory(Path.of(config.controler().getOutputDirectory()).resolve(modelName)
.resolve(usedSmallScaleCommercialTrafficType.toString() + "_" + sampleName + "pct" + "_"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static HashMap<String, Object2DoubleMap<String>> createInputDataDistribution(Pat
}
}
}
log.info("Data distribution for " + resultingDataPerZone.size() + " zones was imported from ",
log.info("Data distribution for " + resultingDataPerZone.size() + " zones was imported from " +
existingDataDistribution);
Files.copy(existingDataDistribution, outputFileInOutputFolder, StandardCopyOption.COPY_ATTRIBUTES);
}
Expand Down Expand Up @@ -211,7 +211,7 @@ private static void createResultingDataForLanduseInZones(
resultingNumberPerCategory, Double::sum);
}
if (totalEmployeesInCategoriesPerZone.get(investigationArea).getDouble(zoneId) != 0)
resultingDataPerZone.get(zoneId).mergeDouble("Employee",
resultingDataPerZone.get(zoneId).mergeDouble("Employee",
totalEmployeesInCategoriesPerZone.get(investigationArea).getDouble(zoneId), Double::sum);
}
}
Expand Down Expand Up @@ -337,7 +337,7 @@ static void analyzeBuildingType(List<SimpleFeature> buildingsFeatures,
.point2Coord(((Geometry) singleBuildingFeature.getDefaultGeometry()).getCentroid());
String singleZone = indexZones.query(centroidPointOfBuildingPolygon);
String buildingType = String.valueOf(singleBuildingFeature.getAttribute("type"));
if (buildingType.equals("") || buildingType.equals("null") || buildingType.equals("yes")) {
if (buildingType.isEmpty() || buildingType.equals("null") || buildingType.equals("yes")) {
buildingType = indexLanduse.query(centroidPointOfBuildingPolygon);
buildingTypes = new String[] { buildingType };
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ static String getSampleNameOfOutputFolder(double sample) {
* scenario reduces the demand of the small scale commercial traffic. The
* dispersedTraffic will be added additionally.
*/
static void readExistingModels(Scenario scenario, double sampleScenario, Path inputDataDirectory,
static void readExistingModels(Scenario scenario, double sampleScenario,
Map<String, HashMap<Id<Link>, Link>> regionLinksMap) throws Exception {

String locationOfExistingModels = inputDataDirectory.resolve("existingModels")
.resolve("existingModels.csv").toString();
Path existingModelsFolder = Path.of(scenario.getConfig().getContext().toURI()).getParent().resolve("existingModels");
String locationOfExistingModels = existingModelsFolder.resolve("existingModels.csv").toString();
CSVParser parse = CSVFormat.Builder.create(CSVFormat.DEFAULT).setDelimiter('\t').setHeader()
.setSkipHeaderRecord(true).build().parse(IOUtils.getBufferedReader(locationOfExistingModels));
for (CSVRecord record : parse) {
Expand All @@ -304,8 +304,7 @@ static void readExistingModels(Scenario scenario, double sampleScenario, Path in
vehicleType = null;
final String modelMode = record.get("networkMode");

Path scenarioLocation = inputDataDirectory.resolve("existingModels")
.resolve(modelName);
Path scenarioLocation = existingModelsFolder.resolve(modelName);
if (!Files.exists(scenarioLocation.resolve("output_carriers.xml.gz")))
throw new Exception("For the existing model " + modelName
+ " no carrierFile exists. The carrierFile should have the name 'output_carriers.xml.gz'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ public void testReadOfDataDistributionPerZoneAndBuildingAnalysis() throws IOExce
Assert.assertTrue(categories.containsKey("Employee Traffic/Parcels"));
Assert.assertTrue(categories.containsKey("Employee Tertiary Sector Rest"));

employeeSum += categories.getDouble("Employee Primary Sector");
employeeSum += categories.getDouble("Employee Construction");
employeeSum += categories.getDouble("Employee Secondary Sector Rest");
employeeSum += categories.getDouble("Employee Retail");
employeeSum += categories.getDouble("Employee Traffic/Parcels");
employeeSum += categories.getDouble("Employee Tertiary Sector Rest");
employeeSum += (int) categories.getDouble("Employee Primary Sector");
employeeSum += (int) categories.getDouble("Employee Construction");
employeeSum += (int) categories.getDouble("Employee Secondary Sector Rest");
employeeSum += (int) categories.getDouble("Employee Retail");
employeeSum += (int) categories.getDouble("Employee Traffic/Parcels");
employeeSum += (int) categories.getDouble("Employee Tertiary Sector Rest");

Assert.assertEquals(categories.getDouble("Employee"), employeeSum, MatsimTestUtils.EPSILON);

Expand Down Expand Up @@ -276,12 +276,12 @@ public void testLanduseDistribution() throws IOException {
Assert.assertTrue(categories.containsKey("Employee Traffic/Parcels"));
Assert.assertTrue(categories.containsKey("Employee Tertiary Sector Rest"));

employeeSum += categories.getDouble("Employee Primary Sector");
employeeSum += categories.getDouble("Employee Construction");
employeeSum += categories.getDouble("Employee Secondary Sector Rest");
employeeSum += categories.getDouble("Employee Retail");
employeeSum += categories.getDouble("Employee Traffic/Parcels");
employeeSum += categories.getDouble("Employee Tertiary Sector Rest");
employeeSum += (int) categories.getDouble("Employee Primary Sector");
employeeSum += (int) categories.getDouble("Employee Construction");
employeeSum += (int) categories.getDouble("Employee Secondary Sector Rest");
employeeSum += (int) categories.getDouble("Employee Retail");
employeeSum += (int) categories.getDouble("Employee Traffic/Parcels");
employeeSum += (int) categories.getDouble("Employee Tertiary Sector Rest");

Assert.assertEquals(categories.getDouble("Employee"), employeeSum, MatsimTestUtils.EPSILON);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ public class RunGenerateSmallScaleCommercialTrafficTest {

@Test
public void testMainRunAndResults() {
String inputDataDirectory = utils.getPackageInputDirectory();
String inputDataDirectory = utils.getPackageInputDirectory() + "config_demand.xml";
String output = utils.getOutputDirectory();
String sample = "0.1";
String jspritIterations = "2";
String creationOption = "createNewCarrierFile";
String landuseConfiguration = "useExistingDataDistribution";
String smallScaleCommercialTrafficType = "commercialPersonTraffic";
String includeExistingModels = "true";
String zoneShapeFileName = utils.getPackageInputDirectory() + "/shp/testZones.shp";
String buildingsShapeFileName = utils.getPackageInputDirectory() + "/shp/testBuildings.shp";
String landuseShapeFileName = utils.getPackageInputDirectory() + "/shp/testLanduse.shp";
Expand Down
Loading

0 comments on commit f4ad924

Please sign in to comment.