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

Chapter 20: Simulation Re-Visited

Jan 18, 2016

Download

Documents

carsyn

Spreadsheet-Based Decision Support Systems. Chapter 20: Simulation Re-Visited. Prof. Name [email protected] Position (123) 456-7890 University Name. Overview. 20.1 Introduction 20.2 Review of Chapter 9 - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 20: Simulation Re-Visited

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

2

Overview

20.1 Introduction 20.2 Review of Chapter 9 20.3 Simulation using Object-Oriented API in Risk Solver Platform 20.4 Applications 20.5 Summary

Page 3: Chapter 20: Simulation Re-Visited

3

Introduction

Creating and modifying a simulation problem using Object-Oriented API

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

4

Review of Chapter 9

Defining Simulation

Simulation Using Risk Solver Platform

Page 5: Chapter 20: Simulation Re-Visited

5

Review of Chapter 9

In Chapter 9 we defined simulation and discussed how to create simulation models in Excel using Risk Solver Platform.

In this chapter we will show some parallel functionality that can be accomplished using the Object-Oriented API in VBA.

First let us review how simulation is defined and how to create a simulation model in Excel.

Page 6: Chapter 20: Simulation Re-Visited

6

Defining Simulation

Simulation is a modeling tool which we use to imitate a real-world process or system in order to understand its behavior.

We use simulation to evaluate multiple strategies and predict the future performance of the system.

Simulation is a useful tool in that it helps us make observations 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 or system.

Simulation differs from optimization in that instead of seeking to optimize an objective function value, it simulates the behavior of a system to assess its performance under several scenarios.

Page 7: Chapter 20: Simulation Re-Visited

7

Simulation Using Risk Solver Platform

Risk Solver Platform is a useful tool to build simulation models in Excel.

The first step in building a simulation model using Risk Solver Platform is to setup our Excel worksheet.

In a simulation model there is at least one problem input that is stochastic in nature.

We model stochasticity by generating random numbers from a particular distribution function.

We can generate random numbers from a given distribution using the RAND function of Excel and Psi Distribution functions of Risk Solver Platform.

Page 8: Chapter 20: Simulation Re-Visited

8

Building a Simulation Model

To build a simulation model using Risk Solver platform: Define Uncertain Variable (random problem input):

- These are ranges of cells that contain PsiDistribution functions.

Define Uncertain Function (random problem output): - These cell refer problem input cells. - To the formula in these cells type ‘+ PsiOutput()’

Define Statistic Function: provide summary statistics over all simulation runs:

- ‘= PsiMean()’, ‘=PsiStdDev()’, ‘=PsiMin()’, ‘=PsiMax()’, etc.

Run the simulation by clicking on:

Risk Solver Platform > Solve Action > Simulate command.

Page 9: Chapter 20: Simulation Re-Visited

9

Simulation Using Risk Solver Platform (cont’d)

The Model tab of the of Risk Solver’s Task Pane lists – All problem inputs, which are referred to as Uncertain Variables.– All problem outputs which are referred to as Uncertain Functions. – Statistic Functions

We set the value of Trials per Simulation property at the Platform tab of the Task Pane and run the simulation.

Risk Solver Platform generates a wide variety of simulation reports. – We double click on an output cell to activate the Simulations Results dialog

box which summarizes the results of the simulation. – Risk Solver Platform provides tools to perform sensitivity analysis with

respect to different problem parameters.

Page 10: Chapter 20: Simulation Re-Visited

10

Simulation using Object-Oriented API in Risk Solver Platform

Creating a Simulation Problem

Making Runs and Collecting Data

Animation

Analysis

Page 11: Chapter 20: Simulation Re-Visited

11

Simulation using Object-Oriented API in Risk Solver Platform

The Risk Solver Platform tab on the Ribbon provides a user friendly interface to build and analyze advanced Monte Carlo simulation models.

It is recommended to initially build your simulation model using Risk Solver Platform in Excel, and then use Object-Oriented API in VBA to:– Control solver engine parameters

– Perform a simulation

– Collect simulation statistics

We will also discuss how to use VBA to add animation to our simulations.

Page 12: Chapter 20: Simulation Re-Visited

12

Creating a Simulation Model

Use Object-Oriented API in VBA to create an instance of the Problem object. – Check that Risk Solver Platform xx Type Library is selected From Tools >

References list on the VBE.

The Problem object represents the whole simulation problem. – Dim MyProb As New RSP.Problem

We use the Init method to initialize an instance of the Problem object using the simulation model in the current workbook. – MyProb.Init ActiveWorkbook

Upon initializing our problem, a collection of Variable objects and a collection of Function objects are created automatically.

Page 13: Chapter 20: Simulation Re-Visited

13

Creating a Simulation Model (cont’d)

Add an uncertain variable to an existing model. – Range(“A11”). Formula = “=PsiExponential(10)”

Add an uncertain function to an existing model. – Range(“B11”).Formula = Range(“B11”).Formula + “+PsiOutput()”

Note:– Each Variable object corresponds to a range of one or more cells that

contain Psi Distribution functions.

