跳转到内容

SimPy

维基百科,自由的百科全书
SimPy,一款基于Python的离散事件模拟框架
原作者Klaus G. Müller, Tony Vignaux
开发者Ontje Lünsdorf, Stefan Scherfke
首次发布2002年9月17日 (2002-09-17)
当前版本
  • 3.0.11 (2018年11月16日)
编辑维基数据链接
源代码库 编辑维基数据链接
编程语言Python
操作系统Cross-platform
类型Discrete event simulation
许可协议MIT
网站simpy.readthedocs.org

SimPy是“Simulation in Python”的缩写,意为“在Python中模拟”,是一个基于标准Python的过程型离散事件模拟框架[1]。它使用户可以将活动组件(如客户、车辆或代理)建模为简单的Python 生成器函数。SimPy作为开源软体MIT许可证下,第一个版本于2002年12月发布[2]

概述

SimPy的事件调度器是基于Python的生成器,这使得它可以应用于异步网络或实现多代理系统(包括模拟和真实的通信)。模拟可以以“尽可能快”的方式进行,也可以实时进行(即按照实际时间),或者可以手动一步步地进行。尽管理论上SimPy可以进行连续模拟,但它并未提供足够的支援。然而,对于那些具有固定步长、过程之间没有互动或与共享资源交互的模拟,一个简单的while回圈就足以应对[3]

此外,SimPy提供了不同类型的共享资源,以模拟具有有限容量的拥塞点,如服务器、结帐柜台和隧道。在3.1版本及以上,SimPy提供了监控功能,以协助收集有关过程和资源的统计资讯。

有关于版本需求,SimPy 3.0需要Python 3[4],而SimPy 4.0需要Python 3.6+。SimPy发行版提供教程[5]、文件和范例供使用者学习。

范例

实作一个时钟[6],该时钟在每个步骤打印目前模拟时间:

>>> import simpy
>>>
>>> def clock(env, name, tick):
...     while True:
...         print(name, env.now)
...         yield env.timeout(tick)
...
>>> env = simpy.Environment()
>>> env.process(clock(env, 'fast', 0.5))
<Process(clock) object at 0x...>
>>> env.process(clock(env, 'slow', 1))
<Process(clock) object at 0x...>
>>> env.run(until=2)
fast 0
slow 0 
fast 0.5 
slow 1 
fast 1.0 
fast 1.5

参考资料

  1. ^ Iwata, Curtis; Mavris, Dimitri. Object-Oriented Discrete Event Simulation Modeling Environment for Aerospace Vehicle Maintenance and Logistics Process. Procedia Computer Science. 2013 Conference on Systems Engineering Research. 2013-01-01, 16: 187–196 [2024-05-26]. ISSN 1877-0509. doi:10.1016/j.procs.2013.01.020可免费查阅. (原始内容存档于2023-11-09). 
  2. ^ Xiong, Xinli; Ma, Linru; Cui, Chao. Simulation Environment of Evaluation and Optimization for Moving Target Defense: A SimPy Approach. Proceedings of the 2019 9th International Conference on Communication and Network Security. ICCNS '19 (Association for Computing Machinery). 2020-01-13: 114–117. ISBN 978-1-4503-7662-4. doi:10.1145/3371676.3371692.  已忽略未知参数|__cpLocation= (帮助)
  3. ^ Olaitan, Oladipupo; Geraghty, John; Young, Paul; Dagkakis, Georgios; Heavey, Cathal; Bayer, Martin; Perrin, Jerome; Robin, Sebastien. Implementing ManPy, a Semantic-free Open-source Discrete Event Simulation Package, in a Job Shop. Procedia CIRP. 8th International Conference on Digital Enterprise Technology - DET 2014 Disruptive Innovation in Manufacturing Engineering towards the 4th Industrial Revolution. 2014-01-01, 25: 253–260 [2024-05-26]. ISSN 2212-8271. doi:10.1016/j.procir.2014.10.036可免费查阅. (原始内容存档于2023-11-09). 
  4. ^ SimPy History & Change Log — SimPy 4.0.2.dev1+g2973dbe documentation. [2024-05-26]. (原始内容存档于2023-04-06). 
  5. ^ Zinoviev, Dmitry. Discrete Event Simulation. It's Easy with SimPy!. PragPub. February 2018, (104): 1–16. 
  6. ^ Scherfke, Stefan. Discrete-event simulation with SimPy (PDF): 5. July 25, 2014 [August 10, 2016]. (原始内容存档 (PDF)于2024-03-02). 

注解