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

Source Code for Module mosp_examples.poi_act_wiggler

  1  #!/bin/env python 
  2   
  3  """Location and Action example: People acting on the road, pausing action at Location@POI 
  4      - random movement 
  5      - infected persons have infection action: infecting others in distance of 30m 
  6      - location Cafe at any way node with amenity=cafe 
  7          - person enters cafe, waits some time and leaves again 
  8          - actions are stopped while in the cafe 
  9          - you need to try different seeds to execute the different probable scenarios 
 10            on current test system: case 1 if seed=3, case 2 if seed=6 
 11              1. person inside healthy, person passing outside infected => person inside infected 
 12              2. person inside infected, person passing outside healthy => no infect 
 13      - output to visual player, which is executed as child process 
 14      - infect logging to stderr 
 15  """ 
 16   
 17  import sys 
 18  sys.path.append("..") 
 19   
 20  from mosp.core import Simulation, Person, action, start_action 
 21  from mosp.geo import osm 
 22  from mosp.locations import Cafe 
 23  from mosp.impl import movement 
 24  import mosp.collide 
 25  from mosp.monitors import ChildprocessPlayerChamplainMonitor, SocketPlayerMonitor 
 26   
 27  __author__ = "B. Henne" 
 28  __contact__ = "henne@dcsec.uni-hannover.de" 
 29  __copyright__ = "(c) 2011, DCSec, Leibniz Universitaet Hannover, Germany" 
 30  __license__ = "GPLv3" 
31 32 33 -class PoiActWiggler(Person):
34 """Implements a zombie person with random movement, infection, action stopping. 35 36 Zombie with infection range of 30 meters. Movement is random. If walking on a 37 road network node with Cafe Location, zombie enters the cafe. Infect action 38 is stopped while being in the cafe. When leaving action is restarted. 39 Cafe locations have to be added to geo-data by simulation configuration. 40 @author: B. Henne""" 41
42 - def __init__(self, *args, **kwargs):
43 """Init the Cafe Location zombie with action stopping.""" 44 super(PoiActWiggler, self).__init__(*args, **kwargs) 45 self.p_color_rgba = (0.1, 0.1, 1.0, 0.8) 46 self.p_infected = False 47 if kwargs.get('infected'): 48 self.infect()
49 50 next_target = movement.person_next_target_random 51
52 - def infect(self):
53 """The infection routine itself. 54 55 If not infected, person gets infected and speed is set to its half. 56 Action infect_other is started.""" 57 if self.p_infected == False: 58 self.p_infected = True 59 self.p_color = 1 60 self.p_color_rgba = (1.0, 0.1, 0.1, 0.8) 61 self.p_speed = self.p_speed / 2 62 sys.stderr.write('t=%s person %s got INFECTED\n' % (self.sim.now(), self.p_id)) 63 start_action(self.infect_other)
64 65 @action(5, start=False)
66 - def infect_other(self):
67 """The zombie infect action. 68 69 Every 5 ticks this action is called (using the mosp action decorator). 70 This action looks for people in range of 30 meters, and calls their 71 infect() routine. Action is not active at beginning. It is activated by 72 self.infect_other.start() and then is called every 5 ticks.""" 73 if self.p_infected == True: 74 self.get_near(30).call(delay=1).infect()
75
76 - def act_at_node(self, node):
77 """Implementation of act_at_node: stop at Cafe, stop actions (restarted when leaving).""" 78 worldobject = node.worldobject 79 if worldobject is not None: 80 if isinstance(worldobject, Cafe): 81 self.passivate = True 82 self.passivate_with_stop_actions = True 83 worldobject.interact(self)
84
85 86 -def main():
87 """Defines the simulation, map, monitors, persons. Cafe locations are set up at cafe POI on road network.""" 88 s = Simulation(geo=osm.OSMModel('../data/minimap0.osm'), rel_speed=50, seed=6) 89 #m = s.add_monitor(ChildprocessPlayerChamplainMonitor, 2) 90 m = s.add_monitor(SocketPlayerMonitor, 2) 91 s.add_persons(PoiActWiggler, 1, monitor=m, args={"infected":True, "speed":1.7}) 92 s.add_persons(PoiActWiggler, 1, monitor=m) 93 for p in [node for node in s.geo.way_nodes if "amenity" in node.tags and node.tags["amenity"] == "cafe"]: 94 c = Cafe(p.tags['name'], s) 95 p.worldobject = c 96 s.activate(c, c.serve(), 0) 97 s.run(until=50000, real_time=True, monitor=True)
98 99 100 if __name__ == '__main__': 101 main() 102