![]() |
Unexpected simpy simulation output - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Unexpected simpy simulation output (/thread-12640.html) |
Unexpected simpy simulation output - bakas - Sep-05-2018 I have just started with Simpy and python in general and I am trying to build a simulation code, which runs simulation for multiple stores in the same environment. The code takes the number of store and capacity for each store as an input from the user. The problem I am facing is that for a given store, even when the resource is busy i.e. a customer is already using it, the same resource is still getting allocated to the second customer as well, I am new to python so I maybe doing something wrong with the scope of a variable or a function but I am unable to debug the problem. Here is the code: I have just started with Simpy and python in general and I am trying to build a simulation code, which runs simulation for multiple stores in the same environment. The code takes the number of store and capacity for each store as an input from the user. The problem I am facing is that for a given store, even when the resource is busy i.e. a customer is already using it, the same resource is still getting allocated to the second customer as well, I am new to python so I maybe doing something wrong with the scope of a variable or a function but I am unable to debug the problem. Here is the code: # Defining simulation environment variables import random import simpy import traceback RANDOM_SEED = 42 NEW_CUSTOMERS = 5 # Total number of customers INTERVAL_CUSTOMERS = 10.0 # Function to generate prescriptions for a given store def store(env, number, interval, store_counter,store_num): for i in range(number): cust_name = 'Store' + str(store_num) + 'Customer' c = customer(env, cust_name + str(i), store_counter, time_in_input_stn=1.0,time_to_fill=5.0) env.process(c) t = random.expovariate(1.0 / interval) yield env.timeout(t) # Function to define and mimic the process of prescriptions being processed at each work station def customer(env, name, counter, time_in_input_stn,time_to_fill): arrive = env.now print('%7.4f %s: Entered pharmacy' % (arrive, name)) with counter.request() as req: results = yield req wait = env.now - arrive # We got to the str1_counter print('%7.4f %s: Waited in greet queue for %6.3f' % (env.now, name, wait)) tib = time_in_input_stn yield env.timeout(tib) print('%7.4f %s: Greet process completed ' % (env.now, name)) dig_que_wait = env.now with comm_counter.request() as comm: result1 = yield comm wait= env.now - dig_que_wait print('%7.4f %s: Waited in digital queue for %6.3f' % (env.now, name, wait)) tif = time_to_fill yield env.timeout(tif) print('%7.4f %s: Digital process done ' % (env.now, name)) # Setting up the simulation environment random.seed(RANDOM_SEED) env = simpy.Environment() comm_counter = simpy.Resource(env, capacity=2) # Function to dynamically generate resources and process objects def resource_gen(str_no,cap): str_cap_lst['Store_' + str_no + '_counter'] = simpy.Resource(env, capacity=cap) env.process(store(env, NEW_CUSTOMERS, INTERVAL_CUSTOMERS,str_cap_lst['Store_' + str_no + '_counter'],str_no )) # User input on number of stores & their respective capacities sim_list = list() str_cap_lst = {} store_cnt = input("How many stores do you want to choose") for k in range(store_cnt): sim_details = raw_input("Enter the store & capacity") sim_list = sim_details.split(",") resource_gen(sim_list[0],sim_list[1]) # Run the simulation env.run()This is the snippet of the ouput: As you can see for Store200 customer 0 arrives at 0 sec, and another customer 1 arrives at .25 sec, so ideally customer 1 needs to wait for atleast 1 min(processing time for customer 0 at the work station) before getting the resource, but it does not and immediately starts with the processing.How do I solve this? RE: Unexpected simpy simulation output - scidam - Sep-07-2018 I have had some experience with the SimPy framework. It seems that capacity (allowed queue length) of Store200 is greater 1. What parameters did you use to run the model?I slightly restructured your code, .... arrive = env.now print('%7.4f %s: Entered pharmacy' % (env.now, name)) print('%7.4f %s: Started to wait... ' % (env.now, name)) with counter.request() as req: results = yield req print('%7.4f %s: The resource is free, lets use it... ' % (env.now, name)) wait = env.now - arrive # We got to the str1_counter print('%7.4f %s: Waited in greet queue for %6.3f' % (env.now, name, wait)) tib = time_in_input_stn yield env.timeout(tib) print('%7.4f %s: Greet process completed ' % (env.now, name)) .....and ran it with unity story capacities and got the following output: It seems, that everything works fine...
|