Products & Services Solutions Academia Support User Community Company

Learn more about SimEvents   

Building a Simple Discrete-Event Model

Overview of the Example

This section describes how to build a new model representing a discrete-event system. The system is a simple queuing system in which "customers" — entities — arrive at a fixed deterministic rate, wait in a queue, and advance to a server that operates at a fixed deterministic rate. This type of system is known as a D/D/1 queuing system in queuing notation. The notation indicates a deterministic arrival rate, a deterministic service rate, and a single server.

Using the example model, this section shows you how to:

To skip the model-building steps and open a completed version of the example model, enter simeventsdocex('doc_dd1') in the MATLAB Command Window. Save the model in your working folder as dd1.mdl.

Opening a Model and Libraries

The first steps in building a model are to set up your environment, open a new model window, and open the libraries containing blocks.

Setting Default Parameters for Discrete-Event Simulation

To change the default Simulink model settings to values that are appropriate for discrete-event simulation modeling, enter this in the MATLAB Command Window:

simeventsstartup('des');

A message indicates that default simulation settings have changed. The changed settings apply to new models that you create later in this MATLAB software session, but not to previously created models.

Opening a New Model Window

Select File > New > Model from the menu in the MATLAB desktop window. This opens an empty model window.

New model window

To name the model and save it as a file, select File > Save from the model window's menu. Save the model in your working folder under the file name dd1.mdl.

Opening SimEvents Libraries

In the MATLAB Command Window, enter

simeventslib

Alternatively, click the Start button in the lower-left corner of the MATLAB desktop. In the menu that appears, select Simulink > SimEvents > Block Library.

The main SimEvents library window appears. This window contains an icon for each SimEvents library. To open a library and view the blocks it contains, double-click the icon that represents that library.

Main SimEvents library window

Opening Simulink Libraries

In the MATLAB Command Window, enter

simulink

The Simulink Library Browser opens, using a tree structure to display the available libraries and blocks. To view the blocks in a library listed in the left pane, select the library name, and the list of blocks appears in the right pane. The Library Browser provides access not only to Simulink blocks but also to SimEvents blocks. For details about the Library Browser, see Simulink Library Browser in the Simulink documentation.

Moving Blocks into the Model Window

To move blocks from libraries into the model window, follow these steps:

  1. In the main SimEvents library window, double-click the Generators icon to open the Generators library. Then double-click the Entity Generators icon to open the Entity Generators sublibrary.

    Entity Generators sublibrary window

  2. Drag the Time-Based Entity Generator block from the library into the model window.

    This might cause an informational dialog box to open, with a brief description of the difference between entities and events.

    Time-Based Entity Generator block in the model window

  3. In the main SimEvents library window, double-click the Queues icon to open the Queues library.

    Queues library window

  4. Drag the FIFO Queue block from the library into the model window.

  5. In the main SimEvents library window, double-click the Servers icon to open the Servers library.

    Servers library window

  6. Drag the Single Server block from the library into the model window.

  7. In the main SimEvents library window, double-click the SimEvents Sinks icon to open the SimEvents Sinks library.

    SimEvents Sinks library window

  8. Drag the Signal Scope block and the Entity Sink block from the library into the model window.

As a result, the model window looks like the following figure. The model window contains blocks that represent the key processes in the simulation: blocks that generate entities, store entities in a queue, serve entities, and create a plot showing relevant data.

Model window containing all the blocks for the example

Configuring Blocks

Configuring the blocks in dd1 means setting their parameters appropriately to represent the system being modeled. Each block has a dialog box that enables you to specify parameters for the block. Default parameter values might or might not be appropriate, depending on what you are modeling.

Two important parameters in this D/D/1 queuing system are the arrival rate and service rate. The reciprocals of these rates are the duration between successive entities and the duration of service for each entity. To examine these durations, do the following:

  1. Double-click the Time-Based Entity Generator block to open its dialog box. Observe that the Distribution parameter is set to Constant and that the Period parameter is set to 1. This means that the block generates a new entity every second.

  2. Double-click the Single Server block to open its dialog box. Observe that the Service time parameter is set to 1. This means that the server spends one second processing each entity that arrives at the block.

  3. Click Cancel in both dialog boxes to dismiss them without changing any parameters.

The Period and Service time parameters have the same value, which means that the server completes an entity's service at exactly the same time that a new entity is being created. The Event Priorities demo discusses this simultaneity in more detail.

For now, configure blocks to create a plot that shows when each entity departs from the server, and to make the queue have an infinite capacity. Do this as follows:

  1. Double-click the Single Server block to open its dialog box.

  2. Click the Statistics tab to view parameters related to the statistical reporting of the block.

  3. Set the Number of entities departed parameter to On.

    Then click OK. The Single Server block acquires a signal output port labeled #d. During the simulation, the block will produce an output signal at this #d port; the signal's value is the running count of entities that have completed their service and departed from the server.

  4. Double-click the FIFO Queue block to open its dialog box.

  5. Set the Capacity parameter to Inf and click OK.

