From d49288aebaeb5554bc3a918820a96a40dd39a7bf Mon Sep 17 00:00:00 2001 From: Raoul Date: Tue, 5 Mar 2019 18:00:42 +0100 Subject: [PATCH 1/6] Update TripCSVToMATSimPlan.java --- .../TripCSVToMATSimPlan.java | 133 +++++++++++++++++- 1 file changed, 130 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java b/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java index afe0e429..f2685176 100644 --- a/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java +++ b/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java @@ -1,14 +1,141 @@ package de.tum.bgu.msm.modules.trafficAssignment; +import java.util.HashMap; +import java.util.Map; + +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.network.Network; +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.api.core.v01.population.Population; +import org.matsim.api.core.v01.population.PopulationFactory; +import org.matsim.api.core.v01.population.PopulationWriter; +import org.matsim.core.config.Config; +import org.matsim.core.config.ConfigUtils; +import org.matsim.core.network.NetworkUtils; +import org.matsim.core.population.PopulationUtils; +import org.matsim.core.scenario.ScenarioUtils; + +import de.tum.bgu.msm.data.Purpose; +import de.tum.bgu.msm.io.input.readers.GenericCsvReader; +import de.tum.bgu.msm.io.input.readers.GenericCsvReader.GenericCsvTable; + public class TripCSVToMATSimPlan { - //This class will read trip lists in CSV from MITO and creates a MATSim XML plan file + // This class will read trip lists in CSV from MITO and creates a MATSim XML + // plan file + + private static String[] columns = { "id", "originX", "originY", "destinationX", "destinationY", "purpose", "person", + "mode", "distance", "departure_time", "departure_time_return" }; + + private static String filename; + private static GenericCsvTable table; + private static PopulationFactory factory; + private static Network network; + private static Map indexes = new HashMap(); + + public static void main(String[] args) { + filename = args[0]; + String networkFile = args[1]; + + // TODO add logging + GenericCsvReader reader = new GenericCsvReader(filename); + reader.read(); + table = reader.getTable(); + + for (String column : columns) { + indexes.put(column, table.getColumnIndexOf(column)); + } + + Config config = ConfigUtils.createConfig(); + config.network().setInputFile(networkFile); + + Scenario scenario = ScenarioUtils.loadScenario(config); + network = scenario.getNetwork(); + + Population population = PopulationUtils.createPopulation(config); + factory = population.getFactory(); + + for (int i = 0; i < table.getRowCount(); i++) { + population.addPerson(createPersonFromTrips(i)); + } + + PopulationWriter popwriter = new PopulationWriter(population); + popwriter.write(filename + ".xml.gz"); + + System.out.println("done."); + } + + private static Person createPersonFromTrips(int i) { + Id matsimId = Id.createPersonId(table.getString(i, indexes.get("id")) + "_" + i); + + Person p = factory.createPerson(Id.createPersonId(matsimId)); + Plan plan = factory.createPlan(); - public static void main(String[] args){ + Purpose purpose = Purpose.valueOf(table.getString(i, indexes.get("purpose"))); + boolean roundTrip = !(purpose.equals(Purpose.NHBW) || purpose.equals(Purpose.NHBO)); + + String firstActivityType = MatsimPopulationGenerator.getOriginActivity(purpose); + Coord firstCoord = new Coord(table.getDouble(i, indexes.get("originX")), + table.getDouble(i, indexes.get("originY"))); + Activity firstAct = factory.createActivityFromCoord(firstActivityType, firstCoord); + firstAct.setLinkId(NetworkUtils.getNearestLink(network, firstCoord).getId()); + Double departureTime = table.getDouble(i, indexes.get("departure_time")); + firstAct.setEndTime(departureTime); + plan.addActivity(firstAct); + String mode = decodeMode(table.getString(i, indexes.get("mode"))); + Leg firstLeg = factory.createLeg(mode); + firstLeg.setDepartureTime(departureTime); + plan.addLeg(firstLeg); + + String secondActivityType = MatsimPopulationGenerator.getDestinationActivity(purpose); + Coord secondCoord = new Coord(table.getDouble(i, indexes.get("destinationX")), + table.getDouble(i, indexes.get("destinationY"))); + + Activity secondAct = factory.createActivityFromCoord(secondActivityType, secondCoord); + secondAct.setLinkId(NetworkUtils.getNearestLink(network, secondCoord).getId()); + secondAct.setStartTime(departureTime + 1); // TODO include MITO's travel time estimations + + Double departureTimeReturn = null; + if (roundTrip) { + departureTimeReturn = table.getDouble(i, indexes.get("departure_time_return")); + secondAct.setEndTime(departureTimeReturn); + } + plan.addActivity(secondAct); + + if (roundTrip) { + Leg secondLeg = factory.createLeg(mode); + secondLeg.setDepartureTime(departureTimeReturn); + plan.addLeg(secondLeg); + + Activity thirdAct = factory.createActivityFromCoord(firstActivityType, firstCoord); + thirdAct.setLinkId(NetworkUtils.getNearestLink(network, firstCoord).getId()); + thirdAct.setStartTime(departureTimeReturn + 1); // TODO include MITO's travel time estimations + plan.addActivity(thirdAct); + } + p.addPlan(plan); + p.setSelectedPlan(plan); + return p; + } - } + private static String decodeMode(String encodedMode) { + switch (encodedMode) { + case "autoDriver": + return "car"; + case "train": + case "bus": + case "tramOrMetro": + return "pt"; + default: + return encodedMode; + } + } } From 2db2f38aff692c9e596766135927c11302020554 Mon Sep 17 00:00:00 2001 From: Raoul Date: Tue, 5 Mar 2019 22:17:11 +0100 Subject: [PATCH 2/6] Update TripCSVToMATSimPlan.java --- .../TripCSVToMATSimPlan.java | 127 +++++++++++------- 1 file changed, 81 insertions(+), 46 deletions(-) diff --git a/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java b/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java index f2685176..23ff1b45 100644 --- a/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java +++ b/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java @@ -1,8 +1,8 @@ package de.tum.bgu.msm.modules.trafficAssignment; -import java.util.HashMap; -import java.util.Map; - +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; import org.matsim.api.core.v01.Coord; import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.Scenario; @@ -21,36 +21,22 @@ import org.matsim.core.scenario.ScenarioUtils; import de.tum.bgu.msm.data.Purpose; -import de.tum.bgu.msm.io.input.readers.GenericCsvReader; -import de.tum.bgu.msm.io.input.readers.GenericCsvReader.GenericCsvTable; public class TripCSVToMATSimPlan { // This class will read trip lists in CSV from MITO and creates a MATSim XML // plan file - - private static String[] columns = { "id", "originX", "originY", "destinationX", "destinationY", "purpose", "person", - "mode", "distance", "departure_time", "departure_time_return" }; + private static String delimiter = ","; private static String filename; - private static GenericCsvTable table; private static PopulationFactory factory; private static Network network; - private static Map indexes = new HashMap(); public static void main(String[] args) { + // TODO add logging filename = args[0]; String networkFile = args[1]; - // TODO add logging - GenericCsvReader reader = new GenericCsvReader(filename); - reader.read(); - table = reader.getTable(); - - for (String column : columns) { - indexes.put(column, table.getColumnIndexOf(column)); - } - Config config = ConfigUtils.createConfig(); config.network().setInputFile(networkFile); @@ -60,8 +46,28 @@ public static void main(String[] args) { Population population = PopulationUtils.createPopulation(config); factory = population.getFactory(); - for (int i = 0; i < table.getRowCount(); i++) { - population.addPerson(createPersonFromTrips(i)); + try { + FileReader in = null; + BufferedReader br = null; + try { + in = new FileReader(filename); + br = new BufferedReader(in); + + String line; + int i = 0; + br.readLine(); // skip CSV header + while ((line = br.readLine()) != null) { + population.addPerson(createPersonFromTrip(i++, line)); + } + } finally { + if (br != null) + br.close(); + + if (in != null) + in.close(); + } + } catch (IOException e) { + e.printStackTrace(); } PopulationWriter popwriter = new PopulationWriter(population); @@ -70,54 +76,50 @@ public static void main(String[] args) { System.out.println("done."); } - private static Person createPersonFromTrips(int i) { - Id matsimId = Id.createPersonId(table.getString(i, indexes.get("id")) + "_" + i); + private static Person createPersonFromTrip(int i, String line) { + Trip t = new Trip(line); + + Id matsimId = Id.createPersonId(t.person + "_" + i); Person p = factory.createPerson(Id.createPersonId(matsimId)); Plan plan = factory.createPlan(); - Purpose purpose = Purpose.valueOf(table.getString(i, indexes.get("purpose"))); + Purpose purpose = Purpose.valueOf(t.purpose); boolean roundTrip = !(purpose.equals(Purpose.NHBW) || purpose.equals(Purpose.NHBO)); - + String firstActivityType = MatsimPopulationGenerator.getOriginActivity(purpose); - Coord firstCoord = new Coord(table.getDouble(i, indexes.get("originX")), - table.getDouble(i, indexes.get("originY"))); + Coord firstCoord = new Coord(t.originX, t.originY); Activity firstAct = factory.createActivityFromCoord(firstActivityType, firstCoord); firstAct.setLinkId(NetworkUtils.getNearestLink(network, firstCoord).getId()); - Double departureTime = table.getDouble(i, indexes.get("departure_time")); - firstAct.setEndTime(departureTime); + firstAct.setEndTime(t.departure_time); plan.addActivity(firstAct); - String mode = decodeMode(table.getString(i, indexes.get("mode"))); + String mode = decodeMode(t.mode); Leg firstLeg = factory.createLeg(mode); - firstLeg.setDepartureTime(departureTime); + firstLeg.setDepartureTime(t.departure_time); plan.addLeg(firstLeg); - + String secondActivityType = MatsimPopulationGenerator.getDestinationActivity(purpose); - Coord secondCoord = new Coord(table.getDouble(i, indexes.get("destinationX")), - table.getDouble(i, indexes.get("destinationY"))); - + Coord secondCoord = new Coord(t.destinationX, t.destinationY); + Activity secondAct = factory.createActivityFromCoord(secondActivityType, secondCoord); secondAct.setLinkId(NetworkUtils.getNearestLink(network, secondCoord).getId()); - secondAct.setStartTime(departureTime + 1); // TODO include MITO's travel time estimations - - Double departureTimeReturn = null; - if (roundTrip) { - departureTimeReturn = table.getDouble(i, indexes.get("departure_time_return")); - secondAct.setEndTime(departureTimeReturn); - } + secondAct.setStartTime(t.departure_time + 1); // TODO include MITO's travel time estimations + + if (roundTrip) + secondAct.setEndTime(t.departure_time_return); plan.addActivity(secondAct); - + if (roundTrip) { Leg secondLeg = factory.createLeg(mode); - secondLeg.setDepartureTime(departureTimeReturn); + secondLeg.setDepartureTime(t.departure_time_return); plan.addLeg(secondLeg); - + Activity thirdAct = factory.createActivityFromCoord(firstActivityType, firstCoord); thirdAct.setLinkId(NetworkUtils.getNearestLink(network, firstCoord).getId()); - thirdAct.setStartTime(departureTimeReturn + 1); // TODO include MITO's travel time estimations + thirdAct.setStartTime(t.departure_time_return + 1); // TODO include MITO's travel time estimations plan.addActivity(thirdAct); } @@ -134,8 +136,41 @@ private static String decodeMode(String encodedMode) { case "bus": case "tramOrMetro": return "pt"; + case "bicycle": + return "bike"; default: return encodedMode; } } + + public final static class Trip { + public final double originX; + public final double originY; + public final double destinationX; + public final double destinationY; + public final String purpose; + public final String person; + public final double distance; + public final String mode; + public final double departure_time; + public final double departure_time_return; + + public Trip(String line) { + String[] data = line.split(delimiter); + this.originX = Double.parseDouble(data[2]); + this.originY = Double.parseDouble(data[3]); + this.destinationX = Double.parseDouble(data[4]); + this.destinationY = Double.parseDouble(data[5]); + this.purpose = data[7]; + this.person = data[8]; + this.distance = Double.parseDouble(data[9]); + this.mode = data[14]; + this.departure_time = Double.parseDouble(data[15]); + + if (data.length >= 17) + this.departure_time_return = Double.parseDouble(data[16]); + else + this.departure_time_return = -1; + } + } } From 51e8f4023448fed345ff141de8e39018467e7cfd Mon Sep 17 00:00:00 2001 From: Raoul Date: Tue, 5 Mar 2019 22:21:53 +0100 Subject: [PATCH 3/6] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1c2ba5fe..d8912c9f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ out .idea/ *.iml +*.log From dc1af90db7aa908796925685fbde889b1c15cce3 Mon Sep 17 00:00:00 2001 From: Raoul Date: Wed, 6 Mar 2019 10:20:45 +0100 Subject: [PATCH 4/6] Filter "autoPassenger" from MATSim population --- .../TripCSVToMATSimPlan.java | 69 ++++++++++--------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java b/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java index 23ff1b45..0aee6f5c 100644 --- a/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java +++ b/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java @@ -57,7 +57,9 @@ public static void main(String[] args) { int i = 0; br.readLine(); // skip CSV header while ((line = br.readLine()) != null) { - population.addPerson(createPersonFromTrip(i++, line)); + Person p = createPersonFromTrip(i++, line); + if (p != null) + population.addPerson(p); } } finally { if (br != null) @@ -78,6 +80,10 @@ public static void main(String[] args) { private static Person createPersonFromTrip(int i, String line) { Trip t = new Trip(line); + + String mode = decodeMode(t.mode); + if (mode.equals("autoPassenger")) + return null; Id matsimId = Id.createPersonId(t.person + "_" + i); @@ -95,8 +101,7 @@ private static Person createPersonFromTrip(int i, String line) { firstAct.setEndTime(t.departure_time); plan.addActivity(firstAct); - - String mode = decodeMode(t.mode); + Leg firstLeg = factory.createLeg(mode); firstLeg.setDepartureTime(t.departure_time); plan.addLeg(firstLeg); @@ -108,7 +113,7 @@ private static Person createPersonFromTrip(int i, String line) { secondAct.setLinkId(NetworkUtils.getNearestLink(network, secondCoord).getId()); secondAct.setStartTime(t.departure_time + 1); // TODO include MITO's travel time estimations - if (roundTrip) + if (roundTrip) secondAct.setEndTime(t.departure_time_return); plan.addActivity(secondAct); @@ -144,33 +149,33 @@ private static String decodeMode(String encodedMode) { } public final static class Trip { - public final double originX; - public final double originY; - public final double destinationX; - public final double destinationY; - public final String purpose; - public final String person; - public final double distance; - public final String mode; - public final double departure_time; - public final double departure_time_return; - - public Trip(String line) { - String[] data = line.split(delimiter); - this.originX = Double.parseDouble(data[2]); - this.originY = Double.parseDouble(data[3]); - this.destinationX = Double.parseDouble(data[4]); - this.destinationY = Double.parseDouble(data[5]); - this.purpose = data[7]; - this.person = data[8]; - this.distance = Double.parseDouble(data[9]); - this.mode = data[14]; - this.departure_time = Double.parseDouble(data[15]); - - if (data.length >= 17) - this.departure_time_return = Double.parseDouble(data[16]); - else - this.departure_time_return = -1; - } + public final double originX; + public final double originY; + public final double destinationX; + public final double destinationY; + public final String purpose; + public final String person; + public final double distance; + public final String mode; + public final double departure_time; + public final double departure_time_return; + + public Trip(String line) { + String[] data = line.split(delimiter); + this.originX = Double.parseDouble(data[2]); + this.originY = Double.parseDouble(data[3]); + this.destinationX = Double.parseDouble(data[4]); + this.destinationY = Double.parseDouble(data[5]); + this.purpose = data[7]; + this.person = data[8]; + this.distance = Double.parseDouble(data[9]); + this.mode = data[14]; + this.departure_time = Double.parseDouble(data[15]); + + if (data.length >= 17) + this.departure_time_return = Double.parseDouble(data[16]); + else + this.departure_time_return = -1; + } } } From 07b0b9ad75f1465654915ab78d263184bd19e036 Mon Sep 17 00:00:00 2001 From: Raoul Date: Thu, 7 Mar 2019 22:01:40 +0100 Subject: [PATCH 5/6] All activities will be on car network links (instead of also on pt links) --- .../trafficAssignment/TripCSVToMATSimPlan.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java b/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java index 0aee6f5c..ef912ac1 100644 --- a/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java +++ b/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java @@ -3,6 +3,9 @@ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + import org.matsim.api.core.v01.Coord; import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.Scenario; @@ -17,6 +20,7 @@ import org.matsim.core.config.Config; import org.matsim.core.config.ConfigUtils; import org.matsim.core.network.NetworkUtils; +import org.matsim.core.network.algorithms.TransportModeNetworkFilter; import org.matsim.core.population.PopulationUtils; import org.matsim.core.scenario.ScenarioUtils; @@ -30,7 +34,7 @@ public class TripCSVToMATSimPlan { private static String filename; private static PopulationFactory factory; - private static Network network; + private static Network carNetwork; public static void main(String[] args) { // TODO add logging @@ -41,7 +45,11 @@ public static void main(String[] args) { config.network().setInputFile(networkFile); Scenario scenario = ScenarioUtils.loadScenario(config); - network = scenario.getNetwork(); + TransportModeNetworkFilter filter = new TransportModeNetworkFilter(scenario.getNetwork()); + Set modesCar = new HashSet<>(); + modesCar.add("car"); + carNetwork = NetworkUtils.createNetwork(); + filter.filter(carNetwork, modesCar); Population population = PopulationUtils.createPopulation(config); factory = population.getFactory(); @@ -97,7 +105,7 @@ private static Person createPersonFromTrip(int i, String line) { Coord firstCoord = new Coord(t.originX, t.originY); Activity firstAct = factory.createActivityFromCoord(firstActivityType, firstCoord); - firstAct.setLinkId(NetworkUtils.getNearestLink(network, firstCoord).getId()); + firstAct.setLinkId(NetworkUtils.getNearestLink(carNetwork, firstCoord).getId()); firstAct.setEndTime(t.departure_time); plan.addActivity(firstAct); @@ -110,7 +118,7 @@ private static Person createPersonFromTrip(int i, String line) { Coord secondCoord = new Coord(t.destinationX, t.destinationY); Activity secondAct = factory.createActivityFromCoord(secondActivityType, secondCoord); - secondAct.setLinkId(NetworkUtils.getNearestLink(network, secondCoord).getId()); + secondAct.setLinkId(NetworkUtils.getNearestLink(carNetwork, secondCoord).getId()); secondAct.setStartTime(t.departure_time + 1); // TODO include MITO's travel time estimations if (roundTrip) @@ -123,7 +131,7 @@ private static Person createPersonFromTrip(int i, String line) { plan.addLeg(secondLeg); Activity thirdAct = factory.createActivityFromCoord(firstActivityType, firstCoord); - thirdAct.setLinkId(NetworkUtils.getNearestLink(network, firstCoord).getId()); + thirdAct.setLinkId(NetworkUtils.getNearestLink(carNetwork, firstCoord).getId()); thirdAct.setStartTime(t.departure_time_return + 1); // TODO include MITO's travel time estimations plan.addActivity(thirdAct); } From f62d8c5d9dfd1397acf017c5a525766b5c863125 Mon Sep 17 00:00:00 2001 From: Raoul Date: Thu, 7 Mar 2019 22:38:07 +0100 Subject: [PATCH 6/6] Include autoPassengers in MATSim population with teleportation-based transport mode --- .../msm/modules/trafficAssignment/TripCSVToMATSimPlan.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java b/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java index ef912ac1..669e5dfe 100644 --- a/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java +++ b/src/main/java/de/tum/bgu/msm/modules/trafficAssignment/TripCSVToMATSimPlan.java @@ -90,9 +90,6 @@ private static Person createPersonFromTrip(int i, String line) { Trip t = new Trip(line); String mode = decodeMode(t.mode); - if (mode.equals("autoPassenger")) - return null; - Id matsimId = Id.createPersonId(t.person + "_" + i); Person p = factory.createPerson(Id.createPersonId(matsimId)); @@ -145,6 +142,8 @@ private static String decodeMode(String encodedMode) { switch (encodedMode) { case "autoDriver": return "car"; + case "autoPassenger": + return "car_passenger"; case "train": case "bus": case "tramOrMetro":