Skip to content

Commit

Permalink
Merge pull request #3494 from matsim-org/fixFreightGenerationToolBug
Browse files Browse the repository at this point in the history
Fix freight generation tool bug
  • Loading branch information
rewertvsp authored Sep 26, 2024
2 parents 405ba79 + 68cc6c4 commit f7997e2
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 164 deletions.
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 f7997e2

Please sign in to comment.