社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

美雨知春 • 4 年前 • 357 次点击  

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

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社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/73512
 
357 次点击