-
Notifications
You must be signed in to change notification settings - Fork 2
/
trafficgenerator.py
55 lines (46 loc) · 1.64 KB
/
trafficgenerator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
import random
class TrafficGenerator:
"""Object of this class generates traffic requesting
service from elevator(s)."""
def __init__(self, environment, dist_lambda: float, floors: tuple):
"""
Initialize the traffic object
"""
self.env = environment
if dist_lambda > 0:
self.dist_lambda = dist_lambda
else:
raise ValueError("Lambda for exponential distribution must be positive")
if type(floors) == tuple:
self.floors = floors
else:
raise TypeError("Floors must be of type tuple")
def generate_time(self):
return np.random.exponential(self.dist_lambda)
@staticmethod
def generate_count():
return random.randint(1, 6)
def generate_origin_destination(self):
origin = random.choice(self.floors)
destination = random.choice([floor for floor in self.floors
if floor != origin])
return origin, destination
def time_out(self, time):
"""
Method calls simpy timeout with required time
:param time: int or float
:return: None
"""
if time > 0:
yield self.env.timeout(time)
def next_traffic(self):
"""
Method returns random configuration of traffic
at every call
:return: count of people, origin floor, destination floor
"""
yield self.env.process(self.time_out(self.generate_time()))
count = random.randint(1, 6)
origin, destination = self.generate_origin_destination()
return count, origin, destination