MATSIM
RunWithFacilitiesExample.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.* *
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2008 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 package tutorial.programming.facilitiesAndOpenTimes;
20 
21 import org.matsim.api.core.v01.Coord;
22 import org.matsim.api.core.v01.Id;
23 import org.matsim.api.core.v01.Scenario;
33 import org.matsim.core.config.Config;
47 
52 public final class RunWithFacilitiesExample {
53 
54  // NOTE: facilities has been in the matsim main distribution for a long time. But it never has been in the core, and thus was
55  // never maintained by the core team. The following is an attempt to piece together what seems to be used in other places.
56  // You need to try out yourself what works and what doesn't, e.g. if now all activities have to take place at facilities, if
57  // there is a way to define "always open", etc. kai, feb'16
58 
59  private Scenario scenario;
60 
61  void run() {
62  final Config config = ConfigUtils.createConfig() ;
63  config.controler().setOverwriteFileSetting( OverwriteFileSetting.deleteDirectoryIfExists );
64  config.controler().setLastIteration(0);
65  {
66  ActivityParams params = new ActivityParams("home") ;
67  params.setTypicalDuration(12.*3600);
68  params.setTypicalDurationScoreComputation( TypicalDurationScoreComputation.relative );
69  // note no opening/closing times for the activity type (given by facility)
70  config.planCalcScore().addActivityParams(params);
71  }
72  {
73  ActivityParams params = new ActivityParams("shop") ;
74  params.setTypicalDuration(1.*3600);
75  params.setTypicalDurationScoreComputation( TypicalDurationScoreComputation.relative );
76  // note no opening/closing times for the activity type (given by facility)
77  config.planCalcScore().addActivityParams(params);
78  }
79 
80  scenario = ScenarioUtils.createScenario( config ) ;
81 
82  Coord defaultCoord = new Coord(0.0, 0.0);
83 
84  // create network
85  final Network network = scenario.getNetwork() ;
86  final NetworkFactory nf = network.getFactory() ;
87 
88  Node node0 = nf.createNode( Id.createNodeId("0"), defaultCoord ) ;
89  network.addNode(node0);
90 
91  Node node1 = nf.createNode( Id.createNodeId("1"), new Coord(100.,0.) ) ;
92  network.addNode(node1);
93 
94  {
95  Link link =nf.createLink(Id.createLinkId("0-1"), node0, node1 ) ;
96  network.addLink(link);
97  }
98  {
99  Link link =nf.createLink(Id.createLinkId("1-0"), node1, node0 ) ;
100  network.addLink(link);
101  }
102 
103  // create facilities, activities in it and open times
104  final ActivityFacilities facilities = scenario.getActivityFacilities() ;
105  final ActivityFacilitiesFactory ff = facilities.getFactory();
106 
107  ActivityFacility testFacility = ff.createActivityFacility(Id.create(0, ActivityFacility.class), defaultCoord) ;
108  facilities.addActivityFacility(testFacility);
109  {
110  ActivityOption ao = ff.createActivityOption("home") ;
111  testFacility.addActivityOption(ao);
112  ao.addOpeningTime(new OpeningTimeImpl(0.0 * 3600, 9999.0 * 3600));
113  // yy is there a setting for "always open"? An interval until 24*3600 almost surely means that after 24 it is closed. kai, feb'16
114  }
115  {
116  ActivityOption ao = ff.createActivityOption("shop") ;
117  testFacility.addActivityOption(ao);
118  ao.addOpeningTime(new OpeningTimeImpl(9.0 * 3600, 10.0 * 3600));
119  }
120  // create a person that has an activity at the facility:
121  final Population population = scenario.getPopulation() ;
122  final PopulationFactory pf = population.getFactory();
123  {
124  Person person = pf.createPerson(Id.create(1, Person.class));
125  population.addPerson(person);
126 
127  Plan plan = pf.createPlan() ;
128  person.addPlan(plan);
129  {
130  Activity act = pf.createActivityFromCoord("home", defaultCoord ) ;
131  plan.addActivity(act);
132  act.setFacilityId(testFacility.getId());
133  act.setEndTime(9.0 * 3600);
134  }
135  {
136  Activity act = pf.createActivityFromCoord("shop", defaultCoord ) ;
137  plan.addActivity(act);
138  act.setFacilityId(testFacility.getId());
139  act.setEndTime(10.0 * 3600);
140  }
141  {
142  Activity act = pf.createActivityFromCoord("home", defaultCoord ) ;
143  plan.addActivity(act);
144  act.setFacilityId(testFacility.getId());
145  }
146  }
147  {
148  Person person = pf.createPerson(Id.create(2, Person.class));
149  population.addPerson(person);
150 
151  Plan plan = pf.createPlan() ;
152  person.addPlan(plan);
153  {
154  Activity act = pf.createActivityFromCoord("home", defaultCoord ) ;
155  plan.addActivity(act);
156  act.setFacilityId(testFacility.getId());
157  act.setEndTime(10.0 * 3600); // i.e. arrive at shop when it closes
158  }
159  {
160  Activity act = pf.createActivityFromCoord("shop", defaultCoord ) ;
161  plan.addActivity(act);
162  act.setFacilityId(testFacility.getId());
163  act.setEndTime(11.0 * 3600);
164  }
165  {
166  Activity act = pf.createActivityFromCoord("home", defaultCoord ) ;
167  plan.addActivity(act);
168  act.setFacilityId(testFacility.getId());
169  }
170  }
171  // ---
172  Controler controler = new Controler( scenario ) ;
173 
174  // make the controler react to facility open times:
175  controler.addOverridingModule( new AbstractModule(){
176  @Override public void install() {
178  this.bindScoringFunctionFactory().toInstance( factory ) ;
179  }
180  });
181 
182  controler.run() ;
183 
184  // in the following, the second person has a lower score than the first because it spends its shopping activity while
185  // the shop is closed.
186  for ( Person pp : population.getPersons().values() ) {
187  System.out.println( pp ) ;
188  System.out.println( pp.getSelectedPlan() ) ;
189  }
190  }
191 
192  public static void main( String[] args ) {
193  new RunWithFacilitiesExample().run();
194  }
195 
197  return this.scenario ;
198  }
199 
200 }
final void addOverridingModule(AbstractModule abstractModule)
Definition: Controler.java:410
void addActivityOption(ActivityOption option)
void addActivityFacility(ActivityFacility facility)
Map< Id< Person >,?extends Person > getPersons()
ActivityOption createActivityOption(String type)
abstract void addActivity(final Activity act)
ActivityFacilitiesFactory getFactory()
Link createLink(final Id< Link > id, final Node fromNode, final Node toNode)
ActivityFacility createActivityFacility(Id< ActivityFacility > id, Coord coord)
ControlerConfigGroup controler
Definition: Config.java:87
static Config createConfig(final String filename)
Node createNode(final Id< Node > id, final Coord coord)
Activity createActivityFromCoord(String actType, Coord coord)
ActivityFacilities getActivityFacilities()
final PlanCalcScoreConfigGroup planCalcScore()
Definition: Config.java:444
void addOpeningTime(OpeningTime openingTime)
void setFacilityId(final Id< ActivityFacility > id)
static Scenario createScenario(final Config config)
void setEndTime(final double seconds)