Python Forum
Unexpected simpy simulation output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected simpy simulation output
#1
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:

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?
Reply


Messages In This Thread
Unexpected simpy simulation output - by bakas - Sep-05-2018, 06:00 AM
RE: Unexpected simpy simulation output - by scidam - Sep-07-2018, 01:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected output Starter 2 527 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 767 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 550 Oct-24-2023, 05:56 AM
Last Post: buran
  Unexpected output from df.loc when indexing by label idratherbecoding 6 1,270 Apr-19-2023, 12:11 AM
Last Post: deanhystad
  unexpected output asyrafcc99 0 1,523 Oct-24-2020, 02:40 PM
Last Post: asyrafcc99
  Unexpected output: symbols for derivative not being displayed saucerdesigner 0 2,084 Jun-22-2020, 10:06 PM
Last Post: saucerdesigner
  Unexpected output palladium 4 2,785 Jan-11-2020, 03:26 PM
Last Post: palladium
  Unexpected output: if statement CabbageMan 1 1,792 Sep-04-2019, 04:12 PM
Last Post: ThomasL
  Unexpected Output using classes and inheritance langley 2 1,980 Jul-04-2019, 09:33 AM
Last Post: langley
  float multiplication - unexpected output inesk 3 3,384 Dec-11-2018, 10:59 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020