美文网首页
基于优先队列的离散事件模拟-Python

基于优先队列的离散事件模拟-Python

作者: 美雨知春 | 来源:发表于2020-09-16 19:03 被阅读0次

    被模拟的系统的行为可以抽象为一些离散时间的发生,所发生的的时间可以引发新的时间等,人们希望通过计算机模拟理解系统行为,评价或设计真实世界中实际的或所需的系统。
    下面说一下通用的模拟框架,主要也是面向对象的方法:
    模拟器和事件,模拟器里面装载事件执行:
    下面是模拟器

    from random import randint
    from prioqueue import PrioQueue
    from queue_list import SQueue
    class Simulation:
        def __init__(self, duration):
            self._eventq = PrioQueue()
            self._time = 0
            self._duration = duration
        def run(self):
            while not self._eventq.is_empty():
                event = self._eventq.dequeue()
                self._time = event.time()
                if self._time > self._duration:
                    break
                event.run()
        def add_event(self,event):
            self._eventq.enqueue(event)
        def cur_time(self):
            return self._time
    

    下面是事件:

    class Event:
        def __init__(self, event_time, host):
            self._ctime = event_time
            self._host = host
        def __it__(self, other_event):
            return self._ctime < other_event._ctime
        def __le__(self,other_event):
            return self._ctime <= other_event._ctime
        def host(self):
            return self._host
        def time(self):
            return self._ctime
        def run(self): #具体时间
            pass
    

    相关文章

      网友评论

          本文标题:基于优先队列的离散事件模拟-Python

          本文链接:https://www.haomeiwen.com/subject/khuhyktx.html