python - 你能在 Python 上使用 SimPy 做一个闭环系统吗?
问题描述
我正在尝试模拟一个可以驾驶汽车的车库。然后随着时间的推移,汽车需要一些维修,它们变得不可用。修好汽车后,它会回到车库。
我想知道您是否可以为这样的系统建模,让您可以将相同的汽车循环回车库?此外,有兴趣跟踪汽车的不可用性......
解决方案
是的你可以。这是一个车库的例子,汽车从服务中被拉出来进行维修。请注意,当汽车需要维护时,它可能不在车库中。
"""
Simple simulation of resources that get pulled from service for maintenance
Programmer: Michael R. Gibbs
"""
import simpy
import random
class Car():
"""
The resouces
Cars require maintenance at random intervals
If maintenace is required while the car is in use, maintenace is done
when it is returned to the garage.
"""
def __init__(self,env , id, garage):
self.env = env
self.id = id
self.garage = garage
self.needs_repair = False
self.down_time = 0
self.env.process(self.repair_events())
def repair_events(self):
"""
Flags when the car needs maintenace
"""
while(True):
yield self.env.timeout(random.randint(15,20))
self.needs_repair = True
# checks if car is in the garge and can have maintence
self.env.process(self.garage.need_repairs(self))
class Garage():
"""
Manages the car resources
does repairs on the cars when needed
"""
def __init__(self, env, numOfCars):
self.env = env
self.store = simpy.Store(env,numOfCars)
self.store.items = [Car(env, id + 1, self) for id in range(numOfCars) ]
def need_repairs(self, car):
"""
Notifies the garage that the car needs maintenace
If the car is in the garage then it is pulled from service,
has it maintenace done, and then put back in service
If the car is not in the garage, maintence is done when the
car returns to the garage
"""
if car in self.store.items:
print(self.env.now, f"car {car.id} needs maintenance and is in the garage")
self.store.items.remove(car)
yield env.process(self.make_repairs(car))
self.store.put(car)
def make_repairs(self, car):
"""
Do maintenace on a car
"""
print(self.env.now, f'making repairs to car {car.id}')
repair_time = random.randint(2,5)
yield self.env.timeout(repair_time)
car.needs_repair = False
car.down_time += repair_time
print(self.env.now,f'finished repairs to car {car.id}')
def put(self, car):
"""
Return a car back to the garage
Checks if car needs maintenace
"""
print (self.env.now, f'car {car.id} has return to the garage')
if car.needs_repair:
yield env.process(self.make_repairs(car))
self.store.put(car)
def get(self):
"""
Pulls a car from the garage
"""
car = yield self.store.get()
print(self.env.now, f"car {car.id} is leaving the garage")
return car
def use_car(env, garage):
"""
Process for getting a car, use it for a random time, and return it to the garage
"""
print(env.now,f"starting use car process with {len(garage.store.items)} cars in the garage")
car = yield env.process(garage.get())
print(env.now, f'using car {car.id}')
yield env.timeout(random.randint(1,4))
yield env.process(garage.put(car))
print(env.now,f'returned car {car.id}')
def sched_cars(env,garage):
"""
Generates requests for cars
"""
while True:
yield env.timeout(random.randint(1,5))
env.process(use_car(env,garage))
# boot up the simulation
env = simpy.Environment()
garage = Garage(env,10)
env.process(sched_cars(env, garage))
env.run(200)
for car in garage.store.items:
print(f'car {car.id} down time is {car.down_time}')
推荐阅读
- node.js - 从前端登录时,Passport JS 身份验证返回 false
- javascript - 单击更改颜色输入字段
- c - 为 F(n)=0.5F(n-1) 编写函数
- regex - 关于正则表达式
- python - Python继承,将列表传递给另一个类
- oracle - ORA-01652: 无法扩展临时空间
- javascript - Javascript中的排序数组:元素最多的项目按降序排列
- c++ - 在 Qt 中将(富)文本高效地附加到 QTextEdit 或 QTextBrowser
- sql - 不带列名的 JSON 格式的查询响应
- javascript - 如果销售价格为 0,则显示标价,否则同时显示