Skip to content

Commit

Permalink
Merge branch 'master' into trip-by-group-analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Apr 8, 2024
2 parents 50c833e + 93e317b commit 3bc3552
Show file tree
Hide file tree
Showing 210 changed files with 19,413 additions and 2,245 deletions.
1 change: 1 addition & 0 deletions .github/workflows/verify-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
- contribs/simwrapper
- contribs/sbb-extensions
- contribs/simulatedannealing
- contribs/small-scale-traffic-generation
- benchmark

steps:
Expand Down
1 change: 1 addition & 0 deletions contribs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The MATSim core development team cannot make any guarantee that these extensions
| [shared_mobility](shared_mobility/README.md) | Simulate human-driven shared mobility (i.e., micromobility)
| [signals](signals/README.md) | Simulate traffic lights microscopically
| [simwrapper](simwrapper/README.md) | Creates dashboards automatically with [SimWrapper](https://simwrapper.github.io/)
| [small-scale-traffic-generation](small-scale-traffic-generation/README.md) | Tool to generate small-scale commercial traffic
| [socnetsim](socnetsim/README.md) | Social network simulation
| [sumo](sumo/README.md) | Converter and integrations for [SUMO](https://sumo.dlr.de/])
| [taxi](taxi/README.md) | Taxi service functionality
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Integer call() throws Exception {
.sample(false)
.separator(CsvOptions.detectDelimiter(input.getPath("trips.csv"))).build());

// Trip filter with start and end
// Trip filter with start AND end
if (shp.isDefined() && filter == LocationFilter.trip_start_and_end) {
Geometry geometry = shp.getGeometry();
GeometryFactory f = new GeometryFactory();
Expand All @@ -156,6 +156,22 @@ public Integer call() throws Exception {
}
}

trips = trips.where(Selection.with(idx.toIntArray()));
// trip filter with start OR end
} else if (shp.isDefined() && filter == LocationFilter.trip_start_or_end) {
Geometry geometry = shp.getGeometry();
GeometryFactory f = new GeometryFactory();
IntList idx = new IntArrayList();

for (int i = 0; i < trips.rowCount(); i++) {
Row row = trips.row(i);
Point start = f.createPoint(new Coordinate(row.getDouble("start_x"), row.getDouble("start_y")));
Point end = f.createPoint(new Coordinate(row.getDouble("end_x"), row.getDouble("end_y")));
if (geometry.contains(start) || geometry.contains(end)) {
idx.add(i);
}
}

trips = trips.where(Selection.with(idx.toIntArray()));
}

Expand Down Expand Up @@ -416,6 +432,7 @@ private void writeTripPurposes(Table trips) {
*/
enum LocationFilter {
trip_start_and_end,
trip_start_or_end,
home,
none
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.matsim.application.options.InputOptions;
import org.matsim.application.options.OutputOptions;
import org.matsim.application.options.ShpOptions;
import org.matsim.application.prepare.network.SampleNetwork;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.router.costcalculators.OnlyTimeDependentTravelDisutility;
import org.matsim.core.router.speedy.SpeedyALTFactory;
Expand Down Expand Up @@ -85,23 +86,6 @@ public class SampleValidationRoutes implements MATSimAppCommand {
@CommandLine.Option(names = "--mode", description = "Mode to validate", defaultValue = TransportMode.car)
private String mode;

/**
* Random coord in the same direction as a link.
*/
static Coord rndCoord(SplittableRandom rnd, double dist, Link link) {

Coord v = link.getFromNode().getCoord();
Coord u = link.getToNode().getCoord();

var angle = Math.atan2(u.getY() - v.getY(), u.getX() - v.getX());

var sample = angle + rnd.nextDouble(-0.2, 0.2) * Math.PI * 2;

var x = Math.cos(sample) * dist;
var y = Math.sin(sample) * dist;

return CoordUtils.round(new Coord(v.getX() + x, v.getY() + y));
}

public static void main(String[] args) {
new SampleValidationRoutes().execute(args);
Expand Down Expand Up @@ -268,7 +252,7 @@ public boolean test(Link link) {
continue;
}

Coord dest = rndCoord(rnd, rnd.nextDouble(distRange.get(0), distRange.get(1)), link);
Coord dest = SampleNetwork.rndCoord(rnd, rnd.nextDouble(distRange.get(0), distRange.get(1)), link);
Link to = NetworkUtils.getNearestLink(network, dest);

if (!to.getAllowedModes().contains(mode) || exclude.test(to)) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package org.matsim.application.options;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.network.Link;
import org.matsim.core.utils.io.IOUtils;
import picocli.CommandLine;

import javax.annotation.Nullable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.*;

/**
* Common options when working with counts data.
*/
public final class CountsOptions {

private static final Logger log = LogManager.getLogger(CountsOptions.class);

@CommandLine.Option(names = "--counts-mapping", description = "Path to csv with count station ids to ignore")
private String input;

private Map<String, Id<Link>> manualMatchedCounts = null;
private Set<String> ignoredCounts = null;

public CountsOptions() {
}

public CountsOptions(@Nullable String input) {
this.input = input;
}

/**
* Get list of ignored count ids.
*/
public Set<String> getIgnored() {
readMapping();
return ignoredCounts;
}

/**
* Return mapping of count id to specified link id.
*/
public Map<String, Id<Link>> getManualMatched() {
readMapping();
return manualMatchedCounts;
}

private synchronized void readMapping() {

// Already read
if (manualMatchedCounts != null)
return;

manualMatchedCounts = new HashMap<>();
ignoredCounts = new HashSet<>();

// No input file
if (input == null)
return;

try (var reader = IOUtils.getBufferedReader(input)) {
CSVFormat format = CSVFormat.Builder.create()
.setAllowMissingColumnNames(true)
.setDelimiter(CsvOptions.detectDelimiter(input))
.setHeader()
.setSkipHeaderRecord(true)
.build();

try (CSVParser csv = new CSVParser(reader, format)) {
Schema schema = parseSchema(csv.getHeaderNames());

log.info("Using schema for counts mapping: {}", schema);

for (CSVRecord row : csv) {

String stationId = row.get(schema.stationColumn);
manualMatchedCounts.put(stationId, Id.createLinkId(row.get(schema.linkColumn)));

if (schema.usingColumn != null) {

String value = row.get(schema.usingColumn).strip().toLowerCase();
boolean val = value.equals("y") || value.equals("x") || value.equals("true");

if (schema.isWhiteList && !val)
ignoredCounts.add(stationId);
else if (!schema.isWhiteList && val)
ignoredCounts.add(stationId);
}
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}

}

/**
* Check whether station id should be ignored.
*/
public boolean isIgnored(String stationId) {
readMapping();
return ignoredCounts.contains(stationId);
}

/**
* Return manually matched link id.
*
* @return null if not matched
*/
public Id<Link> isManuallyMatched(String stationId) {
readMapping();

if (manualMatchedCounts.isEmpty() || !manualMatchedCounts.containsKey(stationId))
return null;

return manualMatchedCounts.get(stationId);
}

private Schema parseSchema(List<String> header) {

List<String> names = header.stream()
.map(String::toLowerCase)
.map(String::strip)
.map(s -> s.replace("_", ""))
.toList();

int linkId = names.indexOf("linkid");

if (linkId < 0)
throw new IllegalArgumentException("Link id column not found in csv: " + header);

int using = names.indexOf("using");
int ignore = names.indexOf("ignore");

// first or second column for station id
int station = linkId == 0 ? 1 : 0;

if (using > 0)
return new Schema(header.get(station), header.get(linkId), header.get(using), true);

if (ignore > 0)
return new Schema(header.get(station), header.get(linkId), header.get(ignore), false);

return new Schema(header.get(station), header.get(linkId), null, false);
}

private record Schema(String stationColumn, String linkColumn, String usingColumn, boolean isWhiteList) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static Character detectDelimiter(String path) throws IOException {
* Get the CSV format defined by the options.
*/
public CSVFormat getFormat() {
CSVFormat.Builder format = this.csvFormat.getFormat().builder().setSkipHeaderRecord(true);
CSVFormat.Builder format = this.csvFormat.getFormat().builder().setHeader().setSkipHeaderRecord(true);
if (csvDelimiter != null)
format = format.setDelimiter(csvDelimiter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public Network getNetwork() {
return NetworkUtils.readNetwork(networkPath);
}

public String getNetworkPath() {
return networkPath;
}

public Population getPopulation() {
if (!spec.requirePopulation())
throw new IllegalArgumentException("Population can not be accessed unless, requirePopulation=true.");
Expand Down
Loading

0 comments on commit 3bc3552

Please sign in to comment.