38
Designing a Discrete Event Simulation Tool Peter L. Jackson School of Operations Research and Industrial Engineering March 15, 2003 Cornell University Applied Systems Engineering II

Designing a Discrete Event Simulation Tool Peter L. Jackson School of Operations Research and Industrial Engineering March 15, 2003 Cornell University

Embed Size (px)

Citation preview

Designing a Discrete Event Simulation Tool

Peter L. JacksonSchool of Operations Research and

Industrial EngineeringMarch 15, 2003

Cornell University

Applied Systems Engineering II

15 March 2003 Peter L. Jackson SimDesign 2

Orientation

15 March 2003 Peter L. Jackson SimDesign 3

Mission

• To develop a graphically-oriented discrete event simulation modeling tool for use in Applied Systems Engineering II

15 March 2003 Peter L. Jackson SimDesign 4

Requirements (Version 1.0)• The tool shall be free, public domain• The tool shall permit graphical

description of simulation model• The tool shall be extensible by students• The tool shall exploit MS Excel 97+

interface• The tool shall provide basic simulation

services• The tool shall be simple (minimal

documentation required)

15 March 2003 Peter L. Jackson SimDesign 5

Functional Analysis

Build Graphical Model Code Components

Debug Model Run Model, Collect Output

Analyze Output

15 March 2003 Peter L. Jackson SimDesign 6

Requirements

• The tool shall not require more than two weekends of development time• The tool shall analyze a graph and

construct the shell of a simulation model

• Students shall provide code for model components

• The tool shall run the model and store outputs (trace)

• Students shall analyze the trace

15 March 2003 Peter L. Jackson SimDesign 7

Derived Requirements

• Graphical model may not use text attributes on arcs (Excel 97 limitation)

• Graphical model must encode all node attributes in text string (to meet development time target)

• State variables for trace shall be defined by a range name• Students must code updates to range

elements

15 March 2003 Peter L. Jackson SimDesign 8

Issue

• What modeling approach to use?• Functional flow block diagram• Event graph• Activity diagram

• Tradeoffs• Resolution

• Event graph (mimic commercial software “Sigma” by Lee Schruben)

15 March 2003 Peter L. Jackson SimDesign 9

Essential Functions of Graphical Model• Events• Conditions• Triggers• Delays• State transitions• Event cancellation• Initialization

• On node• On node• Arc• On node• On node• On arc• On node

15 March 2003 Peter L. Jackson SimDesign 10

Idea for Text String

• “?condition+delay=transition”• Tokens ?,+,= identify components• Condition is function name• Delay is function name• Transition is function name• Students code functions in Visual

Basic

15 March 2003 Peter L. Jackson SimDesign 11

Issues and Resolution

• How to represent event cancellation?• Use dashed or dotted line type

• How to represent initial event?• It is the only node that has no

incoming arcs

15 March 2003 Peter L. Jackson SimDesign 12

Starting Spreadsheet: SimTemplate.xlsPut your model on Sheet1

Check for diagnostics on sheet SimLog

View output on sheet SimTrace

Test/run your model

15 March 2003 Peter L. Jackson SimDesign 13

Sample Model=Initialize

=Arrival +InterArrivalTime

+ServiceTime

=EndService

?IsIdle

=BeginService

?IsQueue

Use any autoshapes and formatting that you want

What matters:•Text strings•Arc connections and direction•Arc type: solid or dashed

15 March 2003 Peter L. Jackson SimDesign 14

Analyze and Run InteractionFirst build the model and check for errors

Debug: run one event at a time

Switch to view different sheets (Model, Log, Trace) as desired

Set simulation duration

Run until done

15 March 2003 Peter L. Jackson SimDesign 15

Automated Model Interpretation: SimLog

15 March 2003 Peter L. Jackson SimDesign 16

Switch to Visual Basic Editor

Development Code: Do not modify (all variables and objects beginning with “Sim” are reserved)

User Code: Put your code here

15 March 2003 Peter L. Jackson SimDesign 17

You Write the Code: Declare Your State Variables

'Declare your variables hereGlobal Queuelength As IntegerGlobal ServerBusy As Integer Global CompletedCustomerCount As Long

‘Queuelength is the variable tracking the number of customers in the queue.

'global means it is available for use in other modules;

'ServerBusy will be true if the server is busy; false otherwise.

15 March 2003 Peter L. Jackson SimDesign 18

Write the Event Functions (to change the state)

Function initialize()'every simulation should have a function which initializes the state variablesQueuelength = 0 'we start with an empty queueServerBusy = False 'we start with an idle serverCompletedCustomerCount = 0End Function

Function arrival()'this represents the arrival of a customerQueuelength = Queuelength + 1End Function

Function beginservice()'this function represents the start of service by a server.ServerBusy = TrueEnd Function

Function endservice()'this function represents the end of service by a server; the customer leaves at this pointQueuelength = Queuelength - 1 'we count the customer as being in the queue until the end of serviceServerBusy = FalseCompletedCustomerCount = CompletedCustomerCount + 1End Function

15 March 2003 Peter L. Jackson SimDesign 19

Write the Condition Functions: to Test the State

Function isidle() As Integer'Functions that return a true/false value must be declared as integerisidle = Not ServerBusy 'ServerBusy is either true/false so Not ServerBusy is either false/true.End Function

Function isqueue() As IntegerIf Queuelength > 0 Then isqueue = True Else isqueue = False 'this illustrates the if...then...else statementEnd Function

