-1

我正在尝试模拟一个可以驾驶汽车的车库。然后随着时间的推移,汽车需要一些维修,它们变得不可用。修好汽车后,它会回到车库。

我想知道您是否可以为这样的系统建模,让您可以将相同的汽车循环回车库?此外,有兴趣跟踪汽车的不可用性......

4

1 回答 1

0

是的你可以。这是一个车库的例子,汽车从服务中被拉出来进行维修。请注意,当汽车需要维护时,它可能不在车库中。

"""
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}')
于 2021-10-03T05:02:47.453 回答