From 7f400561452fdea345299394abf923da1a7376da Mon Sep 17 00:00:00 2001 From: rakow Date: Sun, 9 Jun 2024 17:44:24 +0200 Subject: [PATCH] new class to extract plan indices --- Makefile | 9 ++++ .../prepare/RunOpenBerlinCalibration.java | 5 +- .../prepare/opt/ExtractPlanIndexFromType.java | 53 +++++++++++++++++++ .../prepare/opt/SelectPlansFromIndex.java | 2 +- .../scoring/AdvancedScoringConfigGroup.java | 6 ++- src/main/python/calibrate.py | 8 +-- src/main/python/calibrate_dist.py | 8 +-- 7 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 src/main/java/org/matsim/prepare/opt/ExtractPlanIndexFromType.java diff --git a/Makefile b/Makefile index 4fddd401..4726dac3 100644 --- a/Makefile +++ b/Makefile @@ -257,6 +257,15 @@ eval-opt: $p/berlin-initial-$V-25pct.experienced_plans.xml.gz $(sc) run --mode "routeChoice" --iterations 20 --all-car --output "output/eval-$(ERROR_METRIC)" --25pct --population "berlin-$V-25pct.plans_$(ERROR_METRIC).xml.gz"\ --config $p/berlin-$V.config.xml +$p/berlin-$V-25pct.plans_cadyts.xml.gz: + $(sc) prepare extract-plans-idx\ + --input output/cadyts/cadyts.output_plans.xml.gz\ + --output $p/berlin-$V-25pct.plans_selection_cadyts.csv + + $(sc) prepare select-plans-idx\ + --input $p/berlin-cadyts-input-$V-25pct.plans.xml.gz\ + --csv $p/berlin-$V-25pct.plans_selection_cadyts.csv\ + --output $@ # These depend on the output of optimization runs $p/berlin-$V-25pct.plans-initial.xml.gz: $p/berlin-$V-facilities.xml.gz $p/berlin-$V-network.xml.gz $p/berlin-longHaulFreight-$V-25pct.plans.xml.gz diff --git a/src/main/java/org/matsim/prepare/RunOpenBerlinCalibration.java b/src/main/java/org/matsim/prepare/RunOpenBerlinCalibration.java index 133a3be8..a70e4fec 100644 --- a/src/main/java/org/matsim/prepare/RunOpenBerlinCalibration.java +++ b/src/main/java/org/matsim/prepare/RunOpenBerlinCalibration.java @@ -44,6 +44,7 @@ import org.matsim.core.scoring.SumScoringFunction; import org.matsim.core.scoring.functions.*; import org.matsim.core.utils.geometry.CoordUtils; +import org.matsim.prepare.opt.ExtractPlanIndexFromType; import org.matsim.prepare.population.AssignReferencePopulation; import org.matsim.prepare.choices.ComputePlanChoices; import org.matsim.prepare.choices.ComputeTripChoices; @@ -86,7 +87,7 @@ CreateCountsFromGeoPortalBerlin.class, CreateCountsFromVMZOld.class, CreateCountsFromVMZ.class, ReprojectNetwork.class, RunActivitySampling.class, MergePlans.class, SplitActivityTypesDuration.class, CleanPopulation.class, CleanAttributes.class, GenerateSmallScaleCommercialTrafficDemand.class, CreateDataDistributionOfStructureData.class, - RunCountOptimization.class, SelectPlansFromIndex.class, AssignReferencePopulation.class, + RunCountOptimization.class, SelectPlansFromIndex.class, ExtractPlanIndexFromType.class, AssignReferencePopulation.class, ExtractRelevantFreightTrips.class, CheckCarAvailability.class, FixSubtourModes.class, ComputeTripChoices.class, ComputePlanChoices.class, ApplyNetworkParams.class, SetCarAvailabilityByAge.class }) @@ -390,7 +391,7 @@ public ScoringFunction createNewScoringFunction(Person person) { // sumScoringFunction.addScoringFunction(new CharyparNagelAgentStuckScoring(params)); final CadytsScoring scoringFunction = new CadytsScoring<>(person.getSelectedPlan(), config, cadytsContext); - scoringFunction.setWeightOfCadytsCorrection(config.scoring().getBrainExpBeta()); + scoringFunction.setWeightOfCadytsCorrection(30 * config.scoring().getBrainExpBeta()); sumScoringFunction.addScoringFunction(scoringFunction); return sumScoringFunction; diff --git a/src/main/java/org/matsim/prepare/opt/ExtractPlanIndexFromType.java b/src/main/java/org/matsim/prepare/opt/ExtractPlanIndexFromType.java new file mode 100644 index 00000000..e1371cc0 --- /dev/null +++ b/src/main/java/org/matsim/prepare/opt/ExtractPlanIndexFromType.java @@ -0,0 +1,53 @@ +package org.matsim.prepare.opt; + +import it.unimi.dsi.fastutil.objects.Object2IntMap; +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; +import org.matsim.api.core.v01.population.Person; +import org.matsim.application.MATSimAppCommand; +import org.matsim.core.config.ConfigUtils; +import org.matsim.core.population.algorithms.PersonAlgorithm; +import org.matsim.core.population.io.StreamingPopulationReader; +import org.matsim.core.scenario.ScenarioUtils; +import picocli.CommandLine; + +import java.nio.file.Files; +import java.nio.file.Path; + +@CommandLine.Command(name = "extract-plans-idx", description = "Extract plan index from population.") +public class ExtractPlanIndexFromType implements MATSimAppCommand, PersonAlgorithm { + + @CommandLine.Option(names = "--input", description = "Path to input plans.", required = true) + private Path input; + + @CommandLine.Option(names = "--output", description = "Desired output plans.", required = true) + private Path output; + + private final Object2IntMap mapping = new Object2IntOpenHashMap<>(); + + @Override + public Integer call() throws Exception { + + StreamingPopulationReader reader = new StreamingPopulationReader(ScenarioUtils.createScenario(ConfigUtils.createConfig())); + + reader.addAlgorithm(this); + + reader.readFile(input.toString()); + + try (CSVPrinter csv = new CSVPrinter(Files.newBufferedWriter(output), CSVFormat.DEFAULT)) { + csv.printRecord("id", "idx"); + for (Object2IntMap.Entry e : mapping.object2IntEntrySet()) { + csv.printRecord(e.getKey(), e.getIntValue()); + } + } + + return 0; + } + + @Override + public void run(Person person) { + String type = person.getSelectedPlan().getType(); + mapping.put(person.getId().toString(), Integer.parseInt(type)); + } +} diff --git a/src/main/java/org/matsim/prepare/opt/SelectPlansFromIndex.java b/src/main/java/org/matsim/prepare/opt/SelectPlansFromIndex.java index a5c17572..beef0f85 100644 --- a/src/main/java/org/matsim/prepare/opt/SelectPlansFromIndex.java +++ b/src/main/java/org/matsim/prepare/opt/SelectPlansFromIndex.java @@ -77,7 +77,7 @@ public static void selectPlanWithIndex(Person person, int planIndex) { List plans = person.getPlans(); Set removePlans = new HashSet<>(); - // make sure that one plan is always selected, even if there are less plans than index + // make sure that one plan is always selected, even if there are fewer plans than index int idx = planIndex % plans.size(); for (int i = 0; i < plans.size(); i++) { diff --git a/src/main/java/org/matsim/run/scoring/AdvancedScoringConfigGroup.java b/src/main/java/org/matsim/run/scoring/AdvancedScoringConfigGroup.java index f86190bd..a7b4a35e 100644 --- a/src/main/java/org/matsim/run/scoring/AdvancedScoringConfigGroup.java +++ b/src/main/java/org/matsim/run/scoring/AdvancedScoringConfigGroup.java @@ -16,9 +16,12 @@ public final class AdvancedScoringConfigGroup extends ReflectiveConfigGroup { private final List scoringParameters = new ArrayList<>(); + /** + * Different options for income dependent scoring. + */ public enum IncomeDependentScoring { none, - avg_by_personal_income, + avg_by_personal_income } @Parameter @@ -32,7 +35,6 @@ public enum IncomeDependentScoring { // TODO: maybe option to re-assign variations or use them from attributes // TODO: could load the random variations from a file, helper function to only generate the variations - // TODO: reference population?, for which the loglikelihood could be calculated public AdvancedScoringConfigGroup() { super(GROUP_NAME); diff --git a/src/main/python/calibrate.py b/src/main/python/calibrate.py index c1b99ba7..809d42e5 100755 --- a/src/main/python/calibrate.py +++ b/src/main/python/calibrate.py @@ -8,10 +8,10 @@ modes = ["walk", "car", "pt", "bike", "ride"] fixed_mode = "walk" initial = { - "bike": -2.23, - "pt": -0.25, - "car": -0.62, - "ride": -1.37 + "bike": -1.4, + "pt": 0.6, + "car": -1, + "ride": -1.4 } # Original modal split diff --git a/src/main/python/calibrate_dist.py b/src/main/python/calibrate_dist.py index 58c7d191..69d5e301 100755 --- a/src/main/python/calibrate_dist.py +++ b/src/main/python/calibrate_dist.py @@ -10,10 +10,10 @@ modes = ["walk", "car", "pt", "bike", "ride"] fixed_mode = "walk" initial = { - "bike": -1.6, - "pt": 0.2, - "car": -0.4, - "ride": -1.2 + "bike": -1.4, + "pt": 0.6, + "car": -1, + "ride": -1.4 } target = pd.read_csv("mode_share_ref.csv")