Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions docs/Guides/System/behaviour/custom_exit_node.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
====================
Counting Late Orders
====================

In this example we will collect a statistic while the simulation is running, as each customer leaves the system.
We do this by writing a new :code:`ExitNode` class and passing it to the Simulation with the :code:`exit_node_class` keyword.

Consider a bakery that takes orders at a counter and then bakes them in an oven, one order at a time.
Orders arrive at a rate of 0.5 per minute, taking an order happens at a rate of 1 per minute, and baking at a rate of 0.6 per minute.
The bakery promises that every order will be ready within 15 minutes of arriving, and wants to know how often it breaks this promise.

First create a :code:`LateOrderCounter` class that inherits from :code:`ciw.ExitNode`.
Every individual that leaves the system is passed to the exit node's :code:`accept` method, so we can check each one there before calling the parent's :code:`accept`::

>>> import ciw

>>> class LateOrderCounter(ciw.ExitNode):
... def __init__(self, target=15):
... super().__init__()
... self.target = target
... self.late_orders = 0
...
... def accept(self, next_individual, completed=True):
... arrived = next_individual.data_records[0].arrival_date
... left = next_individual.data_records[-1].exit_date
... if completed and left - arrived > self.target:
... self.late_orders += 1
... super().accept(next_individual, completed)

Note that the time in the system is read from the individual's data records: the first record holds the date they arrived at the counter, and the last record holds the date they left the oven.
By the time an individual reaches the exit node, attributes such as :code:`next_individual.exit_date` have already been reset, and :code:`arrival_date` only refers to the last node visited.

Ciw creates the exit node without any arguments, so any new arguments of :code:`__init__` need default values, like :code:`target` here.

Now create the network::

>>> N = ciw.create_network(
... arrival_distributions=[ciw.dists.Exponential(rate=0.5), None],
... service_distributions=[ciw.dists.Exponential(rate=1.0),
... ciw.dists.Exponential(rate=0.6)],
... routing=[[0.0, 1.0],
... [0.0, 0.0]],
... number_of_servers=[1, 1]
... )

and tell Ciw to use the new exit node by using the :code:`exit_node_class` keyword.
We'll simulate an eight hour day::

>>> ciw.seed(0)
>>> Q = ciw.Simulation(N, exit_node_class=LateOrderCounter)
>>> Q.simulate_until_max_time(480)

The exit node is the last node in :code:`Q.nodes`, and it now holds our count alongside the usual totals::

>>> exit_node = Q.nodes[-1]
>>> exit_node.number_of_completed_individuals
238
>>> exit_node.late_orders
78

So around a third of the orders took longer than 15 minutes.
61 changes: 61 additions & 0 deletions docs/Guides/System/behaviour/custom_individuals.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
=============================
Customers With Own Attributes
=============================

In this example we will give each customer an attribute of their own, and let that attribute drive their service time.
We do this by writing a new :code:`Individual` class and passing it to the Simulation with the :code:`individual_class` keyword.

Consider a supermarket with a single checkout. Customers arrive at a rate of 0.4 per minute, and each one is carrying a basket of somewhere between 1 and 20 items.
Scanning takes 0.1 minutes per item, plus 0.5 minutes to pay, so a customer's service time depends entirely on the size of their basket.

First create a :code:`Shopper` class that inherits from :code:`ciw.Individual`.
Its :code:`__init__` method calls the parent's :code:`__init__` and then picks the size of the basket.
Using Python's :code:`random` module here means that :code:`ciw.seed` still controls the results::

>>> import ciw
>>> import random

>>> class Shopper(ciw.Individual):
... def __init__(self, *args, **kwargs):
... super().__init__(*args, **kwargs)
... self.items = random.randint(1, 20)

Ciw passes the individual being served to the :code:`sample` method of the service distribution, so a custom distribution can read the new attribute::

>>> class PerItemService(ciw.dists.Distribution):
... def sample(self, t=None, ind=None):
... return 0.5 + 0.1 * ind.items

Now create the network as usual::

>>> N = ciw.create_network(
... arrival_distributions=[ciw.dists.Exponential(rate=0.4)],
... service_distributions=[PerItemService()],
... number_of_servers=[1]
... )

and tell Ciw to create every customer as a :code:`Shopper` by using the :code:`individual_class` keyword.
Note that this takes the class itself, not an instance of it.
We'll simulate an eight hour day::

>>> ciw.seed(0)
>>> Q = ciw.Simulation(N, individual_class=Shopper)
>>> Q.simulate_until_max_time(480)

The individuals that have left the system are kept at the exit node, :code:`Q.nodes[-1]`, and they still carry their :code:`items` attribute.
Comparing this with the service time in each customer's data record shows the service times followed the basket sizes::

>>> shoppers = Q.nodes[-1].all_individuals
>>> len(shoppers)
188
>>> [(s.items, round(s.data_records[0].service_time, 2)) for s in shoppers[:5]]
[(14, 1.9), (17, 2.2), (10, 1.5), (12, 1.7), (7, 1.2)]

