MATSIM
RunZPopulationGenerator.java
Go to the documentation of this file.
1 package tutorial.programming.example08DemandGeneration;
2 
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.TreeMap;
7 
8 import org.matsim.api.core.v01.Coord;
9 import org.matsim.api.core.v01.Id;
10 import org.matsim.api.core.v01.Scenario;
21 
26 
27  public static void main(String args[]) {
28 
29  /*
30  * Create population from sample input data.
31  */
32  Scenario scenario = createPopulationFromCensusFile("./input/input_sample_zurich.txt");
33 
34  /*
35  * Write population to file.
36  */
37  PopulationWriter populationWriter = new PopulationWriter(scenario.getPopulation(), scenario.getNetwork());
38  populationWriter.write("./input/population.xml");
39 
40  }
41 
42  private static Scenario createPopulationFromCensusFile(String censusFile) {
43 
45 
46  /*
47  * Use Parser to read the census sample file.
48  */
49  List<ZCensusEntry> censusEntries = new ZCensusParser().readFile(censusFile);
50 
51  /*
52  * Get Population and PopulationFactory objects.
53  * The Population contains all created Agents and their plans,
54  * the PopulationFactory should be used to create Plans,
55  * Activities, Legs and so on.
56  */
57  Population population = scenario.getPopulation();
58  PopulationFactory populationFactory = population.getFactory();
59 
60 
61  /*
62  * The census file contains one line per trip, meaning a typical person
63  * is represented by multiple lines / trips. Therefore in a first step
64  * we have to sort the trips based on the person who executes them.
65  */
66 
67  /*
68  * Create a Map with the PersonIds as key and a list of CensusEntry as values.
69  */
70  Map<Integer, List<ZCensusEntry>> personEntryMapping = new TreeMap<>();
71  for (ZCensusEntry censusEntry : censusEntries) {
72  /*
73  * If the Map already contains an entry for the current person
74  * the list will not be null.
75  */
76  List<ZCensusEntry> entries = personEntryMapping.get(censusEntry.id_person);
77 
78  /*
79  * If no mapping exists -> create a new one
80  */
81  if (entries == null) {
82  entries = new ArrayList<>();
83  personEntryMapping.put(censusEntry.id_person, entries);
84  }
85 
86  /*
87  * Add currently processed entry to the list
88  */
89  entries.add(censusEntry);
90  }
91 
92  /*
93  * Now create a plan for each person - iterate over all entries in the map.
94  */
95  for (List<ZCensusEntry> personEntries : personEntryMapping.values()) {
96  /*
97  * Get the first entry from the list - it will never be null.
98  */
99  ZCensusEntry entry = personEntries.get(0);
100 
101  /*
102  * Get id of the person from the censusEntry.
103  */
104  int idPerson = entry.id_person;
105 
106  /*
107  * Create new person and add it to the population.
108  * Use scenario.createId(String id) to create the Person's Id.
109  */
110  Person person = populationFactory.createPerson(Id.create(idPerson, Person.class));
111  population.addPerson(person);
112 
113 
114  /*
115  * Create new plan and add it to the person.
116  */
117  Plan plan = populationFactory.createPlan();
118  person.addPlan(plan);
119 
120  /*
121  * Every Agent has at least one activity which is being at home.
122  * - set the activity type to "home"
123  * - set the start time to 0.0
124  * - add the Activity to the plan.
125  */
126  Coord homeCoord = new Coord(entry.h_x, entry.h_y);
127  Activity homeActivity = populationFactory.createActivityFromCoord("home", homeCoord);
128  homeActivity.setStartTime(0.0);
129  plan.addActivity(homeActivity);
130 
131  /*
132  * Create objects that are needed when creating the other
133  * Activities and Legs of the Plan.
134  *
135  * Mind that we have to set a start and end time for each Activity
136  * (except the last one - it will last until the end of the simulated
137  * period). The end time of an Activity equals the departure time of
138  * the next Trip. We set the end time of an Activity when we process
139  * the next Trip by using a point to the last previously created
140  * Activity (initially this is the Home Activity).
141  */
142 
143  Coord endCoord = null;
144  String transportMode = null;
145  Leg leg = null;
146  Activity activity = null;
147  Activity previousActivity = homeActivity;
148 
149  /*
150  * Create person's Trips and add them to the Plan.
151  */
152  for (ZCensusEntry personEntry : personEntries) {
153  endCoord = new Coord(personEntry.d_x, personEntry.d_y);
154  transportMode = getTransportMode(personEntry.tripmode);
155  String activityType = getActivityType(personEntry.trippurpose);
156 
157  /*
158  * Create a new Leg using the PopulationFactory and set its parameters.
159  * Mind that MATSim uses seconds as time unit whereas the census uses minutes.
160  */
161  leg = populationFactory.createLeg(transportMode);
162  leg.setDepartureTime(personEntry.starttime * 60);
163  leg.setTravelTime(personEntry.tripduration * 60);
164  previousActivity.setEndTime(personEntry.starttime * 60);
165 
166  /*
167  * Create a new Activity using the Population Factory and set its parameters.
168  */
169  activity = populationFactory.createActivityFromCoord(activityType, endCoord);
170  activity.setStartTime(personEntry.starttime * 60 + personEntry.tripduration * 60);
171 
172  /*
173  * Add the Leg and the Activity to the plan.
174  */
175  plan.addLeg(leg);
176  plan.addActivity(activity);
177 
178  /*
179  * Do not forget to update the pointer to the previousActivity.
180  */
181  previousActivity = activity;
182  }
183 
184  /*
185  * ... and finally: If the last Activity takes place at the Home Coordinates
186  * we assume that the Agent is performing a "home" Activity.
187  */
188  if (activity.getCoord().equals(homeCoord)) {
189  activity.setType("home");
190  }
191  }
192  return scenario;
193 
194  }
195 
196  /*
197  * Helper methods that convert the entries from the census file.
198  */
199 
200  private static String getTransportMode(int mode) {
201  switch (mode) {
202  case 1: return TransportMode.walk;
203  case 2: return TransportMode.bike;
204  case 3: return TransportMode.car;
205  case 4: return TransportMode.pt;
206  case 5: return "undefined";
207 
208  default: return "undefined";
209  }
210  }
211 
212  private static String getActivityType(int activityType) {
213  switch (activityType) {
214  case 1: return "work";
215  case 2: return "education";
216  case 3: return "shop";
217  case 4: return "leisure";
218  case 5: return "other";
219 
220  default: return "undefined";
221  }
222  }
223 
224 }
abstract void addLeg(final Leg leg)
void setDepartureTime(final double seconds)
abstract void addActivity(final Activity act)
static Config createConfig(final String filename)
boolean equals(final Object other)
Definition: Coord.java:94
void setTravelTime(final double seconds)
Activity createActivityFromCoord(String actType, Coord coord)
static Scenario createScenario(final Config config)
void setEndTime(final double seconds)