University of Surrey UniS Department of Computing Exercises in the use of Micro Saint Sharp - Discrete Event Simulation Familiarise yourself with the various windows and how they can be moved and sized. Start with a Maximum size window and select View/Layout/Reset Screen Layout. Individual windows can be set to “Floating” – right click on the blue top boarder of the window; this enables you to move the various windows about freely and to resize them as appropriate. The various windows are listed under the menu: View/Windows. Familiarise yourself initially with the following windows: Tree View, Task Network and Properties. When building a model it is also often useful to start with fixed times for the various processes or tasks before selecting data from a given distribution to represent stochastic behaviour. In these exercises we will assume that the time units are minutes. Please answer the specific questions as indicated by ……… within each exercise. Deadlines for submitting your answers by email to me [email protected] are: Question 1 Questions 2 & 3 Questions 4 & 5 Questions 6 & 7 2400 Wednesday week 4 2400 Wednesday week 6 2400 Wednesday week 8 2400 Wednesday week 10 Note the web site for this module is at www.computing.surrey.ac.uk/courses/cs366/ 1 Balanced flow through a system Build a linear system and call it Balanced with the following structure: Task (1) A spinner Task (1) to generate entities feeding into Task (2). Set the Inter-Arrival Time (IAT) to be fixed at 10 minutes. This can be achieved by setting the standard deviation of a normal distribution to zero. Set the following code to generate a series of entities arriving 10 minutes apart. Timing Tab to establish the Inter Arrival Time (IAT): return 10.0; return 0.0; for a Normal Distribution for the Standard Deviation – resultant will be a fixed IAT of 10.0 Release Effect: return Clock < 500; // Simulation to generate entities for at least 500 minutes. But we need to increment the Tag value of each entity so that each one has a unique value so in the Ending Effect we can set: Entity.Tag = Entity.Tag + 1; 81894329 1 7/9/05 Dr Terry Hinton Route 1 > 1 Route 1 > 2 No special Decision code. No special Decision code. Task (2) Main Tab: The flow into Task (2) is not constrained so the Release Effect in Task (2) is: return true; There are no special Beginning or Ending Effects Timing Tab Set the process time to be fixed at 10 minutes: return 10.0; return 0.0; for a Normal Distribution for the Standard Deviation – resultant will be a fixed time of 10.0 Duplicate Task (2 ) to become Task(3) – use right click to copy Task Icon and then to Paste it. The code that has been written into Task(2) should also copy across to Task(3). Link the 3 tasks to record the number of entities generated we can declare a global variable MaxNum as an Integer and increment it in any one of the code windows; say Ending Effect of Task(3). To declare a global variable right-click Variables in Tree View. Create a Snapshot to display the value of MaxNum at the end of the simulation. Include in this Snapshot the variable Entity.Tag which should give the same value as MaxNum. Task (3) In the Ending Effects window count the total number of entities: MaxNum = MaxNum + 1; //Declare MaxNum as an integer You have your first simulation model using all fixed values of 10 minutes. Run the model. Question – What time do you expect the simulation to end and how many entities have been generated? Time = ……… Max Number = ………. Justify these data …………………………….. Duplicate your fist model and call it Balanced2. Modify this model to simulate stochastic behaviour. Set the Inter Arrival Time IAT to an Exponential Distribution with a mean of 10.0. Set the timing for Tasks (2) & (3) to be a Normal Distribution of 10.0 minutes with a Standard Deviation of 2.0. Note the number of entities generated and the total time of this modified simulation: Time = ……… Max Number = ………. Compare these values with the last set. Note that the flow of entities through the model are such that more than one can be processed at the same time as there are no queues available. Now add queues (FIFO) to the Tasks (2) & (3) and add a condition so that only one entity can be processed at any one time. The following code is one method that can be used. Declare global variables Free2 & Free3 as Boolean then within Task (2) set: the Release Condition to: and in the Beginning Effect: and in the Ending Effect: 81894329 return Free2; Free2 = false; Free2 = true; 2 7/9/05 Dr Terry Hinton Set similar conditions for Task (3) using Free3. This model represents a balanced flow of entities through the system yet queues form; why ……….. Compare the numbers generated and the time the simulation ends now that queues are active. These values can be found recorded in a built-in Snapshot called QueueDataCollection – right click on the icon to see the data values. Time = ……… Max Number = ………. Explain these new data …………………………. Record the value of the queues: the average queue size and the average waiting time. Question - are these values consistent with the parameters used in the model? Average Queue Size = ……… Average Queue Wait = ………. TaskDataCollection is also available that collects data on the time the tasks take to execute. Make a note of the value of the Mean and Standard Deviation of Task(2) and compare with the Timing values entered into the Task. 2 Build a Model of a Bank using Resources In building a simulation model it is sometimes useful to be able to change the number of Task operators or resources to cope with excessive queues of entities. For example in a bank we would expect to have a number of Tellers serving customers. When a Teller engages with a customer there is one less Teller to serve others. As a customer leaves the number of Tellers available increase by one. Hence we can model this situation by declaring a global variable Tellers of type integer that we can increment or decrement as tellers become busy or become free. Initially the bank can set Tellers to the number of available tellers and would usually adjust this number depending on the time of day and the customer demand shown by the size of the queue. Build a model to represent a bank as discussed in the lectures using the same parameters and with a generator (spinner task) and 2 tasks as illustrated below. Set the IAT = 3.0 as an exponential distribution and Task(2) as a normal distribution with time = 6.0 and standard deviation = 2.0. The number of bank staff can be controlled by an integer variable, so add a global integer variable called Tellers with an initial value of 2. Ensure that the customers are generated for 8 hours only. return Tellers > 0; Tellers = Tellers – 1; Tellers = Tellers + 1; Set the Release Condition of Task (2) to: Beginning Effect: Ending Effect: This code should ensure that a maximum of 2 entities can be processed at any one time. We can count the number of customers leaving the Bank by counting the number that enter the third task “Customer Leaves”. Run the program and note the queue sizes for Task (2) and the number of customers served. Queue (2) …………………………..… Number of customers served ………………… Comment on this data …………………………… 81894329 3 7/9/05 Dr Terry Hinton Although there is only one Task(2) icon in your model to represent the bank’s transactions more than one instance of that task can be generated at anyone time based on the arrival of entities and the release condition; in this case a maximum of 2 instances. 3 Balanced flow through a system called Workshare Build the system shown below: Save a copy of your model as WorkShare. Set IAT = 2.4 and generate 100 entities. Set the time for Task (2) to zero and to control the flow to Task(3), Task (4) and Task (5). Set the process time of the three tasks to be: 24.0, 8.0, 4.0 minutes. Now set the out flow from Task (2) to be probabilistic by selecting the Path Tab and set the Decision to be Probabilistic and enter code into the three paths to return a proportion that adds to 1 e.g.: return 0.2; return 0.3; return 0.5; By appropriate choice of these ratios we can minimise the duration of the simulation. Calculate these optimum ratios by considering the flow of entities through the system noting that a flow rate is inversely proportional to an inter-arrival time. Check that your calculated values give the shortest simulation time. Optimisation manually by trial and error is possible. Note these proportions: Task (3)…...…….. Task (4)……….… Task (5)…..…….. 4 Model called WaitingTime The system records statistics about each queue; yet it is often helpful to know the average waiting time and the maximum waiting time for entities passing through the simulation which might involve many queues. To compute this data we need first to declare two entity attributes say StartWait and TotalWait. These attributes can be updated as each queue is encountered and an average calculated at the end as well as the maximum waiting time. We would thus need 2 global variables TotalWaitTime and MaxWait. The following code could be added to Task(2) in the Balanced2 model: Queue Entering Effect: Entity.StartWait = Clock; Queue Departing Effect Entity.TotalWait = Entity.TotalWait + Clock – StartWait; TotalWaitTime = TotalWaitTime + Clock – StartWait; if MaxWait > Entity.TotalWait {MaxWait = Entity.TotalWait;} In the Snapshot we can calculate the average waiting time as TotalWaitTime/MaxNum. Compare these data with the data provided by the built-in Snapshot QueueDataCollection. 81894329 4 7/9/05 Dr Terry Hinton Provide the following answers: Overall Average Wait time ….. Average Wait Queue1 …… Average Wait Queue2 ……. Comment ………………………. 5 Handling Different Entities It is sometimes useful to have more than one type of entity each requiring to be treated differently as they progress through the model. To illustrate this build the following model: It is convenient to have one spinner task to generate entities that are then separated into say 2 groups using a probability to control the path taken say 50% in both paths. In task 3 and 4 we can set the attributes for each entity including the process time. As an example assume that we have an entity attribute of Colour of type string with “Blue” entites having a process time of 10 minutes and “Red” entities having a process time of 20 minutes. We need to create another entity attribute of ProcessTime of type floating point to record these times. These attributes can be assigned values in the Beginning Effect of Task (3) or Task (4). For example in Task (3): Beginning Effect: Entity.PrcessTime = 10.0; Entity.Colour = “Blue”; With corresponding values in Task (4). These values will be used in the main Task (5) to determine the process time: Timing Code: return Entity.ProcessTime; and also to route the entities out according to their Colour using the Tactical Path code: Path 5 > 9 if Colour == “Blue” {return 2;} else {return 1;} //route via the highest value returned Path 5 > 10 if Colour == “Red” {return 2;} else {return 1;} Build the above model and show that all the blue entites end in Task (9) and the red entities in task (10). It should be noted that Task (2), Task (3) & Task (4) do not represent a process therefore it does not involve any time but is used for the control of the routing. 81894329 5 7/9/05 Dr Terry Hinton 7 Confidence Interval - Model called Confidence In presenting the result of a simulation it is important to know how representative of the model the output data is; so that any output value should have attached to it a confidence or range of values because of the very stochastic nature of simulation. For example, today a factory produces 100 items in 1000 minutes how confident are you that tomorrow it will produce the same number in the same time? To explore this issue use the your model WorkShare and save a copy as Confidence. We need to collect data from a number of trials or runs each using a different set of random numbers but from the same distributions. To set this open the window Tree View/Execution Settings. Set the number of runs to 8 and select Randomise the Seed option and set the number of significant digits to 5. Set up a Snapshot to record the Clock at the end of the simulation. After the simulation runs have taken place you can inspect the clock time – right click the Snapshot icon and take the option to open results in a spread sheet. Calculate the mean and standard deviation for these results and compute the 95% confidence limits of this parameter; the time to process a given number of entities is given by the relationship below. Confidence limits Mean SD t n where SD is the standard deviation of the Mean, and n the number of trials and t a t-test parameter as given in the table below: Number of Trials t value 4 3.1 8 2.4 12 2.2 16 2.1 >= 28 2 Thus you obtain 3 values: a lower, an average and an upper. This is the range in which we expect 95% of the subsequent mean values to lie. Thus if you run the simulation again with a different random number seed to obtain another set of data you would expect that 95 times out of 100 the new mean value would lie within this 95% confidence limit range. This analysis is based upon statistical theory and is only strictly true for stochastic processes that behave in a way that is normally distributed. However this is a useful measure particularly when comparing one set of results with another. Carryout an initial 8 trials calculate the 95% confidence limit for the total time and then run a number of similar trials but each starting with a different initial seed and check the truth of this statistic. 7 Introduction to Queue Statistics It is often important to distinguish between the average number of people waiting for a service and the average waiting time for that service. This has been an important discussion in relationship to waiting times for patients in the National Health Service. To compute the average number of entities in a queue depends on the definition used. The simplest method is to record the number in the queue every time that there is a change. This has the problem that no account is taken of how long entities are waiting in the queue thus a queue that is empty for long periods of time and occasionally has one item in it would produce an average of 0.5. An alternative method is to compute the time-weighted average that takes account of how long the entities have been weighting in the queue. Thus if we consider every 81894329 6 7/9/05 Dr Terry Hinton event i when the number in the queue changes to ni and is in the queue for time dti then we sum over all m events from i = 1 to i = m and then divide by the total time the queue is in operation; we obtain the time weighted average given by: m ni dti i 1 m Equation 1 ti i 1 The product-sum nidti represents the area of the graph of queue against time as illustrated in the top diagram below. This product-sum may be difficult to compute but is equal to the total time spent by all entities in the queue. This is illustrated in the lower diagram below where the two graphs have the same area. Thus we can compute this total by summing the time spent by all entities passing through the queue The average queue is then this total divided by the total time the queue is in operation. If there are N entities passing through the queue the average is given by: N ti i 1 Equation 2 Time Queue is in Operation These 2 equations give the same result: Number in Queue n Time-Weighted Average m Area ni dti i th event with ni in queue for time dti i 1 Number in Queue Total of m events over Time The i th entiity resides in the queue for time ti N Area ti i 1 Same area as diagram above N entities pass through the queue Time . 81894329 7 7/9/05 Dr Terry Hinton 7 Exercise - Queue Statistics Use a simple model such as Balanced2 and consider the queue to Task (2). Compute the timeweighted average queue length using Equation 2 above and compare this value with the built-in statistic called the QueueLengthMean. Calculated Average Queue length ……….…. QueueLengthMean provided ……………… Comment on your results ………………….. We do not expect to see much discrepancy unless the queues are very short and falling to zero at times. To achieve that condition you may need to increase the IAT. Try this out and comment… ……………. An alternative and simple approach to recording the average queue length is to take a snapshot of the queue length, at intervals, and average the values. As the number of snapshots increase we would expect the computed average to approach the time-weighted average. As the processes are stochastic in time taking the snapshot at equal intervals would be appropriate. If the processes were fixed in time then we would need to take the snapshots at random intervals. This would be referred to as a Monty Carlo method and noticeable takes less computation time. Try out this method by setting up a Snapshot which checks say every 10 minutes and records the number in the queue and compare with the results obtained above. Use can be made of a builtin Task method to assign the value of the length of the queue to a global variable, at the time the event, in which the code is written, executes: CurrentQ = Task.ItemsInQueue; Note: To access the value of any queue from within another Task use can be made of the method FindTask where the ID (of type string) of the Task is speicified: Queue = Model.FindTask("2").ItemsInTask; 81894329 8 7/9/05 Dr Terry Hinton
© Copyright 2026 Paperzz