Main Content

Manage Entities Using Event Actions

This example shows how to control entity generation rate and write event actions to change entity attributes in a simple queuing system. In a discrete-event simulation, an event is an observation of an instantaneous incident that may change a state variable, an output, or the occurrence of other events. SimEvents® allows you to create custom actions when an event occurs. These actions are called event actions. Events can have corresponding actions. You can write event actions to change entity attributes by using MATLAB® code or Simulink® functions.

Start with a Simple Queuing System

This is a simple queueing system with Entity Generator, Entity Queue, Entity Server, and Entity Terminator blocks. In this example, an entity represents a tank of a truck that arrives at a gas station. The attribute of an entity represents the current gas level in a tank. Event actions represent the changes of the gas level in a tank. Tanks are randomly generated, queued, and they are serviced with a pump which transfers a constant amount of gas for one second. Tanks depart from the station with their new total gas amount.

Modify the Model

  1. Select the whole model or the entity paths originating from the Entity Generator, Entity Queue, and Entity Server blocks and right-click to select Log Selected Signals. Simulation Data Inspector is used to visualize the flow of tanks and their gas level in the model. For more information, see Inspect Simulation Data.

  2. Rename the Entity Generator block as Tank Generator, the Entity Queue block as Waiting Queue, the Entity Server block as Pump, and the Entity Terminator block as Exit.

    The blocks are renamed for this particular application

  3. Rename the path originating from the Tank Generator block as Tank to Queue, the Waiting Queue block as Tank to Pump, and the Pump block as Tank to Exit.

Configure and Simulate Model

  1. Double-click the Tank Generator, and select the Entity type tab. Change the Entity type name to Tank, and the Attribute Name to CurrentGasLevel.

    The entity attribute CurrentGasLevel represents the existing amount of gas in each tank.

  2. Simulate the model. Open the Simulation Data Inspector. Observe that the tanks approach the Waiting Queue, the Pump, and the Exit with the same rate.

    Tanks leave the station with their initial gas amount 1 which is the Attribute Initial Value.

    Simulink Data inspector showing that the CurrentGasLevel value is 1 for each entity that arrives at the queue

    Simulink Data inspector showing that the CurrentGasLevel value is 1 for each entity that arrives at the pump

    Simulink Data inspector showing that the CurrentGasLevel value is 1 for each entity that arrives at the exit

  3. Open the Tank Generator block parameters dialog box. In the Entity generation tab, set Time source to Matlab action. Observe the default MATLAB code.

    dt = rand(1,1);

    The code randomizes the entity intergeneration time parameter dt to represent random tank arrivals.

  4. Simulate the updated model. In the Simulation Data Inspector, observe that tanks arrive randomly with the same initial gas amount 1.

    Simulink Data inspector showing that the entities are generated randomly

    Observe that the tanks are generated randomly but they approach the pump with a regulated fixed rate because service time for the Pump is 1.

    Simulink Data inspector showing that the entity departures from the queue are regulated to a constant value of 1

  5. Open the Tank Generator block dialog box. In the Event actions tab, in the Generate action field, enter the code.

    entity.CurrentGasLevel = randi([1,4]);

    Tanks arrive at the station with a random gas amount that ranges from 1 to 4.

  6. Simulate the updated model. In the Simulation Data Inspector, observe that the tanks arrive with random amounts of gas.

    Simulink Data inspector showing that the entities are generated with random initial gas values

  7. For the Pump block, set these parameters:

    1. In the Event actions tab, select Service complete.

    2. For the Service complete action field, enter the code.

      entity.CurrentGasLevel = entity.CurrentGasLevel + 3;

      Each tank is filled with 3 units of gas for 1s duration, and then it departs the pump.

    Observe that the Tank Generator and the Pump blocks update with the event action icon {...} indicating that the blocks define an event action.

    Model with the event action icon on the Tank Generator and Pump blocks

  8. Simulate the updated model. In the Simulation Data Inspector, observe that each tank leaves the station with 3 additional units of gas.

    Simulink Data inspector showing that the entities depart the pump with added gas

Modified Model to Manage Entities in a Queueing System

This is the modified model after configuring the simple queueing system.

See Also

| | |

Related Topics