15 March 2003 Peter L. Jackson SimDesign 20

Write the Code to Generate Delays and DurationsFunction interarrivaltime() As Variant'this function returns a random interarrival time; functions that return a value for time should use the variant data typeDim duration As Variant 'local variable declaration; duration will be the length of the interarrival timeduration = 5 + Rnd() * 3 'duration will be a random number between 5 and 8.interarrivaltime = duration 'this is how you return a valueEnd Function

Function servicetime() As Variantservicetime = 4 + Rnd() * 3 'servicetime will be a random number between 4 and 7End Function

15 March 2003 Peter L. Jackson SimDesign 21

Debug: Step Through Model

=Initialize

=Arrival +InterArrivalTime

+ServiceTime

=EndService

?IsIdle

=BeginService

?IsQueue

=Initialize

=Arrival +InterArrivalTime

+ServiceTime

=EndService

?IsIdle

=BeginService

?IsQueue

=Initialize

=Arrival +InterArrivalTime

+ServiceTime

=EndService

?IsIdle

=BeginService

?IsQueue

15 March 2003 Peter L. Jackson SimDesign 22

How a Discrete Event Simulation Works

Scheduled Events Sorted in Increasing Order of Scheduled Time

Time:Event:

4.0EndService

4.3Arrival

… later times… other events

Remove Next Scheduled Event

Advance Simulation Clock

Execute State Change Function

If Condition Satisfied…

Generate Next Event(s) with later time(s)

Insert Event into Schedule

Current Time: 3.3

Current Time: 4.0

15 March 2003 Peter L. Jackson SimDesign 23

Common Modeling Problem: What Happens First?• Simulated robots playing soccer

• Given current velocities, what happens first:• Two robots collide?• One robot hits wall?• One robot intercepts ball?• Etc.

• Need to compute the time of all these events and select the minimum as the next event that really happens

• Or…

15 March 2003 Peter L. Jackson SimDesign 24

Canceling Events

• Schedule all possible event types:• Two robots collide• One robot hits wall• Etc.

• Let the simulation determine which happens first

• When any of these event types happens, cancel all other pending events of related type

15 March 2003 Peter L. Jackson SimDesign 25

Example: Random Server Breakdowns =Initialize

=Arrival +InterArrivalTime

+ServiceTime

=EndService

?IsIdle

=Breakdown+RepairAndServiceTime

+TimeToFail=BeginService

?IsQueue

15 March 2003 Peter L. Jackson SimDesign 26

Automated Model Interpretation

15 March 2003 Peter L. Jackson SimDesign 27

More Code To Write

Function Breakdown()'no change in state; ServerBusy stays trueEnd Function

Function TimeToFail() As Variant'this function returns a random time to failDim duration As Variant 'local variable declaration; duration will be the length of the time to failduration = 5 + Rnd() * 3 'duration will be a random number between 5 and 8.TimeToFail = duration 'this is how you return a valueEnd Function

Function RepairAndServiceTime() As Variant'this function returns a random time to repair and complete serviceDim duration As Variantduration = 10 + Rnd() * 3 'duration will be a random number between 10 and 13.RepairAndServiceTime = duration 'this is how you return a valueEnd Function

15 March 2003 Peter L. Jackson SimDesign 28

Define Ranges to Store Variables

15 March 2003 Peter L. Jackson SimDesign 29

Write Code to Store Variables

Function OutputVariables()

Worksheets("Sheet1").Range("Queue_Length").Value = Queuelength

Worksheets("Sheet1").Range("Completions").Value = CompletedCustomerCount

End Function

Your range names Your state variables

15 March 2003 Peter L. Jackson SimDesign 30

Modify Code to Store Variables After Each Event

15 March 2003 Peter L. Jackson SimDesign 31

Input/Output

• You really need to store only the variables that have changed• Code could be made more efficient

• If you want to read input data from the spreadsheet (eg. Initial parameter settings), use ranges in a similar way.

• Now, single step through your simulation watching your variables change with each event.• The more variables you track, the easier it

will be to debug your model.

15 March 2003 Peter L. Jackson SimDesign 32

Creating a Trace

• A trace is a history of your state variables after each event

• The simulator automatically writes out whatever is in the range called “SimTraceRange” before and after each event• Stored on separate lines of sheet

“SimTrace”• It also writes out the labels found in the

range called “SimTraceLabelRange” at the head of this list.

• You must define these two ranges.

15 March 2003 Peter L. Jackson SimDesign 33

Define Trace and Label Ranges

15 March 2003 Peter L. Jackson SimDesign 34

What a Trace Looks Like

Two rows for each event: begin and end.Second row captures time spent in state (“Elapsed time”)

15 March 2003 Peter L. Jackson SimDesign 35

X-Y Scatter Plot

Time Series of State Variable

0

1

2

3

4

5

6

7

0 200 400 600 800 1000 1200

Simulation Time

Qu

eue

Len

gth

Delete the plot to make the simulation run faster: you can add it again later

15 March 2003 Peter L. Jackson SimDesign 36

Statistics Computed After Each Run• Statistics inserted into first four lines of

trace output.• Four statistics computed (Min, Max,

Mean, Std. Dev.) even if they don’t make sense for your particular state variables

15 March 2003 Peter L. Jackson SimDesign 37

Project Completed

Build Graphical Model Code Components

Debug Model Run Model, Collect Output

Analyze Output