Airport Problem Simulation Problem Statement: We have to simulate the average waiting time for an aeroplane to land or takeoff from an airport. Here we assume that it is a Single Server System i.e. the airport has only one runway shared for both landing and takeoff. We also assume that upto three aeroplanes can wait for landing or takeoff at an instance.
Entities: The following are the entities of the given system
Airport Runway
Planes
Events: The following are the events that can occur in the given system
Takeoff
Landing
System States: The states that can be changed by the given events are
Number of Aeroplanes in the Landing queue
Number of Aeroplanes in the Takeoff queue
Status of the Runway (Busy/Idle)
State Variables: The following are the state variables for the given system
nplanes (Number of aeroplanes)
ntakeoff (Number of aeroplanes took off)
nland (Number of aeroplanes landed)
idle_time (Time for which the runway remains idle)
Start
Initialize Routine(initialize airport variables)
Current time <
Start Simulation Clock
simulation end time
Define next event Call timing routine
Event occur ( fly or land)
Update the statistic counter Update system state Generate next event time End
Flowcharts for Airplane Take-off and Landing Events:
Take-off Event- When the airplane arrives, check the runway status. If the runway status is idle, then update upd ate the runway status to busy. Otherwise, that airplane waits in the queue, so the number of airplane in the queue increases one and store the arrival time to the list of arrival time.
Start
Runway idle?
Set Runway to busy
Is take-off queue full?
Increase number of delay by 1
Add number in queue by 1 Add arrival time to last arrival time
Get next take-off time
Next take-off time is infinite?
Get next take-off time
Stop
Stop Simulation
Landing Event- When the airplane arrives, check the runway status. If the runway status is idle, then update upd ate the runway status to busy. Otherwise, that airplane waits in the queue, so the number of airplane in the queue increases one and store the arrival time to the list of arrival time.
Start
Runway idle?
Set Runway to busy
Is take-off queue full?
Increase number of delay by 1
Add number in queue by 1 Add arrival time to last arrival time
Get next take-off time
Next take-off time is infinite?
Get next take-off time
Stop Stop Simulation
Flowcharts for various Routines:
Timing routine- It defines the simulation clock to the next event time.
Event occur routine- Call the timing routine to move the clock to the event. Update the area under Q(t), B(t), and time of last event. If the next is arrival, call function take-off(), else call function landing().
Event Occur Routine
Call timing routine
Set Q(t) = Q(t)+(num in queue)*(clock-last event)
Is runway busy?
Set B(t) = B(t)+(clock -last event)
Set last event = clock
Event type = 0?
Call Take-off routine
Call Landing Routine Stop
Generate next arrival time for airplane take-off routine- This routine is to generate next arrival ar rival time randomly.
Generate next arrival time for airplane landing routine- This routine is to generate next departure time randomly.
Report routine- The report shows about time of simulation end, mean of airplanes in queue, mean of delay time, and the percentage of runway utilisation.
Define next event routine- Define whether next event is take-off or landing. To get the next event, we just look to the next arrival time and next departure time for the airplane. If arrival time is infinite, it means that the plane is departing, hence it is a Take-off event and the opposite occurs for Landing Event.
C Program for the given Problem:
#include #include #include #include #include #include #define MAX 3 #define ARRIVE 0 #define DEPART 1
struct plane { int id ; int tm ; };
struct queue { int count ; int front ; int rear ;
struct plane p[MAX] ; };
void initqueue ( struct queue * ) ; void addqueue ( struct queue *, struct plane ) ; plane delqueue ( struct queue * ) ; int size ( struct queue ) ; int empty ( struct queue ) ; int full ( struct queue ) ;
void initqueue ( struct queue *pq ) { pq -> count = 0 ; pq -> front = 0 ; pq -> rear = -1 ; }
void addqueue ( queue *pq, plane item ) { if ( pq -> count >= MAX ) { printf ( "\nQueue is full.\n" ) ; return ;
} ( pq -> count )++ ;
pq -> rear = ( pq -> rear + 1 ) % MAX ; pq -> p[pq -> rear] = item ; }
plane delqueue ( queue *pq ) { struct plane p1 ;
if ( pq -> count <= 0 ) { printf ( "\nQueue is empty.\n" ) ; p1.id = 0 ; p1.tm = 0 ; } else { ( pq -> count )-- ; p1 = pq -> p[pq -> front] ; pq -> front = ( pq -> front + 1 ) % MAX MAX ; } return p1 ;
}
int size ( queue q ) { return q.count ; }
int empty ( queue q ) { return ( q.count <= 0 ) ; }
int full ( queue q ) { return ( q.count >= MAX ) ; }
struct airport { struct queue landing ; struct queue takeoff ; struct queue *pl ; struct queue *pt ;
int idletime ; int landwait, takeoffwait ; // mean calculations int nvarianceland , nvariancetakeoff; // variance calculations int nland, nplanes, nrefuse, ntakeoff ; struct plane pln ; };
void initairport ( airport * ) ; void start ( int *, double *, double * ) ; void newplane ( airport *, int, int ) ; void refuse ( airport *, int ) ; void land ( airport *, struct plane, int ) ; void fly ( airport *, struct plane, int ) ; void idle ( airport *, int ) ; void conclude ( airport *, int ) ; int randomnumber ( double ) ; void apaddqueue ( airport *, char ) ; plane apdelqueue ( airport *, char ) ; int apsize ( airport, char ) ; int apfull ( airport, char ) ; int apempty ( airport, char ) ; void myrandomize ( ) ;
void initairport ( airport *ap ) { initqueue ( &( ap-> landing ) ) ; initqueue ( &( ap -> takeoff ) ) ;
ap -> pl = &( ap -> landing ) ; ap -> pt = &( ap -> takeoff ) ; ap -> nplanes = ap -> nland = ap -> ntakeoff = ap -> nrefuse = 0 ; ap -> landwait = ap -> takeoffwait = ap -> idletime = 0 ; }
void start ( int *endtime, double *expectarrive, double *expectdepart ) { int flag = 0 ; int wish ;
printf ( "\nProgram that simulates an an airport with only one runway.\n" runway.\n" ) ; printf ( "One plane can land or depart in each unit of time.\n" ) ; printf ( "Up to %d planes can be waiting waiting to land or take off at any time.\n", time.\n", MAX ) ; printf ( "How many units of time time will the simulation simulation run?" ) ; scanf ( "%d", endtime ) ; myrandomize( ) ; do {
printf ( "\nExpected number of arrivals per unit time? " ) ; scanf ( "%lf", expectarrive ) ; printf ( "\nExpected number of departures per unit time? time? " ) ; scanf ( "%lf", expectdepart ) ;
if ( *expectarrive < 0.0 || *expectdepart < 0.0 ) { printf ( "These numbers must be nonnegative.\n" ) ; flag = 0 ; } else { if ( *expectarrive + *expectdepart > 1.0 ) { printf ( "The airport will become become saturated. Read new numbers?(1-yes 2-no " ) ;
scanf ( "%d", &wish );
if ( wish == == 1 ) flag = 0 ; else { if( wish wish == 0 ) flag = 1 ;
} } else flag = 1 ; } } while ( flag == 0 ) ; }
void newplane ( airport *ap, int curtime, int action ) { ( ap -> nplanes )++ ; ap -> pln.id = ap -> nplanes ; ap -> pln.tm = curtime ;
switch ( action ) { case ARRIVE : printf ( "\n" ) ; printf ( "Plane %d ready to land.\n", ap -> nplanes ) ; break ;
case DEPART : printf ( "\nPlane %d ready to take off.\n", ap -> nplanes ) ; break ;
} }
void refuse ( airport *ap, int action ) { switch ( action ) { case ARRIVE :
printf ( "\tplane %d directed to another airport.\n", ap -> pln.id pln.id ) ; break ;
case DEPART :
printf ( "\tplane %d told to try later.\n", ap -> pln.id ) ; break ; } ( ap -> nrefuse )++ ; }
void land ( airport *ap, plane pl, int curtime ) { int wait ;
wait = curtime - pl.tm ; printf ( "%d: Plane %d landed landed ", curtime, pl.id ) ; printf ( "in queue %d units \n", wait wait ) ; ( ap -> nland ) ++ ; ( ap -> landwait ) += wait ; ( ap -> nvarianceland ) += pow(wait,2); }
void fly ( airport *ap, plane pl, int curtime ) { int wait ;
wait = curtime - pl.tm ; printf ( "%d: Plane %d took off ", curtime, pl.id ) ; printf ( "in queue %d units \n", wait wait ) ; ( ap -> ntakeoff )++ ; ( ap -> takeoffwait ) += wait ; ( ap -> nvariancetakeoff ) += pow(wait,2);
}
void idle ( struct airport *ap, int curtime ) {
printf ( "%d: Runway is idle.\n", curtime ) ; ap -> idletime++ ; }
void conclude ( struct airport *ap, int endtime ) { double x = ( ( double ) ap -> landwait / ap -> nland ); double x2 = ( (double)ap -> nvarianceland / ap -> nland ); double variance_land = x2 - pow(x , 2); double xt = ( ( double ) ap -> takeoffwait / ap -> ntakeoff); double x2t =( (double ) ap -> nvariancetakeoff / ap -> ntakeoff); double variance_takeoff = x2t - pow(xt , 2);
printf ( "\tSimulation has concluded after after %d units.\n", endtime ) ; printf ( "\tTotal number of planes processed: %d\n", ap -> nplanes ) ; printf ( "\tNumber of planes landed: %d\n", %d\n", ap -> nland ) ; printf ( "\tNumber of planes taken off: %d\n", ap -> ntakeoff ) ; printf ( "\tNumber of planes refused use: %d\n", %d\n", ap -> nrefuse ) ; printf ( "\tNumber left ready to land: land: %d\n", apsize ( *ap, 'l' ) ) ; printf ( "\tNumber left ready to take take off: %d\n", apsize ( *ap, 't' ) ) ;
printf("\n **** REPORT **** \n");
if ( endtime > 0 ) printf ( "\tPercentage of time runway idle: %lf %lf \n", ( ( double ) ap -> idletime / endtime ) * 100.0 ) ;
if ( ap -> nland > 0 ) { printf ( "\tAverage wait time to land: %lf \n", x ) ; printf("\t Vairance wait time time to land: %lf \n",variance_land); }
if ( ap -> ntakeoff > 0 ) { printf ( "\tAverage wait time to take off: %lf \n", xt ) ; printf("\t Vairance wait time time to take off: %lf \n",variance_takeoff); }
}
int randomnumber ( double expectedvalue ) { int n = 0 ; double em ; double x ;
em = exp ( -expectedvalue ) ; x = rand( ) / ( double ) INT_MAX ;
while ( x > em ) {
n++ ; x *= rand( ) / ( double ) INT_MAX ; }
return n ; }
void apaddqueue ( struct airport *ap, char type ) { switch ( tolower( type ) ) { case'l' : addqueue ( ap -> pl, ap -> pln ) ; break ;
case't' : addqueue ( ap -> pt, ap -> pln ) ; break ; } }
plane apdelqueue ( airport *ap, char type ) { struct plane p1 ;
switch ( tolower ( type ) ) { case'l' : p1 = delqueue ( ap -> pl ) ; break ;
case't' : p1 = delqueue ( ap -> pt ) ; break ; }
return p1 ; }
int apsize ( airport ap, char type ) { switch ( tolower ( type ) ) { case'l' : return ( size ( *( ap.pl ) ) ) ;
case't' : return ( size ( *( ap.pt ) ) ) ;
}
return 0 ; }
int apfull ( airport ap, char type ) { switch ( tolower ( type ) ) { case'l' : return ( full ( *( ap.pl ) ) ) ;
case't' : return ( full ( *( ap.pt ) ) ) ; }
return 0 ; }
int apempty ( airport ap, char type ) { switch ( tolower ( type ) ) { case'l' :
return ( empty ( *( ap.pl ) ) ) ;
case't' : return ( empty ( *( ap.pt ) ) ) ; }
return 0 ; }
void myrandomize( ) { srand ( ( unsigned int ) ( time ( NULL ) % 10000 ) ) ; }
int main( ) { airport a ; int i, pri, curtime, endtime ; double expectarrive, expectdepart ; plane temp ;
initairport ( &a ); // Initialisation Routine
start ( &endtime, &expectarrive, &expectdepart ) ; // Input Parameters
for ( curtime = 1 ; curtime <= endtime ; curtime++ ) // Timing Routine { pri = randomnumber ( expectarrive ) ;
for ( i = 1 ; i <= pri ; i++ ) { newplane ( &a, curtime, ARRIVE ) ; if ( apfull ( a, 'l' ) ) refuse ( &a, ARRIVE ) ; else apaddqueue( &a, 'l' ) ; }
pri = randomnumber ( expectdepart ) ; for ( i = 1 ; i <= pri ; i++ ) { newplane ( &a, curtime, DEPART ) ; if ( apfull ( a, 't' ) ) refuse ( &a, DEPART ) ; else apaddqueue ( &a, 't' ) ; }
if ( ! ( apempty ( a, 'l' ) ) ) { temp = apdelqueue ( &a, 'l' ) ; land ( &a, temp, curtime ) ; } else { if ( ! ( apempty ( a, 't' ) ) ) { temp = apdelqueue ( &a, 't' ) ; fly ( &a, temp, curtime ) ; } else idle ( &a, curtime ) ; } }
conclude ( &a, endtime ) ; // Report Generator Routine
}
Output:
Line Graph for the Output: