Skip to content

Commit

Permalink
add new plan generator based on subtour modechoice
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Sep 2, 2024
1 parent c308161 commit b433c35
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
11 changes: 11 additions & 0 deletions input/v6.3/params/baseline_v1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
scoring:
scoringParameters:
- performing: 7.547802
modeParams:
- mode: walk
constant: 0
- mode: car
constant: 0
dailyMonetaryConstant: -5.1038273
advancedScoring:
incomeExponent: 0.549347
14 changes: 13 additions & 1 deletion src/main/java/org/matsim/prepare/choices/ComputePlanChoices.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.population.PersonUtils;
import org.matsim.core.population.algorithms.ParallelPersonAlgorithmUtils;
import org.matsim.core.population.algorithms.PermissibleModesCalculator;
import org.matsim.core.population.algorithms.PersonAlgorithm;
import org.matsim.core.router.*;
import org.matsim.core.utils.timing.TimeInterpretation;
Expand All @@ -37,6 +38,7 @@
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;


Expand Down Expand Up @@ -84,6 +86,10 @@ public class ComputePlanChoices implements MATSimAppCommand, PersonAlgorithm {
private ThreadLocal<Ctx> thread;
private ProgressBar pb;
private double globalAvgIncome;
/**
* Maximum numbers of plan options generated.
*/
private AtomicInteger maxK = new AtomicInteger(0);

public static void main(String[] args) {
new ComputePlanChoices().execute(args);
Expand Down Expand Up @@ -160,6 +166,9 @@ public Integer call() throws Exception {
case diverse -> new DiversePlanGenerator(topK, injector.getInstance(TopKChoicesGenerator.class));
case random -> new RandomPlanGenerator(topK, injector.getInstance(TopKChoicesGenerator.class));
case carAlternative -> new ExclusiveCarPlanGenerator(injector.getInstance(TopKChoicesGenerator.class));
case subtour -> new SubtourPlanGenerator(topK, injector.getInstance(TopKChoicesGenerator.class),
injector.getInstance(PermissibleModesCalculator.class),
config);
},
calcScores ? new PseudoScorer(injector, population) : null
)
Expand Down Expand Up @@ -211,6 +220,7 @@ public Integer call() throws Exception {
}

csv.printComment("Average global income: " + globalAvgIncome);
csv.printComment("Max number of plan options: " + maxK.get());

csv.printRecord(header);

Expand Down Expand Up @@ -299,6 +309,8 @@ public void run(Person person) {
i++;
}

maxK.accumulateAndGet(i, Math::max);

for (int j = i; j < topK; j++) {
row.addAll(convert(null, ctx.scorer));
// not available
Expand Down Expand Up @@ -384,7 +396,7 @@ private Map<String, ModeStats> collect(Plan plan) {
* Define how candidates are generated.
*/
public enum PlanCandidates {
bestK, diverse, random, carAlternative
bestK, diverse, random, carAlternative, subtour
}

private record ModeStats(int usage, double travelTime, double travelDistance, double rideTime, long numSwitches) {
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/matsim/prepare/choices/SubtourPlanGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.matsim.prepare.choices;


import org.jetbrains.annotations.Nullable;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.core.config.Config;
import org.matsim.core.population.algorithms.PermissibleModesCalculator;
import org.matsim.core.population.algorithms.PlanAlgorithm;
import org.matsim.core.replanning.modules.SubtourModeChoice;
import org.matsim.modechoice.CandidateGenerator;
import org.matsim.modechoice.PlanCandidate;
import org.matsim.modechoice.PlanModel;
import org.matsim.modechoice.search.TopKChoicesGenerator;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
* Uses the subtour mutator to generate plans.
*/
public class SubtourPlanGenerator implements CandidateGenerator {

private final int k;
private final SubtourModeChoice modeChoice;
private final PlanAlgorithm algo;
private final TopKChoicesGenerator gen;

public SubtourPlanGenerator(int k, TopKChoicesGenerator generator, PermissibleModesCalculator permissibleModesCalculator, Config config) {
this.k = k;
this.modeChoice = new SubtourModeChoice(config.global(), config.subtourModeChoice(), permissibleModesCalculator);
this.algo = modeChoice.getPlanAlgoInstance();
this.gen = generator;
}

@Override
public List<PlanCandidate> generate(PlanModel planModel, @Nullable Set<String> set, @Nullable boolean[] booleans) {

List<String[]> result = new ArrayList<>();
result.add(planModel.getCurrentModes());

for (int i = 0; i < k; i++) {
Plan plan = planModel.getPlan();
algo.run(plan);

PlanModel updated = PlanModel.newInstance(plan);
result.add(updated.getCurrentModesMutable());
}

return gen.generatePredefined(planModel, result)
.stream().distinct().toList();
}
}
6 changes: 4 additions & 2 deletions src/main/sh/runPlanChoices.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ run_eval() {
#run_eval "--plan-candidates random --top-k 3"
#run_eval "--plan-candidates random --top-k 5"
#run_eval "--plan-candidates random --top-k 9"
run_eval "--plan-candidates diverse --top-k 9"
run_eval "--plan-candidates diverse --top-k 9 --time-util-only"
#run_eval "--plan-candidates diverse --top-k 9"
#run_eval "--plan-candidates diverse --top-k 9 --time-util-only"

run_eval "--plan-candidates subtour --top-k 70"

0 comments on commit b433c35

Please sign in to comment.