Commit ff5154f9 authored by Stefan Scherfke's avatar Stefan Scherfke
Browse files

Added the Bank Renege example from SimPy 2.

parent 471a1582
===========
Bank Renege
===========
Covers:
- Resources: Resource
- Condition events
A counter with a random service time and customers who renege. Based on the
program bank08.py from TheBank tutorial of SimPy 2. (KGM)
This example models a bank counter and customers arriving t random times. Each
customer has a certain patience. It waits to get to the counter until she’s at
the end of her tether. If she gets to the counter, she uses it for a while
before releasing it.
New customers are created by the ``source`` process every few time steps.
.. literalinclude:: code/bank_renege.py
The simulation's output:
.. literalinclude:: code/bank_renege.out
Bank renege
0.0000 Customer00: Here I am
0.0000 Customer00: Waited 0.000
3.8595 Customer00: Finished
10.2006 Customer01: Here I am
10.2006 Customer01: Waited 0.000
12.7265 Customer02: Here I am
13.9003 Customer02: RENEGED after 1.174
23.7507 Customer01: Finished
34.9993 Customer03: Here I am
34.9993 Customer03: Waited 0.000
37.9599 Customer03: Finished
40.4798 Customer04: Here I am
40.4798 Customer04: Waited 0.000
43.1401 Customer04: Finished
Bank renege
0.0000 Customer00: Here I am
0.0000 Customer00: Waited 0.000
3.8595 Customer00: Finished
10.2006 Customer01: Here I am
10.2006 Customer01: Waited 0.000
12.7265 Customer02: Here I am
13.9003 Customer02: RENEGED after 1.174
23.7507 Customer01: Finished
34.9993 Customer03: Here I am
34.9993 Customer03: Waited 0.000
37.9599 Customer03: Finished
40.4798 Customer04: Here I am
40.4798 Customer04: Waited 0.000
43.1401 Customer04: Finished
"""
Bank renege example
Covers:
- Resources: Resource
- Condition events
Scenario:
A counter with a random service time and customers who renege. Based on the
program bank08.py from TheBank tutorial of SimPy 2. (KGM)
"""
import random
import simpy
RANDOM_SEED = 42
NEW_CUSTOMERS = 5 # Total number of customers
INTERVAL_CUSTOMERS = 10.0 # Generate new customers roughly every x seconds
MIN_PATIENCE = 1 # Min. customer patience
MAX_PATIENCE = 3 # Max. customer patience
def source(env, number, interval, counter):
"""Source generates customers randomly"""
for i in range(number):
c = customer(env, 'Customer%02d' % i, counter, time_in_bank=12.0)
env.start(c)
t = random.expovariate(1.0 / interval)
yield env.timeout(t)
def customer(env, name, counter, time_in_bank):
"""Customer arrives, is served and leaves."""
arrive = env.now
print('%7.4f %s: Here I am' % (arrive, name))
with counter.request() as req:
patience = random.uniform(MIN_PATIENCE, MAX_PATIENCE)
# Wait for the counter or abort at the end of our tether
results = yield req | env.timeout(patience)
wait = env.now - arrive
if req in results:
# We got to the counter
print('%7.4f %s: Waited %6.3f' % (env.now, name, wait))
tib = random.expovariate(1.0 / time_in_bank)
yield env.timeout(tib)
print('%7.4f %s: Finished' % (env.now, name))
else:
# We reneged
print('%7.4f %s: RENEGED after %6.3f' % (env.now, name, wait))
# Setup and start the simulation
print('Bank renege')
random.seed(RANDOM_SEED)
env = simpy.Environment()
# Start processes and simulate
counter = simpy.Resource(env, capacity=1)
env.start(source(env, NEW_CUSTOMERS, INTERVAL_CUSTOMERS, counter))
simpy.simulate(env)
......@@ -12,6 +12,7 @@ Here's a list of examples grouped by features they demonstrate.
Condition events
================
- :doc:`bank_renege`
- :doc:`movie_renege`
......@@ -40,16 +41,17 @@ Resources: Preemptive Resource
Resources: Resource
===================
- :doc:`gas_station_refuel`
- :doc:`bank_renege`
- :doc:`carwash`
- :doc:`gas_station_refuel`
- :doc:`movie_renege`
Resources: Store
================
- :doc:`process_communication`
- :doc:`latency`
- :doc:`process_communication`
Shared events
......@@ -70,6 +72,7 @@ All examples
.. toctree::
bank_renege
carwash
machine_shop
movie_renege
......@@ -77,7 +80,6 @@ All examples
process_communication
latency
.. bank_renege
.. call_center
.. cellphone
.. central-server
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment