What is simulation model?

It’s the process of creating and analysing a digital prototype of a physical model to predict its performance in the real world.

Wikipedia

Let’s start!

We consider that the customer goes to bank. Then he sees 4 tellers serving 4 queues, each teller to each queue.

Fig 1: Tellers serving customers

Another property of the customer is to choose the shortest queue. So, the customer will stand there.

Fig 2: Customers getting into the shortest queue

If the queues are longer, people have to wait for more time. The transaction can also take much more time, keeping the people waiting.

Types of Simulation Models

There are two types of simulation model.

  1. Time-based simulation
  2. Event-based simulation

Time-based Simulation

“In time based simulation, we maintain a timeline or a clock. The clock ticks and things happen.”

Fig 3: Time based Simulation

C1 enters in, after the 2 minutes when bank opens C2 enters at 4 min after the opening of the bank. So, consider the next customers same as this behaviour of C1 and C2.

Pseudocode

The clock will run for 24 hours. Banks are not open for 24 hours but we will run our loop for 24 hrs. The pseudo code is as under:

clock = 0;
while ( clock <= 24*60 ) { // one day
read new customer;

if customer.arrivaltime == clock
insert into shortest queue;

check the customer at head of all four queues.

if transaction is over
remove from queue.

clock = clock + 1;

The main idea behind discussing these things is that we manage this arrival and leave of the customer through queue. In other words, we keep the track about who and when, came and left.

Event-based Simulation

In event based simulation, we manage events. Means, when a specific event occurs then things happen.

Fig 4: Event based simulation

Priority Queue

A queue where dequeue() operation doesn’t depends upon FIFO. Means, things happen depending upon their priority, not on FIFO concept. This is called priority queue.

You can find the use of priority queue is managing traffic lights. Easy!

Implementation of Priority Queue

#include "Event.cpp"
#define PQMAX 30
class PriorityQueue
{
	public:
	PriorityQueue() {
		size = 0; rear = -1;
	};
	~PriorityQueue() {};
	int full(void){
		return ( size == PQMAX ) ? 1 : 0;
	};

	Event* remove() {
		if( size > 0 ) {
			Event* e = nodes[0];
			for(int j=0; j < size-2; j++ )
			nodes[j] = nodes[j+1];
			size = size-1; rear=rear-1;
			if( size == 0 ) rear = -1;
			return e;
		}
		return (Event*)NULL;
		cout << "remove - queue is empty." << endl;
	};
	int insert(Event* e) {
		if( !full() ) {
			rear = rear+1;
			nodes[rear] = e;
			size = size + 1;
			sortElements(); // in ascending order
			return 1;
		}
	cout << "insert queue is full." << endl;
	return 0;
	};

	int length() { return size; };
};

REFERENCE: CS301 Handouts (page 94 to 107)

Categorized in:

Data Structures, Learning,

Last Update: March 22, 2024