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
#2
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:

Output:
How many stores do you want to choose1 Enter the store & capacity1,1 0.0000 Store1Customer0: Entered pharmacy 0.0000 Store1Customer0: Started to wait... 0.0000 Store1Customer0: The resource is free, lets use it... 0.0000 Store1Customer0: Waited in greet queue for 0.000 0.5100 Store1Customer1: Entered pharmacy 0.5100 Store1Customer1: Started to wait... 0.5227 Store1Customer2: Entered pharmacy 0.5227 Store1Customer2: Started to wait... 0.6835 Store1Customer3: Entered pharmacy 0.6835 Store1Customer3: Started to wait... 0.8098 Store1Customer4: Entered pharmacy 0.8098 Store1Customer4: Started to wait... 1.0000 Store1Customer0: Greet process completed 1.0000 Store1Customer0: Waited in digital queue for 0.000 1.0000 Store1Customer1: The resource is free, lets use it... 1.0000 Store1Customer1: Waited in greet queue for 0.490 2.0000 Store1Customer1: Greet process completed 2.0000 Store1Customer1: Waited in digital queue for 0.000 2.0000 Store1Customer2: The resource is free, lets use it... 2.0000 Store1Customer2: Waited in greet queue for 1.477 3.0000 Store1Customer2: Greet process completed 3.0000 Store1Customer3: The resource is free, lets use it... 3.0000 Store1Customer3: Waited in greet queue for 2.316 4.0000 Store1Customer3: Greet process completed 4.0000 Store1Customer4: The resource is free, lets use it... 4.0000 Store1Customer4: Waited in greet queue for 3.190 5.0000 Store1Customer4: Greet process completed 6.0000 Store1Customer0: Digital process done 6.0000 Store1Customer2: Waited in digital queue for 3.000 7.0000 Store1Customer1: Digital process done 7.0000 Store1Customer3: Waited in digital queue for 3.000 11.0000 Store1Customer2: Digital process done 11.0000 Store1Customer4: Waited in digital queue for 6.000 12.0000 Store1Customer3: Digital process done 16.0000 Store1Customer4: Digital process done
It seems, that everything works fine...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected output Starter 2 439 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 654 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 467 Oct-24-2023, 05:56 AM
Last Post: buran
  Unexpected output from df.loc when indexing by label idratherbecoding 6 1,125 Apr-19-2023, 12:11 AM
Last Post: deanhystad
  unexpected output asyrafcc99 0 1,478 Oct-24-2020, 02:40 PM
Last Post: asyrafcc99
  Unexpected output: symbols for derivative not being displayed saucerdesigner 0 2,011 Jun-22-2020, 10:06 PM
Last Post: saucerdesigner
  Unexpected output palladium 4 2,696 Jan-11-2020, 03:26 PM
Last Post: palladium
  Unexpected output: if statement CabbageMan 1 1,729 Sep-04-2019, 04:12 PM
Last Post: ThomasL
  Unexpected Output using classes and inheritance langley 2 1,913 Jul-04-2019, 09:33 AM
Last Post: langley
  float multiplication - unexpected output inesk 3 3,254 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