Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Binder

We give a brief introduction into the fundamendals of discrete-event simulation and give representative examples.

Objectives:

  • What are the components of a discrete event model and how does it work?

  • How is discrete event simulation fundamentally different to all other modelling strategies?

  • In which situations would we use this modelling concept?

Loading...
Loading...
Loading...
Loading...
Loading...

General Idea

If we consider a real system or a real process which, due to its fundamental structure, can only take on discrete values – for example, a queue or a number of entities – it is clear that it cannot change its state continuously. Rather, it will always maintain its current state for a certain period and then change it abruptly. In other words, up to a certain point in time tendt_{end}, there will have been only a finite number of times t1<⋯<tkt_1<\dots<t_k smaller than tendt_{end} at which it changed its state, and, corresponding, a finite number of system states x0,x1,…,xkx_0,x_1,\dots,x_k which the system had in between.

Most dynamic modelling approaches are based on the concept of describing by how much the states of a system change over time. Differential equation models in particular (e.g. determined using a System Dynamics approach) make this especially explicit: the derivative dx/dtdx/dt describes by what infinitesimal amount the system state xx changes as time tt advances by an infinitesimal amount. However, since systems with discrete states, as just described, cannot change by infinitesimally small amounts, this form of description is not necessarily the most accurate. Even temporal discretisation – i.e. updating the model in equidistant time steps as is done with e.g. difference equations – does not change improve the situation, since the points in time at which the system changes its state do not necessarily follow a predefined temporal pattern.

For this reason, discrete event simulation (DES) takes a completely different approach to describing systems: rather than asking by how much the state changes with time, DES asks when the state will next change - event-based instead of activity-based Lackner (1964). On the one hand, this form of description is sufficient because, the idea perfectly mimics how the real system updates without any approximation. In particular, the system’s time-continuous nature is preserved without any discretisation. On the other hand, this form of description is also feasible because, as already mentioned, a value-discrete system only changes at a finite number of points in time. So, knowing that there is no state-change in between, we may simply jump from one point to the next and simulate the whole system as an iterative process.

Discrete-Event Model

With this change of perspective, the idea behind discrete event modelling is now to describe the system in terms of those events that alter its state and the time intervals between them. As with, for example, the relationship between System Dynamics and differential equation models, a distinction must be made here between (conceptual) discrete event modelling and the discrete event simulation model (DES model or discrete event model) itself. For the prior, there are various approaches, which are determined primarily by the chosen model description language (simulation language). Notebook Discrete Event Simulation with Event Graphs gives an example for one of these languages. For now, we define what is formally meant by a DES model.

As given in Banks (2005), a DES model consists of the following components: The current state of the modelled system is descibed as a set of states. The model is updated via so-called events. Whenever an event takes place:

  1. the event may change the state(s) of the system,

  2. the event may schedule new events,

  3. the event may cancel already scheduled events,

  4. the event may change already scheduled events.

With this core definition, a conceptual DES model is already formally defined. The dynamics of the model arises naturally through the iterative processing of events, which in turn schedule new events. However, in order to better establish a link with the implementation, the definition of a DES model goes into greater depths on a number of points:

When a new event is scheduled, a so-called event notice is generated and inserted into an event list (sometimes also called event queue or future event list). The notice contains all relevant information about the event together with the scheduled time. Within the event list, the event notices are kept sorted by their scheduled time so that the first event in the list is always the one to occurs next.

In order to advance the simulation executive (sometimes called scheduler or simulation engine) takes the first event notice out of the event list, advances the simulation time (often referred to as clock) to the scheduled time from the notice (often referred to as advancing the clock), and executes the corresponding event (including state-update and creation and insorting of new event notices). Hereby, the event notice usually refers to one of a finite number of event types, which are used to cluster conceptually equivalent events - e.g. the event type increment would refer to events which increase the state variable’s value by one. After the event execution is completed, the process starts anew and is repeated until (a) the event list is empty or (b) the clock has surpassed a predefined simulation end-time. This process is visualised in Figure 1.

Concept of a DES model: the simulation executive takes the first event notice off the event list, updates the clock and executes the corresponding event. Hereby, states may be updated, new events may be scheduled via creation of new notices, and existing notices may be cancelled or modified.

Figure 1:Concept of a DES model: the simulation executive takes the first event notice off the event list, updates the clock and executes the corresponding event. Hereby, states may be updated, new events may be scheduled via creation of new notices, and existing notices may be cancelled or modified.

We see that, unlike for example a differential equation model, the definition of a DES model is not given from a mathematical perspective but from a computer scientific one. In other words, the implementation of the model is already taken into account during its design. This can easily be explained historically, since the idea of discrete event modelling was always motivated by the development of computer-based simulation languages and simulation environments (see Nance (1996)). In particular, the General Purpose System Simulator (GPSS) and SIMULA were developed in the 1960s and helped to establish DES as a distinct methodology for analyzing industrial, military, and service systems. Since then, advances in computing power and software and more advanced system languages and simulators have expanded DES applications into fields including logistics, healthcare, telecommunications, and transportation.

