2017-11-17 98 views
0

我reffering這個例子:https://simpy.readthedocs.io/en/latest/examples/carwash.html如何在排隊模擬中包含最後一個對象? SimPy中的Python

如果你看到這個算法的輸出,上車/對象到達不離開,因爲模擬已經完成。我想修改算法,例如服務器在仿真時間內完成爲系統中已有的所有汽車提供服務。我嘗試了很多,但是我沒有成功。

""" 
Carwash example. 

Covers: 

- Waiting for other processes 
- Resources: Resource 

Scenario: 
    A carwash has a limited number of washing machines and defines 
    a washing processes that takes some (random) time. 

    Car processes arrive at the carwash at a random time. If one washing 
    machine is available, they start the washing process and wait for it 
    to finish. If not, they wait until they an use one. 

""" 
import random 

import simpy 


RANDOM_SEED = 42 
NUM_MACHINES = 2 # Number of machines in the carwash 
WASHTIME = 5  # Minutes it takes to clean a car 
T_INTER = 7  # Create a car every ~7 minutes 
SIM_TIME = 20  # Simulation time in minutes 


class Carwash(object): 
    """A carwash has a limited number of machines (``NUM_MACHINES``) to 
    clean cars in parallel. 

    Cars have to request one of the machines. When they got one, they 
    can start the washing processes and wait for it to finish (which 
    takes ``washtime`` minutes). 

    """ 
    def __init__(self, env, num_machines, washtime): 
     self.env = env 
     self.machine = simpy.Resource(env, num_machines) 
     self.washtime = washtime 

    def wash(self, car): 
     """The washing processes. It takes a ``car`` processes and tries 
     to clean it.""" 
     yield self.env.timeout(WASHTIME) 
     print("Carwash removed %d%% of %s's dirt." % 
       (random.randint(50, 99), car)) 


def car(env, name, cw): 
    """The car process (each car has a ``name``) arrives at the carwash 
    (``cw``) and requests a cleaning machine. 

    It then starts the washing process, waits for it to finish and 
    leaves to never come back ... 

    """ 
    print('%s arrives at the carwash at %.2f.' % (name, env.now)) 
    with cw.machine.request() as request: 
     yield request 

     print('%s enters the carwash at %.2f.' % (name, env.now)) 
     yield env.process(cw.wash(name)) 

     print('%s leaves the carwash at %.2f.' % (name, env.now)) 


def setup(env, num_machines, washtime, t_inter): 
    """Create a carwash, a number of initial cars and keep creating cars 
    approx. every ``t_inter`` minutes.""" 
    # Create the carwash 
    carwash = Carwash(env, num_machines, washtime) 

    # Create 4 initial cars 
    for i in range(4): 
     env.process(car(env, 'Car %d' % i, carwash)) 

    # Create more cars while the simulation is running 
    while True: 
     yield env.timeout(random.randint(t_inter - 2, t_inter + 2)) 
     i += 1 
     env.process(car(env, 'Car %d' % i, carwash)) 


# Setup and start the simulation 
print('Carwash') 

random.seed(RANDOM_SEED) # This helps reproducing the results 

# Create an environment and start the setup process 
env = simpy.Environment() 
env.process(setup(env, NUM_MACHINES, WASHTIME, T_INTER)) 

# Execute! 
env.run(until=SIM_TIME) 
+0

我修改了「而真正的」,也就是在設置功能這樣的:當真正和env.now>噸。但它不起作用。 –

+0

顯示您的代碼。 – pjs

+0

我做到了...... –

回答

0

要處理這個問題,您需要檢查Car進程數。只要它是積極的,不要停止模擬。

while True: 
    yield env.timeout(random.randint(t_inter - 2, t_inter + 2)) 
    i += 1 
    env.process(car(env, 'Car %d' % i, carwash)) 

嘗試調整的同時,功能是你最後到達的車。你的模擬時間是當你的世界不再存在時,而不是你的汽車停止時。 例如,您可以爲SIM_TIME創建一個setter,以便爲系統中的每輛車提供所需的時間。而不是使用until=SIM_TIME,你可以使用env.exit()

env.exit() reference

env.run(until) reference

相關問題