MATSIM
MyObserver.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.ownMobsimAgentWithPerception;
20 
21 import java.util.HashMap;
22 import java.util.Map;
23 
24 import org.matsim.api.core.v01.Id;
25 import org.matsim.api.core.v01.Scenario;
34 
40 class MyObserver implements BasicEventHandler {
41  Map<Id<Link>,Double> nVehs = new HashMap<>() ;
42  private Scenario scenario;
43 
44  MyObserver( Scenario sc ) {
45  this.scenario = sc ;
46  }
47 
48  @Override
49  public void handleEvent(Event event) {
50  Id<Link> linkId = null ;
51 
52  if ( event instanceof LinkEnterEvent ) {
53  linkId = ((LinkEnterEvent) event).getLinkId() ;
54  } else if ( event instanceof VehicleEntersTrafficEvent ) {
55  linkId = ((VehicleEntersTrafficEvent) event).getLinkId() ;
56  }
57  if ( nVehs.get( linkId ) != null ) {
58  double val = nVehs.get( linkId ) ;
59  nVehs.put( linkId, val++ ) ;
60  } else {
61  nVehs.put( linkId, 1. ) ;
62  }
63 
64  String arrivalMode = TransportMode.car ;
65  if ( event instanceof LinkLeaveEvent ) {
66  linkId = ((LinkLeaveEvent) event).getLinkId() ;
67  } else if ( event instanceof VehicleLeavesTrafficEvent ) {
68  linkId = ((VehicleLeavesTrafficEvent)event).getLinkId() ;
69  arrivalMode = ((VehicleLeavesTrafficEvent)event).getNetworkMode() ;
70  }
71  if ( arrivalMode.equals( TransportMode.car ) ) {
72  if ( nVehs.get( linkId ) != null ) {
73  double val = nVehs.get( linkId ) ;
74  nVehs.put( linkId, val-- ) ;
75  } else {
76  throw new RuntimeException("should not happen; a car should always have to enter a link before leaving it") ;
77  }
78  }
79  }
80 
81  double congestionLevel(Id<Link> linkId) {
82  final Double nn = nVehs.get( linkId );
83  if ( nn == null ) {
84  return 0. ;
85  }
86  final Link link = this.scenario.getNetwork().getLinks().get(linkId);
87  double nLanes = link.getNumberOfLanes() ;
88  double length = link.getLength() ;
89  double estimStorCap = nLanes * length / 7.5 ; // estimated storage capacity
90  return nn / estimStorCap ;
91  }
92 
93  @Override
94  public void reset(int iteration) {
95  }
96 
97 
98 }