337 Simulation and Modelling

download 337 Simulation and Modelling

of 35

Transcript of 337 Simulation and Modelling

  • 8/13/2019 337 Simulation and Modelling

    1/35

  • 8/13/2019 337 Simulation and Modelling

    2/35

    There are many application areas, e.g. Computer architecture

    Networks/distributed systems

    Software performance engineering

    Telecommunications

    Manufacturing

    Healthcare

    Transport

    Finance

    Environmental science/engineering

    Here, well focus on the important common principles, ratherthan become entrenched in specic application areas

    5

    Some Important Questions

    What are we trying to measure? How do we measure it? What sorts of model can we build? Is the model well-dened? How can we solve the model? What formalisms can we use to help dene a model? How does the model compare to the real system?

    6

    The Focus...

    Were going to focus on discrete event systems, with continuous time and discrete states

    Our models will essentially be stochastic transition systemswhere delays are associated with each state

    Different high-level formalisms can be used to dene thesemodels well study some popular approaches

    These models can variously be solved using Simulation Numerical algorithms Analytically

    Broadly, these are in increasing order of efficiency and decreasingorder of generality

    7

    This course...

    Part I: Models

    1. Modelling overview (ajf )

    Examples and motivation Performance measures and their relationships

    2. Timed transition systems (ajf)

    Markov Processes (MPs)

    Generalised Semi-Markov Processes (GSMPs)

    8

  • 8/13/2019 337 Simulation and Modelling

    3/35

    3. Solution methods (ajf)

    Numerical solution of MPs Analytical solution of MPs Simulation of MPs and GSMPs

    Part II: High-level formalisms, tools and techniques

    1. Discrete-event Simulation (a jf)

    Libraries for discrete-event simulation (Java)

    Random number generation & distribution sampling Output analysis Applications

    9

    2. Stochastic Petri Nets (jb)

    Notation Generation of underlying MP Applications

    3. Stochastic Process Algebras (jb)

    Introduction to PEPA Generation of underlying MP

    Applications3. Markovian Queueing Networks (jb)

    10

    Modelling Overview

    Example 1: A Simple TP server

    A TP system accepts and processes a stream of transactions,mediated through a (large) buffer:

    ...

    Transactions arrive randomly at some specied rate The TP server is capable of servicing transactions at a given

    service rate

    Q: If both the arrival rate and service rate are doubled, whathappens to the mean response time?

    11

    Example 2: A simple TP server

    Consider the same system as above:

    ...

    The arrival rate is 15tps The mean service time per transaction is 60ms The current mean response time is around 350ms

    Q: What happens to the mean response time if the arrival rateincreases by 10%?

    12

  • 8/13/2019 337 Simulation and Modelling

    4/35

    Example 3: Heathrow airport

    There are ve security scanners (X-ray) between the check-in anddepartures lounges at Terminal 4. One of them is broken:

    ...

    BROKEN

    DEPATURES LOUNGE

    Around 3.8 customers pass through the terminal each second It takes about 30 minutes on average to get through securityQ: How long would it take on average if all 5 scanners were working?

    13

    Example 4: A central server

    Jobs are submitted to a central server

    2

    3

    1

    Completions

    Arrivals

    Disk2

    Disk 1

    On average, each submitted job makes 121 visits to the CPU, has70 disk 1 accesses and 50 disk 2 accesses

    The mean service times are 5 ms for the CPU, 30 ms for disk 1and 27ms for disk 2Q: How would the system response time change if we replace the

    CPU with one twice as fast?

    14

    Example 5: A manufacturing system

    A factory has 5 assembly areas

    jobs

    Queue

    QueueType 0

    0

    3

    1

    2

    4

    15

    There are three job types, numbered 0, 1 and 2, which occurwith probability 0.3, 0.5 and 0.2 respectively

    Jobs arrive at the rate of 4/hr and visit the stations in the order:0 2, 0, 1, 4

    1 3, 0, 22 1, 4, 0, 3, 2

    The mean time for each job type at each station in visit order is0 0.50, 0.60, 0.85, 0.50

    1 1.10, 0.80, 0.75

    2 1.10, 0.25, 0.70, 0.90, 1.00

    Q: If you were to invest in one extra machine, where would you putit?

    16

  • 8/13/2019 337 Simulation and Modelling

    5/35

    There are many techniques one can use to model such systems Simple models involve nothing more than back-of-the-envelope

    calculations

    The most complex model is a full-blown simulation A tiny taster...

    17

    Two Simple Experiments with Random Numbers

    Example 1: The Birthday Bet

    You meet n people at a party youve never met before

    You bet one of them that at least two of the n people have thesame birthday (e.g. 5th March) (Assume birthdays are uniformlydistributed and ignore leap years)

    Question: How big should n be for your winning chances to be> 50%?

    Alternatively, what is the probability , p

    n say, that two or more

    people in a random group of n people have the same birthday?

    18

    Under the assumptions of the model, there is an analyticalsolution:

    pn = 1 n 1

    i =1

    365 i365

    Amazingly, p22 = 0 .476 and p23 = 0 .507, so the break-even pointis 23

    If we couldnt do the math, we could instead simulate thescenario by generating m sets of n random birthdays (randomintegers between 0 and 364) and counting the number of sets cthat contain a duplicate

    If the result of each of the m experiments is 0 or 1, then eachoutcome is an observation or estimate of pn , albeit a very badone!

    Of course, a better estimate is c/m19

    public class birthday {public static void main( String args[] ) {

    int n = Integer.parseInt( args[0] ) ;int k = Integer.parseInt( args[1] ) ;i n t b = 0 , c = 0 ;

    fo r ( i n t i = 0 ; i < m ; i++ ) {int tot[] = new int[365] ;for ( in t j = 0 ; j < n && to t [b] < 2 ; j++ ) {

    b = (int)( Math.random() * 365 ) ;tot[b]++ ; if ( tot[b] == 2 ) c++ ;

    }}

    System.out.println( (float)c / m ) ;} }

    20

  • 8/13/2019 337 Simulation and Modelling

    6/35

    Here are the averages of 5 runs of the program for differentvalues of n and m

    m n

    5 10 15 20 25 30

    100 0.000 0.000 0.200 0.200 0.400 0.200

    101 0.000 0.080 0.300 0.460 0.520 0.840

    102 0.028 0.100 0.236 0.482 0.580 0.660

    103 0.024 0.117 0.250 0.402 0.567 0.716

    104 0.028 0.117 0.255 0.414 0.565 0.710

    105 0.027 0.117 0.254 0.412 0.569 0.707

    For each n there is a noticeable convergence as m increases

    21

    Lets home in on m = 22, 23 and 24 and look at the 95%condence intervals (see later) for the mean using m = 10 5

    m Mean 95% Condence Interval

    22 0.476 (0.475,0.478)

    23 0.508 (0.506,0.510)24 0.538 (0.537,0.539)

    22

    Example 2: Come, Friendly Bombs

    It is required to drop bombs on a small island that is being usedas an illegal arms stash

    Each of 10 bombers will aim one bomb at the centre of theisland (assume normalised to be the origin (0,0))

    From a safe height there is a bombing error (due to uctuatingair temperature/density/velocity etc.) which is approximatelynormally distributed with standard deviation 180 m in thehorizontal; similarly 100 m in the vertical

    Q: How many bombs will hit the island, on average?

    Q: At greater risk, the bombers can y lower and reduce thestandard deviations by 30%. What is the hit rate now?

    23

    (0,0)

    (180,60)

    (170,110)

    MISS

    MISS

    HIT

    HITBombers

    y

    x

    (280,80)

    (140,30)

    (180,100)

    (40,200)

    (10,140)

    24

  • 8/13/2019 337 Simulation and Modelling

    7/35

    In general, this cannot be solved analytically we must resort tosimulation

    The island will be modelled as a polygon

    We will simulate a number, m, of missions and will estimatethe average number of hits per mission The impact point of each bomb is simulated by sampling the two

    Normal distributions (details later)

    The hit/miss status reduces to a point in polygon test...

    25

    public class Bombers {static boolean isOnTarget( double x, double y ) {

    double[] xs = {-280,10,180,170,40,-180,-140} ;double[] ys = {-80,-140,-60,110,200,100,-30} ;

    int ncrosses = 0, n = xs.length - 1 ;for ( int i = 0 ; i < xs.length ; i++ ) {

    if ( ( ( ys[i] y && y >= ys[n] ) ) &&

    x < xs[i] + ( xs[n] - xs[i] ) * ( y - ys[i] ) /( ys[n] - ys[i] ) )

    ncrosses++ ;

    n = i ;}return ( ncrosses % 2 == 1 ) ; }

    26

    public static void main( String args[] ) {int m = Integer.parseInt( args[0] ) ;int hits = 0 ;fo r ( i n t i = 0 ; i < m ; i++ ) {

    fo r ( i n t j = 0 ; j < 10 ; j++ ) {

    double x = Normal.normal( 0, 180 ) ;double y = Normal.normal( 0, 100 ) ;if ( isOnTarget( x, y ) )

    hits++ ;}

    }System.out.println( (float)hits / m ) ;

    }}

    27

    Here are the results of 5 runs of the program for differentnumbers of simulated missions ( m):

    m Run 1 Run 2 Run 3 Run 4 Run 5

    101 5.800 4.900 5.100 5.800 5.500

    102 5.110 4.930 5.610 5.330 5.010

    103 5.357 5.310 5.326 5.249 5.284

    104 5.359 5.319 5.336 5.345 5.374

    105 5.352 5.346 5.343 5.340 5.354

    The ve results are noticeably more consistent as m increases

    28

  • 8/13/2019 337 Simulation and Modelling

    8/35

    This is what happens if the standard deviations are reduced by30% (to 126m and 70m respectively), for 10 5 missions:

    m Run 1 Run 2 Run 3 Run 4 Run 5

    105 7.679 7.675 7.672 7.676 7.675

    By averaging the results for m = 10 5 an estimate of theimprovement in hit rate is (approximately) 44%

    29

    Some observations so far...

    Mathematical models can be really simple Under the given assumptions, mathematical models deliver exact

    answers

    Mathematical models can only be used in restricted cases Simulation models are extremely exible (just change the code!) Simulation models produce estimates , not exact answers Each run of a simulation model produces a different estimate

    The longer we run a simulation the better the estimatesbecome

    30

    Fundamental Laws

    Consider the following:

    SystemArrivals Completions

    Lets assume we observe the system for time T and let The number of arrivals be A

    The number of completions be C

    From these we can dene a number of useful performancemeasures and establish relationships between them

    31

    Some Direct Measures

    The Arrival Rate is = A/T The Throughput is = C/T

    If the system comprises a single resource whose total busy time is Bthen,

    The Utilisation is U = B/T The average Service Demand of each job (or average service

    time ) is S = B/C

    Note: The net service rate is = 1 /S = C/B

    Now some Fundamental Laws (or Operational Laws )...

    32

  • 8/13/2019 337 Simulation and Modelling

    9/35

    The Utilisation Law

    Since U = B/T = C/T B/C we have:

    U =

    S Sometimes we work with service rates rather than times , in

    which case we have U = /

    Importantly, note that U < 1 so we require = 1 /S > Q: What if = ?

    33

    Littles Law

    Suppose we plot ( A C ) over the observation time ( T ):

    t1 t2 t4 t11t3 t5 t6 t7 t8 t9 t10 time

    1

    4

    3

    2

    T

    Total area

    0

    = I

    0

    AC

    Let the accumulated area be I (in request-seconds)

    34

    The average number of jobs in the system is N = I /T The average time each job spends in the system (the average

    waiting time ) is W = I /C

    Since I /T = C/T I/C we have:

    N = W Note: Assumes ( A C ) is zero at either end of the observation

    period, or that is much larger than the residual populations ateither end

    35

    Littles Law can be applied to any system (and subsystem) inequilibrium, e.g.

    ...

    Clients

    CPU

    Subservers

    36

  • 8/13/2019 337 Simulation and Modelling

    10/35

    The Response Time Law

    The special case of a population of users in think/computemode is ubiquitous and has its own law

    Let Z be the think time, i.e. the time between completion of one request and the submission of the next N is now the total population of users and the request

    submission rate

    The average total time for each cycle is W + Z , so from LittlesLaw, N = (W + Z ), or

    W = N/ Z 37

    The Forced Flow Law

    Suppose the system in question comprises a number of resourcesthat a job can visit during its time in the system

    Let C k be the number of job completions at, resource k The average number of visits each job makes to resource k is

    then V k = C k /C

    Rearranging: C k = V k C so, dividing both sides by T ,C k /T = V k C /T , i.e.

    k =

    V k where k is the throughput of resource k38

    The Flow Balance Asuumption

    It is often useful to assume that A = C in which case,A/T = C/T , i.e. =

    In particular, systems that are in equilibrium (or steady state )must satisfy this property in the long term

    Note: If the ow balance assumption does not hold in the longterm (as time ), then the system is fundamentally unstable

    Motto: What goes in must come out!

    39

    Timed State Transition Systems

    We now focus attention on modelling systems with state Time is continuous, but the states are assumed to be discrete and

    nite, or countably innite, in number

    E1 E2 E4t1 t2 t3

    E3t4 t5

    E5etc.

    Times1 s2 s3 s4 s5Initial state s0

    State transitions

    State holding times

    These are called discrete-event systems Note that the state doesnt change between events (transitions)

    40

  • 8/13/2019 337 Simulation and Modelling

    11/35

    We need to know the distribution of the time the system spendsin a given state

    timeE E

    t t

    (t) (t)t = t t

    Density function

    The state holding times might bebased on measurement, or assumed

    distributionto have a known mathematical

    State s

    Measured/assumed distribution

    41

    In order to understand the key issues, consider a very simpleexample: a single server with a FIFO queue; arrival rate andservice rate :

    Population n Population n1 2

    ... ...

    Infinite capacity Infinite capacity

    1 2

    The simplest way to represent the state of the system is toidentify a state with each queue population (0 , 1, 2..., N ):

    0 1

    ...

    ...arrival arrival arrival arrival

    com plet ion completion completion completion

    NN1

    Transitions are triggered by events, here job arrival and jobcompletion

    42

    With no time delays, this is just a Labelled Transition System orLTS, cf FSP. Its not useful for performance analysis!

    To add state holding times in a completely general way weassociate clocks with events

    aC

    C a(set )arrival

    aCC c

    C a

    C ccompletion

    (set )C ccompletion

    (set )

    C a C c(set , )

    C a C c(set , )

    C aC c

    C aC c

    C c

    ...

    ...

    10

    arrival arrival

    completion

    Clock for arrival eventClock for completion event

    N1 N(set )arrival

    completion

    Events that are active in a given state compete to trigger thenext transition, via expiry of their clock

    Events may schedule other events by setting their clocks duringthe transition

    43

    aC

    C a(set )arrival

    C a

    C ccompletion

    (set )C ccompletion

    (set )

    C a C c(set , )

    C a C c(set , )

    C aC c

    C aC c

    C c

    ...

    ...

    10

    arrival arrival

    completion

    N1 N(set )arrival

    completion

    Clocks run down (to 0) at a state-dependent rate The event whose clock reaches 0 rst in any state triggers the

    next transition

    A clock setting function sets the clocks for new events that arescheduled during the transition

    The clocks of old events, i.e. that are active in both the old andnew states, continue to run down

    Events that were active in the old state, but not the new, arecancelled

    44

  • 8/13/2019 337 Simulation and Modelling

    12/35

    The Generalised Semi-Markov Process (GSMP)

    A formal system that encapsulates this behaviour is theGeneralised Semi-Markov Process ; the components are: A set S of states (nite or countably innite) A set of events E = {e1 ,...,e N } E (s) the set of events schdeuled to occur in state sS p(s ; s, e ) the probability that the next state is s when event

    e occurs in state s F (x; s , e , s , e ) the distribution function used to set the clock

    for new event e when event e triggers a transition from s to s

    r(s, e ) the rate at which the clock for event eE runs downin state s S 0 , F 0 the distribution functions used to assign the initial

    state and to set the clocks for all active events in that state.

    45

    It is important to understand the concept of old , new andcancelled events

    0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

    000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

    0 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 0

    1 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 1

    0 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 0

    1 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 11 1 1

    000000000111111111 000000000111111111000000000000000000000000000000000000111111111111111111111111111111111111

    000000000000000000000000000000000000111111111111111111111111111111111111

    E(s) E(s)Old eventsO(s; s, e ) = E(s) (E(s){e})

    New events N(s; s, e ) = E(s)(E(s){e

    Cancelled events(E(s){e})E(s)

    We talk of scheduling new events and descheduling cancelledevents

    46

    Example: consider the transition from s to s via event e2 below

    C 1C 2C 3

    C 5

    C 4C 3

    NewOldCancelled

    4(e , e )53(e )

    1(e )

    s

    se

    e

    e

    e

    e

    e

    3

    1

    2

    4

    5

    3

    47

    Note: This only allows one event to trigger a transition GSMPsin general allows multiple events to occur simultaneously

    Back to the example. We have... S = {0, 1, 2..., N } E =

    {a, c

    } where a=arrival, c=completion

    E (0) = {a}, E (0 < a < N ) = {a, c}, E (N ) = {c} p(s + 1; s, a ) = 1 , p(s 1; s, c) = 1; 0 elsewhere F (x; s ,a ,s ,e ) = F a (x)

    F (x; s ,c ,s ,e ) = F c(x)

    For all sS , r (s, e ) = 1 for all e in E (s)

    S 0 (0) = 1 , S 0 (s = 0) = 0, F 0 (x; a, 0) = F a (x)

    48

  • 8/13/2019 337 Simulation and Modelling

    13/35

    The state at time t denes a continuous-time process, X (t) say;formally, this is the GSMP

    Note that the clocks in state sS determine how long theprocess remains in state s, prior to the next transition, e.g.

    = time of ith transitioni

    4

    3

    2

    1

    0 Time 1 2 3 4 5 6 7 8 9

    X(t) = 3t

    49

    A sample path of a GSMP is a particular (random) traversal of the transition system

    To start it off, we use S 0 to select an initial state sS ; for eachevent e

    E (s) use F 0 (x; s, e ) to initialise C e

    A sample path is then generated by repeating the following steps:1. Find tnext = min {C e /r (s, e ) | eE (s)} and let e be the

    winning event

    2. Determine the next state s using p(.; s, e )

    3. For each oO(s ; s, e ), reduce C n by tnext r (s, o)

    4. For each n

    N (s ; s, e ), set C n using F (x; s ,n ,s ,e )

    5. Set s = s

    50

    Example: The innite capacity single-server queue in Java (i.e.as above but with N = )

    Assume the inter-arrival times are uniform-random between 0and 1 (i.e. U (0, 1)) and that service times are xed at 0.25...

    class ssqClocks2 {public static void main( String args[] ) {

    int s, sPrime ;double now = 0.0 ; // Keeps track of timedouble stopTime ;double arrClock = 0.0 ;double compClock = 0.0 ;double stopTime = Double.parseDouble( args[0] ) ;

    51

    s = 0 ;arrClock = Math.random() ;while ( now < stopTime ) {

    i f ( s = = 0 ) {// E(0) = {arrival}

    now += arrClock ; // Note: all rates are 1// p(1;0,arrival) = 1sPrime = 1 ;// O(1;0,arrival) = {}// N(1;0,arrival) = {arrival,completion}arrClock = Math.random() ;compClock = 0.25 ;

    52

  • 8/13/2019 337 Simulation and Modelling

    14/35

    } else {if ( arrClock < compClock ) { // Arrival event

    now += arrClock ;// p(s+1;s,arrival) = 1

    sPrime = s + 1 ;// O(s;s,arrival) = {completion}compClock -= arrClock ;// N(s+1;s,arrival) = {arrival}, s>0arrClock = Math.random() ;

    53

    } else { // Completion eventnow += compClock ;// p(s-1;s,completion) = 1sPrime = s - 1 ;

    // O(s;s,completion) = {arrival}, s>0arrClock -= compClock ;// N(s-1;s,completion) = {completion}, s>0// = {}, s=0if ( sPrime > 0 ) {

    compClock = 0.25 ; ;}

    }}s = sPrime ; }}}

    54

    The above code is an example of a discrete event simulation With completely general clock setting functions, simulation is the

    only way to analyse a GSMP

    The code above doesnt accumulate any measures, but we caneasily keep track of, e.g.

    The time the system spends in each state

    The area under some function of the state (see the exampleunder Littles Law earlier)

    The average time between leaving one set of states andentering another

    and so on

    From these, useful performance measures can be computed

    55

    Another example: The Machine repair Problem There are N machines in a factory that are subject to periodic

    failure

    Machines that fail are serviced in fail-time order by a repairer

    The individual time-to-breakage ( B ) and repair times ( R) areeach assumed to be independent and identically distributed This can be modelled as a tandem queueing system:

    ... ...

    Total population N

    Working machines Broken machines

    56

  • 8/13/2019 337 Simulation and Modelling

    15/35

    Because the population ( N ) is xed the state can be describeduniquely by the population of one queue (broken machines, say)

    We need only two events: break and repair , say

    Let F b(x) = P (B

    x) be the distribution function for the time

    to breakage for a given machine, and F r (x) = P (R x) be thatfor the repair time

    A GSMP for this system is as follows...

    57

    S = {0, 1,...,N }E = {break , repair }

    E (0) = {break }E (N ) = {repair }

    E (0 < s < N ) = {break,repair } p(s 1; s, repair) = 1 , 0 < s N p(s + 1; s, break) = 1 , 0 s < N

    p(s ; s, e ) = 0 for all other s , s , e

    F (x; s , break , s , e ) = F b(x) = P (B x)F (x; s , repair , s , e ) = F r (x) = P (R x)

    r (s, e ) = 1 , 0 s N, eE (s)S 0 (0) = 1 , S 0 (s > 0) = 0 , F 0 (x; break , 0) = F b(x)

    58

    Q: What are the new events, N (s ; s, e ), in each state s whenevent e triggers a transition from state s?

    Exercise: sketch the Java code for generating sample paths fromthe GSMP in this case

    59

    Distribution Sampling

    Sample path generation (discrete event simulation) depends onthe ability to sample distributions, e.g. of

    Inter-arrival times

    Service/repair times Message lengths

    Batch size

    etc.

    E.g. in computing a sample path of a GSMP we need to samplesuch distributions in order to set the clocks

    Where possible, we sample from a mathematical distribution, butif this is not possible we must use empirical data

    60

  • 8/13/2019 337 Simulation and Modelling

    16/35

    Example Uses of Mathematical Distributions

    Exponential Approximates inter-arrival times well in somesystems, e.g. independently arriving jobs/customers

    Weibull Fits well time-to-failure of certain components/servicesNormal Often accurate when a time delay is the sum of number

    of independent sub-delays, e.g. product assembly (BEWARE of negative observations!)

    Gamma Very versatile: can be parameterised to model manynon-negative r.v.s

    Cauchy A heavy-tailed distribution; ts well Linux le sizes,for example

    Geometric (Discrete) Sometimes used to model (integer) batchsizes

    61

    Sampling Continuous Distributions

    There are a number of methods; most importantly:1. The inverse tranform method

    2. The rejection method3. The convolution method

    and various special methods for particular distributions (e.g. seetutorial exercise)

    62

    Short Diversion: Sampling U(0,1)

    All distribution samplers assume the ability to generateuniform-random numbers in the interval (0 , 1)

    The robust RNGs exploit number theory and are based on theLinear Congruential Generator which generates a sequencex0 , x 1 , x 2 ... via the rule

    xn +1 = ( ax n + c) mod m

    where a is a multiplier , c is an increment and m is the modulus

    Samples from U (0, 1), viz. u0 , u 1 , u 2 ,... are obtained by settingu i = xi /m,i = 0 , 1, 2,... .

    Note that 0 x i < m so the maximum period is m An important objective is to maximise the period

    63

    Multiplicative Congruential Generators

    A Multiplicative Congruential Generator (MCG) is obtained bysetting c = 0 in an LCG (but note now that x i must be non-zero)

    It can be shown that a multiplicative generator has (maximum)period m

    1 if m is prime and if the smallest integer k for which

    (ak 1) mod m = 0 is k = m 1 Example: a = 3 , m = 7 whereupon, (only) when k = m 1 = 6,

    ak 1 = 728 = 104 7; starting with x 0 = 1, we obtain:x0 = 1 , x 1 = 3 , x 2 = 2 , x 3 = 6 , x 4 = 4 , x 5 = 5

    Period =6, x 6 = 1 , x 7 = 3 ...

    Javas Math.random() uses an MCG with a 48-bit seed

    Modern generators combine several MCGs or generalise theMCG approach e.g. the Mersenne Twister (period 2 19937 1)!

    64

  • 8/13/2019 337 Simulation and Modelling

    17/35

    Well assume the ability to generate U (0, 1) samples by anystandard method (well use Javas Math.random() ) and take itfrom there

    Recall that a continuous random variable, X say, can becharacterised by its density and/or distribution functions:

    P (x < X x+ dx) = f (x)dx F (x) = x f (y)dy = P (X x)

    65

    1. The (Inverse) Transform Method

    Suppose X is a continuous r.v. with cdf F (x) = P (X x) andthat we are trying to sample X

    A curious but useful result is that if we dene R = F (X ) thenRU (0, 1); OR... given RU (0, 1) sample X from F

    1 (R):

    x

    F(x)

    X

    r

    R

    x

    1

    Note that F (x) increases monotonically, so:P (X x) = P (F 1 (R) x) = P (R F (x)) = F (x),

    66

    Algorithm: Sample U (0, 1) giving some value 0 R 1, thencompute F 1 (R)

    Of course, this only works if F (x) has an inverse!

    Example: U(a,b)

    If X U (a, b) thenF (x) =

    x aba

    , a x b

    Setting R = F (x) and inverting F givesX = F 1 (R) = R(ba) + a

    This conrms what we (should!) already know: if RU (0, 1),then ( R(ba) + a)U (a, b)

    67

    Example: exp( )

    If X exp() thenF (x) = 1 exp x , x 0

    Setting R = F (X ) and inverting, we get:

    R = 1 exp X1 R = exp X

    loge (1 R) = X loge (1 R)

    = X

    So, if R

    U (0, 1), then

    log

    e(1

    R)/

    U (0, 1) (this is alsothe case for loge (R)/ since (1 R)U (0, 1))

    68

  • 8/13/2019 337 Simulation and Modelling

    18/35

    2. The Rejection Method

    If F (x) cannot be inverted we can sometimes work with thedensity function f (x)

    The rejection method is a Monte Carlo algorithm (i.e. basedon random sampling): we enclose f (x) in a box and then throwdarts(!)

    f(x)

    xa b

    m

    Y

    Y

    1

    2

    X X 1 2(Accept) (Reject)

    69

    The procedure is as follows:1. Find an a, b and m that bounds all (or most of) f (x)

    2. Pick two random samples X U (a, b) and Y U (0, m)3. If the point ( X, Y ) lies beneath the curve for f (x) accept X ;

    otherwise reject X and repeat from step 2

    Intuitively, the method works because the smaller f (X ) is theless likely you are to accept X

    More rigorously, we need to show that the density function forthose values of X that we accept is precisely f (x), i.e. we need toshow that:

    P (x < X x + dx | Y < f (X )) = f (x) dx

    70

    xa + x b x

    m

    f(x)

    X

    Y

    Using the formula for conditional probability, this becomes:P (x < X x + dx,Y < f (X )) /P (Y < f (X ))

    = dx/ (b

    a)

    f (x)/m

    1/ (m(ba))= f (x) dx

    71

    The efficiency depends on the number of rejections R beforeaccepting a value of X

    The probability of accepting X in any one experiment, p say, issimply the ratio of the area of the box to that under the curve,i.e.

    p = 1

    m(ba) Since each experiment is independent, R is geometrically

    distributed:P (R = r ) = p(1 p)r

    If f has very long tails, p will be small, although it is possible todivide the x axis into regions and treat each separately

    72

  • 8/13/2019 337 Simulation and Modelling

    19/35

    3. The Convolution Method

    A number of distributions can be expressed in terms of the(possibly weighted) sum of two or more random variables fromother distributions (The distribution of the sum is the

    convolution of the distributions of the individual r.v.s)

    Example: Erlang( k, )

    A r.v. X Erlang( k, ) is dened as the sum of k r.v.s each withdistribution exp( k)

    We can think of X being the time taken to pass through a chainof k servers, each with an exp( k) service time distribution:

    k k k

    1 2 k

    73

    Notice thatE [X ] =

    1k

    + 1k

    + . . . 1k

    = 1

    We can generate Erlang( k, ) samples using the sampler for theexponential distribution: if X i

    exp( k) then

    X =k

    i =1X i Erlang( k, )

    If R i U (0, 1) then X i is sampled using log R i / (k) We can save the expensive log calculations in the summation by

    turning the sum into a product:

    X =k

    i =1

    log R ik

    = 1k

    logk

    i =1R i

    74

    Sampling Discrete Distributions

    All discrete distribution sampling is based on the inversetransform method, either through table look-up or algebraicallyin some cases

    We work with the cumulative distribution function F (x), whichfor a discrete r.v. is a step function, e.g.

    F(x)

    1

    0 x

    0.39

    0.15

    0 1 2 3 4 5

    0.66

    0.81

    75

    Here, we have:x p(x) F(x)

    0 0.15 0.15

    1 0.24 0.39

    2 0.22 0.61

    3 0.15 0.81

    4 0.09 1.00

    To generate a sample, we drive the table backwards We simply pick a value on the y-axis (a U (0, 1) sample) and then

    map this back to an interval on the x-axis

    Q This involves a linear-time lookup. Can we do better...?!A Yes! But how...? See the quiz during the tutorial!

    76

  • 8/13/2019 337 Simulation and Modelling

    20/35

    Steady State Output Analysis

    Usually (but not always) were interested in measuring a systemat equilibrium , or in steady state

    A system is in equilibrium if the state probabilites areindependent of time, i.e. for state variable S (t)

    P (S (t) = s) = ps (t) ps as t

    P(s(t) = s2 )

    P(s(t) = s1 )

    P(s(t) = s3 )

    1

    23

    p

    pp

    Transient

    Somewhere here

    Probability

    Equilibriumtime

    77

    To estimate equilibrium behaviour we must ignore thepost-initialisation (warm-up) transient as it may introduce bias

    We must start any measurement (or reset ongoing measurement)at the end of the transient period

    Note: this simply means that the state probabilities areapproximately those at equilibrium the state on an individualsample path at that time may assume any value

    In general, we can only work out the approximate length of thewarm-up transient by doing pilot runs, e.g.

    Plot running mean or moving average over one run

    Plot measures having discarded p% of the initial observations Plot means/distributions of n th observation of many runs

    78

    Condence Intervals

    Suppose we have n independent, identically-distributed (iid)observations X i , 1 i n each with mean and variance 2

    Wed like to compute a point estimate for based on the X i thats easy:

    X = 1n

    n

    i =1X i

    Wed also like a measure of condence in this estimate We seek two numbers, a, b say, for which we can state:

    P (a b) = pfor some probability p, e.g. 0.9, 0.95...

    This is called a condence interval and is usually symmetricabout X , i.e. (a, b) = X h where h is the half-width

    79

    The Central Limit Theorem tells us that the sample mean of theX i approaches the N (, 2 /n ) distribution as n Normalising,

    X N (,

    2 /n )

    X

    2 /n N (0, 1) as n

    Because we know the cdf of N (0, 1) we can easily locate twopoints z and z such that

    P (z < X

    2 /n z) = 1

    for any given

    z is messy to compute, so particular values have been tabulated(see handouts)

    80

  • 8/13/2019 337 Simulation and Modelling

    21/35

    Rearranging...P X

    z n < X +

    z n = 1

    If we know we can nd X

    z/ n by looking up z in tables of

    N (0, 1)

    / 2 / 2

    0z z

    This is ne provided n is sufficiently large We can replace by S (sample standard deviation), again

    provided n is sufficiently large

    81

    Q What does sufficiently large mean?

    A It depends on the distribution of the X i

    However, key fact: it can be shown that if the X i , 1 i n, arethemselves normally distributed then

    X

    S 2 /nhas the Students t distribution with parameter n 1 (degreesof freedom)

    Provided the X i are normally distributed and independent, anexact 100(1

    )% condence interval for the mean ( ) is

    X tn 1 ,1 / 2 S

    n n 2, tn 1 ,1 / 2 from tables

    82

    Example: Suppose the observations 1.20, 1.50, 1.68, 1.89, 0.95,1.49, 1.58, 1.55, 0.50, 1.09 are samples from a normal distributionwith unknown mean . Construct a 90% condence interval for

    The sample mean is

    X = 110

    10

    i =1X i = 1 .34

    where X i is the i th observation

    The sample variance is

    S 2 = 1n 1

    n

    i =1(X i X )

    2 = 0 .17

    so S = 0 .41

    83

    From tables of the Students t distribution , t 9 ,0 .95 = 1 .83 so the90% condence interval is

    X 1.83 0.41 10 = 1 .34 0.24

    In the context of a simulation, how do we ensure in general thatour observations are a. independent and b. approximatelynormally distributed?

    If the observations come from independent runs of thesimulation, then they will be independent

    If the observations are themselves internally-computedpopulation means then they will be approximately normally

    distributed (Central Limit Theorem) problem solved!

    But what if theyre not?!

    84

  • 8/13/2019 337 Simulation and Modelling

    22/35

    Batched Means

    A popular general approach seeks to achieve approximatenormality and independence simultaneously

    Idea: Run the model once and divide the post-warm-up periodinto n batches (typically 10 n 30) where batch i computesthe average of m estimates Y i, 1 ,...,Y i,m

    Compute X i = 1 j m Y i,j , i = 1 ,...,n Provided m is reasonably large, the X i will (hopefully) be

    approximately normal and independent

    ...

    Batc o servations

    m individual observationsor state changes

    1X X 2 3X nXTransient0

    End SimulationReset measures

    85

    Independence

    Independent replications guarantees independent observations atthe expense of a warm-up transient at the start of each run

    The batched means method involves a single run and a singlewarm-up transient, but the observations might now be dependent(why?)

    If the observations are dependent then the formula for the samplevariance S 2 will be wrong because the covariances are ignored

    Consequence: The condence interval will be too narrow

    86

    Recall, covariance measures the dependence between two r.v.s For r.v.s X and Y with means X and Y , and variances 2X and

    2Y respectively

    COV (X, Y ) = = E ([X X ][Y Y ])= E (XY ) X Y

    Note that covariance may be positive (positive correlation),negative (negative correlation) or zero (uncorrelated, whichimplies the r.v.s are independent)

    Note thatCOV (X, X ) = E ([X X ]

    2 ) = V AR(X )

    87

    Recall also,V AR(X + Y ) = E [((X + Y ) (X + Y ))

    2 ]

    = E [((X X ) + ( Y Y ))2 ]

    = E [(X X )2 ] + E [(Y Y )

    2 ]

    + 2 E [((X X )(Y Y ))]= 2X + 2Y + 2 COV (X, Y )

    Of course, if X and Y are independent then C OV (X, Y ) = 0 andV AR(X + Y ) = 2X +

    2Y

    88

  • 8/13/2019 337 Simulation and Modelling

    23/35

    To obtain our condence interval we assumed thatV AR(X ) = 2 /n , but if the X i , 1 i n are dependent then

    V AR(X ) = V AR(1n

    n

    i =1X i )

    = 1

    n 2V AR(

    n

    i =1X i )

    = 1

    n 2E ([(

    n

    i =1X i ) n]

    2 )

    = 1

    n 2E ([

    n

    i =1(X i )]

    2 )

    = 2

    n +

    1n 2

    2n 1

    i =1

    n

    j = i +1Cov( X i , X j )

    89

    The bottom line: Covariances are typically positive so if weignore them altogether S 2 /n becomes an under-estimate of V AR(X ) and the condence intervals will be too narrow

    90

    State Space Coverage

    For any discrete-event model consider the underlying statetransition system

    ...

    Statetransition(Event)

    State space

    The accuracy (consistency) of a measure derived directly, orindirectly, from the (equilibrium) state probabilities is determinedby the average number of times the simulation visits each state

    91

    Many visits high accuracy; few visits low accuracy

    ...

    Parameterisation 1: Simulation time Parameterisation 2: Simulation timespent in a small number of states spent covering a large number of states

    ...

    State with equilibrium probability >

    For the same simulator execution time we would expect narrowercondence intervals from the model on the left than the one onthe right

    92

  • 8/13/2019 337 Simulation and Modelling

    24/35

    Markov Processes

    Note that the future history of a GSMP is determined entirely bythe current state

    However, note that the current state includes clock settings thatare history dependent, e.g.:

    C k

    C k (set )

    C k (set )

    C k (set )s

    e1

    e2

    e3

    In general, the value of clock C k depends on how we entered thestate

    93

    Now... suppose that when entering state s ,1. Each transition into s initialises the clocks of the new events

    in e E (s ) using the same distribution F (x; e ), i.e.

    independently of how the state was entered

    2. All clocks are set according to an exponential dsitribution, i.e.

    F (x; e ) = 1 e rxfor some parameter r that depends only on e

    The exponential distribution is memoryless , in that the future isindependent of the past, i.e. if X exp(r )

    P (t < X t + s | X > t ) = P (X s) s, t 0

    94

    With the above assumptions the GSMP now assumes a verymuch simpler form:

    1. All occurrences of event e are associated with the samedistribution, hence the same distribution parameter

    2. There is no need to remember the time spent in any previous

    state(s), e.g. throught the use of clocks (memorylessness)3. In state s we simply race the distribution samplers

    associated with eE (s) and choose the event with thesmallest time

    4. Transitions need only be labelled with the associated (rate)parameter, e.g. for our queue:

    c0 1...

    ...

    c1

    95

    This much simpler process is called a Markov Process Markov Processes are easily analysed by discrete-event

    simulation (Exercise: sketch the algorithm)

    Crucially, however, they can be analysed Numerically, provided the state space is nite

    Analytically, in some special cases

    provided the process is irreducible : every state can be reachedfrom any other

    96

  • 8/13/2019 337 Simulation and Modelling

    25/35

    Numerical Solution Methods (Equilbrium)

    Let the state space be S and ps (t) be P (S (t) = s) where S (t)S is the state at time t

    Let q

    s,s be the transition rate from state s

    S to states S, s

    = s

    At equilibrium, the process is time homogeneous : ps (t + ) = ps (t) = ps , say

    It can be shown that every nite, irreducible, time-homogeneousMarkov process has a unique equilibrium probability distribution

    ps , sS , which is also limt ps (t)

    97

    At equilibrium, the probability uxes (average number of transitions per unit time) into, and out of, each state must be inbalance, i.e. for each state sS

    pss S,s = s

    q s,s =s S,s = s

    ps q s ,s

    These (linear) ux equations are called the global balance equations and are subject to the normalising equation

    sS ps = 1

    98

    Example:

    s

    q

    s

    ss

    s,sq s,ss,s

    q

    q

    s,s

    The ux out is ps (q s,s + q s,s ); the ux in is ps q s ,s + ps q s ,s Thus, at equilibrium:

    ps (q s,s + q s,s ) = ps q s ,s + ps q s ,s

    We can use standard linear solvers to nd ps for any state s ,given the transition rates q s,s , s , s S

    99

    The problem is usually dened in matrix form, viz. the solutionto p Q = 0 where p = ( ps 1 , ps 2 ,...,p s N ), S = {s1 , s 2 ,...,s N } is thestate space, and Q is the Generator Matrix

    Q =

    q 1 q s 1 ,s 2 q s 1 ,s 3 . .. q s 1 ,s N q s 2 ,s 1 q 2 q s 2 ,s 3 . .. q s 2 ,s N

    ...

    q s N ,s 1 q s N ,s 2 q s N ,s 3 ... q N

    The diagonal terms q i , s i S encode the net output rate fromstate i in the balance equations:

    q i = s j S,j = i

    q s i ,s j

    100

  • 8/13/2019 337 Simulation and Modelling

    26/35

    For example, consider p applied to the rst column: ps 1 q 1 + ps 2 q s 2 ,s 1 + ... + ps N q s N ,s 1 = 0

    which is the balance equation for state s 1

    As an example, consider our single-server queue (the M/M/1/cqueue), state space {0, 1,...,c}

    Q =

    0 0 ... 0 ( + ) 0 ... 00 ( + ) ... 0...

    ...

    Recall: is the service rate ; 1 is the mean service time , S say

    101

    Note that Q is singular and there are innitely many solutions topQ = 0 as written

    In order to solve for the equilibrium distribution p it is necessaryencode the normalising condition:

    sS ps = 1

    Simple: pick a column, j , set all its elements to 1, and likewisethe j th element of the 0 vector, e.g. in our example we mightsolve pQ = 1 c , or

    p

    0 0 ... 1 ( + ) 0 ... 10 ( + ) ... 1...

    ... 1

    = (0 , 0, ..., 1)

    Now choose your favourite solver and click go!102

    Analytical Solution Methods

    For Markov processes with the right structure direct analyticalsolutions to the system of equations p Q = 0 can be obtained

    Our M/M/1/c queue is an example; well consider the casec =

    (the standard M/M/1 queue), although there is a

    solution for nite c

    The balance equations are:p 0 = p1

    ( + ) pi = pi 1 + pi +1 , i > 0

    By inspection, the (unique) solution is pn = n p0 , where =

    103

    p0 is found from the normalising equation:n =0 pn =

    n =0

    n p0 = 1

    Rearranging... p0 =

    n =0n

    1

    = 1

    Notice, importantly, that the sum only converges if < 1 i.e. < ; hence the case = has no solution (the system isunstable )

    Putting this together, we obtain:

    pn = (1

    ) n , n

    0

    104

  • 8/13/2019 337 Simulation and Modelling

    27/35

    Performance Measures for the M/M/1 Queue

    Throughput,

    We can use the above result to determine the throughput : = p0 0 + (1 p0 ) = =

    Server utilisation, U

    The server is idle when n = 0; busy otherwiseU = 1

    Pr

    { The server is idle

    }= 1 p0 = 1 (1 )= =

    = S

    105

    Mean queue length, N

    Recall the denition of the mean queue length:N =

    n =0npn

    The n in the summation is ddly, but notice that:nn =

    dd

    n

    Hence:N =

    n =0

    npn = (1 )

    n =0

    nn

    = (1 ) dd

    n =0n =

    1

    106

    Mean Response ( Waiting) Time, W

    Recall: the mean response time is the mean time that a jobspends in the system (queueing time + service time)

    The easiest way to nd W is to use Littles Law

    N = W = W Thus

    W = N/

    = (1 )

    = 1

    107

    Mean Queueing Time

    The time spent waiting to go into service ( W Q say) is easilycomputed once we know W :

    W Q = W 1

    =

    Note: If a customer is removed from the queue before it goes intoservice then Littles Law applied to W Q delivers the meannumber of customers waiting to be served:

    N Q = W Q

    = 2

    1

    108

  • 8/13/2019 337 Simulation and Modelling

    28/35

    Some Examples

    U N W

    2 4 2 1/ 2 1 1/ 2

    2 5 2 2/ 5 2/ 3 1/ 32 10 2 1/ 5 1/ 4 1/ 8

    3 4 3 3/ 4 3 1

    5 10 5 1/ 2 1 1/ 5

    8 12 8 2/ 3 2 1/ 4

    Compare these with results from the GSMP simulation

    109

    More than one Queue

    Consider two queues in tandem:Population n Population n1 2

    ... ...

    Infinite capacity Infinite capacity

    1 2

    Note: the arrival rate (throughput) is the same for both queues

    Lets proceed as before - the state this time is the population of both queue 1 ( n 1 say) and queue 2 (n 2 say), i.e. a pair ( n1 , n 2 )

    The queue length vector random variable is ( N 1 , N 2 ) and wellwrite pn 1 ,n 2 = P (N 1 = n1 , N 2 = n2 ) at equilibrium

    110

    Because the state is a pair (of queue populations) the statetransition diagram is now two dimensional:

    1

    1 1

    2 2 2

    2 2

    2

    0,0

    0,11,0

    1,1 0,2

    2,0

    111

    Now a small problem: no closed contour gives us an immediatelysolvable balance equation, e.g. the dotted box:

    p 0 ,0 = 2 p0 ,1

    or the solid box:

    ( + 1 ) p2 ,0 = p1 ,0 + 2 p2 ,1

    We can, however, come up with four equations which cover allstates and transitions. For n 1 , n 2 > 0:

    p 0 ,0 = 2 p0 ,1

    ( + 1 ) pn 1 ,0 = pn 1 1 ,0 + 2 pn 1 ,1

    ( + 2 ) p0 ,n 2 = 1 p1 ,n 2 1 + 2 p0 ,n 2 +1

    ( + 1 + 2 ) pn 1 ,n 2 = pn 1 1 ,n 2 + 1 pn 1 +1 ,n 2 1 + 2 pn 1 ,n 2 +1

    112

  • 8/13/2019 337 Simulation and Modelling

    29/35

    One way to proceed is to guess a solution! Lets try: pn 1 ,n 2 = n 11 n 22 p0 ,0

    where 1 = / 1 , 2 = / 2

    We can test it by substitution into the balance equations: 01

    02 p0 ,0 = 2

    01

    12 p0 ,0

    by p0 ,0 =

    ( + 1 ) n 11 02 p0 ,0 = n 1

    11

    02 p0 ,0 + 2 n 11

    12 p0 ,0

    by n 11 p0 ,0 + 1 = / 1 + 2 2 = 1 +

    and so on for the other cases (check them out!)

    113

    So it is a solution, and its unique! We nd p0 ,0 again using the fact the probabilities sum to 1:

    n 1 , n 2

    n 1 , n 2 0

    pn 1 ,n 2 =n 1 , n 2

    n 1 , n 2 0

    n 11 n 22 p0 ,0 = 1

    Hence

    p0 ,0 =

    n 1 =0

    n 2 =0n 11 n

    2

    2

    1

    =

    n 1 =0n 11

    1

    n 2 =0n 22

    1

    = (1 1 )(1 2 )

    114

    Incredibly, we arrive at:

    pn 1 ,n 2 = (1 1 ) n 11 (1 2 ) n 22 , n1 , n 2 0

    So, the joint probability (of being in state ( n 1 , n 2 ) is the productof the marginals (the probability of the rst queue being in staten 1 and similarly the second)

    This is an example of the Product Form result which applies to anarbitrary queueing network, provided the initial assumptions hold

    An extraordinary consequence of this is that each queue nowoperates as if it were an M/M/1 queue with an independentPoisson arrival stream

    115

    In particular, the M/M/1 queue results apply to each node inisolation, hence, for example:

    The utilisations are U 1 = 1 and U 2 = 2 The mean population of queue 2 is

    2

    1 2 The mean time spent in queue 1 is1

    2 The probability that queue 2 contains exactly 4 customers is

    p(N 2 = 4) = (1 2 )42

    and so on...Q: What about arbitrary networks? More later...

    116

    d h

  • 8/13/2019 337 Simulation and Modelling

    30/35

    Part II: High-level Modelling Tools

    GSMPs are a neat way to describe general discrete-event systems BUT - they are low level and hard to get right

    Markov processes provide an elegant way to formulate and solvemathematically tractable discrete-event systems BUT - they are even lower level than GSMPs and hard to get

    right

    The same applies to other transition systems we havent seen yet(e.g. SMPs, STAs...)

    In practice wed like high-level formalisms/tools that correspondto, or are directly translated into, these formal state transitionsystems

    117

    Some Targeted Approches

    1. Discrete-event simulation languages/APIs

    GPSS, Simula, Simscript, SimC++, SimPy, JINQS...

    2. Markovian Stochastic Petri Nets Mobius, PRISM, DNAmaca...

    3. Markovian Process Algebras

    PEPA, EMPA, TIPP, SFSP...4. Generalised/extended Stochastic Process Algebras

    SPADES, EMPA blah , SPADES(!), MoDeST...5. Queueing Networks

    Athene, QNET, QNAT,...

    118

    1. Tools for Discrete-event Simulation

    These all provide abstractions for encoding GSMP-likeoperations at a high level, e.g. for

    Managing simulated (virtual) time

    Dening and scheduling events

    Measurement and analysis

    Meaurement is typically built in to supporting libraries/classes,e.g. for modelling resources, queues

    Explicit measurement classes are also made available, e.g. tocompute explicitly means, variances, bar charts...

    Output analysis may support e.g. independent replications,batch-meanand calculation of condence intervals

    119

    In a GSMP the current state is dened by the value(s) of program variable(s), e.g. class instance variables

    The set of states is implicitly dened in terms of these statevariables

    New events in state s , N (s ; s, e ), are created by explicitly

    scheduling them during the transition from s to s

    Event scheduling involves sampling the appropriate clock settingfunction (distribution sampling)

    Cancelled events in state s , K (s ; s, e ) say, are removed bydescheduling them during the transition from s to s

    Probabilistic choice of next state is achieved by sampling aU (0, 1), i.e. via a RNG

    120

    A i l l ( h JINQS API) E l Th Si l Q ( )

  • 8/13/2019 337 Simulation and Modelling

    31/35

    A simple example (the JINQS API)... now() delivers the current virtual time ( double )

    schedule( e ) adds event e to an event set (aka calendar queue, event diary, future event list...)

    deschedule( e ) removes event e from the event set simulate() invokes events in time order and advances time

    Events are class instances, with associated code ( public voidinvoke() {... }) for effecting a state transition andscheduling/descheduling events

    All events have an invocation time a superclass instancevariable

    The event code models a transition in some underlying GSMP

    121

    Example: The Single-server Queue (no measures)

    The state is the queue population ( int ) stop is an abstract (virtual) method in superclass Sim that

    controls termination

    import tools.* ;

    class SSQSim extends Sim {in t n = 0 ;

    public boolean stop() {return now() > 100000.0 ;

    }

    122

    One arrival schedules the next. In state 1, we set the clock forthe completion event, as in the GSMP...

    class Arrival extends Event {public Arrival( double t ) {

    super( t ) ;}public void invoke() {

    schedule( new Arrival( now() + Math.random() ) ) ;n++ ;if ( n == 1 )

    schedule( new Completion( now() + 0.25 ) ) ;

    }}

    123

    The code for the completion event mirrors the equivalentGSMP...

    class Completion extends Event {public Completion( double t ) {

    super( t ) ;}public void invoke() {

    n-- ;if ( n > 0 )

    schedule( new Completion( now() + 0.25 ) ) ;}

    }

    124

    The initial state is 0 (w p 1 in the GSMP); the only active Application: The Simple Computer Model

  • 8/13/2019 337 Simulation and Modelling

    32/35

    The initial state is 0 (w.p. 1 in the GSMP); the only activeevent in state 0 is Arrival ...

    public SSQSim() {schedule( new Arrival( now() + Math.random() ) ) ;execute() ;

    }}

    public class ssq1Ev {public static void main( String args[] ) {

    new SSQSim() ;}

    }

    125

    Application: The Simple Computer Model

    Jobs are submitted to a job queue for execution by a CPU There are periodic page faults which are serviced by one of two

    disks

    Whilst a fault is being serviced the CPU switches to the next jobin the job queue; once serviced, the job re-enters the back of the job queue

    2

    3

    1

    Completions

    Arrivals

    Disk2

    Disk 1

    126

    From careful observation it has been established that: Each submitted job makes 121 visits to the CPU, has 70 page

    faults serviced by disk 1 and 50 by disk 2 on average

    The mean service times are 0.005s for the CPU, 0.03s for disk1 and 0.027s for disk 2

    The current arrival rate of jobs = 0.1/ s

    Note: the CPU mean service time is the mean time a job spendsat the CPU (before leaving or page-faulting)

    Q: How does the system response time ( W ) vary as the loadincreases?

    For illustration purposes, well assume all time delays areexponentially distributed (this is easily changed)

    127

    The state of the underlying GSMP is the population of each of the three queues (( c,m,n ) say)

    Here well use explicit Queues; each entry is a job, representedby its arrival time

    Events: Arrival new job arrival

    StopJob exit CPU

    Resume( n, ... ) exit disk n

    We need four distribution samplers for setting clocks:inter-arrival time, cpu time slice and two disk (page fault) servicetimes

    Well measure response time W using a CustomerMeasure

    128

    i l * The arrival event ( Arrive ) generates the next arrival places the

  • 8/13/2019 337 Simulation and Modelling

    33/35

    import tools.* ;

    class CPUSim extends Sim {Queue cpuQ = new Queue(),

    diskQ[] = { new Queue(), new Queue() } ;Exp iaTime,sTime[] = { new Exp( 1000.0/30 ),

    new Exp( 1000.0/27 ) },xTime = new Exp( 1000.0/5 ) ;

    CustomerMeasure respTime = new CustomerMeasure() ;

    int completions = 0 ;int stopCount = 100000000 ;

    129

    The arrival event ( Arrive ) generates the next arrival, places thenew job in the job queue ( cpuQ) and starts the CPU if it was idle(state (0 ,m,n ) in the GSMP)

    Event StopJob may exit the job or pass it to one of thediskstransition from ( c,m,n ) to either ( c 1,m,n ) w.p. 1/121,(c 1, m + 1 , n ) w.p. 70/121, or ( c,m,n + 1) w.p. 50/121

    In states ( c, 0, n ), (c,m, 0), c,m, n 0 we need to schedule aResume event at the corresponding disk

    On exit from a server the next job in the job queue is started, if there is one

    Well build in an EndOfWarmUp event to allow us to deletetransient measures

    130

    class Arrival extends Event {public Arrival( double t ) {

    super( t ) ;}public void invoke() {

    schedule( new Arrival( now() + iaTime.next() ) ) ;cpuQ.enqueue( new Double( now() ) ) ;if ( cpuQ.queueLength() == 1 )

    schedule( new StopJob( now() + xTime.next() ) ) ;}

    }

    131

    class StopJob extends Event {public StopJob( double t ) {

    super( t ) ;}public void invoke() {

    Double t = (Double) cpuQ.dequeue() ;if ( Math.random()

  • 8/13/2019 337 Simulation and Modelling

    34/35

    // StopJob continued...else {

    int i = (int) ( Math.random() + 50.0/120 ) ;diskQ[i].enqueue( t ) ;

    if ( diskQ[i].queueLength() == 1 )schedule( new Resume( i, now() + sTime[i].next() ) ) ;}if ( cpuQ.queueLength() > 0 )

    schedule( new StopJob( now() + xTime.next() ) ) ;}

    }

    133

    class Resume extends Event {int d ;public Resume( int disk, double t ) {

    super( t ) ;

    d = disk ;}public void invoke() {

    Double t = (Double) diskQ[d].dequeue() ;cpuQ.enqueue( t ) ;if ( cpuQ.queueLength() == 1 )

    schedule( new StopJob( now() + xTime.next() ) ) ;if ( diskQ[d].queueLength() > 0 )

    schedule( new Resume( d, now() + sTime[d].next() ) ) ;} }

    134

    class EndOfWarmUp extends Event {public EndOfWarmUp( double t ) {

    super( t ) ;}

    public void invoke() {respTime.resetMeasures() ;completions = 0 ;stopCount = 5000 ; // Whatever...!

    }}

    135

    public boolean stop() {return completions == 3000 ;

    }public CPUSim( double lambda ) {

    iaTime = new Exp( lambda ) ;schedule( new Arrival( now() + iaTime.next() ) ) ;schedule( new EndOfWarmUp( now() + 15000 ) ) ; // Whatever!simulate() ;System.out.println( respTime.mean() ) ;

    }}

    136

    Some Results Now lets up the load to = 0 .4...

  • 8/13/2019 337 Simulation and Modelling

    35/35

    Here are three example time series plots for the mean waitingtime (moving average) when = 0 .15:

    CPU model - disk 1 mean queue length for =0.15(three trial runs)

    0

    2

    4

    6

    8

    10

    12

    14

    16

    0 4000 8000 12000 16000 20000 24000 28000

    Time

    M e a n

    W a i

    t i n g

    T i m e ,

    W

    It looks as if the system is approaching equilibrium after around15000 time units

    137

    pCPU model - disk 1 mean queue length for =0.4

    (three trial runs)

    0

    5

    10

    15

    20

    25

    30

    0 4 000 80 00 12000 16000 20000 24000 28000

    Time

    M e a n W a i

    t i n g

    T i m e ,

    W

    As increases the system takes longer to reach equilibrium(why?)

    An an example, wed expect measurements taken between 15000and 30000 time units, say, to have wider condence intervals for = 0 .4 than for = 0 .15. Lets see...

    138

    W (exact) Point Estimate 90% Condence Interval

    (10 independent replications)

    0.1 4.86 4.839 (4.776, 4.902)

    0.15 5.42 5.267 (5.044, 5.490)

    0.20 6.16 6.198 (5.790, 6.607)

    0.25 7.17 6.679 (6.158, 7.199)

    0.30 8.68 8.662 (7.784, 9.541)

    0.35 11.25 11.726 (10.767, 12.685)

    0.40 16.86 16.295 (12.297, 20.293)

    0.45 42.45 30.765 (18.621, 42.909)

    Q: Whats going on? Q: Where did the exact results come from?

    139