If we generalize a finite state automaton by letting the time between two events be stochastic, and possibly also letting the choice of the next state be stochastic, we obtain a model for event based stochastic processes. Such models have many applications, of which one is queuing systems.

As a simple example of discrete-time stochastic process simulation we can consider a simple server model with a queue.

The CustomerGenerator model contains an output connector outCustomer that transmits the customer arrival events to the rest of the system. We first start by defining a model which generates customers at random points in time. The time delay until the next customer arrival is assumed to be a normally distributed stochastic variable T, here computed by calling the random number function normalvariate, see above, where the negative part is folded back on the positive side using abs(T) to avoid unrealistic negative service times. Since this function is side-effect free as all Modelica functions, it returns a randomSeed value to be used at the next call to normalvariate, see the help functions above.

We simulate the CustomerGeneration model for a while to observe the customer arrival times.

We can now design a server model ServerWithQueue which through its inCustomer port will accept the arriving customers from the CustomerGeneration model. The nrOfCustomersInQueue represents the current number of customers in the queue. Other state variables of interest are nrOfArrivedCustomers, the number of customers that have so far arrived and nrOfServedCustomers, the number of customers that have so far been served. The first when-equation services the arrival of a new customer, whereas the second when-equation handles the event when a customer is serviced and is ready to leave the queue.

The event when readyCustomer becomes true represents completed service for the current customer. The time needed to serve a customer is custServeTime, which is constant in this model but could be varying according to some formula or be randomly distributed. The variable lastServeStart represents the starting time for servicing the most recent serviced customer.

The simulation of the whole system can be done by defining a test model which connects the CustomerGeneration model with the ServerWithQueue model.

Using the MathModelica simulation environment we simulate the model for 10 seconds with the customer service time custServeTime = 0.4 seconds.

Number of arrived customers, number of served customers, and number of customers waiting in the queue or being serviced. Customer service time is 0.4 seconds.

We now simulate the model for 10 seconds with the customer service time custServeTime = 1 second.

Number of arrived customers, number of served customers, and number of customers waiting in the queue or being serviced. Customer service time is 1 second.