1 / 43

Chapter 20: Simulation Re-Visited

Spreadsheet-Based Decision Support Systems. Chapter 20: Simulation Re-Visited. Prof. Name name@email.com Position (123) 456-7890 University Name. Overview. 20.1 Introduction 20.2 Review of Chapter 9 20.3 Simulation with VBA

zareh
Download Presentation

Chapter 20: Simulation Re-Visited

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Spreadsheet-Based Decision Support Systems Chapter 20: Simulation Re-Visited Prof. Name name@email.com Position (123) 456-7890 University Name

  2. Overview • 20.1 Introduction • 20.2 Review of Chapter 9 • 20.3 Simulation with VBA • 20.4 Applications • 20.5 Summary

  3. Introduction • Random numbers and distributions in VBA • Performing dynamic simulation runs using VBA • Creating simulation animation using VBA • Dynamic analysis using histograms and VBA • A full dynamic simulation application with animation

  4. Review of Chapter 9 • Defining Simulation • Working with Distributions

  5. Review of Chapter 9 • In Chapter 9 we defined simulation and discussed how to apply it in an Excel spreadsheet. • In this chapter we will show some parallel functionality that can be accomplished using VBA. • First let us review how simulation is defined and how to work with distributions when using simulation in Excel.

  6. Defining Simulation • Simulation is a software-generated display of a process which one wants to observe. • Simulation is a useful tool for analyzing how a set of actions functions under different parameters or settings. • It benefits from trial and error without the cost of materials, labor and time that would be necessary to observe the same results on the original process. • Simulation differs from optimization in that it does not aim to find the best result for some given inputs. • Rather, it seeks to find the best inputs given a desired result by running several trials of a particular scenario.

  7. Working with Distributions • Simulation is useful because it can handle variability of parameters in a model. • That is, we cannot know many settings in a process with full certainty; there is usually some range of numbers into which values fall but we can not know exact numbers. • Thus, we use distributions and randomnumbers.

  8. Random Numbers and Distributions • We use the RAND function in Excel to generate random numbers. • The general distribution functions in Excel have the following format =NORMDIST(x_value, mean, std_dev, cumulative_value) • The x-value is the number for which we are calculating the distribution. • The cumulative value is TRUE if we use the cumulative distribution function (cdf), or FALSE if we use the probability mass function (pmf).

  9. Rand Num and Dist (cont) • To generate a random number within a given distribution, we must use the inverse distribution functions. • The format of these inverse distribution functions is the following • =NORMINV(probability, mean, std_dev) • The probability parameter is a number between 0 and 1 associated with the given distribution.

  10. Rand Num and Dist (cont) • We will use the RAND function as our value for this parameter to generate some numbers between 0 and 1. • For example, to generate ten random numbers in the Normal distribution, we would follow the format. =NORMINV(RAND(), mean, std dev)

  11. Simulation with VBA • Random Numbers and Distributions • Making Runs and Collecting Data • Animation • Analysis

  12. Simulation with VBA • There is no particular “simulation code” in VBA. • We will instead be defining a set of functions we have already learned, and some new ones, which we can use when programming a simulation application.

  13. VBA Rand Num and Dist • To generate random numbers in VBA, we will use the Rnd function as discussed in Chapter 14. • This function can be manipulated to generate a random number between a lower bound and upper bound using the following formula. (upperbound - lowerbound + 1) * Rnd + lowerbound • You can use this equation with the Int function to ensure this random number is an integer.

  14. VBA Rand Num and Dist(cont) • To generate a random number in a particular distribution in VBA, we will usually have to use the distribution formulas from Excel. • To do this, we can use the same techniques from Chapter 9 along with the Formula or FormulaR1C1 methods in VBA. • We can also use some of the Worksheet Functions available with the Application object to use some of Excel’s inverse distribution functions. • Since the Log function is also a defined VBA function, we can use it with the Rnd function to generate random numbers from the Exponential distribution. • To do this we just take the logarithm of the random number. Log(Rnd())

  15. VBA Rand Num and Dist(cont) • To specify a particular Exponential distribution from which the random number is being generated, we simply multiply the mean by the previous function. Mean * Log(Rnd()) • For example, the below code generates 10 random numbers from an Exponential distribution with a user-specified mean. Dim mean As Double mean = InputBox(“Please enter mean of your Exponential distribution.”) For i = 1 to 10 Range(“A1”).Offset(i,0).Value = mean * Log(Rnd()) Next i

  16. Making Runs and Collecting Data • As seen in the code above, we can use For, Next loops to generate multiple data values. • We may ask the user to specify how much data they want to use for the simulation. • We may also perform some calculations with the random data we generate. • To simulate a system, we first prompt the user for the number of runs they want to perform. • A run is a single execution of a series of actions which model the system.

  17. Runs and Data (cont) • We then need to determine if we will be storing this data and the results of the specified calculations in arrays or in a spreadsheet. • When multiple calculations of data are needed, which do not require Excel functions, arrays can be easier structures to work with. • They are easier in the since that range names do not need to be defined and extra worksheet space does not need to be used. • If however, some other Excel functions, such as distribution functions, will need to be used with the generated data, it may be better to store this data in a worksheet.

  18. Runs and Data (cont) • For example, say there is a system which takes as input a number from an Exponential distribution with lambda 5, and outputs the square of that number. Dim runs As Integer, Input() As Double, Output() As Double runs = InputBox(“Please enter the number of runs for this simulation”) ReDim Input(runs) ReDim Output(runs) For i = 1 to runs Input(i) = 5 * Log(Rnd()) Output(i) = (Input(i)) ^ 2 Range(“A1”).Offset(i,0).Value = Output(i) Next i

  19. Runs and Data (cont) • The input and/or output of a simulation can then be stored in a spreadsheet for the user to see. • For some simulation models, it will be unnecessary to store or show the user the input values. • Analysis of the output is usually of most importance to the user.

  20. Animation • Two other VBA methods, both of the Application object, should be used when creating a simulation program: • ScreenUpdating method • Wait method • The ScreenUpdating method should be set to False before the simulation runs begin. • This reduces screen flickering while the program runs and increase efficiency (decrease running time) if the screen is not updated after every action in the code. • It should be set to True when the runs have been completed.

  21. Animation (cont) • The Wait method can be used to create some animation of the simulation. • For example, if we are simulating a production line, we may have several parts being received and shipped from different work stations. • Each of these work stations may have a different distribution for processing time • We can use the functions discussed above to create a list of generated time values at which a part is finished at each station.

  22. Animation (cont) • If Work Station 1 processes parts at an Exponential rate of 2, then we could use the following code to generate times at which parts would leave Work Station 1. ReDim WorkStation1(runs) As Double For i = 1 to runs WorkStation1(i) = 2 * Log(Rnd()) Next i • We can then create a cumulative time array using the following. ReDim CumWork1(runs) As Double WorkStation(0) = 0 For i = 1 to runs CumWork1(i) = WorkStation1(i) + WorkStation1(i – 1) Next i

  23. Animation (cont) • Now we can run a loop to show that a product leaves Work Station 1 at each time value. • To do this, we may highlight some cell which changes position, or disappears and reappears, every time this action occurs. • To create this action we would pick some range to highlight (by changing the color with the Interior property) and each time we loop through a run, we un-highlight this cell and highlight the next cell (using the Offset property). • However, to ensure that the user sees a delay between these actions, we use the Wait method.

  24. Animation (cont) For i = 1 to runs Application.ScreenUpdating = False Range(“Time”).Value = CumWork1(i) Range(“A1”).Offset(i-1, 0).Interior.ColorIndex = 0 Range(“A1”).Offset(i, 0).Interior.ColorIndex = 5 Application.Wait(Now + Time(0:0:03)) Application.ScreenUpdating = True Next i • Running this code will create an animated simulation which appears to move a product from one cell to the next at each iteration. • This idea can be modified to create an animation that better reflects any particular system.

  25. Analysis • The motivation for using simulation is of course to perform some analysis on a system. • There are several ways to analyze the results of a simulation. • For the output produced, a graph can be created, the maximum or minimum can be found, etc. • We can use Chart objects to accomplish these analsyis. • We can also use the Analysis Toolpack with features such as Histograms.

  26. Histograms • Histograms calculate the number of occurrences of values in a particular interval. • There are four main parts to creating a histogram. • Input = range of data on the worksheet. • Bins = the intervals into which values can be counted; they can be defined by the user or can be evenly distributed among the data. • Output = range where the frequency calculations for each bin will be printed. • Charts options = simple chart, cumulative percentages for each bin value, and/or a Pareto organization of the chart.

  27. Histogram (cont)

  28. Histogram (cont) • To create the histogram using VBA, we will use the Application object and the Run method. • The Run method is used to specify the Add-In file from which we will be running the analysis. • The arguments for this method include all of the parameters we have previously seen. Application.Run "ATPVBAEN.XLA!Histogram", Worksheets("Histograms").Range("Input"), _ Worksheets("Histograms").Range("Output"), , True, True, True, False

  29. Histogram (cont) • Note that if you run this code multiple times, you will have multiple charts created. • For better code efficiency, we recommend creating a histogram in Excel first (as we have seen with some Chart applications) and then modifying your code to have the chart option parameters all set to False. • You will also see a message warning you that some data will be overwritten if you repeatedly print the output of the histogram to the same cell. • We therefore recommend clearing this output range of cells before calling the histogram function each time.

  30. Histogram (cont) • Other analysis from the Analysis Toolpack may be useful to your application. • To discover the particular code and parameters for each analysis tool, record a macro first in Excel (most analysis tools will still run an extension of the ATPVBAEN.XLAfile).

  31. Applications • Game of Craps Revisited

  32. Description • In the Craps game simulation in Chapter 9, we determined how often a player wins or loses in up to 5 rolls of the dice. • In the game of Craps, a player rolls two dice. • If the first roll yields a sum of 2, 3, or 12, the player loses. • If the first roll yields a sum of 7 or 11, the player wins. • Otherwise, the player continues rolling the dice until she matches the value thrown on the first roll or rolls a sum of 7. • Rolling a match for the first roll wins the game, rolling a value of 7 before a match loses the game.

  33. VBA Simulation • VBA allows us to perform more runs and include some animation. • We will show the user the value of the dice they roll on each run. • Each run is defined to be one play of the game with one to five rolls of the dice. • After each roll of the dice, we evaluate the value of the sum to determine if they continue rolling or stop the game by winning or losing.

  34. Figure 20.1 • The spreadsheet

  35. Figure 20.2 • Simulation runs and statistical analysis

  36. Figure 20.3 • The initial code

  37. Figure 20.4 • The simulation code

  38. Figure 20.5 • Two functions used in each roll

  39. Figure 20.6 • Stored dice images

  40. Figure 20.7 • The statistical analysis and create histogram code

  41. Application Conclusion • The application is now complete. • We can start the code by assigning the Start procedure to the “START” button.

  42. Summary • Simulation is a modeling tool which we use to imitate a real-world process in order to understand system behavior. • To generate random numbers in VBA, we use the Rnd function. • We can use the Log function with the Rnd function in VBA to generate random numbers from the exponential distribution. • The Application.WorksheetFunction method contains several inverse functions which we can use directly in VBA. • We can use the Wait method to create some animation of the simulation. • We can use the AnalysisToolpack to perform many types of analysis, including histograms. • Histograms calculate the number of occurrences of values in a particular interval.

  43. Additional Links • (place links here)

More Related