37
Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw- Hill

Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Embed Size (px)

Citation preview

Page 1: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Simulation

Based on

Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Page 2: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Why Simulation?

• Test design when cannot analyze– System too complex– Can analyze only for certain cases (Poisson

arrivals, very large N, etc.)

• Verify analysis

• Fast production of results

Page 3: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Simulation types

• Static vs. dynamic

• Deterministic vs. stochastic

• Continuous vs. discrete– Simulation model system model

Page 4: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Discrete Event System

• Discrete system– The system state can change only in a countable

number of points in time!– Event = an instantaneous change in state.

• Example: a queueing system– System state: Number of customer– At each time number of customer can only

change by an integer

Page 5: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Continuous Simulation

• Simulating the flight of a rocket in the air– System state: rocket position and weight– State changes continuously in time (according

to a partial differential equation)

Page 6: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Components of a discrete-event simulation model

• System state: the collection of state variables necessary to describe the system at a particular time.

• Simulation clock: a variable giving the current value of simulated time

• Event list: a list containing the next time when each type of event will occur.

• Statistical counters: variables used for storing statistical information about system performance– How many moments?

Page 7: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Components of a discrete-event simulation model (2)

• Initialization routine: a subprogram to initialize the simulation model at time 0.

• Timing routine: a subprogram that determines the next event from the event list and then advances the simulation clock to the time when the event is to occur.

• Event routine: A subprogram that updates the system state when a particular type of event occurs (one routine per event).

Page 8: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Components of a discrete-event simulation model (3)

• Library routine: A set of subprograms used to generate random observations from probability distributions that were determined as part of the simulation model.

• Report generator: A subprogram that computes estimates (from the statistical counter) of the desired performance measures when simulation ends.

Page 9: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Components of a discrete-event simulation model (4)

• Main program: a subprogram that invokes the timing routine to determine the next event and then transfers control to the corresponding event routine to update the system state. May also check for termination and invoke the report generator.

Page 10: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Flow controlInit

Determine next event

Event 1 Event n

Done?

report

no

Page 11: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Examplea single server queuing system

• System to be simulated– Ai Interarrival times are I.I.D.– Si Service time are I.I.D.– FIFO service– Work preserving

• Initial state– Empty and idle– First arrival after Ai time units from time 0

• Termination after n customers left queue.

Page 12: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Performance measures

• d(n) = expected average delay in queue.– Customer point of view

• q(n) = expected time-average number of customers in queue.– System point of view– Note: average over time which is continuous!!

• u(n) = proportion of time server is busy

Page 13: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

No. of customers in queue.

0

1

2

3

4

t

Q(t)

Page 14: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

No. of customers in queue.

0

1

2

3

4

t

Q(t)

Page 15: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Performance measures (2)

• Averages are not always enough.

• Register max/min values.

• Register the entire “pdf”– histograms

Page 16: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Initialization

• Set simulation time to 0.• Set initial state:

– Server idle– Queue empty– Last event time

• Init event list– Generate 1st arrival

• Zero stat counters– Total delay and number delayed– Area under Q(t) and B(t).

Page 17: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

1st Event: customer arrival

• After init finished the arrival event is selected and time is advanced to this event

• Change server from idle to busy• Add 0 to total delay, increment No of delayed.• Generate two events:

– This customer departure– Next arrival (generate as you go)

• Add 0 to the area under Q(t).• Add 0 to the area under B(t).

Page 18: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

2nd Event: customer arrival

• Arrival event is selected and time is advanced to this event

• Server busy => put customer in queue with arrival time

• Set “No. in queue” to 1.• Generate next-arrival events (don’t mess with dep.)• Add 0 to the area under Q(t).

• Add (t2-t1) to the area under B(t).

Page 19: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Remarks

• While handling an event time is sanding

• But, order of operation is still important:– First update area under Q(t) then update “No. in

queue”

• Two events at the same time– Order may change simulation result!

Page 20: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Determining Events

• In complex systems events sequence may not be trivial.

