MATSIM
CreateSelectedPlansTables.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * CreateSelectedPlansTables.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2008 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.run;
22 
23 import java.io.BufferedWriter;
24 import java.io.IOException;
25 
26 import org.matsim.api.core.v01.Coord;
27 import org.matsim.api.core.v01.Id;
28 import org.matsim.api.core.v01.Scenario;
37 import org.matsim.core.config.Config;
39 import org.matsim.core.gbl.Gbl;
46 import org.matsim.core.utils.io.IOUtils;
49 import org.matsim.lanes.Lanes;
52 
62 
63  private Population plans0;
64  private Population plans1;
65 
66  private final static String outfileTable="./output/analyseSelectedPlansTable.txt";
67  private final static String outfileAverages="./output/analyseSelectedPlansTableAverages.txt";
68 
69  // true if there are two plans to evaluate (compare)
70  private boolean twoPlans;
72 // private NetworkLayer network;
73 
74  private final double [] sumPlanTraveltime={0.0, 0.0};
75  private final double [] sumPlanTraveldistance={0.0, 0.0};
76  private final double [] sumLegTraveltime={0.0, 0.0};
77  private final double [] sumLegTraveldistance={0.0, 0.0};
78  private final int [] sumNrLegs={0, 0};
79 
80 
81 
88  public static void main(final String[] args) {
89 
90  if (args.length < 2 || args.length > 3 ) {
91  System.out.println("Too few arguments.");
92  printUsage();
93  System.exit(1);
94  }
95 
98 
99 
100  if (args.length==3) {
101  table.setTwoPlans(true);
102  table.run(args[0], args[1], args[2]);
103 
104 
105  }
106  else {
107  table.setTwoPlans(false);
108  table.run(args[0], args[1], "");
109 
110  }
111 
113  }
114 
115  private void init(final String networkPath) {
117 
118  this.plans0= ScenarioUtils.createScenario(ConfigUtils.createConfig()).getPopulation();
119  this.plans1= ScenarioUtils.createScenario(ConfigUtils.createConfig()).getPopulation();
120 
121  System.out.println(" reading the network...");
122  new MatsimNetworkReader(this.scenario.getNetwork()).readFile(networkPath);
123  }
124 
125  private void readPlansFile(final String plansfilePath, final Population plans) {
126  System.out.println(" reading file "+plansfilePath);
127  final MatsimReader plansReader = new PopulationReader(new PseudoScenario(this.scenario, plans));
128  plansReader.readFile(plansfilePath);
129  }
130 
131  private void writeAvgFile() {
132 
133  try {
134 
135  final String header="plansfile_nr\tplantraveltime\tplantraveldistance\t" +
136  "legtraveltime\tlegtraveldistance\tnumberoflegs";
137 
138  final BufferedWriter out = IOUtils.getBufferedWriter(outfileAverages);
139  out.write(header);
140  out.write('\n');
141 
142  final int nr_selectedplans=this.plans0.getPersons().size();
143 
144  int i_max=1;
145  if (this.twoPlans) {
146  i_max=2;
147  }
148 
149  for (int i=0; i<i_max; i++) {
150  out.write(i + "\t");
151  out.write(this.sumPlanTraveltime[i]/nr_selectedplans+"\t");
152  out.write(this.sumPlanTraveldistance[i]/nr_selectedplans+"\t");
153  out.write(this.sumLegTraveltime[i]/nr_selectedplans+"\t");
154  out.write(this.sumLegTraveldistance[i]/nr_selectedplans+"\t");
155  out.write(this.sumNrLegs[i]/nr_selectedplans+"\t");
156  out.write('\n');
157  out.flush();
158  }
159  out.close();
160  }
161  catch (final IOException e) {
162  throw new RuntimeException(e);
163  }
164  }
165 
166 
167  private void writeSummaryFile() {
168  try {
169 
170  String header="personid\tsex\tage\tlicense\tcaravail\temployed\thomex\thomey\thomelink\t" +
171  "score0\tplantraveltime0\tplantraveldistance0\tnumberoftrips0\t";
172 
173  if (this.twoPlans) {
174  header=header.concat("score1\tplantraveltime1\tplantraveldistance1\tnumberoftrips1");
175  }
176 
177 
178  final BufferedWriter out = IOUtils.getBufferedWriter(outfileTable);
179  out.write(header);
180  out.write('\n');
181 
182  for (final Id<Person> person_id : this.plans0.getPersons().keySet()) {
183 
184  // method person.toString() not appropriate
185  out.write(person_id.toString()+"\t");
186  final Person person= this.plans0.getPersons().get(person_id);
187  out.write(PersonUtils.getSex(person)+"\t");
188  out.write(PersonUtils.getAge(person)+"\t");
189  out.write(PersonUtils.getLicense(person)+"\t");
190  out.write(PersonUtils.getCarAvail(person)+"\t");
191  if (PersonUtils.isEmployed(person) != null)
192  out.write((PersonUtils.isEmployed(person) ? "yes" : "no"));
193  out.write("\t");
194 
195  Plan selectedPlan = person.getSelectedPlan();
196  Plan selectedPlanImpl = (Plan) selectedPlan;
197  Activity firstActivity = PopulationUtils.getFirstActivity( selectedPlanImpl );
198  Coord c = null;
199  String link_id = "-";
200 
201  if (firstActivity.getType().substring(0,1).equals("h")) {
202  // no home activity in the plan -> no home activity in the knowledge
203  c = firstActivity.getCoord();
204  link_id = firstActivity.getLinkId().toString();
205  }
206 
207  if (c != null) {
208  out.write(c.getX()+"\t");
209  out.write(c.getY()+"\t");
210  }
211  else {
212  out.write("-\t-\t");
213  }
214  out.write(link_id+"\t");
215 
216  // plan0 ----------------------------------------------
217  out.write(selectedPlan.getScore()+"\t");
218  out.write(this.getTravelTime(person)+"\t");
219  this.sumPlanTraveltime[0]+=this.getTravelTime(person);
220 
221  out.write(this.getTravelDist(person)+"\t");
222  this.sumPlanTraveldistance[0]+=this.getTravelDist(person);
223 
224  out.write(this.getNumberOfTrips(person)+"\t");
225  this.sumNrLegs[0]+=this.getNumberOfTrips(person);
226  this.sumLegTraveltime[0]+=(this.getTravelTime(person)/this.getNumberOfTrips(person));
227  this.sumLegTraveldistance[0]+=(this.getTravelDist(person)/this.getNumberOfTrips(person));
228 
229  // plan1 ----------------------------------------------
230  if (this.twoPlans) {
231 
232  final Person person_comp=this.plans1.getPersons().get(person_id);
233  out.write(person_comp.getSelectedPlan().getScore()+"\t");
234  out.write(this.getTravelTime(person_comp)+"\t");
235  this.sumPlanTraveltime[1]+=this.getTravelTime(person_comp);
236 
237  out.write(this.getTravelDist(person_comp)+"\t");
238  this.sumPlanTraveldistance[1]+=this.getTravelDist(person_comp);
239 
240  out.write(this.getNumberOfTrips(person_comp)+"\t");
241  this.sumNrLegs[1]+=this.getNumberOfTrips(person_comp);
242  this.sumLegTraveltime[1]+=(this.getTravelTime(person_comp)/this.getNumberOfTrips(person_comp));
243  this.sumLegTraveldistance[1]+=(this.getTravelDist(person_comp)/this.getNumberOfTrips(person_comp));
244  }
245  out.write('\n');
246  out.flush();
247  }
248  out.close();
249  }
250  catch (final IOException e) {
251  throw new RuntimeException(e);
252  }
253  }
254 
255  /* TODO [AH] Combine the following three methods and make one method.
256  * See playground.anhorni.locationchoice.analysis.PersonTimeDistanceCalculator */
257 
258  private double getTravelTime(final Person person) {
259  double travelTime=0.0;
260  for (PlanElement pe : person.getSelectedPlan().getPlanElements()) {
261  if (pe instanceof Leg) {
262  travelTime+= ((Leg)pe).getTravelTime().seconds();
263  }
264  }
265  return travelTime;
266  }
267 
268  private double getTravelDist(final Person person) {
269  double travelDist=0.0;
270  for (PlanElement pe : person.getSelectedPlan().getPlanElements()) {
271  if (pe instanceof Leg) {
272  travelDist+=((Leg) pe).getRoute().getDistance();
273  }
274  }
275  return travelDist;
276  }
277 
278  private int getNumberOfTrips(final Person person) {
279  int numberOfLegs=0;
280  for (PlanElement pe : person.getSelectedPlan().getPlanElements()) {
281  if (pe instanceof Leg) {
282  numberOfLegs++;
283  }
284  }
285  return numberOfLegs;
286  }
287 
288  // --------------------------------------------------------------------------
289 
290  private void run(final String networkPath, final String plansfilePath0, final String plansfilePath1) {
291  this.init(networkPath);
292  this.readPlansFile(plansfilePath0, this.plans0);
293 
294  System.out.println(this.twoPlans);
295 
296  if (this.twoPlans) {
297  this.readPlansFile(plansfilePath1, this.plans1);
298  }
299 
301  writeAvgFile();
302  System.out.println("finished");
303  }
304 
305  private static void printUsage() {
306  System.out.println();
307  System.out.println("CreateSelectedPlansTables:");
308  System.out.println();
309  System.out.println( "Creates an agent-based table including all agent \n" +
310  "attributes, the selected plan score and the plan and \n" +
311  "leg travel times and distances and \n" +
312  "another table containing the aggregated values.");
313  System.out.println();
314  System.out.println("usage: CompareSelectedPlansTable args");
315  System.out.println(" arg 0: path to network file (required)");
316  System.out.println(" arg 1: path to plans file 0 (required)");
317  System.out.println(" arg 2: path to plans file 1 (optional)");
318 
319  System.out.println("----------------");
320  System.out.println("2008, matsim.org");
321  System.out.println();
322  }
323 
324  public void setTwoPlans(final boolean twoPlans) {
325  this.twoPlans=twoPlans;
326  }
327 
334  private static class PseudoScenario implements Scenario {
335 
336  private final Scenario scenario;
337  private final Population myPopulation;
338 
339  public PseudoScenario(final Scenario scenario, final Population population) {
340  this.scenario = scenario;
341  this.myPopulation = population;
342  }
343 
344  @Override
346  return this.myPopulation;
347  }
348 
349  public Coord createCoord(double x, double y) {
350  return new Coord(x, y);
351  }
352 
353  @Override
354  public Config getConfig() {
355  return this.scenario.getConfig();
356  }
357 
358  @Override
359  public Network getNetwork() {
360  return this.scenario.getNetwork();
361  }
362 
363  @Override
365  return this.scenario.getActivityFacilities();
366  }
367 
368  @Override
370  return scenario.getTransitSchedule();
371  }
372 
373  @Override
374  public void addScenarioElement(String name, Object o) {
375  scenario.addScenarioElement(name, o);
376  }
377 
378  @Override
379  public Object getScenarioElement(String name) {
380  return scenario.getScenarioElement(name);
381  }
382 
383  @Override
385  return scenario.getTransitVehicles();
386  }
387 
388  @Override
390  return scenario.getHouseholds();
391  }
392 
393  @Override
394  public Lanes getLanes() {
395  return scenario.getLanes();
396  }
397 
398  @Override
400  return scenario.getVehicles() ;
401  }
402 
403  }
404 }
static final void startMeasurement()
Definition: Gbl.java:145
void readPlansFile(final String plansfilePath, final Population plans)
static String getCarAvail(Person person)
void addScenarioElement(String name, Object o)
Map< Id< Person >,? extends Person > getPersons()
void run(final String networkPath, final String plansfilePath0, final String plansfilePath1)
Object getScenarioElement(String name)
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
Definition: IOUtils.java:390
static Boolean isEmployed(Person person)
static String getSex(Person person)
static final void printElapsedTime()
Definition: Gbl.java:149
ActivityFacilities getActivityFacilities()
TransitSchedule getTransitSchedule()
static Integer getAge(Person person)
static Scenario createScenario(final Config config)
PseudoScenario(final Scenario scenario, final Population population)
static String getLicense(Person person)
static Config createConfig(final String context)