So customers with small baskets were served much faster than those with full ones::

>>> small = [s.data_records[0].service_time for s in shoppers if s.items <= 5]
>>> full = [s.data_records[0].service_time for s in shoppers if s.items >= 16]
>>> round(sum(small) / len(small), 2)
0.79
>>> round(sum(full) / len(full), 2)
2.32
56 changes: 56 additions & 0 deletions docs/Guides/System/behaviour/custom_servers.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
=============================
Servers With Different Speeds
=============================

In this example we will give servers an attribute of their own, so that some servers work faster than others.
We do this by writing a new :code:`Server` class and passing it to the Simulation with the :code:`server_class` keyword.

Consider a help desk with three members of staff. Customers arrive at a rate of 2 per minute.
A typical member of staff serves at a rate of 0.6 per minute, but Server 1 is experienced and works twice as fast as the others.

First create a :code:`StaffMember` class that inherits from :code:`ciw.Server`.
Ciw creates servers by passing in the node they belong to, an ID number, and a start date, so the new :code:`__init__` method must accept the same arguments and pass them on to the parent class.
It then gives the server a :code:`speed` based on its ID number::

>>> import ciw

>>> class StaffMember(ciw.Server):
... def __init__(self, node, id_number, start_date=0.0):
... super().__init__(node, id_number, start_date)
... self.speed = 2.0 if id_number == 1 else 1.0

By the time a service time is sampled the customer has already been attached to a server, available as :code:`ind.server`.
So a custom distribution can sample a service time as usual and divide it by that server's speed::

>>> class SpeedAdjusted(ciw.dists.Exponential):
... def sample(self, t=None, ind=None):
... return super().sample(t, ind) / ind.server.speed

Now create the network::

>>> N = ciw.create_network(
... arrival_distributions=[ciw.dists.Exponential(rate=2.0)],
... service_distributions=[SpeedAdjusted(rate=0.6)],
... number_of_servers=[3]
... )

and tell Ciw to use :code:`StaffMember` for every server by using the :code:`server_class` keyword.
We'll simulate an eight hour shift::

>>> ciw.seed(0)
>>> Q = ciw.Simulation(N, server_class=StaffMember)
>>> Q.simulate_until_max_time(480)

The servers at the node are now :code:`StaffMember` objects, each with their own speed::

>>> [(s.id_number, s.speed) for s in Q.nodes[1].servers]
[(1, 2.0), (2, 1.0), (3, 1.0)]

Each data record notes which server served the customer, so we can count how many customers each member of staff served.
The experienced server got through roughly twice as many as each of the others::

>>> recs = Q.get_all_records()
>>> [len([r for r in recs if r.server_id == i]) for i in [1, 2, 3]]
[484, 226, 259]

Servers that are added during the simulation, for example by a server schedule, are created with the same class, so they would get a speed too.
9 changes: 6 additions & 3 deletions docs/Guides/System/behaviour/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
How to Get More Custom Behaviour
================================

Custom behaviour can be obtained by writing new :code:`Node`, :code:`ArrivalNode`, :code:`ExitNode`, :code:`Individual`, and/or :code:`Server` classes, that inherit from the original :code:`ciw.Node`, :code:`ciw.ArrivalNode`, :code:`ciw.Individual` and :code:`ciw.Server` classes respectively, that introduce new beahviour into the system.
The classes that can be overwitten are:
Custom behaviour can be obtained by writing new :code:`Node`, :code:`ArrivalNode`, :code:`ExitNode`, :code:`Individual`, and/or :code:`Server` classes, that inherit from the original :code:`ciw.Node`, :code:`ciw.ArrivalNode`, :code:`ciw.ExitNode`, :code:`ciw.Individual` and :code:`ciw.Server` classes respectively, that introduce new behaviour into the system.
The classes that can be overwritten are:

- :code:`Node`: the main node class used to represent a service centre.
- :code:`ArrivalNode`: the node class used to generate individuals and route them to a specific :code:`Node`.
- :code:`ExitNode`: the node class used to collect customers as they leave the system.
- :code:`Individual`: the individual class used to represent the individual entities.
- :code:`Server`: the class used to represent the servers that sit at a service centre.

These new classes can be used with the Simulation class by using the keyword arugments :code:`node_class`, :code:`arrival_node_class`, :code:`exit_node_class`, :code:`individual_class`, and :code:`server_class`.
These new classes can be used with the Simulation class by using the keyword arguments :code:`node_class`, :code:`arrival_node_class`, :code:`exit_node_class`, :code:`individual_class`, and :code:`server_class`.
These arguments take a class (not an instance of a class), to use throughout the whole simulation.
The argument :code:`node_class` can also take a list of classes, indicating which node class to use on each node of the network. This allows for different nodes to exhibit very different behaviours to one another.

Expand All @@ -29,4 +29,7 @@ Here's a library of examples of this functionality:
custom_arrivals.rst
custom_number_servers.rst
server_dependent_dist.rst
custom_individuals.rst
custom_servers.rst
custom_exit_node.rst
hybrid.rst