MATSIM
RunWithinDayReplanningAgentExample.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2012 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.withinDayReplanningAgents;
21 
22 import com.google.inject.Provider;
23 import org.apache.log4j.Logger;
24 import org.matsim.api.core.v01.Id;
25 import org.matsim.api.core.v01.Scenario;
30 import org.matsim.core.config.Config;
40 import org.matsim.core.mobsim.qsim.QSim;
45 import org.matsim.facilities.Facility;
46 import org.matsim.vehicles.Vehicle;
50 
51 import javax.inject.Inject;
52 
54 
55  public static void main(String[] args) {
56 
57  // I want a config out of nothing:
58  Config config = ConfigUtils.createConfig() ;
59 
60  // set some config stuff:
61  config.network().setInputFile("../../../matsim/trunk/examples/siouxfalls/network-wo-dummy-node.xml") ;
62  config.controler().setLastIteration(0) ;
63  config.qsim().setEndTime(26.*3600) ;
64  config.qsim().setSnapshotStyle( QSimConfigGroup.SnapshotStyle.queue ) ;
65 
66  // base the controler on that:
67  Controler ctrl = new Controler( config ) ;
69  @Override
70  public void install() {
71  bindMobsim().toProvider(new Provider<Mobsim>() {
72 
73  @Inject Scenario sc;
74  @Inject EventsManager ev;
75 
76  @Override
77  public Mobsim get() {
78  // take the default mobsim, but since the population is empty, it will not be filled with demand:
79  final QSim qsim = QSimUtils.createDefaultQSim(sc, ev);
80 
81  // add my own agent(s):
82  qsim.addAgentSource(new AgentSource() {
83  VehicleType basicVehicleType = new VehicleTypeImpl(Id.create("basicVehicleType", VehicleType.class));
84 
85  @Override
86  public void insertAgentsIntoMobsim() {
87  final Id<Link> startLinkId = (Id<Link>) (sc.getNetwork().getLinks().keySet().toArray())[0];
88  final MobsimVehicle veh = new QVehicle(new VehicleImpl(Id.create("testVehicle", Vehicle.class), basicVehicleType));
89  qsim.addParkedVehicle(veh, startLinkId);
90  qsim.insertAgentIntoMobsim(new MyAgent(sc, ev, qsim, startLinkId, veh));
91  // (the Id of the parked vehicle needs to be known to the agent, otherwise it will not work!)
92  }
93  });
94 
95  // add otfvis live. Can't do this in core since otfvis is not accessible from there.
96  // OTFClientLive.run(sc.getConfig(), OTFVis.startServerAndRegisterWithQSim(sc.getConfig(), sc, ev, qsim));
97 
98  return qsim;
99  }
100  });
101  }
102  });
103  ctrl.run();
104 
105  }
106 
107 }
108 
116 class MyAgent implements MobsimDriverAgent {
117  private static Logger log = Logger.getLogger("MyAgent") ;
118 
119  private MobsimVehicle vehicle;
120  private Scenario sc;
121  private EventsManager ev;
122  private Id<Link> currentLinkId;
123  private Id<Person> myId;
124  private State state = State.ACTIVITY;
125  private Netsim netsim;
126  private Id<Link> destinationLinkId = Id.create("dummy", Link.class);
127  private Id<Vehicle> plannedVehicleId ;
128 
129  private double activityEndTime = 1. ;
130 
131  MyAgent( Scenario sc, EventsManager ev, Netsim netsim, Id<Link> startLinkId, MobsimVehicle veh ) {
132  log.info( "calling MyAgent" ) ;
133  this.sc = sc ;
134  this.ev = ev ;
135  this.myId = Id.create("testveh", Person.class) ;
136  this.netsim = netsim ;
137  this.currentLinkId = startLinkId ;
138  this.plannedVehicleId = veh.getId() ;
139  }
140 
141  @Override
142  public void setStateToAbort(double now) {
143  this.state = State.ABORT ;
144  log.info( "calling abort; setting state to: " + this.state ) ;
145  }
146 
147  @Override
148  public void endActivityAndComputeNextState(double now) {
149  this.state = State.LEG ; // want to move
150  log.info( "calling endActivityAndComputeNextState; setting state to: " + this.state ) ;
151  }
152 
153  @Override
154  public void endLegAndComputeNextState(double now) {
155  this.state = State.ACTIVITY ;
156  this.activityEndTime = Double.POSITIVE_INFINITY ;
157  log.info( "calling endLegAndComputeNextState; setting state to: " + this.state ) ;
158  }
159 
160  @Override
161  public double getActivityEndTime() {
162  log.info ("calling getActivityEndTime; answer: " + this.activityEndTime ) ;
163  return this.activityEndTime ;
164  }
165 
166  @Override
167  public Double getExpectedTravelTime() {
168  return 0. ; // what does this matter for?
169  }
170 
171  @Override
172  public Double getExpectedTravelDistance() {
173  return null;
174  }
175 
176  @Override
177  public String getMode() {
178  return TransportMode.car ; // either car or nothing
179  }
180 
181  @Override
182  public State getState() {
183  log.info( "calling getState; answer: " + this.state ) ;
184  return this.state ;
185  }
186 
187  @Override
188  public void notifyArrivalOnLinkByNonNetworkMode(Id<Link> linkId) {
189  this.currentLinkId = linkId ;
190  }
191 
192  @Override
193  public Id<Link> getCurrentLinkId() {
194  return this.currentLinkId ;
195  }
196 
197  @Override
198  public Id<Link> getDestinationLinkId() {
199  return this.destinationLinkId ;
200  }
201 
202  @Override
203  public Id<Person> getId() {
204  return this.myId ;
205  }
206 
207  @Override
208  public Id<Link> chooseNextLinkId() {
209  Link currentLink = sc.getNetwork().getLinks().get(this.currentLinkId) ;
210  Object[] outLinks = currentLink.getToNode().getOutLinks().keySet().toArray() ;
211  int idx = MatsimRandom.getRandom().nextInt(outLinks.length) ;
212  if ( this.netsim.getSimTimer().getTimeOfDay() < 24.*3600 ) {
213  return (Id<Link>) outLinks[idx] ;
214  } else {
215  this.destinationLinkId = (Id<Link>) outLinks[idx] ;
216  return null ;
217  }
218  }
219 
220  @Override
221  public Id<Vehicle> getPlannedVehicleId() {
222  return this.plannedVehicleId ;
223  }
224 
225  @Override
226  public MobsimVehicle getVehicle() {
227  return this.vehicle ;
228  }
229 
230  @Override
231  public void notifyMoveOverNode(Id<Link> newLinkId) {
232  this.currentLinkId = newLinkId ;
233  }
234 
235  @Override
236  public void setVehicle(MobsimVehicle veh) {
237  this.vehicle = veh ;
238  }
239 
240  @Override
241  public boolean isWantingToArriveOnCurrentLink() {
242  // TODO Auto-generated method stub
243  throw new RuntimeException("not implemented") ;
244  }
245 
246  @Override
247  public Facility<? extends Facility<?>> getCurrentFacility() {
248  // TODO Auto-generated method stub
249  throw new RuntimeException("not implemented") ;
250  }
251 
252  @Override
253  public Facility<? extends Facility<?>> getDestinationFacility() {
254  // TODO Auto-generated method stub
255  throw new RuntimeException("not implemented") ;
256  }
257 
258 }
final void addOverridingModule(AbstractModule abstractModule)
Definition: Controler.java:410
void addAgentSource(AgentSource agentSource)
Definition: QSim.java:609
static QSim createDefaultQSim(final Scenario scenario, final EventsManager eventsManager)
Definition: QSimUtils.java:42
void insertAgentIntoMobsim(final MobsimAgent agent)
Definition: QSim.java:395
ControlerConfigGroup controler
Definition: Config.java:87
static Config createConfig(final String filename)
void endLegAndComputeNextState(final double now)
QSimConfigGroup qsim()
Definition: Config.java:484
NetworkConfigGroup network
Definition: Config.java:92
void addParkedVehicle(MobsimVehicle veh, Id< Link > startLinkId)
Definition: QSim.java:294
void notifyArrivalOnLinkByNonNetworkMode(final Id< Link > linkId)