Case-Study: Collatz Sequence, DES vs. Time-Discrete - Conceptual Model

In the following we give a very simple example of how a DES model is conceptualised and implemented using a case study from pure mathematics: the Collatz sequence. This sequence is defined recusively via

xn+1={xn/2if xn is even,3xn+1otherwise,x_{n+1}=\left\lbrace\begin{array}{cl}x_n/2& \text{if $x_n$ is even},\\3x_n+1& \text{otherwise,}\end{array}\right.

with some initial value x0∈Nx_0\in\mathbb{N}. It is well known from the corresponding Collatz conjecture (or 3k+13k+1 conjecture) which states that the sequence always eventually drops to 1 independent from its initial value. Note that, as soon as the sequence reaches 1, it will be caught in the limit cycle 1,4,2,1,…1,4,2,1,\dots. So, this will pose our termination criterium. Note that reaching this limit cycle is subject of the still unproven famous Collatz conjecture stating, that the sequence will always eventually terminate in this cycle, independent of the initial value x0x_0 (Lothar Collatz, 1937).

For the purposes of modelling, let us assume that there is a dynamic system in which the scalar integer-valued state is updated every second according to the rules of the Collatz sequence, i.e.

∀t∈R+,x(t):=x⌊t⌋\forall t\in \mathbb{R}^+, x(t):=x_{\lfloor t\rfloor}

then we can then use this as an (unusual) case-study for discrete-event modelling: The system can be modelled using a scalar integer-valued state variable xx and three event types:

event typestate-changescheduling
halfx←x2x\leftarrow \frac{x}{2}Depends on the value of the updated xx.
Even and >1>1: new event with type half in 1 second
Odd and >1>1: new event with type 3k+1 in one second.
3k+1x←3x+1x\leftarrow 3x+1Same as above.
runx←x0x\leftarrow x_0Same as above.

As a DES must be triggered by something, it is common practice to define initial event(s) which are manually added to the event list at the start of the simulation. In our case, the run event serves this purpose which also sets the initial value of the sequence.

At present, the conceptual model for the case study is described in a very unintuitive manner using free-form text. This situation is highly problematic in terms of producing a reproducible model description, communicating the model, and implementing it. For this reason, there are a number of simulation languages for DES (such as the event graph formalism introduced in Discrete Event Simulation with Event Graphs or the DEVS formalism) that support creating a valid and visibly appealing reproducible model desciption which can be communicated to domain experts, and allow integration into suitable DES simulators.

Simulation of DES Models

As we have seen, the implementation of DES models is implied by its model description. Accordingly, it is not particularly difficult to implement a DES model from scratch in any programming language – object-oriented languages such as Python or Java are the preferred choice here. However, to ensure that the self-implemented simulation executive and the event list performs well and is generic, it is advisable to give this some thought (see later). Accordingly, it is usually a better option to use a fully developed DES simulator.

Case-Study: Collatz Sequence, DES vs. Time-Discrete - Implemented Model(s)

Nevertheless, for the Collatz case-study we want to develop the implemented model from scratch. On the one hand, this allows us to see in detail how a simulation executive works. On the other hand, it enables us to compare the implementation with a classic time-discrete implementation of the same system (intrepreted as a difference equation), to see the structural differences in the implementation.

Loading...
Loading...
Loading...

The graphs of the results show immediatly why this mathematical problem is so extraordinarily fascinating. The number of steps it takes for the sequence to drop to 1 seems to depend almost arbitrarily of the chosen initial value and is virtually impossible to predict. Although the number increases on average with to the size of the initial value, the volatility is astonishing. Note how the sequence starting in 27 surpasses values of 9000 before eventually dropping to the 1-4-2 cycle.

However, the key takeaway from the case study is, of course, the implementation as a DES model. One immediately recognises that a key distinguishing feature of the DES model is that the update loop does not run over the simulation time, but rather iterates through the event list. It is quite common to use some while loop here, as the number of events up to the simulation end-time is usually unpredictable.

Moreover, note how the interpolation method interpolation='steps-post' was used to make sure that the plots are displayed with a zero-hold and not by linear spline interpolation between the states. This is important for discrete event simulation, since the states are defined to remain constant between any two consecutive events.

Collatz Sequence, DES vs. Time-Discrete - Flexibility of DES

While the DES strategy clearly produces overheads in this particular case compared to the time-discrete strategy, it gives a lot of freedom for designing the update - not only in terms of how the update evaluates but also when it happens. For example, one quickly notices that the update 3x+13x+1 for an odd xx will always result in an even number and lead to consequent halving of the number. In other words:

xn+2=3xn+12x_{n+2}=\frac{3x_n+1}{2}

if xnx_n is odd. In case we only want to find the number of iterations until the sequence collapses, we may skip computing xn+1x_{n+1} in these cases. This feature is comparably tricky to include in a sole time-discrete approach, because it would be necessary to skip selected time-steps. However, as seen below, it is a trivial change in the event-based architecture.

Loading...
Loading...

While the model shows different dynamics due to skipping the xn+1=3xn+1x_{n+1}=3x_n+1 values, the number of steps for dropping to 1 are identical. The lower plots show the fascinated dynamics between the starting value x0x_0 and the time until the model state reaches 1 for the first time.

Although the Collatz sequence is not a typical example of when DES is used for modelling or simulation, even in this simple case-study one can already see the advantages of not only defining how an update takes place, but also when it should occur.

Stochasticity in DES

Stochasticity is one of the most important components in DES models. Although, as the Collatz example shows, it is possible to define DES models deterministically, the vast majority of DES models are stochastic. It should be noted here that stochasticity may relate not only to the updating of the state, but also, and above all, to the time intervals between updates, i.e. the scheduling times of the events.

Collatz Sequence, DES vs. Time-Discrete - Stochasticity

Altough the original Collatz problem is deterministic, there are indeed stochastic variants of it which we could embed in our case-study, for example, the one published by Ingo Althofer on his homepage where the update for odd numbers is either 3xn+13x_n+1 or 3xn−13x_n-1 with equal probability. However, this randomness relates purely to the update of the state and could be depicted with a time discrete model as well. In our version, however, we want to modify the times of the state-updates to incorporate stochasticity to better see the advantages of the approach.

Suppose, the investigated system xx does not update reliably every second, but updates my occur at any time with a certain constant likelihood. A system like this would then be modelled using a so-called Markov process with a certain rate - the higher the rate, the higher the probability that an update occurs within a given interval. Theory about these processes tells us, that the time between any two updates of a Markov proess is distributed exponentially Exp(λ)Exp(\lambda). Hereby the parameter λ\lambda of the exponential disribution is equal to the transition rate of the Markov process and the expected number of updates per time.

We use this information to modify our model. Instead of scheduling an update event every second instead schedule it with a time delay of X∼E(1)X\sim E(1) seconds.

Loading...

While the overall state updates are identical, the stochasticity of the schedule times causes temporarily streched and shifted curves. Moreover, every execution of the stochastic model leads different results due to the use of randomness. We also see the impact of the skewness of the exponential distribution, i.e. that short timespans are quite likely, but very long timespans are also possible.

Computational Effort of DES

Above case study also raises interesting questions related to the comparison of computational efforts.

In a time-discrete update, the computational costs of the approach always increase linearly with the simulation time tendt_{end} divided by the length hh of the used time-steps. In the Collatz example above, h=1h=1, however thinking about numerical simulation of differential equation models (e.g. using the Euler method) choosing a small stepsize hh is important for getting a proper approximation to the analytic solution. Summarising,

costs=O(tendh)costs=\mathcal{O}\left(\frac{t_{end}}{h}\right)

for implementation of time discrete(tised) models.

In DES approaches, we do not use any time-steps but iterate of the event-list. Let E(h)\mathbb{E}(h) refer to the average time-distance between any two consecutive events, then we get, again, a computational effort of O(tend/E(h))\mathcal{O}(t_{end}/\mathbb{E}(h)). However, in this computation we are neglecting a potentially critical contributer to computation time: efforts for magagement of the event list. On every update step, the simulation executive must (a) take and remove the next-scheduled event notice from the event list, (b) insort any new created event notices, and (c) search and modify/delete already scheduled event notices. The computational effort of theses processes varies with the used data structure, however, it can be generally stated that at least one of them has a computational effort which depends on the average length E(K)\mathbb{E}(K) of the event list. So, generally speaking,

costs=O(tend⋅f(E(K))E(h)),costs=\mathcal{O}\left(\frac{t_{end}\cdot f(\mathbb{E}(K))}{\mathbb{E}(h)}\right),

for some strict monotonically increasing function ff. The Collatz example was extreme in this sense, since K≡1K\equiv 1 all the time. However, in the more general case, event lists can become really long in models with lots of different event types, in particular, in agent-based models with event-based update. On the one hand, one should consider this when opting for or against an event-based update strategy compared to a discret(ised) one in case both options are viable. On the other hand, one should have a proper look at how event lists and the corresponding adding and removing of event notices is implemented in the corresponding software (i.e. what is “ff”).

We give some examples for the efforts of handling event lists with KK elements with different data structures / methods in Python:

data structure for event listexample Python object (alternatives)find and remove next event noticeinsert new event notice at the right placeoverall ff
list (sorted) or linked listlist (deque, np.ndarray)pop O(1)\mathcal{O}(1)append O(1)\mathcal{O}(1) + sort O(K)\mathcal{O}(K)O(K)\mathcal{O}(K)
list (unsorted)list (deque, np.ndarray)min O(K)\mathcal{O}(K) + remove O(K)\mathcal{O}(K)append O(1)\mathcal{O}(1)O(K)\mathcal{O}(K)
setsetfind O(K)\mathcal{O}(K) + remove O(1)\mathcal{O}(1)add O(1)\mathcal{O}(1)O(K)\mathcal{O}(K)
heapheapq (queue.PriorityQueue)heappop O(ln(K))\mathcal{O}(ln(K))heappush O(ln(K))\mathcal{O}(ln(K))O(ln(K))\mathcal{O}(ln(K))
binary treesortedcontainers.SortedListpop O(ln(K))\mathcal{O}(ln(K))add O(ln(K))\mathcal{O}(ln(K))O(ln(K))\mathcal{O}(ln(K))

However, in a Jupyter Notebook, we don’t necessarily have to rely solely on theoretical considerations and embed some actual benchmarks...

Loading...
Loading...
Loading...
Loading...
Loading...

The test displays times for adding and getting events from corresponding event lists, absolute and relative to the logarithm and the total size of the queue. The red column(s) in the tables have, according to the benchmark results, the highest likelihood to represent the order of the computational effort. The methods with tree or heap structure are clearly superior to the other approaches in terms of computational performance. In both cases, the computational effort increases, at most, with the logarithm of the queue size. For the list and set approaches, at least one of the two operations scales linearly with the queue length.

When it comes to searching and modification/removal of events, basically all options are equally good/bad, since O(N)\mathcal{O}(N) cannot be avoided. As a result, it is always a good idea to avoid these in the model design, when possible.

DES Simulators

As previously said, one should carefully consider how to implement a DES model in order to avoid performance bottlenecks. Note that due to the common stochasticity of DES models,a single simulation run is often of very limited informative value - so high computational performance is even more important to create mulitple runs (Monte Carlo simulation). Anyway, using an existing simulator is usually a good strategy.

In most cases, simulators are specifically tailored to the chosen simulation / model description language. For example, the SIGMA simulator supports the EventGraph formalism, PowerDevs supports the DEVS formalism, and AnyLogic supports a formalism based on process diagrams.

In Python, the simulator SimPy is likely the most frequently used. It is a general purpose simulation environment, which offers a multitude of methods for discrete event simulation without bein explicitly specificied for amy particular model description language.

Loading...

Usage of DES

Given the features discussed, DES is particularly advantageous for systems that change only at specific points in time, however it should be noted here that DES can only truly shine with its full potential if these points do not follow a predefined time cycle and/or include stochasticity. Otherwise, a time-discrete approach in which a time-loop steers the simulation is usually more suitable.

Queueing systems are undoubtedly the prime example of the application of DES. The state (the length of the queue) is discrete and changes only when an entity arrives or is served (see Figure 2). We refer to Discrete Event Simulation with Event Graphs for more queueing case examples.

Parade example of a system which is best modelled using DES: a queue with server.

Figure 2:Parade example of a system which is best modelled using DES: a queue with server.

While systems with discrete values are best suitet for DES it should be noted that also value-continuous systems can be modelled with this strategy, when interpreted in the right way. A good example for this is a bouncing ball (see Figure 3, panel [a]): the moments at which the ball touches the ground could be modelled as events, at which the ball’s velocity changes its sign and is damped. The time interval until the next impact (event) can then be derived quite easily from the laws of physics. In other words, DES is applicable when a continuous variable changes only in discrete steps (or when the discrete change is the essential element of the modelling). Another example where DES is applicable for value-continuous systems is given by a train (Figure 3, panel [b]) which, although it is generally always located at a continuous position, is reduced to the states: ‘at station A’, ‘between A and B’ and ‘at station B’. In other words, DES is applicable for value-continuous systems, when it is reasonable to discretise its value.

Examples for systems with continuous values which are still eligble for DES modelling: a bouncing ball modelled via bounce-events (lefT) and a train’s position simplified to “in station A/B” and “in between stations” modelled with departure and arrival events.

Figure 3:Examples for systems with continuous values which are still eligble for DES modelling: a bouncing ball modelled via bounce-events (lefT) and a train’s position simplified to “in station A/B” and “in between stations” modelled with departure and arrival events.

References
  1. Lackner, M. R. (1964). Digital Simulation and System Theory [Techreport]. System Development Corporation, SDC SP-1612.
  2. Banks, J. (2005). Discrete event system simulation. Pearson Education India.
  3. Nance, R. E. (1996). A history of discrete event simulation programming languages. In History of Programming Languages—II (pp. 369–427). Association for Computing Machinery. 10.1145/234286.1057822