当前位置: 首页 > news >正文

【SimPy系列博客之官方example学习与解读】—— Example 3: Car Wash

Hello,CSDN的各位小伙伴们,又见面啦!今天我们要学习的例程是:Car Wash!我们开始吧!

例程背景

这个例程相对于example 2来说会简单一些,有一个洗车厂,里面有若干台洗车机器,一台机器在一段时间内只能给一台车进行洗车服务。车辆陆陆续续到达洗车厂,如果有机器空闲,则开始为该车辆服务,如果没有机器空闲,则车辆会排队等待,直到有空闲的机器。

例程代码分析

照旧,基本的头文件和参数的定义我们就不赘述了:

import simpy
import randomRANDOM_SEED = 42
NUM_MACHINES = 2
WASHTIME = 5
T_INTER = 7
SIM_TIME = 20

下面我们先定义洗车厂,首先我们想洗车场有n个机器 ,每个机器自然就是建模成simpy中的resource。另外,洗车场还会有洗车的功能,洗车功能则需要花费一些时间。有了这些理解,我们可以直接写出洗车场的类:

class CarWash:# carwash里面包含了若干个机器,能够同时为车辆进行洗车服务def __init__(self, env, num_machines, washtime):self.env = envself.machine = simpy.Resource(env, capacity=num_machines)self.washtime = washtimedef wash(self):yield self.env.timeout(self.washtime)  # 洗车需要花费的时间

下面我们来定义单一台车辆的行为,值得注意的是:车辆什么时候到达洗车场我们是不需要在车辆自己的建模中考虑的,应该把它分离出来,我们需要考虑的是车辆到达洗车场后,如何做:请求机器资源,然后开始洗车,然后释放机器资源并离开。所以我们可以写出车辆行为的代码:

def car(env, name, cw):print('Car: ', name, ' arrives at the carwash at: ', env.now)with cw.machine.request() as request:  # 请求机器资源yield request  # 等待洗车机print('Car: ', name, ' enters the carwash at: ', env.now)yield env.process(cw.wash())  # 开始洗车print('Car: ', name, ' leaves the carwash at: ', env.now)

with 跳出后,simpy会自动释放resource。最后,我们来简单定义一个车辆陆陆续续到达洗车场的函数即可:

def set_up(env, num_machines, washtime, t_inter):# 先创建一个carwash类carwash = CarWash(env, num_machines, washtime)i = 0# 初始有四辆车for _ in range(4):i += 1env.process(car(env, i, carwash))# 创建后续驶来的车辆while True:i += 1yield env.timeout(random.randint(t_inter - 2, t_inter + 2))env.process(car(env, i, carwash))

最后启动仿真:

print('EXAMPLE 3: CAR WAHS...')
random.seed(RANDOM_SEED)
env = simpy.Environment()
env.process(set_up(env, NUM_MACHINES, WASHTIME, T_INTER))
env.run(until=SIM_TIME)

仿真结果如下:

EXAMPLE 3: CAR WAHS...
Car:  1  arrives at the carwash at:  0
Car:  2  arrives at the carwash at:  0
Car:  3  arrives at the carwash at:  0
Car:  4  arrives at the carwash at:  0
Car:  1  enters the carwash at:  0
Car:  2  enters the carwash at:  0
Car:  5  arrives at the carwash at:  5
Car:  1  leaves the carwash at:  5
Car:  2  leaves the carwash at:  5
Car:  3  enters the carwash at:  5
Car:  4  enters the carwash at:  5
Car:  6  arrives at the carwash at:  10
Car:  3  leaves the carwash at:  10
Car:  4  leaves the carwash at:  10
Car:  5  enters the carwash at:  10
Car:  6  enters the carwash at:  10
Car:  5  leaves the carwash at:  15
Car:  6  leaves the carwash at:  15
Car:  7  arrives at the carwash at:  17
Car:  7  enters the carwash at:  17
http://www.lryc.cn/news/277967.html

相关文章:

  • 前端随机验证码安全验证sdk
  • 语境化语言表示模型
  • PDO【配置】
  • CMake入门教程【高级篇】管理MSVC编译器警告
  • 【JaveWeb教程】(8)Web前端基础:Vue组件库Element之Table表格组件和Pagination分页组件 详细示例介绍
  • llama_index 创始人为我们展示召回提升策略(提升15%)
  • RAG 详解
  • 【llm 部署运行videochat--完整教程】
  • Talking about likes
  • DeepSeek 发布全新开源大模型,数学推理能力超越 LLaMA-2
  • 代码随想录算法训练营第二十一天| 回溯 216. 组合总和 III 17. 电话号码的字母组合
  • 微服务架构最佳实践
  • 国内首款支持苹果Find My芯片-伦茨科技ST17H6x
  • linux 01 centos镜像下载,服务器,vmware模拟服务器
  • Linux安装RabbitMq明白纸(无图)
  • Android - CrashHandler 全局异常捕获器
  • 商品源数据如何采集,您知道吗?
  • 输入输出流、字符字节流、NIO
  • js中对数字,超大金额(千位符,小数点)格式化处理
  • Android 打开热点2.4G系统重启解决
  • 全链路压力测试有哪些主要作用
  • 【python基础教程】print输出函数和range()函数的正确使用方式
  • LeetCode255.用队列实现栈
  • PHPStudy快速搭建网站并结合内网穿透远程访问本地站点
  • AI嵌入式K210项目(1)-芯片开发板介绍
  • Blazor中使用impress.js
  • ros2 ubuntu 20.04 安装 foxy
  • Blazor 错误笔记
  • 【深度学习1对1指导】
  • XUbuntu22.04之快速复制绝对路径(二百零五)