Name: Singh, Prabhjot EXECUTION TESTS ========= ===== ====================================================================== hw6-sim < hw6.in1 Enter the name of the input file: Enter the start time: Enter the stop time: Enter the minimum number of queues to simulate: Enter the maximum number of queues to simulate:000 File: /home/course/cs113/homework/grading/hw6/q.data3 number of customers: 14 start time: 0 customer arrival rate: 0.14 stop time: 100 average service time: 9.64 total time: 100.00 number of Queues 2 3 4 percent idle time 19.50% 46.67% 60.25% ave wait 5.86 1.36 0.00 longest wait 17 12 0 time of long wait 53 53 0 ave queue length 0.41 0.06 0.00 cust in queues at end 0 0 0 ====================================================================== hw6-sim < hw6.in2 Enter the name of the input file: Enter the start time: Enter the stop time: Enter the minimum number of queues to simulate: Enter the maximum number of queues to simulate:00000 File: /home/course/cs113/homework/grading/hw6/q.data4 number of customers: 172 start time: 0 customer arrival rate: 0.17 stop time: 1000 average service time: 17.88 total time: 1000.00 number of Queues 1 2 3 4 percent idle time 1.80% 1.85% 2.33% 17.20% ave wait 342.80 194.69 50.01 10.37 longest wait 689 360 140 68 time of long wait 298 613 818 810 ave queue length 58.12 16.56 2.91 0.44 cust in queues at end 122 71 21 1 number of Queues 5 percent idle time 33.62% ave wait 3.23 longest wait 27 time of long wait 778 ave queue length 0.11 cust in queues at end 0 ====================================================================== hw6-sim < hw6.in3 Enter the name of the input file: Enter the start time: Enter the stop time: Enter the minimum number of queues to simulate: Enter the maximum number of queues to simulate:0 File: /home/course/cs113/homework/grading/hw6/q.data7 number of customers: 208 start time: 1000 customer arrival rate: 0.14 stop time: 2500 average service time: 19.64 total time: 1500.00 number of Queues 7 percent idle time 57.20% ave wait 2.73 longest wait 34 time of long wait 1978 ave queue length 0.05 cust in queues at end 0 ====================================================================== SOURCE FILES ====== ===== /* * File: hw6-sim.c * Author: Prabhjot Singh Anand (psanand) * Class: CS113 * Assignment: Homework # 6 * Due Date: 11/13/97 * Last modified: 11/13/97 * ----------------------------------------------------- * OVERVIEW: * This program simulates checkout lines, such as the ones you * might encounter in a grocery store. The mininum and maximum * number of checkout lines are inputed by the user. The user also inputs the * the starting and ending time of the simulation and the file in which the * the arrival and service times for customers are recorded. Customers arrive * at the checkout stands and get in the line with the shortest queue. The * customers Those customers wait in their line until the cashier is free, * at which point they are served and occupy the cashier for some period * of time. After the service time is complete, the cashier is free to * serve the next customer in the line. * In each unit of time between the start and end times of the simulation, * the following operations are performed: * * 1. Determine whether a new customer has arrived. * New customers arrive if the arrival time of the next customer read, * from the file matches the current time. * * 2. If all the cashiers are busy, note that the cashiers have * spent another minute with their customer. Eventually, * the customer's time request is satisfied, which frees * the cashiers. * * 3. Check if any of the cashiers are free. If none of the cashiers are * free put the customer that just arrived in the queue with the * shortest line. Serve the next customer in line. The service time is * read in from the file at the same time as the arrival time of the * customer * * At the end of the simulation, the program displays the * parameters and the following computed results: * * * 1.name of data file, * 2.time (start time, end time, total time), * 3.the numbers of queues used in simulation, * 4.number of customers * 5.arrival rate (number of customers/total time), * 6.the average service time for customers, * 7.the average wait for customers, * 8.the time and length of the longest wait by a customer * 9.the average queue length, *10.the number of customers waiting in the queues when the simulation ends, *11.percent idle time (idle cashier time/total time). * ------------------------------------------------------ * ALGORITHYMIC NOTES: This program makes use of functions from the queue.h * library. * ------------------------------------------------------ */ /* INCLUDES */ #include #include #include "strlib.h" #include "genlib.h" #include "simpio.h" #include "queue.h" /* TYPEDEFS */ /* * Type: customerT * --------------- * A customer is represented using a pointer to a record * containing the following information: * * o The customer number (for debugging traces) * o The arrival time (to compute the waiting time) * o The time required for service */ typedef struct { int customerNumber; /* stores the arrival number of the customer */ int arrivalTime; /* stores the arrival time of the customer */ int serviceTime; /* stores the service time needed for the customer */ } *customerT; /* * Type: simDataT, simT * -------------- * This type stores the data required for the simulation. The * main program declares a variable of this type and then passes * it by reference to every other function in the program. simT is just * another way to identify this structure. */ typedef struct { queueADT *queue; /* pointer to an array of queues */ customerT *activeCustomer; /* pointer to an array of active customers */ int time; /* stores the current time of the simulation */ int numCustomers; /* stores the total number of customers that arrive */ int numServed; /* stores the total number of customers that arrive */ long totalServiceTime; /* stores the totalServiceTime for all the customers served */ long totalWaitTime; /* stores the waitTime for all the customers served*/ long totalLineLength; /* stores the total line length at the end of each simulation */ int numQueues; /* stores the number of queues used for each simulation */ int idleTime; /* stores the total idle time for all the queues */ int longestWaitLength; /* stores the longest wait time for a customer */ int longestWaitTime; /* stores the time when the customer with the longest wait time arrived */ int finalQueueLength; /* stores the final length of all the queues at the end of the simulation */ } *simDataT, simT; /* Private function declarations */ /* * Function: InitializeSimulation * Usage: InitializeSimulation(simData, maxQueues, numSim); * -------------------------------------- * This function takes, simData, maxQueues, and numSim as its arguments and * initializes the simulation data required to run the simulation. */ static simDataT InitializeSimulation(simDataT sdp, int maxQueues, int numSim); /* * Function: RunSimulation * Usage: RunSimulation(&simData[i], startTime, stopTime, filename); * ------------------------------- * This function takes, simData[i], maxQueues, and numSim as its argument * and runs the actual simulation for each time unit. */ static void RunSimulation(simDataT sdp, int StartTime, int StopTime, string filename); /* * Function: EnqueueCustomer * Usage: EnqueueCustomer(sdp, aTime, sTime, shortestQueue); * --------------------------------- * This function takes sdp, aTime, sTime, shortestQueue as its argument and * simulates the arrival of a new customer. */ static void EnqueueCustomer(simDataT sdp, int aTime, int sTime, int shortestQueue); /* * Function: ProcessQueue * Usage: ProcessQueue(sdp); * ------------------------------ * This function takes sdp as its argument and processes a single time cycle * for the queue. */ static void ProcessQueue(simDataT sdp); /* * Function: ServeCustomer * Usage: ServeCustomer(sdp, index); * ------------------------------- * This function takes sdp and the index of the shortest queue and sets * up the customer so he can be proccessed. */ static void ServeCustomer(simDataT sdp, int index); /* * Function: DismissCustomer * Usage: DismissCustomer(sdp); * --------------------------------- * This function is called when the each queue's active customer's service * time has dropped to 0. The cashier becomes free and the * program no longer needs to hold the customer's storage. It takes sdp * as its argument. */ static void DismissCustomer(simDataT sdp, int index); /* * Function: ReportResults * Usage: ReportResults(simData, filename, startTime, stopTime, numSim); * ------------------------------- * This function takes sdp, filename, the startTime, stopTime, and numSim as * its arguments and reports the results of the simulation. */ static void ReportResults(simDataT sdp, string filename, int startTime, int stopTime, int numSim); /* * Function: FindShortestQueue * Usage: FindShortestQueue(sdp); * ------------------------------- * This function takes sdp as its argument and returns the index of the * queue that has the shortest length. */ static int FindShortestQueue(simDataT sdp); /* * Function: FreeSimData * Usage: FreeSimData(simData, numSim, maxQueues) * ------------------------------- * This function takes simData, the number of simulations, and * the maximum number of queues as its argument and frees everything * that was dynamically allocated to run the simulation. */ static void FreeSimData(simDataT sdp, int numSim, int maxQueues); /* * Function: FindFinalQueueLength * Usage: FindFinalQueueLength(sdp) * ------------------------------- * This function takes sdp as its argument and adds the queuelengths * of all the queues used to run that simulation. */ static void FindFinalQueueLength(simDataT sdp); /* Main program */ int main() { string filename; /* stores the name of the file wth simulation data */ simDataT simData; /* Stores information for each simulation */ int startTime; /* stores the start time for the simulation */ int stopTime; /* stores the stop time for the simulation */ int minQueues; /* stores the minimum number of queues to simulate */ int maxQueues; /* stores the maximum number of queues to simulate */ int n; /* loop variable */ int i; /* tracks the current simulation being performed */ int numSim; /* stores the number of simulations to perform */ printf("\nEnter the name of the input file: "); filename = GetLine(); printf("\nEnter the start time:"); startTime = GetInteger(); printf("\nEnter the stop time:"); stopTime = GetInteger(); printf("\nEnter the minimum number of queues to simulate:"); minQueues = GetInteger(); printf("\nEnter the maximum number of queues to simulate:"); maxQueues = GetInteger(); numSim = maxQueues - minQueues + 1; simData = InitializeSimulation(simData, maxQueues, numSim); i=0; for(n = minQueues; n <= maxQueues; n++){ simData[i].numQueues = n; RunSimulation(&simData[i], startTime, stopTime, filename); i++; } /* end for(n = minQueues; n <= maxQueues; n++) */ ReportResults(simData, filename, startTime, stopTime, numSim); FreeSimData(simData, numSim, maxQueues); return(0); } /* end MAIN */ /* * Function: InitializeSimulation * Usage: InitializeSimulation(simData, maxQueues, numSim); * -------------------------------------- * This function initializes the simulation data block whose * address is passed as the argument. This is done by dynamically allocating * all of the elements required to run the simulation. */ static simDataT InitializeSimulation(simDataT sdp, int maxQueues, int numSim) { int i; /* Loop Variable */ int j; /* Loop Variable */ /* Allocates memory for the structure that stores all of the data */ sdp = (simDataT)malloc(sizeof(simT)*numSim); for(i=0; i= startTime) break; } /* end while(fscanf(ifp, "%d, ", &aTime) != EOF) */ printf("%d", sdp->numCustomers); for(sdp->time = startTime; sdp->time <= stopTime; sdp->time++) { if(sentinel == 0) { if(aTime == sdp->time) { shortestQueue = FindShortestQueue(sdp); EnqueueCustomer(sdp, aTime, sTime, shortestQueue); if((fscanf(ifp, "%d, ", &aTime)) == EOF) sentinel = 1; fscanf(ifp, "%d; ", &sTime); } /* end if(aTime == sdp->time) */ } /* end if(sentinel == 0) */ ProcessQueue(sdp); } /* end for(sdp->time = startTime; sdp->time <= stopTime; sdp->time++)*/ FindFinalQueueLength(sdp); fclose(ifp); /* closes the input file */ } /* end RunSimulation */ /* * Function: EnqueueCustomer * Usage: EnqueueCustomer(sdp, aTime, sTime, shortestQueue); * --------------------------------- * This function simulates the arrival of a new customer. */ static void EnqueueCustomer(simDataT sdp, int aTime, int sTime, int shortestQueue) { customerT c; /* stores information for each new customer */ sdp->numCustomers++; c = New(customerT); c->customerNumber = sdp->numCustomers; c->arrivalTime = aTime; c->serviceTime = sTime; Enqueue(sdp->queue[shortestQueue], c); } /* end EnqueueCustomer */ /* * Function: ProcessQueue * Usage: ProcessQueue(sdp); * ------------------------------ * This function processes a single time cycle for the queue. */ static void ProcessQueue(simDataT sdp) { int i; /* loop variable */ for(i=0; i < sdp->numQueues; i++) { if (sdp->activeCustomer[i] == NULL) { if (!QueueIsEmpty(sdp->queue[i])){ ServeCustomer(sdp, i); } else { sdp->idleTime++; } } else { if (sdp->activeCustomer[i]->serviceTime == 0) { DismissCustomer(sdp, i); } else { sdp->activeCustomer[i]->serviceTime--; } } sdp->totalLineLength += QueueLength(sdp->queue[i]); } /* end for(i=0; i < sdp->numQueues; i++) */ } /* end ProcessQueue */ /* * Function: ServeCustomer * Usage: ServeCustomer(sdp, index); * ------------------------------- * This function is called when the cashier is free and a * customer is waiting. The effect is to serve the first * customer in the line and update the total waiting time. */ static void ServeCustomer(simDataT sdp, int index) { customerT c; /* stores information for each dequeued customer */ int waitTime; /* stores the wait time for each customer */ c = Dequeue(sdp->queue[index]); /* Dequeues a customer from a queue */ sdp->activeCustomer[index] = c; /* Increments the total number of customers made */ sdp->numServed++; /* Increments the total service time */ sdp->totalServiceTime += c->serviceTime; /* Determines the wait time for each customer */ waitTime = sdp->time - c->arrivalTime; /* Determines wheather the current wait time is the longest wait time */ if( waitTime > sdp->longestWaitLength){ sdp->longestWaitLength = waitTime; sdp->longestWaitTime = c->arrivalTime; } /* Increments the total wait time */ sdp->totalWaitTime += waitTime; } /* end ServeCustomer */ /* * Function: DismissCustomer * Usage: DismissCustomer(sdp); * --------------------------------- * This function is called when the each queue's active customer's service * time has dropped to 0. The cashier becomes free and the * program no longer needs to hold the customer's storage. */ static void DismissCustomer(simDataT sdp, int index) { FreeBlock(sdp->activeCustomer[index]); sdp->activeCustomer[index] = NULL; } /* end DismissCustomer */ /* * Function: ReportResults * Usage: ReportResults(simData, filename, startTime, stopTime, numSim); * ------------------------------- * This function reports the results of the simulation. */ static void ReportResults(simDataT sdp, string filename, int startTime, int stopTime, int numSim) { float totalTime; /* stores the total time of the simulation */ int i; /* loop variable */ int j; /* loop variable */ int count = 0; /* stores the number of simulations proccessed */ int loopMax = 4; /* stores the maximum number of simulations to print in one row, with four being the max. */ totalTime = stopTime - startTime; printf("\nFile: %s\n",filename); printf(" number of customers: %d\t",sdp[0].numCustomers); printf("\tstart time: %d\n", startTime); printf(" customer arrival rate: %.2f\t",sdp[0].numCustomers/totalTime); printf(" \tstop time: %d\n",stopTime); printf(" average service time: %.2f\t", (float)sdp[0].totalServiceTime / sdp[0].numServed); printf("\ttotal time: %.2f", totalTime); /* The outside for loop, for(i=1; i <= numSim; i+=4), controls the number of simulations to be printed. The inside for loops, for (j=0; j < loopMax; j++), print out data in one row, for a maximum number of simulations given by loopMax. The variable count is incremented by the value of loopMax each time the outside for loop is executed. Count is then compared with the number of total simulations to determine if data for any more simulations needs to be displayed. If (The number of simulations - count) is less than the value of loopMax, which is originally set to 4. loopMax is given the value of (The number of simulations - count) and the final set of inside for loops are executed. */ for(i=1; i <= numSim; i+=4) { /* checks if the number of displayed simulations equals the total number of simulations */ if(count == numSim) return; if((numSim - count) < 4) loopMax = numSim - count; printf ("\n\nnumber of Queues\t"); for (j=0; j < loopMax; j++) { printf("\t%d",sdp[(count + j)].numQueues); } /* end for (j=0; j < loopMax; j++) */ printf("\npercent idle time\t"); for(j=0; j< loopMax; j++) { printf("\t%.2f%%",(sdp[count+j].idleTime/ (totalTime * sdp[count+j].numQueues))*100 ); } /* end for (j=0; j < loopMax; j++) */ printf("\nave wait \t"); for(j=0; j< loopMax; j++) { printf("\t%.2f",(float)sdp[count+j].totalWaitTime/ sdp[count+j].numServed); } /* end for (j=0; j < loopMax; j++) */ printf("\nlongest wait \t"); for(j=0; j < loopMax; j++) { printf("\t%d",sdp[count+j].longestWaitLength); } /* end for (j=0; j < loopMax; j++) */ printf("\ntime of long wait\t"); for(j=0;j < loopMax; j++) { printf("\t%d",sdp[count+j].longestWaitTime); } /* end for (j=0; j < loopMax; j++) */ printf("\nave queue length\t"); for(j=0; j < loopMax; j++) { printf("\t%.2f", (float)sdp[count+j].totalLineLength/ (totalTime*sdp[count+j].numQueues)); } /* end for (j=0; j < loopMax; j++) */ printf("\ncust in queues at end\t"); for(j=0; j < loopMax; j++) { printf("\t%d", sdp[count+j].finalQueueLength); } /* end for (j=0; j < loopMax; j++) */ for(j=0; j < loopMax; j++) { count++; } /* end for (j=0; j < loopMax; j++) */ printf("\n"); } /* end for(i=1; i <= numSim; i+=4) */ } /* end ReportResults */ /* * Function: FindShortestQueue * Usage: FindShortestQueue(sdp); * ------------------------------- * This function finds the queue with the shortest line and returns the * index of the queue. */ static int FindShortestQueue(simDataT sdp) { int i; /* loop variable */ /* stores the index of the shortest queue, the index is initailly set to zero for comparison purposes */ int index=0; for(i=0;inumQueues;i++) { if(sdp->activeCustomer[i] == NULL){ return(i); } /* end for(i=0;inumQueues;i++) */ } for(i=0; i< sdp->numQueues-1; i++) { if(QueueLength(sdp->queue[index]) <= QueueLength(sdp->queue[i+1])) index = index; else index = i +1; } /* end for(i=0; i< sdp->numQueues-1; i++) */ return(index); } /* end FindShortestQueue */ /* * Function: FindFinalQueueLength * Usage: FindFinalQueueLength(sdp) * ------------------------------- * This function finds the final queue length after each simulation by adding * up the queuelengths of all the queues. */ static void FindFinalQueueLength(simDataT sdp) { int i; /* loop variable */ for(i=0; i< sdp->numQueues; i++){ sdp->finalQueueLength += QueueLength(sdp->queue[i]); } /* end for(i=0; i< sdp->numQueues; i++) */ } /* end FindFinalQueueLength */ /* * Function: FreeSimData * Usage: FreeSimData(simData, numSim, maxQueues) * ------------------------------- * This function frees everything that was dynamically allocated to * run the simulation. */ static void FreeSimData(simDataT sdp, int numSim, int maxQueues) { int i; /* loop variable */ int j; /* loop variable */ for(i=0;i