– Each Function object corresponds to an output (formula) cell which contains an Excel function that manipulates problem variables, and references the PsiOutput() function.

Page 14: Chapter 20: Simulation Re-Visited

14

Setting Engine Parameters and Running the Simulation

We present a procedure that can be used to cerate and initialize a problem, set Solver and Engine parameters, and simulate the problem.

Sub Simulate()

Dim MyProb as New RSP.Problem

MyProb.Init ActiveWorkbook

MyProb.Solver.NumTrials = 5000

MyProb.Engine.Params("SamplingMethod") = 2

MyProb.Engine.Params("RandomSeed") = 12

MyProb.Solver.Simulate

Set MyProb = Nothing

End Sub

Page 15: Chapter 20: Simulation Re-Visited

15

Collecting Simulation Results

Sub Simulate() Dim MyProb as New RSP.Problem, i as Integer, j as Integer MyProb.Init ActiveWorkbook MyProb.Solver.Simulate  For i = 0 To MyProb.Functions.Count -1 For j = 0 To MyProb.Functions(i).Size -1 MsgBox MyProb.Functions(i).Statistics.Min(j) MsgBox MyProb.Functions(i).Statistics.Mean(j) MsgBox MyProb.Functions(i).Statistics.Max(j) MsgBox MyProb.Functions(i).Statistics.StdDev(j) Next j Next i End Sub

Page 16: Chapter 20: Simulation Re-Visited

16

Collecting Simulation Results (cont’d)

AllTrials is a property of Variable and Function objects. – It is used to access the value that the variable/function cells take during each

simulation trial.

– This property yields a DoubleMatrix object that takes two subscripts: one indicating the location of the variable, and the other indicating the simulation trial.

– The following lines of code displays the value that a random variable takes during the i-th simulation trial.

MyProb.Variables(0).AllTrials(1,i-1)

– The following lines of code display the value that a random Function takes during the i-th simulation trial.

MyProb.Functions(1).AllTrials(2,i-1)

Page 17: Chapter 20: Simulation Re-Visited

17

Collecting Simulation Results (cont’d)

Instead of the summary statistics, we often want to know exactly the value of an output cell during each simulation trial.

We can use the AllTrials property to access the value that output cells take during each simulation trial.

For i = 0 To MyProb.Functions.Count -1

For j = 0 To MyProb.Functions(i).Size -1

For k = 0 to MyProb.Solver.NumTrials – 1

MsgBox MyProb.Functions(i).AllTrials(j, k)

If (i = 1) then

Range(“D1”).Offset(k,j).Value= MyProb.Functions(i).AllTrials(j, k)

End if

Next k

Next j

Next i

Page 18: Chapter 20: Simulation Re-Visited

18

Animation

The two VBA methods that we often use to create a simulation program are the ScreenUpdating and Wait methods of the Application object .

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.

The Wait method can be used to create some delays in the simulation. – For example, if we are simulating a production line, we may have several

parts being received and shipped from different work stations. We use the Wait method to ensure that the user sees a delay between these actions.

Page 19: Chapter 20: Simulation Re-Visited

19

An Example of Animation

We are simulating a production line. This line has several parts being received and shipped from different work stations. Each work station has a different distribution for processing time.

We plan to animate the movement of parts in and out of Work Station 1. We use the PsiLogNormal() function to randomly generate the time it takes for a part to be processed in this workstation.

Page 20: Chapter 20: Simulation Re-Visited

20

An Example of Animation (cont’d) We collect the processing time at Work Station 1 using AllTrials

property of the Variable object.

ReDim WorkStation1(BatchSize) As Double

For i = 0 To BatchSize - 1

    WorkStation1(i) = MyProb.Variables(0).AllTrials(i, 3)

Next i

We then create a cumulative time array using the following VBA code.

ReDim CumWork1(BatchSize) As Double

CumWork1 (0) = 0

For i = 1 To BatchSize - 1

    CumWork1(i) = WorkStation1(i) + WorkStation1(i - 1)

Next i

Page 21: Chapter 20: Simulation Re-Visited

21

An Example of Animation (cont’d)

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

To ensure that the user sees a delay between these actions, we use the Wait method.

Page 22: Chapter 20: Simulation Re-Visited

22

An Example of Animation (cont’d)

