43
Chapter 20: Simulation Re-Visited Spreadsheet-Based Decision Support Systems Prof. Name [email protected] Position (123) 456-7890 University Name

Chapter 20: Simulation Re-Visited Spreadsheet-Based Decision Support Systems Prof. Name [email protected] Position (123) 456-7890 University Name

Embed Size (px)

Citation preview

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

Chapter 20: Simulation Re-Visited

Spreadsheet-Based Decision Support Systems

Prof. Name [email protected] (123) 456-7890University Name

Page 2: Chapter 20: Simulation Re-Visited Spreadsheet-Based Decision Support Systems 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

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

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

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

4

Review of Chapter 9

Defining Simulation

Working with Distributions

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

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.

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

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.

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

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 random numbers.

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

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).

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

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.

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

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)

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

11

Simulation with VBA

Random Numbers and Distributions

Making Runs and Collecting Data

Animation

Analysis

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

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.

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

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.

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

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())

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

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

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

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.

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

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.

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

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 Doubleruns = 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)) ^ 2Range(“A1”).Offset(i,0).Value = Output(i)

Next i

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

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.

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

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.

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

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.

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

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

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

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.

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

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.

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

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.

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

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.

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

27

Histogram (cont)

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

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

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

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.

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

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.XLA file).

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

31

Applications

Game of Craps Revisited

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

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.

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

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.

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

34

Figure 20.1

The spreadsheet

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

35

Figure 20.2

Simulation runs and statistical analysis

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

36

Figure 20.3

The initial code

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

37

Figure 20.4

The simulation code

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

38

Figure 20.5

Two functions used in each roll

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

39

Figure 20.6

Stored dice images

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

40

Figure 20.7

The statistical analysis and create histogram code

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

41

Application Conclusion

The application is now complete.

We can start the code by assigning the Start procedure to the “START” button.

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

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 Analysis Toolpack to perform many types of analysis,

including histograms. Histograms calculate the number of occurrences of values in a particular

interval.

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

43

Additional Links

(place links here)