Skip to Main Content Skip to Search
Product Documentation

Example: Choosing the Shortest Queue

The model below directs entities to the shortest of three queues. It uses an Output Switch block to create the paths to the different queues. To implement the choice of the shortest queue, a discrete event subsystem queries each queue for its current length, determines which queue or queues achieve the minimum length, and provides that information to the Output Switch block. To ensure that the information is up to date when the Output Switch block attempts to output the arriving entity, the block uses the Store entity before switching option; for details, see Using the Storage Option to Prevent Latency Problems.

For simplicity, the model omits any further processing of the entities after they leave their respective queues.

Although the block diagram shows signals at the #n signal output ports from the queue blocks and another signal at the p signal input port of the Output Switch block, the block diagram does not indicate how to compute p from the set of #n values. That computation is performed inside a discrete event subsystem that contains the MATLAB Function block.

Subsystem Contents

If you double-click the MATLAB Function block in a model, an editor window shows the MATLAB function that specifies the block. In this example, the following MATLAB function computes the index of a queue having the shortest length, where the individual queue lengths are n1, n2, and n3. If more than one queue achieves the minimum, then the computation returns the smallest index among the queues that minimize the length.

function p = findmin(n1, n2, n3)

% p is the index of a queue having the shortest length.
[~, p] = min([n1 n2 n3]);

The figure below shows a sample plot. Each stem corresponds to an entity departing from the switch block via one of the three entity output ports.

  


Free Discrete Event Simulation Technical Kit

Model electronic system architectures, process flows, and logistics as queuing systems or agent-based systems.

Get free kit

Trials Available

Try the latest version of discrete-event simulation products.

Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS