Package mosp_examples :: Module passivate_wiggler
[hide private]
[frames] | no frames]

Source Code for Module mosp_examples.passivate_wiggler

 1  #!/bin/env python 
 2   
 3  """Passivate example: passivating users later being reactivated 
 4      - random movement 
 5      - demonstration of passivating a user 
 6          - if person arrives at an node (here: any cafe road node) it is passivated 
 7            and reactivated using an own Alarm process, cmp. simulation.person_alarm_clock  
 8            as used by pause_movement. Reactivating people if > 1/4 of people have been passivated. 
 9      - output to visual player, which is executed as child process 
10  """ 
11   
12  import sys 
13  sys.path.append("..") 
14   
15  from SimPy import SimulationRT 
16  from mosp.core import Simulation, Person 
17  from mosp.geo import osm 
18  from mosp.impl import movement 
19  from mosp.monitors import ChildprocessPlayerChamplainMonitor, SocketPlayerMonitor 
20   
21  __author__ = "B. Henne" 
22  __contact__ = "henne@dcsec.uni-hannover.de" 
23  __copyright__ = "(c) 2011, DCSec, Leibniz Universitaet Hannover, Germany" 
24  __license__ = "GPLv3" 
25   
26   
27 -class Alarm(SimulationRT.Process):
28 """Alarm process stores passivated users and reactivates if overcrowded. 29 30 Stores passivated people in passive_persons. If more than a quarter 31 of all people are passivated, all people get reactivated. 32 @author: B. Henne""" 33
34 - def __init__(self, name, sim):
35 """Inits the Alarm process.""" 36 SimulationRT.Process.__init__(self, name=name, sim=sim) 37 self.passive_persons = {}
38
39 - def go(self):
40 """For every 10th tick: If more than a quarter of all people are passivated, all people get reactivated.""" 41 while 42: 42 yield 1, self, 10 43 if len(self.passive_persons) > 0.25*len(self.sim.monitors[0]): 44 pp = self.passive_persons.copy() 45 for p in pp: 46 p.reactivate() 47 del self.passive_persons[p] 48 sys.stderr.write(' %s reactivated\n' % p.name)
49 50
51 -class PassivateWiggler(Person):
52 """Random moving person, when arriving at an cafe road node being passivated.""" 53 54 next_target = movement.person_next_target_random 55
56 - def act_at_node(self, node):
57 """When arriving at node with amenity=cafe, person is passivated and stored at Alarm for reactivation.""" 58 if 'amenity' in node.tags: 59 if node.tags['amenity'] == 'cafe': 60 sys.stderr.write(' '+self.name+' visited '+str(node.id)+' '+str(node.tags)+' at '+str(self.sim.now())+'\n') 61 self.passivate = True 62 self.sim.a.passive_persons[self] = self.next_node
63 64
65 -def main():
66 """Defines the simulation, map, monitors, persons. Sets up the reactivation alarm.""" 67 s = Simulation(geo=osm.OSMModel('../data/minimap0.osm'), rel_speed=50) 68 #m = s.add_monitor(ChildprocessPlayerChamplainMonitor, 2) 69 m = s.add_monitor(SocketPlayerMonitor, 2) 70 sys.stderr.write('Number of cafe nodes: %s \n' % len([node for node in s.geo.way_nodes if "amenity" in node.tags and node.tags["amenity"] == "cafe"])) 71 s.add_persons(PassivateWiggler, 4, monitor=m) 72 s.a = Alarm('alarm', s) 73 s.activate(s.a, s.a.go(), 0) 74 s.run(until=500000, real_time=True, monitor=True)
75 76 77 if __name__ == '__main__': 78 main() 79