Skip to content

Commit

Permalink
extract PRStation class
Browse files Browse the repository at this point in the history
  • Loading branch information
tschlenther committed Sep 1, 2023
1 parent edb3671 commit ce1b77b
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private void writeTransitStops(String outputFile) throws IOException {
//write a stop for each P+R link
Set<PRStation> prStations = ReplaceCarByDRT.readPRStationFile(url2PRStations);
for (PRStation prStation : prStations) {
writeStop(csvWriter, network.getLinks().get(prStation.linkId));
writeStop(csvWriter, network.getLinks().get(prStation.getLinkId()));
}
} catch (IOException e){
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@
package org.matsim.run.replaceCarByDRT;

import com.opencsv.CSVWriter;
import org.apache.commons.math3.distribution.EnumeratedDistribution;
import org.apache.commons.math3.util.Pair;
import org.apache.log4j.Logger;
import org.locationtech.jts.geom.Point;
import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.contrib.dvrp.fleet.DvrpVehicle;
import org.matsim.contrib.dvrp.fleet.DvrpVehicleSpecification;
import org.matsim.contrib.dvrp.fleet.FleetWriter;
Expand All @@ -41,16 +38,11 @@
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.network.algorithms.TransportModeNetworkFilter;
import org.matsim.core.network.io.NetworkWriter;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.io.PopulationReader;
import org.matsim.core.router.StageActivityTypeIdentifier;
import org.matsim.core.router.TripStructureUtils;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.utils.geometry.CoordinateTransformation;
import org.matsim.core.utils.geometry.geotools.MGC;
import org.matsim.core.utils.geometry.transformations.TransformationFactory;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.facilities.MatsimFacilitiesReader;
import org.matsim.prepare.drt.DrtVehicleCreator;
import org.matsim.run.drt.BerlinShpUtils;
import org.matsim.run.drt.RunDrtOpenBerlinScenario;
Expand Down Expand Up @@ -245,9 +237,9 @@ public final void createVehiclesByRandomPointInShape(int amount, int seats, List
public final void createVehiclesAtPRStation(PRStation station, int amount, int seats, List<DvrpVehicleSpecification> vehicles) {

for (int i = 1; i <= amount; i++) {
Link link = drtNetwork.getLinks().get(station.linkId);
Link link = drtNetwork.getLinks().get(station.getLinkId());

if(link == null) log.warn("will crash. station link id was " + station.linkId);
if(link == null) log.warn("will crash. station link id was " + station.getLinkId());

vehicles.add(ImmutableDvrpVehicleSpecification.newBuilder().id(Id.create("drt-" + station.getName() + "-" + i, DvrpVehicle.class))
.startLinkId(link.getId())
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/org/matsim/run/replaceCarByDRT/PRStation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* *********************************************************************** *
* project: org.matsim.*
* Controler.java
* *
* *********************************************************************** *
* *
* copyright : (C) 2007 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* *********************************************************************** */

package org.matsim.run.replaceCarByDRT;

import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.network.Link;
class PRStation {

private final String name;
private final Id<Link> linkId;
private final Coord coord;

PRStation(String name, Id<Link> linkId, Coord coord) {
this.name = name;
this.linkId = linkId;
this.coord = coord;
}

protected Coord getCoord() {
return coord;
}

protected String getName() {
return name;
}

protected Id<Link> getLinkId() {
return linkId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void handleEvent(ActivityEndEvent event) {
@Nullable
private PRStation getPRStationWithCoord(Coord coord){
for (PRStation prStation : activitiesPerPRStation.keySet()) {
if(prStation.coord.equals(coord)) return prStation;
if(prStation.getCoord().equals(coord)) return prStation;
}
return null;
}
Expand Down Expand Up @@ -209,7 +209,7 @@ private void writeAgentsPerPRStation(String outputFileName) throws IOException {
MutableInt agentsPerPRStation = entry.getValue();


writer.writeNext(new String[]{station.getName(),String.valueOf(agentsPerPRStation),String.valueOf(station.coord.getX()),String.valueOf(station.coord.getY())});
writer.writeNext(new String[]{station.getName(),String.valueOf(agentsPerPRStation),String.valueOf(station.getCoord().getX()),String.valueOf(station.getCoord().getY())});
}
writer.close();
}
Expand Down
34 changes: 10 additions & 24 deletions src/main/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRT.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.population.*;
import org.matsim.contrib.common.util.StraightLineKnnFinder;
import org.matsim.core.gbl.Gbl;
import org.matsim.core.network.algorithms.MultimodalNetworkCleaner;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.routes.NetworkRoute;
Expand All @@ -56,6 +57,8 @@ class ReplaceCarByDRT {
static final String TRIP_TYPE_ATTR_KEY = "tripType";
static final String PR_ACTIVITY_TYPE = "P+R";

private static boolean hasWarnedHardcodedChainMode = false;

/**
*
* @param scenario
Expand Down Expand Up @@ -247,7 +250,7 @@ static void prepareInputPlansForCarProhibitionWithPRLogic(Scenario scenario,

Activity parkAndRideAct = fac.createActivityFromCoord(PR_ACTIVITY_TYPE, prStation.getCoord());
parkAndRideAct.setMaximumDuration(5 * 60);
parkAndRideAct.setLinkId(prStation.linkId);
parkAndRideAct.setLinkId(prStation.getLinkId());

newTrip = new ArrayList<>();
Leg l1 = fac.createLeg(replacingMode);
Expand Down Expand Up @@ -290,7 +293,7 @@ static void prepareInputPlansForCarProhibitionWithPRLogic(Scenario scenario,
// Activity parkAndRideAct = fac.createActivityFromLinkId(PR_ACTIVITY_TYPE, prStation);
Activity parkAndRideAct = fac.createActivityFromCoord(PR_ACTIVITY_TYPE, prStation.getCoord());
parkAndRideAct.setMaximumDuration(5 * 60);
parkAndRideAct.setLinkId(prStation.linkId);
parkAndRideAct.setLinkId(prStation.getLinkId());

newTrip = new ArrayList<>();

Expand Down Expand Up @@ -362,7 +365,11 @@ private static Plan createPTOnlyPlan(Plan originalPlan, boolean enforceMassConse
.findAny().isPresent();

if(subtourTouchesProhibitionZoneWithCarOrRide){
log.warn("assuming (with hardcoding) that ride is considered as non-chain-based and car is chain-based");
if(!hasWarnedHardcodedChainMode){
log.warn("assuming (with hardcoding) that ride is considered as non-chain-based and car is chain-based");
log.warn(Gbl.ONLYONCE);
hasWarnedHardcodedChainMode = true;
}
//if car is used, change all trips to pt
if(subtour.getTripsWithoutSubSubtours().stream()
.filter(trip -> mainModeIdentifier.identifyMainMode(trip.getTripElements()).equals(TransportMode.car))
Expand Down Expand Up @@ -558,24 +565,3 @@ enum PRStationChoice{
}


//TODO extract class
class PRStation {

private String name;
Id<Link> linkId;
Coord coord;

PRStation(String name, Id<Link> linkId, Coord coord){
this.name = name;
this.linkId = linkId;
this.coord = coord;
}

public Coord getCoord() {
return coord;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.population.*;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.population.PopulationUtils;
Expand All @@ -20,7 +23,8 @@
import java.util.*;
import java.util.stream.Collectors;

import static org.matsim.run.replaceCarByDRT.ReplaceCarByDRT.*;
import static org.matsim.run.replaceCarByDRT.ReplaceCarByDRT.PR_ACTIVITY_TYPE;
import static org.matsim.run.replaceCarByDRT.ReplaceCarByDRT.readPRStationFile;
import static org.matsim.run.replaceCarByDRT.RunBerlinNoInnerCarTripsScenario.URL_2_PR_STATIONS;

/**
Expand Down Expand Up @@ -173,15 +177,15 @@ public void testBrandenburgerWith2PRActivities(){
Assert.assertEquals(2, prActs.size()); //nr of PR acts

if(scenario.equals(SCENARIO_CLOSEST_INSIDE)) {
Assert.assertEquals(PR_STATIONS.get("Innsbrucker").linkId,prActs.get(0).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Innsbrucker").coord,prActs.get(0).getCoord());
Assert.assertEquals(PR_STATIONS.get("Innsbrucker").linkId,prActs.get(1).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Innsbrucker").coord,prActs.get(1).getCoord());
Assert.assertEquals(PR_STATIONS.get("Innsbrucker").getLinkId(),prActs.get(0).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Innsbrucker").getCoord(),prActs.get(0).getCoord());
Assert.assertEquals(PR_STATIONS.get("Innsbrucker").getLinkId(),prActs.get(1).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Innsbrucker").getCoord(),prActs.get(1).getCoord());
} else if(scenario.equals(SCENARIO_CLOSEST_OUTSIDE)) {
Assert.assertEquals(PR_STATIONS.get("Bundesplatz").linkId,prActs.get(0).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Bundesplatz").coord,prActs.get(0).getCoord());
Assert.assertEquals(PR_STATIONS.get("Bundesplatz").linkId,prActs.get(1).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Bundesplatz").coord,prActs.get(1).getCoord());
Assert.assertEquals(PR_STATIONS.get("Bundesplatz").getLinkId(),prActs.get(0).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Bundesplatz").getCoord(),prActs.get(0).getCoord());
Assert.assertEquals(PR_STATIONS.get("Bundesplatz").getLinkId(),prActs.get(1).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Bundesplatz").getCoord(),prActs.get(1).getCoord());
}


Expand Down Expand Up @@ -211,14 +215,14 @@ public void testBrandenburgerWith4PRActivities(){
if(!plan.getType().equals("ptOnly")){
Assert.assertEquals(4, prActs.size()); //nr of PR acts

Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").linkId,prActs.get(0).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").coord,prActs.get(0).getCoord());
Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").linkId,prActs.get(1).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").coord,prActs.get(1).getCoord());
Assert.assertEquals(PR_STATIONS.get("Wedding").linkId,prActs.get(2).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Wedding").coord,prActs.get(2).getCoord());
Assert.assertEquals(PR_STATIONS.get("Wedding").linkId,prActs.get(3).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Wedding").coord,prActs.get(3).getCoord());
Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").getLinkId(),prActs.get(0).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").getCoord(),prActs.get(0).getCoord());
Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").getLinkId(),prActs.get(1).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").getCoord(),prActs.get(1).getCoord());
Assert.assertEquals(PR_STATIONS.get("Wedding").getLinkId(),prActs.get(2).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Wedding").getCoord(),prActs.get(2).getCoord());
Assert.assertEquals(PR_STATIONS.get("Wedding").getLinkId(),prActs.get(3).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Wedding").getCoord(),prActs.get(3).getCoord());

} else {
Assert.assertEquals(0, prActs.size()); //nr of PR acts
Expand All @@ -245,14 +249,14 @@ public void testInhabitantOfProhibitionZoneWithMultipleBorderCrossings(){
if(!plan.getType().equals("ptOnly")){
Assert.assertEquals(4, prActs.size()); //nr of PR acts

Assert.assertEquals(PR_STATIONS.get("Jungfernheide").linkId,prActs.get(0).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Jungfernheide").coord,prActs.get(0).getCoord());
Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").linkId,prActs.get(1).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").coord,prActs.get(1).getCoord());
Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").linkId,prActs.get(2).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").coord,prActs.get(2).getCoord());
Assert.assertEquals(PR_STATIONS.get("Jungfernheide").linkId,prActs.get(3).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Jungfernheide").coord,prActs.get(3).getCoord());
Assert.assertEquals(PR_STATIONS.get("Jungfernheide").getLinkId(),prActs.get(0).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Jungfernheide").getCoord(),prActs.get(0).getCoord());
Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").getLinkId(),prActs.get(1).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").getCoord(),prActs.get(1).getCoord());
Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").getLinkId(),prActs.get(2).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").getCoord(),prActs.get(2).getCoord());
Assert.assertEquals(PR_STATIONS.get("Jungfernheide").getLinkId(),prActs.get(3).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Jungfernheide").getCoord(),prActs.get(3).getCoord());

} else {
Assert.assertEquals(0, prActs.size()); //nr of PR acts
Expand Down Expand Up @@ -366,15 +370,15 @@ public void testRiderWithDifferentPRStations(){

if(scenario.equals(SCENARIO_CLOSEST_INSIDE)) {
// here it´s actually same PRStation. TODO: find better agent to test?
Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").linkId,prActs.get(0).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").coord,prActs.get(0).getCoord());
Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").linkId,prActs.get(1).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").coord,prActs.get(1).getCoord());
Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").getLinkId(),prActs.get(0).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").getCoord(),prActs.get(0).getCoord());
Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").getLinkId(),prActs.get(1).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").getCoord(),prActs.get(1).getCoord());
} else if(scenario.equals(SCENARIO_CLOSEST_OUTSIDE)) {
Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").linkId,prActs.get(0).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").coord,prActs.get(0).getCoord());
Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").linkId,prActs.get(1).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").coord,prActs.get(1).getCoord());
Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").getLinkId(),prActs.get(0).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").getCoord(),prActs.get(0).getCoord());
Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").getLinkId(),prActs.get(1).getLinkId());
Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").getCoord(),prActs.get(1).getCoord());
}


Expand Down

0 comments on commit ce1b77b

Please sign in to comment.