Skip to content

Commit

Permalink
Merge branch 'master' into noiseAnalysis
Browse files Browse the repository at this point in the history
  • Loading branch information
tschlenther committed Oct 1, 2024
2 parents 3d73ccc + 4a0aa89 commit 1e452c5
Show file tree
Hide file tree
Showing 69 changed files with 4,575 additions and 400 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.matsim.api.core.v01.population.*;
import org.matsim.application.MATSimAppCommand;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.algorithms.PersonAlgorithm;
import org.matsim.core.population.algorithms.TripsToLegsAlgorithm;
import org.matsim.core.router.RoutingModeMainModeIdentifier;
import picocli.CommandLine;
Expand All @@ -24,7 +25,7 @@
mixinStandardHelpOptions = true,
showDefaultValues = true
)
public class CleanPopulation implements MATSimAppCommand {
public class CleanPopulation implements MATSimAppCommand, PersonAlgorithm {

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

Expand All @@ -46,6 +47,10 @@ public class CleanPopulation implements MATSimAppCommand {
@CommandLine.Option(names = "--output", description = "Output file name", required = true)
private Path output;

// Using the analysis main mode identifier instead of the routing mode based one on purpose
// to be able to process older population files without any routing modes!
private final TripsToLegsAlgorithm trips2Legs = new TripsToLegsAlgorithm(new RoutingModeMainModeIdentifier());

public static void main(String[] args) {
System.exit(new CommandLine(new CleanPopulation()).execute(args));
}
Expand All @@ -63,43 +68,64 @@ public Integer call() throws Exception {
if (output.getParent() != null)
Files.createDirectories(output.getParent());

// Using the analysis main mode identifier instead of the routing mode based one on purpose
// to be able to process older population files without any routing modes!
TripsToLegsAlgorithm trips2Legs = new TripsToLegsAlgorithm(new RoutingModeMainModeIdentifier());

for (Person person : population.getPersons().values()) {
run(person);
}

PopulationUtils.writePopulation(population, output.toString());

return 0;
}

@Override
public void run(Person person) {
if (rmUnselected) {
removeUnselectedPlans(person);
}

if (rmUnselected) {
Plan selected = person.getSelectedPlan();
for (Plan plan : Lists.newArrayList(person.getPlans())) {
if (plan != selected)
person.removePlan(plan);
for (Plan plan : person.getPlans()) {
if (tripsToLegs)
trips2Legs.run(plan);

for (PlanElement el : plan.getPlanElements()) {
if (rmRoutes) {
removeRouteFromLeg(el);
}
}

for (Plan plan : person.getPlans()) {
if (tripsToLegs)
trips2Legs.run(plan);

for (PlanElement el : plan.getPlanElements()) {
if (rmRoutes) {
if (el instanceof Leg) {
((Leg) el).setRoute(null);
}
}

if (rmActivityLocations) {
if (el instanceof Activity) {
((Activity) el).setLinkId(null);
((Activity) el).setFacilityId(null);
}
}
if (rmActivityLocations) {
removeActivityLocation(el);
}
}
}
}

PopulationUtils.writePopulation(population, output.toString());
/**
* Remove link and facility information from activity.
*/
public static void removeActivityLocation(PlanElement el) {
if (el instanceof Activity act) {
act.setLinkId(null);
act.setFacilityId(null);
}
}

return 0;
/**
* Remove route information from leg.
*/
public static void removeRouteFromLeg(PlanElement el) {
if (el instanceof Leg leg) {
leg.setRoute(null);
}
}

/**
* Remove all unselected plans for given person.
*/
public static void removeUnselectedPlans(Person person) {
Plan selected = person.getSelectedPlan();
for (Plan plan : Lists.newArrayList(person.getPlans())) {
if (plan != selected)
person.removePlan(plan);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ static void createDemandForCarriers(Scenario scenario, ShpOptions.Index indexSha
CoordinateTransformation crsTransformationNetworkAndShape) {

for (DemandInformationElement newDemandInformationElement : demandInformation) {
log.info("Create demand for carrier {}", newDemandInformationElement.getCarrierName());
if (newDemandInformationElement.getTypeOfDemand().equals("service"))
createServices(scenario, newDemandInformationElement, indexShape, population, combineSimilarJobs,
crsTransformationNetworkAndShape);
Expand Down Expand Up @@ -585,12 +586,16 @@ private static void createServices(Scenario scenario, DemandInformationElement n
Integer numberOfServiceLocations = newDemandInformationElement.getNumberOfFirstJobElementLocations();
ArrayList<String> usedServiceLocations = new ArrayList<String>();
int numberOfLinksInNetwork = scenario.getNetwork().getLinks().size();
HashMap<Id<Person>, Person> possiblePersonsForService = new HashMap<Id<Person>, Person>();
HashMap<Id<Person>, Person> possiblePersonsForService = new HashMap<>();
HashMap<Id<Person>, HashMap<Double, String>> nearestLinkPerPerson = new HashMap<>();

// set number of jobs
if (shareOfPopulationWithThisService == null)
if (shareOfPopulationWithThisService == null) {
numberOfJobs = newDemandInformationElement.getNumberOfJobs();
if (population != null)
log.warn(
"You have a population but no share of the population for the demand. The number of jobs will be set to the number of jobs you set in the csv file. The population will not be used for the demand generation.");
}
else if (population == null)
throw new RuntimeException(
"No population found although input parameter <ShareOfPopulationWithThisDemand> is set");
Expand All @@ -606,15 +611,14 @@ else if (population == null)
possiblePersonsForService.putAll(population.getPersons());
int numberPossibleServices = (int) Math
.round(shareOfPopulationWithThisService * possiblePersonsForService.size());
if (sampleSizeInputPopulation == sampleTo)
numberOfJobs = (int) Math.round(shareOfPopulationWithThisService * possiblePersonsForService.size());
else if (samplingOption.equals("changeNumberOfLocationsWithDemand"))
numberOfJobs = (int) Math.round((sampleTo / sampleSizeInputPopulation)
* (shareOfPopulationWithThisService * possiblePersonsForService.size()));
else if (samplingOption.equals("changeDemandOnLocation")) {
demandToDistribute = (int) Math.round((sampleTo / sampleSizeInputPopulation) * demandToDistribute);
numberOfJobs = (int) Math.round(shareOfPopulationWithThisService * possiblePersonsForService.size());
} else
int sampledNumberPossibleServices = (int) Math.round((sampleTo / sampleSizeInputPopulation) * numberPossibleServices);
if (sampleSizeInputPopulation == sampleTo || samplingOption.equals("changeDemandOnLocation"))
numberOfJobs = numberPossibleServices;
else if (samplingOption.equals("changeNumberOfLocationsWithDemand")) {
numberOfJobs = sampledNumberPossibleServices;
numberPossibleServices = numberOfJobs;
}
else
throw new RuntimeException(
"Error with the sampling of the demand based on the population. Please check sampling sizes and sampling options!!");
if (numberPossibleServices != 0)
Expand Down Expand Up @@ -773,16 +777,20 @@ private static void createShipments(Scenario scenario, DemandInformationElement
String[] areasForDeliveryLocations = newDemandInformationElement.getAreasSecondJobElement();
String[] setLocationsOfPickup = newDemandInformationElement.getLocationsOfFirstJobElement();
String[] setLocationsOfDelivery = newDemandInformationElement.getLocationsOfSecondJobElement();
ArrayList<String> usedPickupLocations = new ArrayList<String>();
ArrayList<String> usedDeliveryLocations = new ArrayList<String>();
HashMap<Id<Person>, Person> possiblePersonsPickup = new HashMap<Id<Person>, Person>();
HashMap<Id<Person>, Person> possiblePersonsDelivery = new HashMap<Id<Person>, Person>();
ArrayList<String> usedPickupLocations = new ArrayList<>();
ArrayList<String> usedDeliveryLocations = new ArrayList<>();
HashMap<Id<Person>, Person> possiblePersonsPickup = new HashMap<>();
HashMap<Id<Person>, Person> possiblePersonsDelivery = new HashMap<>();
HashMap<Id<Person>, HashMap<Double, String>> nearestLinkPerPersonPickup = new HashMap<>();
HashMap<Id<Person>, HashMap<Double, String>> nearestLinkPerPersonDelivery = new HashMap<>();

// set number of jobs
if (shareOfPopulationWithThisPickup == null && shareOfPopulationWithThisDelivery == null)
if (shareOfPopulationWithThisPickup == null && shareOfPopulationWithThisDelivery == null){
if (population != null)
log.warn(
"You have a population but no share of the population for the demand. The number of jobs will be set to the number of jobs you set in the csv file. The population will not be used for the demand generation.");
numberOfJobs = newDemandInformationElement.getNumberOfJobs();
}
else if (population == null)
throw new RuntimeException(
"No population found although input parameter <ShareOfPopulationWithThisDemand> is set");
Expand Down Expand Up @@ -814,37 +822,24 @@ else if (population == null)
int sampledNumberPossibleJobsPickup = (int)Math.round((sampleTo / sampleSizeInputPopulation) * numberPossibleJobsPickup);
int sampledNumberPossibleJobsDelivery = (int) Math.round((sampleTo / sampleSizeInputPopulation) * numberPossibleJobsDelivery);
if (numberPossibleJobsPickup > numberPossibleJobsDelivery) {
if (sampleSizeInputPopulation == sampleTo) {
numberOfJobs = (int) Math.round(shareOfPopulationWithThisPickup * numberPossibleJobsPickup);
numberPossibleJobsPickup = numberOfJobs;
if (shareOfPopulationWithThisDelivery != null)
numberPossibleJobsDelivery = (int) Math
.round(shareOfPopulationWithThisDelivery * numberPossibleJobsDelivery);
if (sampleSizeInputPopulation == sampleTo ||samplingOption.equals("changeDemandOnLocation")) {
numberOfJobs = numberPossibleJobsPickup;
} else if (samplingOption.equals("changeNumberOfLocationsWithDemand")) {
numberOfJobs = sampledNumberPossibleJobsPickup;
numberPossibleJobsPickup = numberOfJobs;
if (shareOfPopulationWithThisDelivery != null)
numberPossibleJobsDelivery = sampledNumberPossibleJobsDelivery;
} else if (samplingOption.equals("changeDemandOnLocation")) {
demandToDistribute = (int) Math.round((sampleTo / sampleSizeInputPopulation) * demandToDistribute);
numberOfJobs = numberPossibleJobsPickup;
} else
throw new RuntimeException(
"Error with the sampling of the demand based on the population. Please check sampling sizes and sampling options!!");
} else {
if (sampleSizeInputPopulation == sampleTo) {
numberOfJobs = (int) Math.round(shareOfPopulationWithThisDelivery * numberPossibleJobsDelivery);
numberPossibleJobsDelivery = numberOfJobs;
numberPossibleJobsPickup = (int) Math
.round(shareOfPopulationWithThisPickup * numberPossibleJobsPickup);
if (sampleSizeInputPopulation == sampleTo ||samplingOption.equals("changeDemandOnLocation")) {
numberOfJobs = numberPossibleJobsDelivery;
} else if (samplingOption.equals("changeNumberOfLocationsWithDemand")) {
numberOfJobs = sampledNumberPossibleJobsDelivery;
numberPossibleJobsDelivery = numberOfJobs;
if (shareOfPopulationWithThisDelivery != null)
numberPossibleJobsPickup = sampledNumberPossibleJobsPickup;
} else if (samplingOption.equals("changeDemandOnLocation")) {
demandToDistribute = (int) Math.round((sampleTo / sampleSizeInputPopulation) * demandToDistribute);
numberOfJobs = numberPossibleJobsDelivery;
} else
throw new RuntimeException(
"Error with the sampling of the demand based on the population. Please check sampling sizes and sampling options!!");
Expand Down Expand Up @@ -1289,6 +1284,7 @@ private static HashMap<Id<Link>, Link> findAllPossibleLinks(Scenario scenario,
Integer numberOfLocations, String[] areasForLocations, String[] setLocations,
HashMap<Id<Person>, Person> possiblePersons,
HashMap<Id<Person>, HashMap<Double, String>> nearestLinkPerPerson) {
log.info("Finding possible links for the demand in the selected areas {}", Arrays.toString(areasForLocations));
HashMap<Id<Link>, Link> possibleLinks = new HashMap<>();
if (numberOfLocations == null) {
for (Link link : scenario.getNetwork().getLinks().values())
Expand Down Expand Up @@ -1364,7 +1360,7 @@ private static Link findNextUsedLink(Scenario scenario, ShpOptions.Index indexSh
private static HashMap<Id<Person>, Person> findPossiblePersons(Population population,
String[] areasForJobElementLocations, ShpOptions.Index indexShape,
CoordinateTransformation crsTransformationNetworkAndShape) {

log.info("Finding possible persons for the demand in the selected areas {}", Arrays.toString(areasForJobElementLocations));
HashMap<Id<Person>, Person> possiblePersons = new HashMap<>();

for (Person person : population.getPersons().values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,16 @@ private void createDemand(DemandGenerationOptions selectedDemandGenerationOption
*/
FreightDemandGenerationUtils.preparePopulation(population, sampleSizeInputPopulation,
upSamplePopulationTo, "changeNumberOfLocationsWithDemand");
case increaseDemandOnLocation ->
case increaseDemandOnLocation -> {
/*
* If the demand sample is higher than the population sample, the demand per
* person will be increased.
*/
FreightDemandGenerationUtils.preparePopulation(population, sampleSizeInputPopulation,
upSamplePopulationTo, "changeDemandOnLocation");
log.warn("You have selected the option to increase the demand on the location. "
+ "Because the simulation always uses the given demand the results are similar to the option with the same sample size.");
FreightDemandGenerationUtils.preparePopulation(population, sampleSizeInputPopulation,
upSamplePopulationTo, "changeDemandOnLocation");
}
case noPopulationSampling ->
/*
* If the demand sample is equal to the population sample, the demand is created
Expand Down
Loading

0 comments on commit 1e452c5

Please sign in to comment.