Skip to content

Commit

Permalink
add options for count scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Jul 12, 2023
1 parent cc54f96 commit 741c2bb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
13 changes: 9 additions & 4 deletions src/main/java/org/matsim/prepare/RunOpenBerlinCalibration.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
})
public class RunOpenBerlinCalibration extends MATSimApplication {

/**
* Scaling factor if all persons use car (~20.6% share).
*/
public static final double CAR_FACTOR = 4.85;
/**
* Flexible activities, which need to be known for location choice and during generation.
* A day can not end on a flexible activity.
Expand All @@ -96,11 +100,11 @@ public class RunOpenBerlinCalibration extends MATSimApplication {
private final SampleOptions sample = new SampleOptions(100, 25, 10, 1);
@CommandLine.Option(names = "--mode", description = "Calibration mode that should be run.")
private CalibrationMode mode;
@CommandLine.Option(names = "--weight", description = "Strategy weight for calibration config.", defaultValue = "1")
@CommandLine.Option(names = "--weight", description = "Strategy weight.", defaultValue = "1")
private double weight;
@CommandLine.Option(names = "--population", description = "Path to population")
@CommandLine.Option(names = "--population", description = "Path to population.")
private Path populationPath;
@CommandLine.Option(names = "--all-car", description = "All plans will use car mode. Capacity is adjusted automatically by 4.85", defaultValue = "false")
@CommandLine.Option(names = "--all-car", description = "All plans will use car mode. Capacity is adjusted automatically by " + CAR_FACTOR, defaultValue = "false")
private boolean allCar;

@CommandLine.Option(names = "--scale-factor", description = "Scale factor for capacity to avoid congestions.", defaultValue = "1.5")
Expand Down Expand Up @@ -158,7 +162,7 @@ protected Config prepareConfig(Config config) {
if (sample.isSet()) {
double sampleSize = sample.getSample();

double countScale = allCar ? 4.85 : 1;
double countScale = allCar ? CAR_FACTOR : 1;

config.qsim().setFlowCapFactor(sampleSize * countScale);
config.qsim().setStorageCapFactor(sampleSize * countScale);
Expand All @@ -178,6 +182,7 @@ protected Config prepareConfig(Config config) {
log.info("Running with flow and storage capacity: {} / {}", config.qsim().getFlowCapFactor(), config.qsim().getStorageCapFactor());

if (allCar) {
log.info("Converting all agents to car plans.");
config.transit().setUseTransit(false);
}

Expand Down
34 changes: 17 additions & 17 deletions src/main/java/org/matsim/prepare/network/FreeSpeedOptimizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class FreeSpeedOptimizer implements MATSimAppCommand {
@CommandLine.Option(names = "--params", description = "Apply params and write to output if given")
private Path params;

@CommandLine.Parameters(arity = "1..*", description = "Input validation files loaded from APIs")
@CommandLine.Parameters(arity = "0..*", description = "Input validation files loaded from APIs")
private List<String> validationFiles;

private Network network;
Expand Down Expand Up @@ -116,7 +116,6 @@ public Integer call() throws Exception {
return 0;
}


Server server = new Server(9090);

ServletHandler handler = new ServletHandler();
Expand All @@ -143,7 +142,7 @@ private DoubleDoublePair evaluateNetwork(Request request, String save) throws IO
double speed = speeds.getDouble(link.getId());

if (request.f == 0) {
double speedFactor = (double) link.getAttributes().getAttribute("speed_Factor");
double speedFactor = (double) link.getAttributes().getAttribute("speed_factor");

if (allowedSpeed <= 31 / 3.6) {
link.setFreespeed(speed * request.b30);
Expand Down Expand Up @@ -214,27 +213,28 @@ private Object2DoubleMap<Entry> readValidation() throws IOException {
// entry to hour and list of speeds
Map<Entry, Int2ObjectMap<DoubleList>> entries = new LinkedHashMap<>();

for (String file : validationFiles) {
if (validationFiles != null)
for (String file : validationFiles) {

log.info("Loading {}", file);
log.info("Loading {}", file);

try (CSVParser parser = new CSVParser(Files.newBufferedReader(Path.of(file)),
CSVFormat.DEFAULT.builder().setHeader().setSkipHeaderRecord(true).build())) {
try (CSVParser parser = new CSVParser(Files.newBufferedReader(Path.of(file)),
CSVFormat.DEFAULT.builder().setHeader().setSkipHeaderRecord(true).build())) {

for (CSVRecord r : parser) {
Entry e = new Entry(Id.createNodeId(r.get("from_node")), Id.createNodeId(r.get("to_node")));
double speed = Double.parseDouble(r.get("dist")) / Double.parseDouble(r.get("travel_time"));
for (CSVRecord r : parser) {
Entry e = new Entry(Id.createNodeId(r.get("from_node")), Id.createNodeId(r.get("to_node")));
double speed = Double.parseDouble(r.get("dist")) / Double.parseDouble(r.get("travel_time"));

if (!Double.isFinite(speed)) {
log.warn("Invalid entry {}", r);
continue;
}
if (!Double.isFinite(speed)) {
log.warn("Invalid entry {}", r);
continue;
}

Int2ObjectMap<DoubleList> perHour = entries.computeIfAbsent(e, (k) -> new Int2ObjectLinkedOpenHashMap<>());
perHour.computeIfAbsent(Integer.parseInt(r.get("hour")), k -> new DoubleArrayList()).add(speed);
Int2ObjectMap<DoubleList> perHour = entries.computeIfAbsent(e, (k) -> new Int2ObjectLinkedOpenHashMap<>());
perHour.computeIfAbsent(Integer.parseInt(r.get("hour")), k -> new DoubleArrayList()).add(speed);
}
}
}
}

Object2DoubleMap<Entry> result = new Object2DoubleOpenHashMap<>();

Expand Down
23 changes: 20 additions & 3 deletions src/main/java/org/matsim/prepare/opt/RunCountOptimization.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.matsim.counts.Counts;
import org.matsim.counts.MatsimCountsReader;
import org.matsim.counts.Volume;
import org.matsim.prepare.RunOpenBerlinCalibration;
import org.optaplanner.core.api.solver.Solver;
import org.optaplanner.core.api.solver.SolverFactory;
import picocli.CommandLine;
Expand Down Expand Up @@ -47,6 +48,9 @@ public class RunCountOptimization implements MATSimAppCommand {
@CommandLine.Option(names = "--counts", description = "Path to counts", required = true)
private Path countsPath;

@CommandLine.Option(names = "--all-car", description = "Plans have been created with the all car option and counts should be scaled. ", defaultValue = "false")
private boolean allCar;

@CommandLine.Option(names = "--metric")
private ErrorMetric metric = ErrorMetric.abs_error;

Expand Down Expand Up @@ -85,6 +89,8 @@ public Integer call() throws Exception {
if (volumes.containsKey(i)) {
int idx = k * H + i;
counts[idx] = (int) volumes.get(i).getValue();
if (allCar)
counts[idx] = (int) (counts[idx] * RunOpenBerlinCalibration.CAR_FACTOR);
}
}

Expand Down Expand Up @@ -132,10 +138,15 @@ private List<PlanPerson> processPopulation(Path input, Network network, Counts<L

Set<Id<Link>> links = linkCounts.getCounts().keySet();

int scale = (int) (1 / sampleSize);
// TODO: determin scaling with all car plans
// TODO: commercial agent need to be scaled with car share

SplittableRandom rnd = new SplittableRandom(0);

for (Person person : population.getPersons().values()) {

int scale = (int) (1 / sampleSize);

Int2IntMap[] plans = new Int2IntMap[maxK];
for (int i = 0; i < plans.length; i++) {
plans[i] = new Int2IntOpenHashMap();
Expand All @@ -144,9 +155,15 @@ private List<PlanPerson> processPopulation(Path input, Network network, Counts<L
boolean keep = false;

int offset = 0;
// Commercial traffic, can also be removed completely
if (person.getId().toString().startsWith("Berlin_"))
// Commercial traffic, which can be chosen to not be included at all
if (person.getId().toString().startsWith("commercialPersonTraffic")) {

offset = 1;
// if other trips have been scaled, these unscaled trips are scaled as well
if (allCar)
// scale with mean of CAR_FACTOR
scale += (rnd.nextDouble() < 0.85 ? 5: 4);
}

// Index for plan
int k = offset;
Expand Down

0 comments on commit 741c2bb

Please sign in to comment.