For i = 0 To BatchSize - 1

    Application.ScreenUpdating = False

    Range(“Time”).Value = CumWork1(i)

    Range(“A1”).Offset(i, 0).Interior.ColorIndex = 0

    Range(“A1”).Offset(i+1, 0).Interior.ColorIndex = 5

    Application.Wait(Now + TimeValue(“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 23: Chapter 20: Simulation Re-Visited

23

Analysis

The motivation for using simulation is 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 24: Chapter 20: Simulation Re-Visited

24

Figure 20.1 Use properties and methods of Statistics objects to collect statistics

about simulation model outputs.

Page 25: Chapter 20: Simulation Re-Visited

25

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 26: Chapter 20: Simulation Re-Visited

26

Histogram (cont’d)

Page 27: Chapter 20: Simulation Re-Visited

27

Histogram (cont’d)

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.XLAM!Histogram”, InputRange, OutputRange, BinRange, Labels, Pareto, Cumulative, Chart

Note that, if we run this code multiple times with the Chart parameter set to True, we will have multiple charts created on the spreadsheet.

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.

Page 28: Chapter 20: Simulation Re-Visited

28

Histogram (cont’d)

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.

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

Page 29: Chapter 20: Simulation Re-Visited

29

Applications

Single Server Queuing Problem Revisited

Page 30: Chapter 20: Simulation Re-Visited

30

Description

The purpose of building this simulation model is to mimic the behavior of an ATM system to determine the expected customer waiting time, and the utilization of the ATM.

The ATM system consists of the ATM and its queue.

Upon the arrival in the system, if the queue is empty, the customer is served at the ATM; otherwise, the customer waits in the queue.

The interarrival time of customers in the system and the corresponding service time are randomly generated using Psi functions.

Interarrival and service times are used to calculate the waiting time in queue and time in the system for each customer.

Page 31: Chapter 20: Simulation Re-Visited

31

VBA Simulation

VBA will allow us to perform the simulation and analyze the results.

In addition, VBA is used to create a user friendly interface for this problem and to animate the queue.

Page 32: Chapter 20: Simulation Re-Visited

32

Figure 20.2

The spreadsheet setup for the ATM simulation model.

Page 33: Chapter 20: Simulation Re-Visited

33

Figure 20.3

The Simulation Input form is used to get user’s input about the total number of customers to simulate in each simulation trial, the mean interarrival time, and minimum, maximum and average service time at the ATM.

The input box is used to get user’s input about the total number of simulation trials.

Page 34: Chapter 20: Simulation Re-Visited

34

Figure 20.4

We animate the activities at the ATM and at the queue to visualize and better understand system’s behavior. – The yellow cell represents the customer being served. Customers arrive at a

queue line; if the server is “Idle,” then the customer begins service; if the server is “Busy,” the customers stay in the queue until the server becomes “Idle” again.

Page 35: Chapter 20: Simulation Re-Visited

35

Figure 20.5

In the end of the simulation, the user is presented a number of related statistics, such as, the expected mean waiting time in the queue, standard deviation of the mean waiting time, maximum waiting time, etc.

We use the mean waiting time from all simulation trials to create a histogram.

Page 36: Chapter 20: Simulation Re-Visited

36

Code

The Main procedure is used to – Call the user input frame, and subroutines that clear previous values.

– Create the simulation model in Excel.

– Simulate the model using Risk Solver Platform.

– Present simulation results.

– Animate the model.

The ClearAll procedure used to clear:– The data in the Excel spreadsheet model.

– The results of the simulation.

– The data used to create the histogram.

Page 37: Chapter 20: Simulation Re-Visited

37

Figure 20.6

Page 38: Chapter 20: Simulation Re-Visited

We use the form frmInput to get user’s input about the total number of customers, customer mean interarrival time in the system, and minimum, average and maximum service time at the ATM.

38

Figure 20.7

We assign to public variables the values entered by the user.

Page 39: Chapter 20: Simulation Re-Visited

The ExcelSimModel uses user’s input (already stored in public variables) to complete the spreadsheet Simulation model for this problem.

We are ready now to simulate the model using SimulateSS procedure.

39

Figure 20.8

Page 40: Chapter 20: Simulation Re-Visited

40

Figure 20.9

The SimulationResults procedure prints the results of the simulation in Excel, and creates a histogram of customer waiting time in the queue.

Page 41: Chapter 20: Simulation Re-Visited

41

Figure 20.10

This timeline table is generated by the VBA code in Figure 20.11.

Page 42: Chapter 20: Simulation Re-Visited

42

Figure 20.11

The Animate procedure imitates the movement of customers through the queue to the server.

Page 43: Chapter 20: Simulation Re-Visited

43

Figure 20.12

This part of the Animate procedure browses through the timeline table.

Page 44: Chapter 20: Simulation Re-Visited

44

Application Conclusion

The application is now complete.

Assigning the Main procedure to the RUN SIMULATION button. We are ready now to use this application.

Page 45: Chapter 20: Simulation Re-Visited

45

Summary

Simulation is a modeling tool which we use to imitate a real-world process in order to understand system behavior.

The three most important parts of a simulation model created using Risk Solver Platform are the: uncertain variables, uncertain functions, and statistic functions.

We use the RAND function of Excel and the galleries of Psi Distribution functions provided by Risk Solver Platform to generate the input data for a simulation model.

We use the Simulate method of Solver object to run a simulation using Object-Oriented API in VBA.

When building a simulation model, we often need to determine if the data and the results of the specified calculations will be stored in arrays or in a worksheet.

We can use the Wait method to create some animation of the simulation. We can use the Analysis Toolpack to perform many types of data analysis,

including histograms. Histograms calculate the number of occurrences of values in a particular interval. We use the Application object and the Run method in VBA to specify the Add-In

file from which we will be running the analysis.

Page 46: Chapter 20: Simulation Re-Visited

46

Additional Links

(place links here)