• Use of event graph to aim us is designing the events

Page 21: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Event Graph

• States are bulbs, connected with arrows

• Bold arrow: event may occur.

• Thin arrow: event must occur.

arrival depar-ture

Page 22: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Alternative Event Graph

• This design is correct as well!• One more event => more complexity• Simplification rule:

– If an event node has incoming arcs that are all thin and smooth, it can be eliminated.

• A strongly connected connected component that have no incoming arcs from other nodes must be initialized.

arrivalEnter

servicedepar-ture

Page 23: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Take care

• GIGO = garbage in garbage out– Realistic scenarios (arrival process, service time)– Full cover of system behavior

• Statistical confidence• What to model

– Not enough details => hurts accuracy– Too many details => slows simulation

• Attempt to verify correctness– Simulate cases you can analyze

Page 24: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Random Generator

• Make sure you can regenerate your random sequence, or debugging is hell.

• For long simulations, use 32 bit pseudo random generator. 16 bit is too short!

Page 25: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Statistical Confidence

• Better a few short runs than one long one.

• However, make sure run time is long enough to make end conditions negligible.

• Given a set of IID random variables we can calculate the confidence interval.

Page 26: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Confidence Interval

• X1, X2, …Xn are IID random variable with finite mean and finite variance 2>0.

nnXZn

/))((

2

• By central limit theorem Fn(z)(z) (the std. Normal r.v.)

• For sufficiently large n, approx. 100(1-) percent confidence interval for is given by

Fn(z)=P(Zn z) is Dist. Func. of Zn

nnSznX /)()( 22/1

Page 27: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

-5 -3 0 3 50

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

2/1 z 2/1 z

Area:

1-

Page 28: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Confidence Interval for Small n

• For small number of observations, we need to correct the coefficient of the interval width

• The small n the larger tn-1,1-/2 is.

• tn-1,1-/2 is taken from a table.

nnStnX n /)()( 22/1,1

Page 29: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Confidence Interval

• In general all plots has to have a confidence interval on every mark

• Exception: high confidence (mark size)

Page 30: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

A good pseudo random generator

float myrand()

{

b32 = (314159269*b32 + 453860245);

if (b32<0)

b32 = -b32;

return(0.00000000046566128730 * b32);

}

Page 31: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Generating Non-Uniform Distributions

• F(x), the PDF, of any distribution is a function to [0,1], => use the inverse transform– Generate U~U(0,1)– Return X=F-1(U)

• P(Xx) = P(F-1(U) x)=P(U F(x))=F(x)

Since U is U(0,1) and

0 F(x) 1 ,P(U y) = y

Page 32: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Generating the Exponential distribution

)1ln()(

otherwise

0 if

0

1)(

1

/

uuF

xexF

x

Note we can use - ln(u).

Page 33: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

0 20 40 60 80 100 120 140 160 180 200

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Page 34: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Simulating complex systems

• Systems often have many identical components– A queueing system with multiple queues

– A switch with multiple ports running a distributed algorithm

• Must keep state for each component– Array of structures in C,

– multiple instances of the basic object (C++)

• Keep the connectivity (when applicable)

Page 35: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Exercise No. 1

• Write an event simulator for a single queue• M/M/1• M/D/1

• Hand ins:– Code transcript– Utilization vs. load plot with confidence

interval of 90%, and the theoretical expectation• With 5 executions, and with 20 executions

– Average delay (queueing +service) vs. load

Page 36: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Remarks

• Code has to work anywhere (not just at home), especially in TAU.

• Make it portable.

• Oral exams through the semester.

• Send code to [email protected]– Specify development env. (Unix/Visual C/..)– One tar/zip file with makefile etc.

Page 37: Simulation Based on Law & Kelton, Simulation Modeling & Analysis, McGraw-Hill

Exercise No.2

1. Give an exact expression (neglect history) to the bound of the throughput of an NxN switch given that the scheduler can select any of the two packets at the HOL. What happens when N.

2. Plot the bound as a function of the depth of the queue the scheduler can look at.