Connecting Blocks

Now that the model window for dd1 contains blocks that represent the key processes, connect the blocks to indicate relationships among them as shown. To connect blocks with the mouse, drag from the output port of one block to the input port of another block.

Model window after blocks have been connected

Running the Simulation

Save the dd1 model you have created. Then start the simulation by choosing Simulation > Start from the model window's menu.

Suppressing Solver Warnings

If you skipped Setting Default Parameters for Discrete-Event Simulation, then you might see warning messages in the MATLAB Command Window about continuous states and the maximum step size. These messages appear because certain default parameters for a Simulink model are inappropriate for this particular example model. The application overrides the inappropriate parameters and alerts you to that fact.

One way to suppress the warning messages when you run this simulation in the future is to enter this command in the MATLAB Command Window:

simeventsconfig('dd1','des');

A message indicates that the simulation settings for this particular model have changed.

Results of the Simulation

When the simulation runs, the Signal Scope block opens a window containing a plot. The horizontal axis represents the times at which entities depart from the server, while the vertical axis represents the total number of entities that have departed from the server.

After an entity departs from the Single Server block, the block updates its output signal at the #d port. The updated values are reflected in the plot and highlighted with plotting markers. From the plot, you can make these observations:

Exploring the Simulation Using the Debugger

The plot in the previous section indicates how many entities have departed from the server, but does not address the following question: Is any entity still in the server at the conclusion of the simulation? To answer the question, you can use the SimEvents debugger, as described in this section. Using the debugger involves running the simulation in a special debugging mode that lets you suspend a simulation at each step or breakpoint and query simulation behavior. Using the debugger does not require you to change the model. The topics in this section are as follows:

To open a completed version of the example model for this tutorial, enter simeventsdocex('doc_dd1') in the MATLAB Command Window. Save the model in your working folder as dd1.mdl.

Starting the Debugger

To start running the dd1 simulation in debugging mode, enter this command at the MATLAB command prompt:

sedebug('dd1')

The output in the MATLAB Command Window indicates that the debugger is active and includes hyperlinks to information sources.

*** SimEvents Debugger ***

Functions | Quick Start | Debugger Help

%==============================================================================%
Initializing Model dd1
sedebug>> 

The sedebug>> notation is the debugger prompt, where you enter commands.

Running the Simulation

The simulation has initialized but does not proceed. In debugging mode, you indicate to the debugger when to proceed through the simulation and how far to proceed before returning control to you. The purpose of this example is to find out whether an entity is in the server when the simulation ends. To continue the simulation until it ends, enter this command at the sedebug>> prompt:

cont

The Command Window displays a long series of messages that indicate what is happening during the simulation. The end of the output indicates that the debugger has suspended the simulation just before the end:

Hit built-in breakpoint for the end of simulation.
Use 'cont' to end the simulation or any other function to inspect final states of the system.

%==============================================================================%
Terminating Model dd1

To understand the long series of messages, see The Debugger Environment or the debugger resources listed in For Further Information.

Querying the Server Block

The debugger has suspended the simulation just before the end and the sedebug>> prompt indicates that you can still enter debugging commands. In this way, you have an opportunity to inspect the final states of blocks or other aspects of the simulation. To get information about the Single Server block, enter this command:

blkinfo('dd1/Single Server')

The output shows the state of the Single Server block at the current time, T=10. The last two rows of the output represent a table that lists entities in the block. The table has one row because the server is currently storing one entity. The entity has a unique identifier, en11, and is currently in service. This output affirmatively answers the question of whether an entity is in the server when the simulation ends.

Ending the Simulation

The simulation is still suspended just before the end. To proceed, enter this command:

cont

The simulation ends, the debugging session ends, and the MATLAB command prompt returns.

For Further Information

For additional information about the SimEvents debugger, see these resources.

TopicDescription
Building a Simple Hybrid Model, specifically the section, Confirming Event-Based Behavior Using the SimEvents DebuggerAn example that illustrates single-stepping, which is another important debugger feature
Video tutorialA video example that introduces a variety of debugger features
Debugger function listList of functions related to debugging
Overview of the SimEvents DebuggerOverview of debugging and links to other debugging topics

Creating Additional Plots

The dd1 model that you created earlier in this tutorial plots the number of entities that depart from the server. This section modifies the model to plot other quantities that can reveal aspects of the simulation. The topics are as follows:

To open a completed version of the example model described earlier in this tutorial, enter simeventsdocex('doc_dd1') in the MATLAB Command Window. Before modifying the model as described below, consider saving it with a different file name.

Enabling the Pending-Entity Signal

