Skip to content

Commit

Permalink
Merge pull request #14 from matsim-vsp/patch-20241002
Browse files Browse the repository at this point in the history
Update CreateRandomOdPairs.java
  • Loading branch information
luchengqi7 authored Oct 2, 2024
2 parents 846743d + 5ffbed1 commit 68c3587
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.matsim.accessibillityDrtOptimizer.network_calibration.od_pairs;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.matsim.accessibillityDrtOptimizer.utils.CsvUtils;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.network.Link;
Expand All @@ -14,6 +17,8 @@
import picocli.CommandLine;

import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
Expand All @@ -26,6 +31,9 @@ public class CreateRandomOdPairs implements MATSimAppCommand {
@CommandLine.Option(names = "--network", description = "path to network file", required = true)
private String networkPath;

@CommandLine.Option(names = "--input", description = "path to output od-pairs file", defaultValue = "")
private String inputOdPairsPath;

@CommandLine.Option(names = "--output", description = "path to output od-pairs file", required = true)
private String outputPath;

Expand Down Expand Up @@ -54,11 +62,22 @@ public Integer call() throws Exception {
.collect(Collectors.toList());
int size = linkList.size();
int generatedOdPairs = 0;
Set<Tuple<Id<Node>, Id<Node>>> existingOdPairs = new HashSet<>();
if (!inputOdPairsPath.isEmpty()){
// read in the existing od-pairs
try (CSVParser parser = CSVFormat.Builder.create(CSVFormat.DEFAULT).
setDelimiter(CsvUtils.detectDelimiter(inputOdPairsPath)).setHeader().setSkipHeaderRecord(true).
build().parse(Files.newBufferedReader(Path.of(inputOdPairsPath)))) {
for (CSVRecord record : parser.getRecords()) {
Id<Node> fromNodeId = Id.createNodeId(record.get(FROM_NODE));
Id<Node> toNodeId = Id.createNodeId(record.get(TO_NODE));
existingOdPairs.add(new Tuple<>(fromNodeId, toNodeId));
}
}
}

CSVPrinter tsvWriter = new CSVPrinter(new FileWriter(outputPath), CSVFormat.DEFAULT);
tsvWriter.printRecord(FROM_NODE, TO_NODE, HOUR);
Set<Tuple<Id<Node>, Id<Node>>> existingOdPairs = new HashSet<>();

while (generatedOdPairs < maxNumODPairs) {
Node fromNode = linkList.get(random.nextInt(size)).getToNode();
Node toNode = linkList.get(random.nextInt(size)).getToNode();
Expand Down

0 comments on commit 68c3587

Please sign in to comment.