Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

SIMULATION

USING Modeling and


Simulation

SIMPY
PYTHON IS A POPULAR GENERAL PURPOSE
PROGRAMMING LANGUAGE WITH MANY LIBRARIES TO
BOOT. PANDAS, NUMPY, SCIPY, SKLEARN, RANDOM,
MATPLOTLIB, SIMPY ARE SOME OF THE COMMONLY
USED LIBRARIES IN DATA SCIENCE.

SIMPY IS A SIMULATION LIBRARY FOR PYTHON. IT IS


OBJECT-ORIENTED AND ALLOWS FOR PROCESS
BASED DISCRETE-EVENT SIMULATION.
�Coffee shops are generally a popular place to
hangout. Starbucks, Tim Hortons, CBTL, etc. It
is not just the rejuvenating coffee that attracts
customers, but the ambience draws people
who want to read, work on their laptops, write,
do some deep thinking, carry on conversation,
or just relax. However, sometimes getting a cup
COFFEE
of coffee can take a long wait. Coffee shops try
to minimize the wait times so that customers
SHOP
leave satisfied. SCENARI
�In this lecture we will simulate a simple coffee
shop. The purpose of this simulation is to see O
how the number of cashiers and baristas affect
the wait times of customers.
PROCESS
1. A customer arrives.
2. A customer go to the cashier to place the order and make the payment.
3. The cashier accepts payments through cash or cards.
4. It generally takes 15 to 30 seconds to service a payment by cash and 10
to 20 seconds to service a payment by card.
5. Once the payment is made the item is prepared by the barista.
6. The items on order are: Regular Coffee (10-15), Latte (30-45), Mocha
(30-45), Cold Brew (10-20), Frappe (50-70), and Espresso (20-35).
7. When the item is ready the customer picks up the order.
HOW LONG WILL THE
WAIT BE?
HOW LONG WILL THE WAIT
BE?
▪ There are two wait periods in the above process.
▪ The first is the queue at the cashier and the second is the queue at the
barista.
▪ The wait times at both the queues depend on the number of cashiers and
baristas at work.
▪ More the number of cashiers and baristas, shorter will be the wait time.
▪ However, economic factors would stipulate that the shop could employ only
a certain number of staff.
▪ This balance between number of staff and customer wait times is important
for customer satisfaction as well as business viability.
SIMPY
▪ SimPy models execute in an environment.
▪ Models comprise of processes which correspond to active components like
customers in a shop, aircrafts in an airport, vehicles in a parking lot, etc.
▪ Processes interact with each other and the environment through events.
▪ Processes get suspended when they yield an event and resume when the
event occurs.
▪ Timeouts are triggered after a user-defined amount of time has passed.
▪ This allows the process to sleep or hold its state for the given time.
▪ Events make use of resources which correspond to check-out counters in a
shop, runways in an airport, parking spots in a lot, etc. Resources may be
finite or infinite.
SIMPY
▪ Processes request resources during an event and release them when they
are done.
▪ If resources are not available when requested, the process joins a queue
and waits for the next available resource.
▪ There are two other kinds of resources called level, and store.
▪ These are used when we want to keep track of quantities that are being
consumed by processes during events.
▪ An example for level would be the total amount of gasoline in the gas
station. Whenever vehicles fill gas, the level of gasoline decreases.
▪ This can be factored in a simulation. A store tracks the production and
consumption of items of multiple types.
Here are some of the commands that we will be using:
• Import the library: import simpy

• Create the environment: env = simpy.Environment()

• Define a resource called cashier (with 2 cashiers): cashier = simpy.Resource(env,


2)

• Generate a random number to indicate payment by cash or card (1=cash;


2=card): payment_type = random.randint(1,2)

• Yield a timeout event (to prepare the coffee): yield env.timeout(time_to_prepare)

• Start the process to generate customers: env.process(generate_customer(env,


cashier, barista))
THE SIMULATION
▪ We are simulating for 20 customers and a
coffee shop with 2 cashiers and 3 baristas.
▪ menu is a dictionary that contains the items
that may be ordered and their preparation
time ranges.
▪ payment_options is a dictionary that
contains the options for making the
payment and the time range it takes to
process the payment.
THE SIMULATION
▪ We create four lists (payment_wait_time,
payment_time, order_wait_time,
and order_time)
▪ To keep track of the time a customer has to
wait until a cashier is available, time it takes
for a customer to order, the time until a
barista is available, and the time to get the
order prepared.
THE SIMULATION
▪ This simulation involves two processes.
▪ The first is a process to generate customers at a given frequency.
▪ The second process is the customer itself with two events: making a
payment, and collecting the order.
▪ Making the payment involves the cashier resource and collecting the order
involves the barista resource.
THE SIMULATION

▪ generate-customer waits between 1 to 20


seconds before generating a customer.
▪ The timeout event uses
the random.randint(1,20) function call which
returns a number between 1 and 20 using the
uniform distribution.
▪ Once a customer is generated,
the customer process is called.
▪ We will use other distributions to better replicate
the arrival times of customers in the next line of
codes.
The customer process first prints
the customer number and arrival
time. env.now returns the current
time in seconds since start of the
simulation.

The start_cq variable holds the


time the customer arrived at the
coffee shop. When the customer
arrives, they make a request for
the cashier resource using yield
req (note that req is defined with
the with statement).

When the req is made available


(that is, the customer reaches the
cashier counter), we randomly
choose an item from the menu to
order and a payment type. Both
these random numbers are drawn
using uniform distribution.
Next a request is made for
a barista and start_bq holds the
time the customer has to wait
until a barista begins preparing
their order.

When the barista is available,


a timeout is marked while the
order is prepared.

This random time duration is


from the uniform distribution
depending on the time range of
the particular item ordered.

Once the item is prepared, the


time taken is printed and the
customer gets the item ordered.
THE SIMULATION
Next we define the
environment and resources
and start the process of
generating customers. The
environment is run for 400
seconds.

We print the average time it


takes for customers to pay
and get the order serviced.
THANK
YOU
Email
jdbungay@earist.ph.educati
on

You might also like