When at least one entity is in the FIFO Queue block but is unable to depart and advance to the subsequent block, the FIFO Queue block's entity output port (labeled OUT) is said to be blocked and the entity at the head of the queue is said to be a pending entity. The FIFO Queue block can report whether it has a pending entity using a signal whose value is 1 if an entity is pending and 0 if no entity is pending or a previously pending entity has just departed. Note that a value of 0 could mean either that the queue is empty or that an entity trying to depart is successful. To configure the FIFO Queue block to report pending-entity status, do the following:

  1. Double-click the FIFO Queue block to open its dialog box. Click the Statistics tab to view parameters related to the statistical reporting of the block.

  2. Set the Status of pending entity departure parameter to On and click OK. This causes the block to have a signal output port for the pending-entity signal. The port label is pe.

Plotting the Pending-Entity Signal

The model already contains a Signal Scope block for plotting the entity count signal. To add another Signal Scope block for plotting the pending-entity signal (enabled above), follow these steps:

  1. In the main SimEvents library window, double-click the SimEvents Sinks icon to open the SimEvents Sinks library.

  2. Drag the Signal Scope block from the library into the model window. The block automatically assumes a unique block name, Signal Scope1, to avoid a conflict with the existing Signal Scope block in the model.

  3. Connect the pe signal output port of the FIFO Queue block to the in signal input port of the Signal Scope1 block by dragging the mouse pointer from one port to the other. The model now looks like the following figure.

Simulating with Different Intergeneration Times

By changing the intergeneration time (that is, the reciprocal of the entity arrival rate) in the Time-Based Entity Generator block, you can see how the queue's entity output port becomes blocked and unblocked. Try this procedure:

  1. Double-click the Time-Based Entity Generator block to open its dialog box, set the Period parameter to 0.85, and click OK. This causes entities to arrive somewhat faster than the Single Server block can process them. As a result, the queue is not always empty.

  2. Save and run the simulation. The plot whose title bar is labeled Signal Scope1 represents the pending-entity signal, where a value of 1 means true and a value of 0 means false. The figure below explains some of the points on the plot. The vertical range on the plot has been modified to fit the data better.

  3. Reopen the Time-Based Entity Generator block's dialog box and set Period to 0.3.

  4. Run the simulation again. Now the entities arrive much faster than the server can process them. The pending-entity signal is true nearly all the time because the queue nearly always has at least one entity that is trying and failing to advance to the server. However, the pending-entity signal is false at the beginning of the simulation. The pending-entity signal is also false at time instants when an entity in the queue is able to advance to the server; these values of false change back to true when the queue determines that it still has an entity that cannot depart.

  5. Reopen the Time-Based Entity Generator block's dialog box and set Period to 1.1.

  6. Run the simulation again. Now the entities arrive slower than the server's service rate, so entities never stay in the queue for a positive amount of time. The pending-entity signal is always false because every entity that arrives at the queue is able to depart immediately.

Viewing Waiting Times and Utilization

The pending-entity signal shown above is an example of a statistic that quantifies a state at a particular instant. Other statistics, such as average waiting time and server utilization, summarize behavior between T=0 and the current time. To modify the model so that you can view entities' average waiting time in the queue and server, as well as the proportion of time that the server spends storing an entity, use the following procedure:

  1. Double-click the FIFO Queue block to open its dialog box. Click the Statistics tab, set the Average wait parameter to On, and click OK. This causes the block to have a signal output port for the signal representing the average duration that entities wait in the queue. The port label is w.

  2. Double-click the Single Server block to open its dialog box. Click the Statistics tab, set both the Average wait and Utilization parameters to On, and click OK. This causes the block to have a signal output port labeled w for the signal representing the average duration that entities wait in the server, and a signal output port labeled util for the signal representing the proportion of time that the server spends storing an entity.

  3. Copy the Signal Scope1 block and paste it into the model window.

      Note   If you modified the plot corresponding to the Signal Scope1 block, then one or more parameters in its dialog box might be different from the default values. Copying a block also copies parameter values.

  4. Double-click the new copy to open its dialog box.

  5. Set Plot type to Continuous and click OK. For summary statistics like average waiting time and utilization, a continuous-style plot is more appropriate than a stairstep plot. Note that the Continuous option refers to the appearance of the plot and does not change the signal itself to make it continuous-time.

  6. Copy the Signal Scope2 block that you just modified and paste it into the model window twice. You now have five scope blocks.

    Each copy assumes a unique name. If you want to make the model and plots easier to read, you can click the names underneath each scope block and rename the block to use a descriptive name like Queue Waiting Time, for example.

  7. Connect the util signal output port and the two w signal output ports to the in signal input ports of the unconnected scope blocks by dragging the mouse pointer from port to port. The model now looks like the following figure. Save the model.

  8. Run the simulation with different values of the Period parameter in the Time-Based Entity Generator block, as described in Simulating with Different Intergeneration Times. Look at the plots to see how they change if you set the intergeneration time to 0.3 or 1.1, for example.

Observations from Plots

Information About Race Conditions and Random Times

Other examples modify this one by varying the processing sequence for simultaneous events or by making the intergeneration times and/or service times random. The modified examples are:

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS