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:
How do I solve this?
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:
Output:0.0000 Store100Customer0: Entered pharmacy
0.0000 Store200Customer0: Entered pharmacy
0.0000 Store100Customer0: Waited in greet queue for 0.000
0.0000 Store200Customer0: Waited in greet queue for 0.000
0.2533 Store200Customer1: Entered pharmacy
0.2533 Store200Customer1: Waited in greet queue for 0.000
1.0000 Store100Customer0: Greet process completed
1.0000 Store200Customer0: Greet process completed
1.0000 Store100Customer0: Waited in digital queue for 0.000
1.0000 Store200Customer0: Waited in digital queue for 0.000
1.2533 Store200Customer1: Greet process completed
3.4695 Store200Customer2: Entered pharmacy
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?