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

Source Code for Module mosp_examples.routing_wiggler

 1  #!/bin/env python 
 2   
 3  """Routing example for new go method: routed movement 
 4      - routed movement (currently still shortest-path) 
 5      - 50% chance to use random movement after destination was reached 
 6      - 25% chance to get back to routed movement after node was reached 
 7      - showing routing destinations as markers 
 8      - output to socket player 
 9  """ 
10   
11  import sys 
12  sys.path.append("..")  
13   
14  import struct 
15   
16  from mosp.core import Simulation, Person 
17  from mosp.geo import osm 
18  from mosp.impl import movement 
19  from mosp.monitors import SocketPlayerMonitor 
20   
21  __author__ = "P. Tute" 
22  __maintainer__ = "B. Henne" 
23  __contact__ = "henne@dcsec.uni-hannover.de" 
24  __copyright__ = "(c) 2010-2012, DCSec, Leibniz Universitaet Hannover, Germany" 
25  __license__ = "GPLv3" 
26   
27  COLOR = {0: [1.0, 0.0, 0.0, 1.0], 
28           1: [0.0, 1.0, 0.0, 1.0]} 
29   
30   
31 -class RoutingShowcaseWiggler(Person):
32 """Person for routing demonstration. 33 34 If person arrives at its destination, next destination 35 is randomly selected and user routed to it. 36 With a 50% chance the person will then switch to random movement. 37 It switches back with 25% probability. 38 @author: P. Tute""" 39
40 - def __init__(self, *args):
41 """Init the RoutingShowcaseWiggler person.""" 42 Person.__init__(self, *args) 43 self.dest_node = self.next_node 44 self.p_color_rgba = COLOR[self.p_id] 45 self.routed = True 46 self.next_target = self.next_target_routed
47
48 - def think(self):
49 """Find next destination, change movement and/or check if next_node was reached.""" 50 if self.next_node == self.dest_node and self.routed: 51 self.start_node = self.dest_node 52 self.dest_node = self._random.choice([n for n in self.sim.geo.way_nodes if "border" not in n.tags]) 53 color = self.p_color_rgba[:-1] + [0.5] 54 self.sim.monitors[0].draw_point(self.p_id + 100, self.dest_node.lat, self.dest_node.lon, 5, self.p_color_rgba, ttl=0) 55 if self._random.random() < 0.5: 56 self.routed = False 57 self.p_color_rgba[2] += 1 58 self.next_target = self.next_target_random 59 self.sim.monitors[0].remove_object('point', self.p_id + 100) 60 elif not self.routed: 61 if self._random.random() < 0.25: 62 self.p_color_rgba[2] -= 1 63 self.routed = True 64 self.next_target = self.next_target_routed 65 self.sim.monitors[0].draw_point(self.p_id + 100, self.dest_node.lat, self.dest_node.lon, 5, self.p_color_rgba, ttl=0) 66 self.need_next_target = True
67
68 - def next_target_random(self):
69 """Wrapper for random movement. 70 71 This is necessary to be able to exchange routing methods. 72 """ 73 movement.person_next_target_random(self)
74
75 - def next_target_routed(self):
76 """Find a new next_node to move to. 77 Person gets routed to it.""" 78 self.last_node = self.next_node 79 next = self.last_node.get_route(self.dest_node) 80 if not next: 81 self.dest_node = self.next_node 82 else: 83 self.next_node = next
84 85
86 -def main():
87 """Defines the simulation, map, monitors, persons.""" 88 s = Simulation(geo=osm.OSMModel('../data/hannover1.osm'), rel_speed=40) 89 m = s.add_monitor(SocketPlayerMonitor, 2) 90 s.add_persons(RoutingShowcaseWiggler, 2, monitor=m) 91 s.run(until=10000, real_time=True)
92 93 if __name__ == '__main__': 94 main() 95