MATSIM
RunTeleportationMobsimWithCustomRoutingExample.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * SimulateTeleportation.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2013 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 package tutorial.programming.example13MultiStageTripRouting;
21 
22 import java.util.Map;
23 
24 import com.google.inject.Key;
25 import com.google.inject.name.Names;
26 import org.matsim.api.core.v01.Coord;
27 import org.matsim.api.core.v01.Id;
28 import org.matsim.api.core.v01.Scenario;
31 import org.matsim.core.config.Config;
44 
49  private static final String configFile = "examples/pt-tutorial/config.xml";
50 
51  public static void main(final String[] args) {
52  // make sure we get all the log messages in the logfile
54 
55  // load the config ...
56  final Config config = ConfigUtils.loadConfig( configFile );
57  // ... and add local changes:
58  tuneConfig( config );
59 
60  // load the scenario:
61  final Scenario scenario = ScenarioUtils.loadScenario( config );
62 
63  // load the controler:
64  final Controler controler = new Controler( scenario );
65 
66  // create the teleportation station on a central link
67  final ActivityFacility teleport =
69  Id.create( "teleport" , ActivityFacility.class),
70  controler.getScenario().getNetwork().getLinks().get( Id.create( "2333", Link.class ) ));
71 
72  // now, plug our stuff in
73  controler.addOverridingModule(new AbstractModule() {
74  @Override
75  public void install() {
76  addRoutingModuleBinding(MyRoutingModule.TELEPORTATION_MAIN_MODE).toProvider(
78  // the module uses the trip router for the PT part.
79  // This allows to automatically adapt to user settings,
80  // including if they are specified at a later stage
81  // in the initialisation process.
82  binder().getProvider(Key.get(RoutingModule.class, Names.named(TransportMode.pt))),
83  scenario.getPopulation().getFactory(),
84  teleport));
85  // we still need to provide a way to identify our trips
86  // as being teleportation trips.
87  // This is for instance used at re-routing.
88  bind(MainModeIdentifier.class).toInstance(new MyMainModeIdentifier(new MainModeIdentifierImpl()));
89  }
90  });
91 
92  // run the controler:
93  controler.run();
94  }
95 
96  private static void tuneConfig(final Config config) {
97  config.getModule( "changeLegMode" ).addParam( "modes" , "car,pt,"+MyRoutingModule.TELEPORTATION_MAIN_MODE );
98 
99  final ActivityParams scoreTelepInteract = new ActivityParams( MyRoutingModule.STAGE );
100  scoreTelepInteract.setTypicalDuration( 2 * 60 );
101  scoreTelepInteract.setOpeningTime( 0 );
102  scoreTelepInteract.setClosingTime( 0 );
103  config.planCalcScore().addActivityParams( scoreTelepInteract );
104  }
105 
107  final Id<ActivityFacility> id,
108  final Link link) {
109  if ( link == null ) throw new IllegalArgumentException( "link == null");
110 
111  return new ActivityFacility() {
112  @Override
113  public Coord getCoord() {
114  return link.getFromNode().getCoord();
115  }
116 
117  @Override
118  public Id<ActivityFacility> getId() {
119  return id;
120  }
121 
122  @Override
123  public Map<String, Object> getCustomAttributes() {
124  throw new UnsupportedOperationException();
125  }
126 
127  @Override
128  public Id<Link> getLinkId() {
129  return link.getId();
130  }
131 
132  @Override
133  public void addActivityOption(ActivityOption option) {
134  throw new UnsupportedOperationException();
135  }
136 
137  @Override
138  public Map<String, ActivityOption> getActivityOptions() {
139  throw new UnsupportedOperationException();
140  }
141 
142  @Override
143  public void setCoord(Coord coord) {
144  // TODO Auto-generated method stub
145  throw new RuntimeException("not implemented") ;
146  }
147  };
148  }
149 }
150 
final void addOverridingModule(AbstractModule abstractModule)
Definition: Controler.java:410
Map< Id< Link >,?extends Link > getLinks()
static Config loadConfig(final String filename, ConfigGroup...customModules)
static Scenario loadScenario(final Config config)
final PlanCalcScoreConfigGroup planCalcScore()
Definition: Config.java:444
final ConfigGroup getModule(final String moduleName)
Definition: Config.java:333
void addParam(final String paramName, final String value)