你能在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}')

相关内容

最新更新