Revision a395aaa8 BGPpysim/util/EventScheduler.py

View differences:

BGPpysim/util/EventScheduler.py
1
#!/usr/bin/python
2

  
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
#
16
# Copyright (C) 2019  Mattia Milani <mattia.milani@studenti.unitn.it> & Lorenzo Ghiro <lorenzo.ghiro@unitn.it>
17

  
1 18
import heapq
2 19
import random
3 20

  
21

  
4 22
class EventScheduler:
5
        def __init__(self):
6
                self.queue = []
7
                self.time = 0
8
                self.last = 0
9
                self._step = 0
10
                self.counter = 0
11

  
12
        def jitter(self, positive=True):
13
            if positive:
14
                return random.uniform(0, 0.1)
15
            else:
16
                return [-1, 1][random.randrange(2)]*random.uniform(0, 0.866)
17

  
18
        def schedule_event(self, interval, e):
19
                t = self.time + interval
20
                if t > self.last:
21
                    self.last = t
22
                heapq.heappush(self.queue, (t, e))
23

  
24
        def pop_event(self):
25
                e = heapq.heappop(self.queue)
26
                self._step = e[0] - self.time
27
                self.time = e[0]
28
                self.counter += 1
29
                return e[1]
30

  
31
        def elapsed_time(self):
32
                return self.time
33

  
34
        def last_event_time(self):
35
                return self.last
36

  
37
        def step(self):
38
            return self._step
39

  
40
        def processed_events(self):
41
            return self.counter
23
    """
24
    Class used to schedule events in an event discrete space
25
    """
26

  
27
    def __init__(self):
28
        self.queue = []
29
        self.time = 0
30
        self.last = 0
31
        self._step = 0
32
        self.counter = 0
33

  
34
    def jitter(self, positive=True):
35
        """
36
        Function to introduce a jitter in the a jitter in the event
37
        :param positive: if true it will introduce the jitter
38
        :return:
39
        """
40
        if positive:
41
            return random.uniform(0, 0.1)
42
        else:
43
            return [-1, 1][random.randrange(2)] * random.uniform(0, 0.866)
44

  
45
    def schedule_event(self, interval, e):
46
        """
47
        Function to schedule an event after a certain interval
48
        :param interval: After how much the event will be scheduled
49
        :param e: event to schedule
50
        """
51
        t = self.time + interval
52
        if t > self.last:
53
            self.last = t
54
        heapq.heappush(self.queue, (t, e))
55

  
56
    def pop_event(self):
57
        """
58
        Function to take the next event in the queue
59
        :return: the next event in the queue
60
        """
61
        e = heapq.heappop(self.queue)
62
        self._step = e[0] - self.time
63
        self.time = e[0]
64
        self.counter += 1
65
        return e[1]
66

  
67
    def elapsed_time(self):
68
        """
69
        Function to know how much time is elapsed
70
        :return: the actual time
71
        """
72
        return self.time
73

  
74
    def last_event_time(self):
75
        """
76
        Function to know when the last event will happen
77
        :return: the time of the last event
78
        """
79
        return self.last
80

  
81
    def step(self):
82
        """
83
        Function to get the step time
84
        :return: the step time
85
        """
86
        return self._step
87

  
88
    def processed_events(self):
89
        """
90
        Function to get the total amount of events processed
91
        :return: number of events
92
        """
93
        return self.counter

Also available in: Unified diff