Skip to content

Commit

Permalink
new class to extract plan indices
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Jun 9, 2024
1 parent 5b2e9d4 commit 7f40056
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 13 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -390,7 +391,7 @@ public ScoringFunction createNewScoringFunction(Person person) {
// sumScoringFunction.addScoringFunction(new CharyparNagelAgentStuckScoring(params));

final CadytsScoring<Link> scoringFunction = new CadytsScoring<>(person.getSelectedPlan(), config, cadytsContext);
scoringFunction.setWeightOfCadytsCorrection(config.scoring().getBrainExpBeta());
scoringFunction.setWeightOfCadytsCorrection(30 * config.scoring().getBrainExpBeta());
sumScoringFunction.addScoringFunction(scoringFunction);

return sumScoringFunction;
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/matsim/prepare/opt/ExtractPlanIndexFromType.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static void selectPlanWithIndex(Person person, int planIndex) {
List<? extends Plan> plans = person.getPlans();
Set<Plan> 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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ public final class AdvancedScoringConfigGroup extends ReflectiveConfigGroup {

private final List<ScoringParameters> scoringParameters = new ArrayList<>();

/**
* Different options for income dependent scoring.
*/
public enum IncomeDependentScoring {
none,
avg_by_personal_income,
avg_by_personal_income
}

@Parameter
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/main/python/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/main/python/calibrate_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 7f40056

Please sign in to comment.