MATSIM
RunPopulationDownsamplingExample.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2007 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package tutorial.programming.downsamplePopulation;
21 
25 import org.matsim.core.config.Config;
28 
32 class RunPopulationDownsamplingExample {
33 
34  void run(final String[] args) {
35  String inputPopFilename = null ;
36  String outputPopFilename = null ;
37  String netFilename = null ;
38 
39  if ( args!=null ) {
40  if ( !(args.length==2 || args.length==3) ) {
41  System.err.println( "Usage: cmd inputPop.xml.gz outputPop.xml.gz [network.xml.gz]");
42  } else {
43  inputPopFilename = args[0] ;
44  outputPopFilename = args[1] ;
45  if ( args.length==3 ) {
46  netFilename = args[2] ;
47  }
48  }
49  }
50 
51 
52  Config config = ConfigUtils.createConfig() ;
53  config.network().setInputFile( netFilename ) ;
54  config.plans().setInputFile( inputPopFilename ) ;
55 
56  Population pop = ScenarioUtils.loadScenario(config).getPopulation() ;
57 
58  Population newPop = ScenarioUtils.createScenario(ConfigUtils.createConfig()).getPopulation() ;
59  for ( Person person : pop.getPersons().values() ) {
60  if ( Math.random() < 0.1 ) {
61  System.out.println("adding person...");
62  newPop.addPerson(person);
63  }
64  }
65 
66 
67  PopulationWriter popwriter = new PopulationWriter(newPop,ScenarioUtils.loadScenario(config).getNetwork()) ;
68  popwriter.write( outputPopFilename ) ;
69 
70  System.out.println("done.");
71  }
72 
73  public static void main(final String[] args) {
74  RunPopulationDownsamplingExample app = new RunPopulationDownsamplingExample();
75  app.run(args);
76  }
77 
78 }
static Scenario loadScenario(final Config config)